Exemple #1
0
    def buildPipeline(self):
        # Run lsq12 registration prior to non-linear
        self.lsq12Params = mp.setLSQ12MinctraccParams(
            self.fileRes,
            subject_matter=self.subject_matter,
            reg_protocol=self.lsq12_protocol)
        lsq12reg = lsq12.LSQ12(self.inputFH,
                               self.targetFH,
                               blurs=self.lsq12Params.blurs,
                               step=self.lsq12Params.stepSize,
                               gradient=self.lsq12Params.useGradient,
                               simplex=self.lsq12Params.simplex,
                               w_translations=self.lsq12Params.w_translations,
                               defaultDir=self.defaultDir)
        self.p.addPipeline(lsq12reg.p)

        #Resample using final LSQ12 transform and reset last base volume.
        res = ma.mincresample(self.inputFH,
                              self.targetFH,
                              likeFile=self.targetFH,
                              argArray=["-sinc"])
        self.p.addStage(res)
        self.inputFH.setLastBasevol(res.outputFiles[0])
        lsq12xfm = self.inputFH.getLastXfm(self.targetFH)

        #Get registration parameters from nlin protocol, blur and register
        #Assume a SINGLE generation here.
        self.nlinParams = mp.setOneGenMincANTSParams(
            self.fileRes, reg_protocol=self.nlin_protocol)
        for b in self.nlinParams.blurs:
            for j in b:
                #Note that blurs for ANTS params in an array of arrays.
                if j != -1:
                    self.p.addStage(ma.blur(self.targetFH, j, gradient=True))
                    self.p.addStage(ma.blur(self.inputFH, j, gradient=True))

        sp = ma.mincANTS(
            self.inputFH,
            self.targetFH,
            defaultDir=self.defaultDir,
            blur=self.nlinParams.blurs[0],
            gradient=self.nlinParams.gradient[0],
            similarity_metric=self.nlinParams.similarityMetric[0],
            weight=self.nlinParams.weight[0],
            iterations=self.nlinParams.iterations[0],
            radius_or_histo=self.nlinParams.radiusHisto[0],
            transformation_model=self.nlinParams.transformationModel[0],
            regularization=self.nlinParams.regularization[0],
            useMask=self.nlinParams.useMask[0])
        self.p.addStage(sp)
        nlinXfm = sp.outputFiles[0]
        #Reset last base volume to original input for future registrations.
        self.inputFH.setLastBasevol(setToOriginalInput=True)
        #Concatenate transforms to get final lsq12 + nlin. Register volume handles naming and setting of lastXfm
        output = self.inputFH.registerVolume(self.targetFH, "transforms")
        xc = ma.xfmConcat([lsq12xfm, nlinXfm], output,
                          fh.logFromFile(self.inputFH.logDir, output))
        self.p.addStage(xc)
Exemple #2
0
 def buildPipeline(self):
     # Run lsq12 registration prior to non-linear
     self.lsq12Params = mp.setLSQ12MinctraccParams(self.fileRes, 
                                                   subject_matter=self.subject_matter,
                                                   reg_protocol=self.lsq12_protocol)
     lsq12reg = lsq12.LSQ12(self.inputFH, 
                            self.targetFH, 
                            blurs=self.lsq12Params.blurs,
                            step=self.lsq12Params.stepSize,
                            gradient=self.lsq12Params.useGradient,
                            simplex=self.lsq12Params.simplex,
                            w_translations=self.lsq12Params.w_translations,
                            defaultDir=self.defaultDir)
     self.p.addPipeline(lsq12reg.p)
     
     #Resample using final LSQ12 transform and reset last base volume. 
     res = ma.mincresample(self.inputFH, self.targetFH, likeFile=self.targetFH, argArray=["-sinc"])   
     self.p.addStage(res)
     self.inputFH.setLastBasevol(res.outputFiles[0])
     lsq12xfm = self.inputFH.getLastXfm(self.targetFH)
     
     #Get registration parameters from nlin protocol, blur and register
     #Assume a SINGLE generation here. 
     self.nlinParams = mp.setOneGenMincANTSParams(self.fileRes, reg_protocol=self.nlin_protocol)
     for b in self.nlinParams.blurs:
         for j in b:
             #Note that blurs for ANTS params in an array of arrays. 
             if j != -1:            
                 self.p.addStage(ma.blur(self.targetFH, j, gradient=True))
                 self.p.addStage(ma.blur(self.inputFH, j, gradient=True))
                 
     sp = ma.mincANTS(self.inputFH,
                      self.targetFH,
                      defaultDir=self.defaultDir, 
                      blur=self.nlinParams.blurs[0],
                      gradient=self.nlinParams.gradient[0],
                      similarity_metric=self.nlinParams.similarityMetric[0],
                      weight=self.nlinParams.weight[0],
                      iterations=self.nlinParams.iterations[0],
                      radius_or_histo=self.nlinParams.radiusHisto[0],
                      transformation_model=self.nlinParams.transformationModel[0], 
                      regularization=self.nlinParams.regularization[0],
                      useMask=self.nlinParams.useMask[0])
     self.p.addStage(sp)
     nlinXfm = sp.outputFiles[0]
     #Reset last base volume to original input for future registrations.
     self.inputFH.setLastBasevol(setToOriginalInput=True)
     #Concatenate transforms to get final lsq12 + nlin. Register volume handles naming and setting of lastXfm
     output = self.inputFH.registerVolume(self.targetFH, "transforms")
     xc = ma.xfmConcat([lsq12xfm, nlinXfm], output, fh.logFromFile(self.inputFH.logDir, output))
     self.p.addStage(xc)
Exemple #3
0
    def __init__(self,
                 inputArray,
                 outputDir,
                 likeFile=None,
                 maxPairs=None,
                 lsq12_protocol=None,
                 subject_matter=None,
                 resolution=None):
        self.p = Pipeline()
        """Initial inputs should be an array of fileHandlers with lastBasevol in lsq12 space"""
        self.inputs = inputArray
        """Output directory should be _nlin """
        self.lsq12Dir = outputDir
        """likeFile for resampling"""
        self.likeFile = likeFile
        """Maximum number of pairs to calculate"""
        self.maxPairs = maxPairs
        """Final lsq12 average"""
        self.lsq12Avg = None
        """Final lsq12 average file handler (e.g. the file handler associated with lsq12Avg)"""
        self.lsq12AvgFH = None
        """ Dictionary of lsq12 average transforms, which will include one per input.
            Key is input file handler and value is string pointing to final average lsq12
            transform for that particular subject. 
            These xfms may be used subsequently for statistics calculations. """
        self.lsq12AvgXfms = {}
        self.fileRes = None
        """Create the blurring resolution from the file resolution"""
        if (subject_matter == None and resolution == None):
            print "\nError: the FullLSQ12 module was called without specifying the resolution that it should be run at, and without specifying a subject matter. Please indicate one of the two. Exiting...\n"
            sys.exit()
        elif (subject_matter and resolution):
            # subject matter has precedence over resolution
            self.fileRes = None
        elif resolution:
            self.fileRes = resolution
        """"Set up parameter array"""
        self.lsq12Params = mp.setLSQ12MinctraccParams(
            self.fileRes,
            subject_matter=subject_matter,
            reg_protocol=lsq12_protocol)
        self.blurs = self.lsq12Params.blurs
        self.stepSize = self.lsq12Params.stepSize
        self.useGradient = self.lsq12Params.useGradient
        self.simplex = self.lsq12Params.simplex
        self.w_translations = self.lsq12Params.w_translations
        self.generations = self.lsq12Params.generations

        # Create new lsq12 group for each input prior to registration
        for input in self.inputs:
            input.newGroup(groupName="lsq12")
Exemple #4
0
    def __init__(self,
                 inputArray,
                 outputDir,
                 likeFile=None,
                 maxPairs=None,
                 lsq12_protocol=None,
                 subject_matter=None):
        self.p = Pipeline()
        """Initial inputs should be an array of fileHandlers with lastBasevol in lsq12 space"""
        self.inputs = inputArray
        """Output directory should be _nlin """
        self.lsq12Dir = outputDir
        """likeFile for resampling"""
        self.likeFile = likeFile
        """Maximum number of pairs to calculate"""
        self.maxPairs = maxPairs
        """Final lsq12 average"""
        self.lsq12Avg = None
        """Final lsq12 average file handler (e.g. the file handler associated with lsq12Avg)"""
        self.lsq12AvgFH = None
        """ Dictionary of lsq12 average transforms, which will include one per input.
            Key is input file handler and value is string pointing to final average lsq12
            transform for that particular subject. 
            These xfms may be used subsequently for statistics calculations. """
        self.lsq12AvgXfms = {}
        """Create the blurring resolution from the file resolution"""
        if (subject_matter == None and lsq12_protocol == None):
            self.fileRes = rf.returnFinestResolution(self.inputs[0])
        else:
            self.fileRes = None
        """"Set up parameter array"""
        self.lsq12Params = mp.setLSQ12MinctraccParams(
            self.fileRes,
            subject_matter=subject_matter,
            reg_protocol=lsq12_protocol)
        self.blurs = self.lsq12Params.blurs
        self.stepSize = self.lsq12Params.stepSize
        self.useGradient = self.lsq12Params.useGradient
        self.simplex = self.lsq12Params.simplex
        self.w_translations = self.lsq12Params.w_translations
        self.generations = self.lsq12Params.generations

        # Create new lsq12 group for each input prior to registration
        for i in range(len(self.inputs)):
            self.inputs[i].newGroup(groupName="lsq12")
Exemple #5
0
    def buildPipeline(self):

        # Do LSQ12 alignment prior to non-linear stages if desired
        if self.includeLinear:
            self.lsq12Params = mp.setLSQ12MinctraccParams(
                self.fileRes,
                subject_matter=self.subject_matter,
                reg_protocol=self.lsq12_protocol)
            lsq12reg = lsq12.LSQ12(
                self.inputFH,
                self.targetFH,
                blurs=self.lsq12Params.blurs,
                step=self.lsq12Params.stepSize,
                gradient=self.lsq12Params.useGradient,
                simplex=self.lsq12Params.simplex,
                w_translations=self.lsq12Params.w_translations,
                defaultDir=self.defaultDir)
            self.p.addPipeline(lsq12reg.p)

        # create the nonlinear registrations
        self.nlinParams = mp.setNlinMinctraccParams(
            self.fileRes, reg_protocol=self.nlin_protocol)
        for b in self.nlinParams.blurs:
            if b != -1:
                self.p.addStage(ma.blur(self.inputFH, b, gradient=True))
                self.p.addStage(ma.blur(self.targetFH, b, gradient=True))
        for i in range(len(self.nlinParams.stepSize)):
            #For the final stage, make sure the output directory is transforms.
            if i == (len(self.nlinParams.stepSize) - 1):
                self.defaultDir = "transforms"
            nlinStage = ma.minctracc(
                self.inputFH,
                self.targetFH,
                defaultDir=self.defaultDir,
                blur=self.nlinParams.blurs[i],
                gradient=self.nlinParams.useGradient[i],
                iterations=self.nlinParams.iterations[i],
                step=self.nlinParams.stepSize[i],
                w_translations=self.nlinParams.w_translations[i],
                simplex=self.nlinParams.simplex[i],
                optimization=self.nlinParams.optimization[i])
            self.p.addStage(nlinStage)
Exemple #6
0
 def buildPipeline(self):
         
     # Do LSQ12 alignment prior to non-linear stages if desired
     if self.includeLinear: 
         self.lsq12Params = mp.setLSQ12MinctraccParams(self.fileRes,
                                         subject_matter=self.subject_matter,
                                         reg_protocol=self.lsq12_protocol)
         lsq12reg = lsq12.LSQ12(self.inputFH, 
                                self.targetFH, 
                                blurs=self.lsq12Params.blurs,
                                step=self.lsq12Params.stepSize,
                                gradient=self.lsq12Params.useGradient,
                                simplex=self.lsq12Params.simplex,
                                w_translations=self.lsq12Params.w_translations,
                                defaultDir=self.defaultDir)
         self.p.addPipeline(lsq12reg.p)
     
     # create the nonlinear registrations
     self.nlinParams = mp.setNlinMinctraccParams(self.fileRes, reg_protocol=self.nlin_protocol)
     for b in self.nlinParams.blurs: 
         if b != -1:           
             self.p.addStage(ma.blur(self.inputFH, b, gradient=True))
             self.p.addStage(ma.blur(self.targetFH, b, gradient=True))
     for i in range(len(self.nlinParams.stepSize)):
         #For the final stage, make sure the output directory is transforms.
         if i == (len(self.nlinParams.stepSize) - 1):
             self.defaultDir = "transforms"
         nlinStage = ma.minctracc(self.inputFH, 
                                  self.targetFH,
                                  defaultDir=self.defaultDir,
                                  blur=self.nlinParams.blurs[i],
                                  gradient=self.nlinParams.useGradient[i],
                                  iterations=self.nlinParams.iterations[i],
                                  step=self.nlinParams.stepSize[i],
                                  w_translations=self.nlinParams.w_translations[i],
                                  simplex=self.nlinParams.simplex[i],
                                  memory=self.nlinParams.memory[i] if self.nlinParams.memory else None,
                                  optimization=self.nlinParams.optimization[i])
         self.p.addStage(nlinStage)
    def run(self):
        options = self.options
        args = self.args

        # Setup output directories for two-level model building:
        (subjectDirs,
         dirs) = rf.setupTwoLevelDirectories(args[0],
                                             self.outputDir,
                                             options.pipeline_name,
                                             module="ALL")

        # read in files from CSV
        subjects = rf.setupSubjectHash(args[0], subjectDirs, options.mask_dir)

        #firstlevelNlins stores per subject NLIN avgs, subjStats stores first level stats, to be resampled at the end
        firstlevelNlins = []
        subjStats = []

        ### first level of registrations: register within subject
        for i in range(len(subjects)):
            baseVol = subjects[i][0].getLastBasevol()
            subjBase = splitext(split(baseVol)[1])[0]
            if options.input_space == "native":
                initModel, targetPipeFH = rf.setInitialTarget(
                    options.init_model, options.lsq6_target,
                    subjectDirs[i].lsq6Dir, self.outputDir)
                #LSQ6 MODULE, NUC and INORM
                runLSQ6NucInorm = lsq6.LSQ6NUCInorm(subjects[i], targetPipeFH,
                                                    initModel,
                                                    subjectDirs[i].lsq6Dir,
                                                    options)
                self.pipeline.addPipeline(runLSQ6NucInorm.p)
            if options.input_space == "native" or options.input_space == "lsq6":
                #LSQ12+NLIN (registration starts here or is run after LSQ6)
                if options.input_space == "lsq6":
                    initModel = None
                lsq12Nlin = mm.FullIterativeLSQ12Nlin(subjects[i],
                                                      subjectDirs[i],
                                                      options,
                                                      avgPrefix=subjBase,
                                                      initModel=initModel)
                self.pipeline.addPipeline(lsq12Nlin.p)
                finalNlin = lsq12Nlin.nlinFH
                #If no protocols are specified, use same lsq12 and nlin protocols as for first level registration
                if not options.lsq12_protocol:
                    options.lsq12_protocol = lsq12Nlin.lsq12Params
                if not options.nlin_protocol:
                    options.nlin_protocol = lsq12Nlin.nlinParams
            elif options.input_space == "lsq12":
                #If inputs in lsq12 space, run NLIN only
                lsq12AvgFile = abspath(
                    subjectDirs[i].lsq12Dir) + "/" + subjBase + "-lsq12avg.mnc"
                nlinObj = nl.initializeAndRunNLIN(
                    subjectDirs[i].lsq12Dir,
                    subjects[i],
                    subjectDirs[i].nlinDir,
                    avgPrefix=subjBase,
                    createAvg=True,
                    targetAvg=lsq12AvgFile,
                    targetMask=options.target_mask,
                    nlin_protocol=options.nlin_protocol,
                    reg_method=options.reg_method)

                self.pipeline.addPipeline(nlinObj.p)
                finalNlin = nlinObj.nlinAverages[-1]
                # If no protocols are specified, get lsq12 based on resolution of one of the existing input files.
                # Use same nlin protocol as the one we ran previously.
                if not options.lsq12_protocol:
                    if not options.lsq12_subject_matter:
                        fileRes = rf.returnFinestResolution(subjects[i][0])
                    options.lsq12_protocol = mp.setLSQ12MinctraccParams(
                        fileRes, subject_matter=options.lsq12_subject_matter)
                if not options.nlin_protocol:
                    options.nlin_protocol = nlinObj.nlinParams
            else:
                print "--input-space can only be native, lsq6 or lsq12. You specified: " + str(
                    options.input_space)
                sys.exit()

            # add the last NLIN average to the volumes that will proceed to step 2
            firstlevelNlins.append(finalNlin)
            if options.calc_stats:
                tmpStats = []
                for s in subjects[i]:
                    stats = st.CalcStats(s, finalNlin, options.stats_kernels)
                    self.pipeline.addPipeline(stats.p)
                    tmpStats.append(stats)
                subjStats.append(tmpStats)
        # second level of registrations: register final averages from first level
        # TODO: Allow for LSQ6 reg first, or just NLIN. Right now, we allow LSQ12+NLIN only
        firstLevelNlinsNewFH = []
        for nlin in firstlevelNlins:
            nlinFH = rfh.RegistrationPipeFH(nlin.getLastBasevol(),
                                            mask=nlin.getMask(),
                                            basedir=dirs.processedDir)
            firstLevelNlinsNewFH.append(nlinFH)
        lsq12Nlin = mm.FullIterativeLSQ12Nlin(firstLevelNlinsNewFH,
                                              dirs,
                                              options,
                                              avgPrefix="second_level")
        self.pipeline.addPipeline(lsq12Nlin.p)
        finalNlin = lsq12Nlin.nlinFH
        initialTarget = lsq12Nlin.initialTarget

        if options.calc_stats:
            for s in firstLevelNlinsNewFH:
                stats = st.CalcStats(s, finalNlin, options.stats_kernels)
                self.pipeline.addPipeline(stats.p)
                # now resample the stats files from the first level registration to the common space
                # created by the second level of registration
                for i in range(len(subjects)):
                    for s in range(len(subjects[i])):
                        # get the last xfm from the second level registrations
                        xfm = firstLevelNlinsNewFH[i].getLastXfm(finalNlin)
                        p = mm.resampleToCommon(xfm, subjects[i][s],
                                                subjStats[i][s].statsGroup,
                                                options.stats_kernels,
                                                initialTarget)
                        self.pipeline.addPipeline(p)
 def run(self):
     options = self.options
     args = self.args
     
     # Setup output directories for two-level model building: 
     (subjectDirs, dirs) = rf.setupTwoLevelDirectories(args[0], self.outputDir, options.pipeline_name, module="ALL")
     
     # read in files from CSV
     subjects = rf.setupSubjectHash(args[0], subjectDirs, options.mask_dir)
     
     #firstlevelNlins stores per subject NLIN avgs, subjStats stores first level stats, to be resampled at the end
     firstlevelNlins = [] 
     subjStats = [] 
     
     ### first level of registrations: register within subject
     for i in range(len(subjects)):   
         baseVol = subjects[i][0].getLastBasevol()
         subjBase = splitext(split(baseVol)[1])[0]
         if options.input_space == "native":
             initModel, targetPipeFH = rf.setInitialTarget(options.init_model, 
                                                           options.lsq6_target, 
                                                           subjectDirs[i].lsq6Dir,
                                                           self.outputDir,
                                                           options.pipeline_name)
             #LSQ6 MODULE, NUC and INORM
             runLSQ6NucInorm = lsq6.LSQ6NUCInorm(subjects[i],
                                                 targetPipeFH,
                                                 initModel, 
                                                 subjectDirs[i].lsq6Dir, 
                                                 options)
             self.pipeline.addPipeline(runLSQ6NucInorm.p)
         if options.input_space=="native" or options.input_space=="lsq6":
             # LSQ12+NLIN (registration starts here or is run after LSQ6)
             if options.input_space == "lsq6":
                 initModel=None
             lsq12Nlin = mm.FullIterativeLSQ12Nlin(subjects[i], 
                                                   subjectDirs[i], 
                                                   options, 
                                                   avgPrefix=subjBase,
                                                   initModel=initModel)
             self.pipeline.addPipeline(lsq12Nlin.p)
             finalNlin = lsq12Nlin.nlinFH
             #If no protocols are specified, use same lsq12 and nlin protocols as for first level registration
             if not options.lsq12_protocol:
                 options.lsq12_protocol = lsq12Nlin.lsq12Params
             if not options.nlin_protocol:
                 options.nlin_protocol = lsq12Nlin.nlinParams
         elif options.input_space=="lsq12":
             #If inputs in lsq12 space, run NLIN only 
             lsq12AvgFile = abspath(subjectDirs[i].lsq12Dir) + "/" + subjBase + "-lsq12avg.mnc"
             nlinObj = nl.initializeAndRunNLIN(subjectDirs[i].lsq12Dir,
                                               subjects[i],
                                               subjectDirs[i].nlinDir,
                                               avgPrefix=subjBase,
                                               createAvg=True,
                                               targetAvg=lsq12AvgFile,
                                               targetMask=options.target_mask,
                                               nlin_protocol=options.nlin_protocol,
                                               reg_method=options.reg_method)
     
             self.pipeline.addPipeline(nlinObj.p)
             finalNlin = nlinObj.nlinAverages[-1]
             # If no protocols are specified, get lsq12 based on resolution of one of the existing input files.
             # Use same nlin protocol as the one we ran previously. 
             if not options.lsq12_protocol: 
                 if not options.lsq12_subject_matter:
                     fileRes = rf.returnFinestResolution(subjects[i][0])
                 options.lsq12_protocol = mp.setLSQ12MinctraccParams(fileRes, 
                                                                     subject_matter=options.lsq12_subject_matter)
             if not options.nlin_protocol:
                 options.nlin_protocol = nlinObj.nlinParams
         else:
             print("--input-space can only be native, lsq6 or lsq12. You specified: " + str(options.input_space))
             sys.exit()
         
         # add the last NLIN average to the volumes that will proceed to step 2
         firstlevelNlins.append(finalNlin)
         if options.calc_stats:
             tmpStats=[]
             for s in subjects[i]:
                 stats = st.CalcStats(s, finalNlin, options.stats_kernels)
                 self.pipeline.addPipeline(stats.p)
                 tmpStats.append(stats)
             subjStats.append(tmpStats)
     # second level of registrations: register final averages from first level 
     # TODO: Allow for LSQ6 reg first, or just NLIN. Right now, we allow LSQ12+NLIN only
     firstLevelNlinsNewFH = []
     for nlin in firstlevelNlins:
         nlinFH = rfh.RegistrationPipeFH(nlin.getLastBasevol(), mask=nlin.getMask(), basedir=dirs.processedDir)
         firstLevelNlinsNewFH.append(nlinFH)
     # the following call needs to figure out at what resolution the LSQ12 and NLIN stages
     # are supposed to be run. For this reason (if no subject matter is specified), 
     # we will pass along the initial model
     lsq12Nlin = mm.FullIterativeLSQ12Nlin(firstLevelNlinsNewFH, 
                                           dirs, 
                                           options, 
                                           avgPrefix="second_level",
                                           initModel=initModel)
     self.pipeline.addPipeline(lsq12Nlin.p)
     finalNlin = lsq12Nlin.nlinFH
     initialTarget = lsq12Nlin.initialTarget
     
     if options.calc_stats:
         for s in firstLevelNlinsNewFH:
             stats = st.CalcStats(s, finalNlin, options.stats_kernels)
             self.pipeline.addPipeline(stats.p)
             # now resample the stats files from the first level registration to the common space
             # created by the second level of registration
             for i in range(len(subjects)):
                 for s in range(len(subjects[i])):
                     # get the last xfm from the second level registrations
                     xfm = firstLevelNlinsNewFH[i].getLastXfm(finalNlin)
                     p = mm.resampleToCommon(xfm,
                                             subjects[i][s], 
                                             subjStats[i][s].statsGroup, 
                                             options.stats_kernels, 
                                             initialTarget)
                     self.pipeline.addPipeline(p)