Esempio n. 1
0
    def __init__(self,
                 inputArray,
                 targetFH,
                 nlinOutputDir,
                 avgPrefix,
                 nlin_protocol,
                 resolution=None):
        self.p = Pipeline()
        """Initial inputs should be an array of fileHandlers with lastBasevol in lsq12 space"""
        self.inputs = inputArray
        """Initial target should be the file handler for the lsq12 average"""
        self.target = targetFH
        """Output directory should be _nlin """
        self.nlinDir = nlinOutputDir
        """Prefix to pre-pend to averages at each generation"""
        self.avgPrefix = avgPrefix
        """Empty array that we will fill with averages as we create them"""
        self.nlinAverages = []
        """Create the blurring resolution from the file resolution"""
        self.fileRes = resolution

        if (nlin_protocol == None and resolution == None):
            print "\nError: NLIN module was initialized without a protocol, and without a resolution for the registrations to be run at. Please specify one of the two. Exiting\n"
            sys.exit()
        if (nlin_protocol and resolution):
            # we should have the nlin_protocol be able to overwrite the given resolution:
            self.fileRes = None

        # Create new nlin group for each input prior to registration
        for i in range(len(self.inputs)):
            self.inputs[i].newGroup(groupName="nlin")
Esempio n. 2
0
    def __init__(self,
                 inputFH,
                 targetFH,
                 blurs=[0.3, 0.2, 0.15],
                 step=[1, 0.5, 0.333333333333333],
                 gradient=[False, True, False],
                 simplex=[3, 1.5, 1],
                 w_translations=[0.4, 0.4, 0.4],
                 defaultDir="tmp"):

        # TO DO: Might want to take this out and pass in # of generations, since
        # checking happens there.
        if len(blurs) == len(step) == len(simplex):
            # do nothing - all lengths are the same and we're therefore happy
            pass
        else:
            logger.error(
                "The same number of entries are required for blurs, step, and simplex in LSQ12"
            )
            sys.exit()

        self.p = Pipeline()
        self.inputFH = inputFH
        self.targetFH = targetFH
        self.blurs = blurs
        self.step = step
        self.blurs = blurs
        self.gradient = gradient
        self.simplex = simplex
        self.w_translations = w_translations
        self.defaultDir = defaultDir

        self.blurFiles()
        self.buildPipeline()
Esempio n. 3
0
    def __init__(self,
                 inputFH,
                 targetFH,
                 lsq12_protocol=None,
                 nlin_protocol=None,
                 includeLinear=True,
                 subject_matter=None,
                 defaultDir="tmp"):

        self.p = Pipeline()
        self.inputFH = inputFH
        self.targetFH = targetFH
        self.lsq12_protocol = lsq12_protocol
        self.nlin_protocol = nlin_protocol
        self.includeLinear = includeLinear
        self.subject_matter = subject_matter
        self.defaultDir = defaultDir

        if ((self.lsq12_protocol == None and self.subject_matter == None)
                or self.nlin_protocol == None):
            # the resolution of the registration should be based on the target
            self.fileRes = rf.returnFinestResolution(self.targetFH)
        else:
            self.fileRes = None

        self.buildPipeline()
Esempio n. 4
0
def MAGeTRegister(inputFH,
                  templateFH,
                  regMethod,
                  name="initial",
                  createMask=False,
                  lsq12_protocol=None,
                  nlin_protocol=None):

    p = Pipeline()
    if createMask:
        defaultDir = "tmp"
    else:
        defaultDir = "transforms"
    if regMethod == "minctracc":
        sp = HierarchicalMinctracc(inputFH,
                                   templateFH,
                                   lsq12_protocol=lsq12_protocol,
                                   nlin_protocol=nlin_protocol,
                                   defaultDir=defaultDir)
        p.addPipeline(sp.p)
    elif regMethod == "mincANTS":
        register = LSQ12ANTSNlin(inputFH,
                                 templateFH,
                                 lsq12_protocol=lsq12_protocol,
                                 nlin_protocol=nlin_protocol,
                                 defaultDir=defaultDir)
        p.addPipeline(register.p)

    rp = LabelAndFileResampling(inputFH,
                                templateFH,
                                name=name,
                                createMask=createMask)
    p.addPipeline(rp.p)

    return (p)
Esempio n. 5
0
    def __init__(self,
                 inputPipeFH,
                 templatePipeFH,
                 name="initial",
                 createMask=False):
        self.p = Pipeline()
        self.name = name

        if createMask:
            resampleDefault = "tmp"
            labelsDefault = "tmp"
        else:
            resampleDefault = "resampled"
            labelsDefault = "labels"

        # Resample all inputLabels
        inputLabelArray = templatePipeFH.returnLabels(True)
        if len(inputLabelArray) > 0:
            """ for the initial registration, resulting labels should be added
                to inputLabels array for subsequent pairwise registration
                otherwise labels should be added to labels array for voting """
            if self.name == "initial":
                addOutputToInputLabels = True
            else:
                addOutputToInputLabels = False
            for i in range(len(inputLabelArray)):
                """Note: templatePipeFH and inputPipeFH have the reverse order
                   from how they are passed into this function. This is intentional
                   because the mincresample classes use the first argument as the 
                   one from which to get the file to be resampled. Here, either the 
                   mask or labels to be resampled come from the template."""
                if createMask:
                    resampleStage = ma.mincresampleMask(
                        templatePipeFH,
                        inputPipeFH,
                        defaultDir=labelsDefault,
                        likeFile=inputPipeFH,
                        argArray=["-invert"],
                        outputLocation=inputPipeFH,
                        labelIndex=i,
                        setInputLabels=addOutputToInputLabels)
                else:
                    resampleStage = ma.mincresampleLabels(
                        templatePipeFH,
                        inputPipeFH,
                        defaultDir=labelsDefault,
                        likeFile=inputPipeFH,
                        argArray=["-invert"],
                        outputLocation=inputPipeFH,
                        labelIndex=i,
                        setInputLabels=addOutputToInputLabels)
                self.p.addStage(resampleStage)
            # resample files
            resampleStage = ma.mincresample(templatePipeFH,
                                            inputPipeFH,
                                            defaultDir=resampleDefault,
                                            likeFile=inputPipeFH,
                                            argArray=["-invert"],
                                            outputLocation=inputPipeFH)
            self.p.addStage(resampleStage)
Esempio n. 6
0
    def __init__(
            self,
            targetOutputDir,  #Output directory for files related to initial target (often _lsq12)
            inputFiles,
            nlinDir,
            avgPrefix,  #Prefix for nlin-1.mnc, ... nlin-k.mnc 
            createAvg=True,  #True=call mincAvg, False=targetAvg already exists
            targetAvg=None,  #Optional path to initial target - passing name does not guarantee existence
            targetMask=None,  #Optional path to mask for initial target
            nlin_protocol=None,
            reg_method=None):
        self.p = Pipeline()
        self.targetOutputDir = targetOutputDir
        self.inputFiles = inputFiles
        self.nlinDir = nlinDir
        self.avgPrefix = avgPrefix
        self.createAvg = createAvg
        self.targetAvg = targetAvg
        self.targetMask = targetMask
        self.nlin_protocol = nlin_protocol
        self.reg_method = reg_method

        # setup initialTarget (if needed) and initialize non-linear module
        self.setupTarget()
        self.initNlinModule()

        #iterate through non-linear registration and setup averages
        self.nlinModule.iterate()
        self.p.addPipeline(self.nlinModule.p)
        self.nlinAverages = self.nlinModule.nlinAverages
        self.nlinParams = self.nlinModule.nlinParams
Esempio n. 7
0
def resampleToCommon(xfm, FH, statsGroup, statsKernels, nlinFH):
    blurs = []
    if isinstance(statsKernels, list):
        blurs = statsKernels
    elif isinstance(statsKernels, str):
        for i in statsKernels.split(","):
            blurs.append(float(i))
    else:
        print "Improper type of blurring kernels specified for stats calculation: " + str(statsKernels)
        sys.exit()
    pipeline = Pipeline()
    outputDirectory = FH.statsDir
    filesToResample = []
    for b in blurs:
        filesToResample.append(statsGroup.relativeJacobians[b])
        if statsGroup.absoluteJacobians:
            filesToResample.append(statsGroup.absoluteJacobians[b])
    for f in filesToResample:
        outputBase = fh.removeBaseAndExtension(f).split(".mnc")[0]
        outputFile = fh.createBaseName(outputDirectory, outputBase + "_common" + ".mnc")
        logFile = fh.logFromFile(FH.logDir, outputFile)
        targetAndLike=nlinFH.getLastBasevol()
        res = ma.mincresample(f, 
                              targetAndLike,
                              likeFile=targetAndLike,
                              transform=xfm,
                              output=outputFile,
                              logFile=logFile,
                              argArray=["-sinc"]) 
        pipeline.addStage(res)
    
    return pipeline
Esempio n. 8
0
def maskFiles(FH, isAtlas, numAtlases=1):
    """ Assume that if there is more than one atlas, multiple
        masks were generated and we need to perform a voxel_vote. 
        Otherwise, assume we are using inputLabels from crossing with
        only one atlas. 
    """
    #MF TODO: Make this more general to handle pairwise option.
    p = Pipeline()
    if not isAtlas:
        if numAtlases > 1:
            voxel = voxelVote(FH, False, True)
            p.addStage(voxel)
            mincMathInput = voxel.outputFiles[0]
        else:
            mincMathInput = FH.returnLabels(True)[0]
        FH.setMask(mincMathInput)
    else:
        mincMathInput = FH.getMask()
    mincMathOutput = fh.createBaseName(FH.resampledDir, FH.basename)
    mincMathOutput += "_masked.mnc"
    logFile = fh.logFromFile(FH.logDir, mincMathOutput)
    cmd = ["mincmath"] + ["-clobber"] + ["-mult"]
    cmd += [InputFile(mincMathInput)] + [InputFile(FH.getLastBasevol())]
    cmd += [OutputFile(mincMathOutput)]
    mincMath = CmdStage(cmd)
    mincMath.setLogFile(LogFile(logFile))
    p.addStage(mincMath)
    FH.setLastBasevol(mincMathOutput)
    return (p)
Esempio n. 9
0
def MAGeTMask(atlases,
              inputs,
              numAtlases,
              regMethod,
              lsq12_protocol=None,
              nlin_protocol=None):
    """ Masking algorithm is as follows:
        1. Run HierarchicalMinctracc or mincANTS with mask=True, 
           using masks instead of labels. 
        2. Do voxel voting to find the best mask. (Or, if single atlas,
            use that transform)
        3. mincMath to multiply original input by mask to get _masked.mnc file
            (This is done for both atlases and inputs, though for atlases, voxel
             voting is not required.)
        4. Replace lastBasevol with masked version, since once we have created
            mask, we no longer care about unmasked version. 
        5. Clear out labels arrays, which were used to keep track of masks,
            as we want to re-set them for actual labels.
                
        Note: All data will be placed in a newly created masking directory
        to keep it separate from data generated during actual MAGeT. 
        """
    p = Pipeline()
    for atlasFH in atlases:
        maskDirectoryStructure(atlasFH, masking=True)
    for inputFH in inputs:
        maskDirectoryStructure(inputFH, masking=True)
        for atlasFH in atlases:
            sp = MAGeTRegister(inputFH,
                               atlasFH,
                               regMethod,
                               name="initial",
                               createMask=True,
                               lsq12_protocol=lsq12_protocol,
                               nlin_protocol=nlin_protocol)
            p.addPipeline(sp)
    """ Prior to final masking, set log and tmp directories as they were."""
    for atlasFH in atlases:
        """Retrieve labels for use in new group. Assume only one"""
        labels = atlasFH.returnLabels(True)
        maskDirectoryStructure(atlasFH, masking=False)
        mp = maskFiles(atlasFH, True)
        p.addPipeline(mp)
        atlasFH.newGroup()
        atlasFH.addLabels(labels[0], inputLabel=True)
    for inputFH in inputs:
        maskDirectoryStructure(inputFH, masking=False)
        mp = maskFiles(inputFH, False, numAtlases)
        p.addPipeline(mp)
        # this will remove the "inputLabels"; labels that
        # come directly from the atlas library
        inputFH.clearLabels(True)
        # this will remove the "labels"; second generation
        # labels. I.e. labels from labels from the atlas library
        inputFH.clearLabels(False)
        inputFH.newGroup()
    return (p)
Esempio n. 10
0
 def __init__(self, inputs, dirs, options, avgPrefix=None, initModel=None):
     self.inputs = inputs
     self.dirs = dirs
     self.options = options
     self.avgPrefix = avgPrefix
     self.initModel = initModel
     self.nlinFH = None
     
     self.p = Pipeline()
     
     self.buildPipeline()
Esempio n. 11
0
 def __init__(self, subjects, timePoint, nlinFH, statsKernels, commonName):
     
     self.subjects = subjects
     self.timePoint = timePoint
     self.nlinFH = nlinFH
     self.blurs = [] 
     self.setupBlurs(statsKernels)
     self.commonName = commonName
     
     self.p = Pipeline()
     
     self.buildPipeline()
Esempio n. 12
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")
Esempio n. 13
0
 def __init__(self, inputFH, targetFH, statsKernels, additionalXfm=None):
     self.p = Pipeline()
     self.inputFH = inputFH
     self.targetFH = targetFH
     self.blurs = []
     self.setupBlurs(statsKernels)
     self.statsGroup = StatsGroup()
     self.setupXfms()
     """ additionalXfm is an optional transform that may be specified. If it is, 
         it is concatenated with the lastXfm from input to target. This additional
         transform must also be in the same direction as the lastXfm (e.g. input to target)
         Example usage: if the lastXfm from input to target goes from lsq12 to nlin space
         and you would like to calculate the absolute jacobians to lsq6 space, the additional
         transform specified may be the lsq6 to lsq12 transform from input to target. 
     """
     self.additionalXfm = additionalXfm
     self.fullStatsCalc()
Esempio n. 14
0
    def __init__(self, inFile, targetFile, nameForStage=None, **kwargs):
        self.p = Pipeline()
        self.outputFiles = [
        ]  # this will contain the outputFiles from the mincresample of the main MINC file
        self.outputFilesMask = [
        ]  # this will contain the outputFiles from the mincresample of the mask belonging to the main MINC file

        # the first step is to simply run the mincresample command:
        fileRS = mincresample(inFile, targetFile, **kwargs)
        if (nameForStage):
            fileRS.name = nameForStage
        self.p.addStage(fileRS)
        self.outputFiles = fileRS.outputFiles

        # initialize the array of outputs for the mask in case there is none to be resampled
        self.outputFilesMask = [None] * len(self.outputFiles)

        # next up, is this a file handler, and if so is there a mask that needs to be resampled?
        if (isFileHandler(inFile)):
            if (inFile.getMask()):
                # there is a mask associated with this file, should be updated
                # we have to watch out in terms of interpolation arguments, if
                # the original resample command asked for "-sinc" or "-tricubic"
                # for instance, we should remove that argument for the mask resampling
                # these options would reside in the argArray...
                maskArgs = copy.deepcopy(kwargs)
                if maskArgs.has_key("argArray"):
                    argList = maskArgs["argArray"]
                    for i in range(len(argList)):
                        if (re.match("-sinc", argList[i])
                                or re.match("-trilinear", argList[i])
                                or re.match("-tricubic", argList[i])):
                            del argList[i]
                    maskArgs["argArray"] = argList

                # if the output file for the mincresample command was already
                # specified, add "_mask.mnc" to it
                if maskArgs.has_key("output"):
                    maskArgs["output"] = re.sub(".mnc", "_mask.mnc",
                                                maskArgs["output"])

                maskRS = mincresampleMask(inFile, targetFile, **maskArgs)
                if (nameForStage):
                    maskRS.name = nameForStage + "--mask--"
                self.p.addStage(maskRS)
                self.outputFilesMask = maskRS.outputFiles
Esempio n. 15
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")
Esempio n. 16
0
 def __init__(self, filesToResample, resolution):
     """During initialization make sure all files are resampled
        at resolution we'd like to use for each pipeline stage
     """
     self.p = Pipeline()
     
     for FH in filesToResample:
         dirForOutput = self.getOutputDirectory(FH)
         currentRes = volumeFromFile(FH.getLastBasevol()).separations
         if not abs(abs(currentRes[0]) - abs(resolution)) < 0.01:
             crop = ma.autocrop(resolution, FH, defaultDir=dirForOutput)
             self.p.addStage(crop)
             mask = FH.getMask()
             if mask:
                 #Need to resample the mask as well.
                 cropMask = ma.mincresampleMask(FH,
                                                FH,
                                                outputLocation=FH,
                                                likeFile=FH)
                 self.p.addStage(cropMask)
Esempio n. 17
0
    def __init__(self, inputArray, targetFH, nlinOutputDir, avgPrefix,
                 nlin_protocol):
        self.p = Pipeline()
        """Initial inputs should be an array of fileHandlers with lastBasevol in lsq12 space"""
        self.inputs = inputArray
        """Initial target should be the file handler for the lsq12 average"""
        self.target = targetFH
        """Output directory should be _nlin """
        self.nlinDir = nlinOutputDir
        """Prefix to pre-pend to averages at each generation"""
        self.avgPrefix = avgPrefix
        """Empty array that we will fill with averages as we create them"""
        self.nlinAverages = []
        """Create the blurring resolution from the file resolution"""
        if nlin_protocol == None:
            self.fileRes = rf.returnFinestResolution(self.inputs[0])
        else:
            self.fileRes = None

        # Create new nlin group for each input prior to registration
        for i in range(len(self.inputs)):
            self.inputs[i].newGroup(groupName="nlin")
Esempio n. 18
0
 def __init__(self,
              inputFH,
              targetFH,
              lsq12_protocol=None,
              nlin_protocol=None,
              subject_matter=None,
              defaultDir="tmp"):
     
     self.p = Pipeline()
     self.inputFH = inputFH
     self.targetFH = targetFH
     self.lsq12_protocol = lsq12_protocol
     self.nlin_protocol = nlin_protocol
     self.subject_matter = subject_matter
     self.defaultDir = defaultDir
     
     if ((self.lsq12_protocol == None and self.subject_matter==None) or self.nlin_protocol == None):
         self.fileRes = rf.returnFinestResolution(self.inputFH)
     else:
         self.fileRes = None
     
     self.buildPipeline()    
Esempio n. 19
0
def maskFiles(FH, isAtlas, numAtlases=1):
    """ Assume that if there is more than one atlas, multiple
        masks were generated and we need to perform a voxel_vote. 
        Otherwise, assume we are using inputLabels from crossing with
        only one atlas. 
    """
    #MF TODO: Make this more general to handle pairwise option.
    p = Pipeline()
    if not isAtlas:
        if numAtlases > 1:
            voxel = voxelVote(FH, False, True)
            p.addStage(voxel)
            mincMathInput = voxel.outputFiles[0]
        else:
            mincMathInput = FH.returnLabels(True)[0]
        FH.setMask(mincMathInput)
    else:
        mincMathInput = FH.getMask()
    mincMathOutput = fh.createBaseName(FH.resampledDir, FH.basename)
    mincMathOutput += "_masked.mnc"
    logFile = fh.logFromFile(FH.logDir, mincMathOutput)
    cmd = ["mincmath"] + ["-clobber"] + ["-mult"]
    # In response to issue #135
    # the order of the input files to mincmath matters. By default the
    # first input files is used as a "like file" for the output file.
    # We should make sure that the mask is not used for that, because
    # it has an image range from 0 to 1; not something we want to be
    # set for the masked output file
    #            average                              mask
    cmd += [InputFile(FH.getLastBasevol())] + [InputFile(mincMathInput)]
    cmd += [OutputFile(mincMathOutput)]
    mincMath = CmdStage(cmd)
    mincMath.setLogFile(LogFile(logFile))
    p.addStage(mincMath)
    FH.setLastBasevol(mincMathOutput)
    return (p)
Esempio n. 20
0
 def _setup_pipeline(self):
     self.pipeline = Pipeline()
Esempio n. 21
0
 def _setup_pipeline(self, options):
     self.pipeline = Pipeline()
     self.pipeline.main_options_hash = options