<s> from __future__ import print_function <EOL> from pydpiper . pipeline import Pipeline , InputFile , OutputFile , LogFile , CmdStage <EOL> import atoms_and_modules . LSQ12 as lsq12 <EOL> import atoms_and_modules . NLIN as nlin <EOL> import atoms_and_modules . minc_atoms as ma <EOL> import atoms_and_modules . registration_file_handling as rfh <EOL> import atoms_and_modules . stats_tools as st <EOL> import atoms_and_modules . minc_parameters as mp <EOL> import atoms_and_modules . registration_functions as rf <EOL> from pydpiper . file_handling import removeBaseAndExtension , createBaseName , logFromFile <EOL> from atoms_and_modules . registration_functions import isFileHandler <EOL> import pydpiper . file_handling as fh <EOL> from pyminc . volumes . factory import volumeFromFile <EOL> import sys <EOL> from os . path import splitext <EOL> class SetResolution : <EOL> def __init__ ( self , filesToResample , resolution ) : <EOL> """<STR_LIT>""" <EOL> self . p = Pipeline ( ) <EOL> for FH in filesToResample : <EOL> dirForOutput = self . getOutputDirectory ( FH ) <EOL> currentRes = volumeFromFile ( FH . getLastBasevol ( ) ) . separations <EOL> if not abs ( abs ( currentRes [ <NUM_LIT:0> ] ) - abs ( resolution ) ) < <NUM_LIT> : <EOL> crop = ma . autocrop ( resolution , FH , defaultDir = dirForOutput ) <EOL> self . p . addStage ( crop ) <EOL> mask = FH . getMask ( ) <EOL> if mask : <EOL> cropMask = ma . mincresampleMask ( FH , <EOL> FH , <EOL> outputLocation = FH , <EOL> likeFile = FH ) <EOL> self . p . addStage ( cropMask ) <EOL> def getOutputDirectory ( self , FH ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( FH , rfh . RegistrationPipeFH ) : <EOL> outputDir = "<STR_LIT>" <EOL> else : <EOL> outputDir = FH . basedir <EOL> return outputDir <EOL> class LSQ12ANTSNlin : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> inputFH , <EOL> targetFH , <EOL> lsq12_protocol = None , <EOL> nlin_protocol = None , <EOL> subject_matter = None , <EOL> defaultDir = "<STR_LIT>" ) : <EOL> self . p = Pipeline ( ) <EOL> self . inputFH = inputFH <EOL> self . targetFH = targetFH <EOL> self . lsq12_protocol = lsq12_protocol <EOL> self . nlin_protocol = nlin_protocol <EOL> self . subject_matter = subject_matter <EOL> self . defaultDir = defaultDir <EOL> if ( ( self . lsq12_protocol == None and self . subject_matter == None ) or self . nlin_protocol == None ) : <EOL> self . fileRes = rf . returnFinestResolution ( self . targetFH ) <EOL> else : <EOL> self . fileRes = None <EOL> self . buildPipeline ( ) <EOL> def buildPipeline ( self ) : <EOL> self . lsq12Params = mp . setLSQ12MinctraccParams ( self . fileRes , <EOL> subject_matter = self . subject_matter , <EOL> reg_protocol = self . lsq12_protocol ) <EOL> lsq12reg = lsq12 . LSQ12 ( self . inputFH , <EOL> self . targetFH , <EOL> blurs = self . lsq12Params . blurs , <EOL> step = self . lsq12Params . stepSize , <EOL> gradient = self . lsq12Params . useGradient , <EOL> simplex = self . lsq12Params . simplex , <EOL> w_translations = self . lsq12Params . w_translations , <EOL> defaultDir = self . defaultDir ) <EOL> self . p . addPipeline ( lsq12reg . p ) <EOL> res = ma . mincresample ( self . inputFH , self . targetFH , likeFile = self . targetFH , argArray = [ "<STR_LIT>" ] ) <EOL> self . p . addStage ( res ) <EOL> self . inputFH . setLastBasevol ( res . outputFiles [ <NUM_LIT:0> ] ) <EOL> lsq12xfm = self . inputFH . getLastXfm ( self . targetFH ) <EOL> self . nlinParams = mp . setOneGenMincANTSParams ( self . fileRes , reg_protocol = self . nlin_protocol ) <EOL> for b in self . nlinParams . blurs : <EOL> for j in b : <EOL> if j != - <NUM_LIT:1> : <EOL> self . p . addStage ( ma . blur ( self . targetFH , j , gradient = True ) ) <EOL> self . p . addStage ( ma . blur ( self . inputFH , j , gradient = True ) ) <EOL> sp = ma . mincANTS ( self . inputFH , <EOL> self . targetFH , <EOL> defaultDir = self . defaultDir , <EOL> blur = self . nlinParams . blurs [ <NUM_LIT:0> ] , <EOL> gradient = self . nlinParams . gradient [ <NUM_LIT:0> ] , <EOL> similarity_metric = self . nlinParams . similarityMetric [ <NUM_LIT:0> ] , <EOL> weight = self . nlinParams . weight [ <NUM_LIT:0> ] , <EOL> iterations = self . nlinParams . iterations [ <NUM_LIT:0> ] , <EOL> radius_or_histo = self . nlinParams . radiusHisto [ <NUM_LIT:0> ] , <EOL> transformation_model = self . nlinParams . transformationModel [ <NUM_LIT:0> ] , <EOL> regularization = self . nlinParams . regularization [ <NUM_LIT:0> ] , <EOL> useMask = self . nlinParams . useMask [ <NUM_LIT:0> ] ) <EOL> self . p . addStage ( sp ) <EOL> nlinXfm = sp . outputFiles [ <NUM_LIT:0> ] <EOL> self . inputFH . setLastBasevol ( setToOriginalInput = True ) <EOL> output = self . inputFH . registerVolume ( self . targetFH , "<STR_LIT>" ) <EOL> xc = ma . xfmConcat ( [ lsq12xfm , nlinXfm ] , output , fh . logFromFile ( self . inputFH . logDir , output ) ) <EOL> self . p . addStage ( xc ) <EOL> class HierarchicalMinctracc : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> inputFH , <EOL> targetFH , <EOL> lsq12_protocol = None , <EOL> nlin_protocol = None , <EOL> includeLinear = True , <EOL> subject_matter = None , <EOL> defaultDir = "<STR_LIT>" ) : <EOL> self . p = Pipeline ( ) <EOL> self . inputFH = inputFH <EOL> self . targetFH = targetFH <EOL> self . lsq12_protocol = lsq12_protocol <EOL> self . nlin_protocol = nlin_protocol <EOL> self . includeLinear = includeLinear <EOL> self . subject_matter = subject_matter <EOL> self . defaultDir = defaultDir <EOL> if ( ( self . lsq12_protocol == None and self . subject_matter == None ) or self . nlin_protocol == None ) : <EOL> self . fileRes = rf . returnFinestResolution ( self . targetFH ) <EOL> else : <EOL> self . fileRes = None <EOL> self . buildPipeline ( ) <EOL> def buildPipeline ( self ) : <EOL> if self . includeLinear : <EOL> self . lsq12Params = mp . setLSQ12MinctraccParams ( self . fileRes , <EOL> subject_matter = self . subject_matter , <EOL> reg_protocol = self . lsq12_protocol ) <EOL> lsq12reg = lsq12 . LSQ12 ( self . inputFH , <EOL> self . targetFH , <EOL> blurs = self . lsq12Params . blurs , <EOL> step = self . lsq12Params . stepSize , <EOL> gradient = self . lsq12Params . useGradient , <EOL> simplex = self . lsq12Params . simplex , <EOL> w_translations = self . lsq12Params . w_translations , <EOL> defaultDir = self . defaultDir ) <EOL> self . p . addPipeline ( lsq12reg . p ) <EOL> self . nlinParams = mp . setNlinMinctraccParams ( self . fileRes , reg_protocol = self . nlin_protocol ) <EOL> for b in self . nlinParams . blurs : <EOL> if b != - <NUM_LIT:1> : <EOL> self . p . addStage ( ma . blur ( self . inputFH , b , gradient = True ) ) <EOL> self . p . addStage ( ma . blur ( self . targetFH , b , gradient = True ) ) <EOL> for i in range ( len ( self . nlinParams . stepSize ) ) : <EOL> if i == ( len ( self . nlinParams . stepSize ) - <NUM_LIT:1> ) : <EOL> self . defaultDir = "<STR_LIT>" <EOL> nlinStage = ma . minctracc ( self . inputFH , <EOL> self . targetFH , <EOL> defaultDir = self . defaultDir , <EOL> blur = self . nlinParams . blurs [ i ] , <EOL> gradient = self . nlinParams . useGradient [ i ] , <EOL> iterations = self . nlinParams . iterations [ i ] , <EOL> step = self . nlinParams . stepSize [ i ] , <EOL> w_translations = self . nlinParams . w_translations [ i ] , <EOL> simplex = self . nlinParams . simplex [ i ] , <EOL> memory = self . nlinParams . memory [ i ] if self . nlinParams . memory else None , <EOL> optimization = self . nlinParams . optimization [ i ] ) <EOL> self . p . addStage ( nlinStage ) <EOL> class FullIterativeLSQ12Nlin : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inputs , dirs , options , avgPrefix = None , initModel = None , fileResolution = None ) : <EOL> self . inputs = inputs <EOL> self . dirs = dirs <EOL> self . options = options <EOL> self . avgPrefix = avgPrefix <EOL> self . initModel = initModel <EOL> self . nlinFH = None <EOL> self . providedResolution = fileResolution <EOL> self . p = Pipeline ( ) <EOL> self . buildPipeline ( ) <EOL> def buildPipeline ( self ) : <EOL> lsq12LikeFH = None <EOL> resolutionForLSQ12 = None <EOL> if self . initModel : <EOL> lsq12LikeFH = self . initModel [ <NUM_LIT:0> ] <EOL> elif self . options . lsq12_likeFile : <EOL> lsq12LikeFH = self . options . lsq12_likeFile <EOL> if lsq12LikeFH == None and self . options . lsq12_subject_matter == None and self . providedResolution == None : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if not ( lsq12LikeFH == None ) : <EOL> resolutionForLSQ12 = rf . returnFinestResolution ( lsq12LikeFH ) <EOL> if resolutionForLSQ12 == None and self . providedResolution == None : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if resolutionForLSQ12 == None and self . providedResolution : <EOL> resolutionForLSQ12 = self . providedResolution <EOL> lsq12module = lsq12 . FullLSQ12 ( self . inputs , <EOL> self . dirs . lsq12Dir , <EOL> queue_type = self . options . queue_type , <EOL> likeFile = lsq12LikeFH , <EOL> maxPairs = self . options . lsq12_max_pairs , <EOL> lsq12_protocol = self . options . lsq12_protocol , <EOL> subject_matter = self . options . lsq12_subject_matter , <EOL> resolution = resolutionForLSQ12 ) <EOL> lsq12module . iterate ( ) <EOL> self . p . addPipeline ( lsq12module . p ) <EOL> self . lsq12Params = lsq12module . lsq12Params <EOL> if lsq12module . lsq12AvgFH . getMask ( ) == None : <EOL> if self . initModel : <EOL> lsq12module . lsq12AvgFH . setMask ( self . initModel [ <NUM_LIT:0> ] . getMask ( ) ) <EOL> if not self . avgPrefix : <EOL> self . avgPrefix = self . options . pipeline_name <EOL> nlinModule = nlin . initializeAndRunNLIN ( self . dirs . lsq12Dir , <EOL> self . inputs , <EOL> self . dirs . nlinDir , <EOL> avgPrefix = self . avgPrefix , <EOL> createAvg = False , <EOL> targetAvg = lsq12module . lsq12AvgFH , <EOL> nlin_protocol = self . options . nlin_protocol , <EOL> reg_method = self . options . reg_method , <EOL> resolution = resolutionForLSQ12 ) <EOL> self . p . addPipeline ( nlinModule . p ) <EOL> self . nlinFH = nlinModule . nlinAverages [ - <NUM_LIT:1> ] <EOL> self . nlinParams = nlinModule . nlinParams <EOL> self . initialTarget = nlinModule . initialTarget <EOL> for i in self . inputs : <EOL> linXfm = lsq12module . lsq12AvgXfms [ i ] <EOL> nlinXfm = i . getLastXfm ( self . nlinFH ) <EOL> outXfm = st . createOutputFileName ( i , nlinXfm , "<STR_LIT>" , "<STR_LIT>" ) <EOL> xc = ma . xfmConcat ( [ linXfm , nlinXfm ] , outXfm , fh . logFromFile ( i . logDir , outXfm ) ) <EOL> self . p . addStage ( xc ) <EOL> i . addAndSetXfmToUse ( self . nlinFH , outXfm ) <EOL> class LongitudinalStatsConcatAndResample : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subjects , timePoint , nlinFH , statsKernels , commonName ) : <EOL> self . subjects = subjects <EOL> self . timePoint = timePoint <EOL> self . nlinFH = nlinFH <EOL> self . blurs = [ ] <EOL> self . setupBlurs ( statsKernels ) <EOL> self . commonName = commonName <EOL> self . p = Pipeline ( ) <EOL> self . buildPipeline ( ) <EOL> def setupBlurs ( self , statsKernels ) : <EOL> if isinstance ( statsKernels , list ) : <EOL> self . blurs = statsKernels <EOL> elif isinstance ( statsKernels , str ) : <EOL> for i in statsKernels . split ( "<STR_LIT:U+002C>" ) : <EOL> self . blurs . append ( float ( i ) ) <EOL> else : <EOL> print ( "<STR_LIT>" + str ( statsKernels ) ) <EOL> sys . exit ( ) <EOL> def statsCalculation ( self , inputFH , targetFH , xfm = None , useChainStats = True ) : <EOL> """<STR_LIT>""" <EOL> if useChainStats : <EOL> stats = st . CalcChainStats ( inputFH , targetFH , self . blurs ) <EOL> else : <EOL> stats = st . CalcStats ( inputFH , targetFH , self . blurs ) <EOL> self . p . addPipeline ( stats . p ) <EOL> """<STR_LIT>""" <EOL> if xfm : <EOL> if not self . nlinFH : <EOL> likeFH = targetFH <EOL> else : <EOL> likeFH = self . nlinFH <EOL> res = resampleToCommon ( xfm , inputFH , stats . statsGroup , self . blurs , likeFH ) <EOL> self . p . addPipeline ( res ) <EOL> def statsAndConcat ( self , s , i , count , beforeAvg = True ) : <EOL> """<STR_LIT>""" <EOL> if beforeAvg : <EOL> xfm = s [ i ] . getLastXfm ( s [ i + <NUM_LIT:1> ] ) <EOL> else : <EOL> xfm = s [ i ] . getLastXfm ( s [ i - <NUM_LIT:1> ] ) <EOL> """<STR_LIT>""" <EOL> if self . nlinFH : <EOL> self . xfmToCommon . insert ( <NUM_LIT:0> , xfm ) <EOL> """<STR_LIT>""" <EOL> xtc = createBaseName ( s [ i ] . transformsDir , s [ i ] . basename + "<STR_LIT>" + self . commonName + "<STR_LIT>" ) <EOL> xc = ma . xfmConcat ( self . xfmToCommon , xtc , fh . logFromFile ( s [ i ] . logDir , xtc ) ) <EOL> self . p . addStage ( xc ) <EOL> inputResampledToCommon = createBaseName ( s [ i ] . resampledDir , s [ i ] . basename + "<STR_LIT>" + self . commonName + "<STR_LIT>" ) <EOL> logToCommon = fh . logFromFile ( s [ i ] . logDir , inputResampledToCommon ) <EOL> resampleCmd = ma . mincresample ( s [ i ] , <EOL> self . nlinFH , <EOL> likeFile = self . nlinFH , <EOL> transform = xtc , <EOL> output = inputResampledToCommon , <EOL> logFile = logToCommon , <EOL> argArray = [ "<STR_LIT>" ] ) <EOL> self . p . addStage ( resampleCmd ) <EOL> s [ i ] . addAndSetXfmToUse ( self . nlinFH , xtc ) <EOL> self . statsCalculation ( s [ i ] , self . nlinFH , xfm = None , useChainStats = False ) <EOL> else : <EOL> xtc = None <EOL> """<STR_LIT>""" <EOL> if count - i > <NUM_LIT:1> : <EOL> self . statsCalculation ( s [ i ] , s [ i + <NUM_LIT:1> ] , xfm = xtc , useChainStats = True ) <EOL> def buildPipeline ( self ) : <EOL> for subj in self . subjects : <EOL> s = self . subjects [ subj ] <EOL> count = len ( s ) <EOL> """<STR_LIT>""" <EOL> if self . timePoint == - <NUM_LIT:1> : <EOL> timePointToUse = len ( s ) - <NUM_LIT:1> <EOL> else : <EOL> timePointToUse = self . timePoint <EOL> currGroup = s [ timePointToUse ] . currentGroupIndex <EOL> index = s [ timePointToUse ] . getGroupIndex ( "<STR_LIT>" ) <EOL> xfmToNlin = s [ timePointToUse ] . getLastXfm ( self . nlinFH , groupIndex = index ) <EOL> if xfmToNlin : <EOL> self . xfmToCommon = [ xfmToNlin ] <EOL> else : <EOL> self . xfmToCommon = [ ] <EOL> if self . nlinFH : <EOL> s [ timePointToUse ] . currentGroupIndex = index <EOL> self . statsCalculation ( s [ timePointToUse ] , self . nlinFH , xfm = None , useChainStats = False ) <EOL> s [ timePointToUse ] . currentGroupIndex = currGroup <EOL> """<STR_LIT>""" <EOL> if count - timePointToUse > <NUM_LIT:1> : <EOL> self . statsCalculation ( s [ timePointToUse ] , s [ timePointToUse + <NUM_LIT:1> ] , xfm = xfmToNlin , useChainStats = True ) <EOL> if not timePointToUse - <NUM_LIT:1> < <NUM_LIT:0> : <EOL> """<STR_LIT>""" <EOL> for i in reversed ( range ( timePointToUse ) ) : <EOL> self . statsAndConcat ( s , i , count , beforeAvg = True ) <EOL> if xfmToNlin : <EOL> self . xfmToCommon = [ xfmToNlin ] <EOL> else : <EOL> self . xfmToCommon = [ ] <EOL> for i in range ( timePointToUse + <NUM_LIT:1> , count ) : <EOL> self . statsAndConcat ( s , i , count , beforeAvg = False ) <EOL> def resampleToCommon ( xfm , FH , statsGroup , statsKernels , nlinFH ) : <EOL> blurs = [ ] <EOL> if isinstance ( statsKernels , list ) : <EOL> blurs = statsKernels <EOL> elif isinstance ( statsKernels , str ) : <EOL> for i in statsKernels . split ( "<STR_LIT:U+002C>" ) : <EOL> blurs . append ( float ( i ) ) <EOL> else : <EOL> print ( "<STR_LIT>" + str ( statsKernels ) ) <EOL> sys . exit ( ) <EOL> pipeline = Pipeline ( ) <EOL> outputDirectory = FH . statsDir <EOL> filesToResample = [ ] <EOL> for b in blurs : <EOL> filesToResample . append ( statsGroup . relativeJacobians [ b ] ) <EOL> if statsGroup . absoluteJacobians : <EOL> filesToResample . append ( statsGroup . absoluteJacobians [ b ] ) <EOL> for f in filesToResample : <EOL> outputBase = removeBaseAndExtension ( f ) . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> outputFile = createBaseName ( outputDirectory , outputBase + "<STR_LIT>" + "<STR_LIT>" ) <EOL> logFile = fh . logFromFile ( FH . logDir , outputFile ) <EOL> targetAndLike = nlinFH . getLastBasevol ( ) <EOL> res = ma . mincresample ( f , <EOL> targetAndLike , <EOL> likeFile = targetAndLike , <EOL> transform = xfm , <EOL> output = outputFile , <EOL> logFile = logFile , <EOL> argArray = [ "<STR_LIT>" ] ) <EOL> pipeline . addStage ( res ) <EOL> return pipeline <EOL> class createQualityControlImages ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> inputFiles , <EOL> createMontage = True , <EOL> montageOutPut = None , <EOL> scalingFactor = <NUM_LIT:20> , <EOL> message = "<STR_LIT>" ) : <EOL> self . p = Pipeline ( ) <EOL> self . individualImages = [ ] <EOL> self . individualImagesLabeled = [ ] <EOL> self . message = message <EOL> if createMontage and montageOutPut == None : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> for inFile in inputFiles : <EOL> if isFileHandler ( inFile ) : <EOL> inputToMincpik = inFile . getLastBasevol ( ) <EOL> outputMincpik = createBaseName ( inFile . tmpDir , <EOL> removeBaseAndExtension ( inputToMincpik ) + "<STR_LIT>" ) <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , scalingFactor , <EOL> "<STR_LIT>" , <EOL> InputFile ( inputToMincpik ) , <EOL> OutputFile ( outputMincpik ) ] <EOL> mincpik = CmdStage ( cmd ) <EOL> mincpik . setLogFile ( LogFile ( logFromFile ( inFile . logDir , outputMincpik ) ) ) <EOL> self . p . addStage ( mincpik ) <EOL> self . individualImages . append ( outputMincpik ) <EOL> outputConvert = createBaseName ( inFile . tmpDir , <EOL> removeBaseAndExtension ( inputToMincpik ) + "<STR_LIT>" ) <EOL> cmdConvert = [ "<STR_LIT>" , "<STR_LIT>" , inFile . basename , <EOL> InputFile ( outputMincpik ) , <EOL> OutputFile ( outputConvert ) ] <EOL> convertAddLabel = CmdStage ( cmdConvert ) <EOL> convertAddLabel . setLogFile ( LogFile ( logFromFile ( inFile . logDir , outputConvert ) ) ) <EOL> self . p . addStage ( convertAddLabel ) <EOL> self . individualImagesLabeled . append ( outputConvert ) <EOL> if createMontage : <EOL> cmdmontage = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] + map ( InputFile , self . individualImagesLabeled ) + [ OutputFile ( montageOutPut ) ] <EOL> montage = CmdStage ( cmdmontage ) <EOL> montage . setLogFile ( splitext ( montageOutPut ) [ <NUM_LIT:0> ] + "<STR_LIT>" ) <EOL> message_to_print = "<STR_LIT>" <EOL> message_to_print += "<STR_LIT>" <EOL> message_to_print += "<STR_LIT>" % self . message <EOL> message_to_print += "<STR_LIT>" % ( montageOutPut ) <EOL> message_to_print += "<STR_LIT>" <EOL> def printMessageForMontage ( ) : <EOL> print ( message_to_print ) <EOL> montage . finished_hooks . append ( <EOL> lambda : printMessageForMontage ( ) ) <EOL> self . p . addStage ( montage ) </s>
