def statsAndConcat(self, s, i, count, beforeAvg=True): """Construct array to common space for this timepoint. This builds upon arrays from previous calls.""" if beforeAvg: xfm = s[i].getLastXfm(s[i+1]) else: xfm = s[i].getLastXfm(s[i-1]) """Set this transform as last xfm from input to nlin and calculate nlin to s[i] stats""" if self.nlinFH: self.xfmToCommon.insert(0, xfm) """ Concat transforms to get xfmToCommon and calculate statistics Note that inverted transform, which is what we want, is calculated in the statistics module. """ xtc = createBaseName(s[i].transformsDir, s[i].basename + "_to_" + self.commonName + ".xfm") xc = ma.xfmConcat(self.xfmToCommon, xtc, fh.logFromFile(s[i].logDir, xtc)) self.p.addStage(xc) # here in order to visually inspect the alignment with the common # time point, we should resample this subject: inputResampledToCommon = createBaseName(s[i].resampledDir, s[i].basename + "_to_" + self.commonName + ".mnc") logToCommon = fh.logFromFile(s[i].logDir, inputResampledToCommon) resampleCmd = ma.mincresample(s[i], self.nlinFH, likeFile=self.nlinFH, transform=xtc, output=inputResampledToCommon, logFile=logToCommon, argArray=["-sinc"]) self.p.addStage(resampleCmd) s[i].addAndSetXfmToUse(self.nlinFH, xtc) self.statsCalculation(s[i], self.nlinFH, xfm=None, useChainStats=False) else: xtc=None """Calculate i to i+1 stats for all but final timePoint""" if count - i > 1: self.statsCalculation(s[i], s[i+1], xfm=xtc, useChainStats=True)
def iterate(self): if not self.maxPairs: xfmsToAvg = {} lsq12ResampledFiles = {} for inputFH in self.inputs: """Create an array of xfms, to compute an average lsq12 xfm for each input""" xfmsToAvg[inputFH] = [] for targetFH in self.inputs: if inputFH != targetFH: lsq12 = LSQ12(inputFH, targetFH, self.blurs, self.stepSize, self.useGradient, self.simplex) self.p.addPipeline(lsq12.p) xfmsToAvg[inputFH].append(inputFH.getLastXfm(targetFH)) """Create average xfm for inputFH using xfmsToAvg array""" cmd = ["xfmavg"] for i in range(len(xfmsToAvg[inputFH])): cmd.append(InputFile(xfmsToAvg[inputFH][i])) avgXfmOutput = createBaseName(inputFH.transformsDir, inputFH.basename + "-avg-lsq12.xfm") cmd.append(OutputFile(avgXfmOutput)) xfmavg = CmdStage(cmd) xfmavg.setLogFile(LogFile(logFromFile(inputFH.logDir, avgXfmOutput))) self.p.addStage(xfmavg) self.lsq12AvgXfms[inputFH] = avgXfmOutput """ resample brain and add to array for mincAveraging""" if not self.likeFile: likeFile=inputFH else: likeFile=self.likeFile rslOutput = createBaseName(inputFH.resampledDir, inputFH.basename + "-resampled-lsq12.mnc") res = ma.mincresample(inputFH, inputFH, transform=avgXfmOutput, likeFile=likeFile, output=rslOutput, argArray=["-sinc"]) self.p.addStage(res) lsq12ResampledFiles[inputFH] = rslOutput """ After all registrations complete, setLastBasevol for each subject to be resampled file in lsq12 space. We can then call mincAverage on fileHandlers, as it will use the lastBasevol for each by default.""" for inputFH in self.inputs: inputFH.setLastBasevol(lsq12ResampledFiles[inputFH]) """ mincAverage all resampled brains and put in lsq12Directory""" self.lsq12Avg = abspath(self.lsq12Dir) + "/" + basename(self.lsq12Dir) + "-pairs.mnc" self.lsq12AvgFH = RegistrationPipeFH(self.lsq12Avg, basedir=self.lsq12Dir) avg = ma.mincAverage(self.inputs, self.lsq12AvgFH, output=self.lsq12Avg, defaultDir=self.lsq12Dir) self.p.addStage(avg) else: print "Registration using a specified number of max pairs not yet working. Check back soon!" sys.exit()
def finalGenerationFileNames(inputFH): """Set up and return filenames for final nlin generation, since we don't want to use defaults here. The naming of the final resampled files/transforms will be the same regardless of registration protocol (minctracc vs mincANTS) or number of generations. """ registerDir = inputFH.setOutputDirectory("transforms") registerFileName = removeBaseAndExtension(inputFH.basename) + "-final-nlin.xfm" registerOutput = createBaseName(registerDir, registerFileName) resampleDir = inputFH.setOutputDirectory("resampled") resampleFileName = removeBaseAndExtension(inputFH.basename) + "-resampled-final-nlin.mnc" resampleOutput = createBaseName(resampleDir, resampleFileName) return (registerOutput, resampleOutput)
def finalGenerationFileNames(inputFH): """Set up and return filenames for final nlin generation, since we don't want to use defaults here. The naming of the final resampled files/transforms will be the same regardless of registration protocol (minctracc vs mincANTS) or number of generations. """ registerDir = inputFH.setOutputDirectory("transforms") registerFileName = removeBaseAndExtension( inputFH.basename) + "-final-nlin.xfm" registerOutput = createBaseName(registerDir, registerFileName) resampleDir = inputFH.setOutputDirectory("resampled") resampleFileName = removeBaseAndExtension( inputFH.basename) + "-resampled-final-nlin.mnc" resampleOutput = createBaseName(resampleDir, resampleFileName) return (registerOutput, resampleOutput)
def setOutputFile(self, inFile, defaultDir): outDir = inFile.setOutputDirectory(defaultDir) outBase = (fh.removeBaseAndExtension(inFile.getLastBasevol()) + "_" + self.resolution + "res.mnc") outputFile = fh.createBaseName(outDir, outBase) inFile.setLastBasevol(outputFile) return (outputFile)
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
def setOutputFile(self, inFile, defaultDir): outDir = inFile.setOutputDirectory(defaultDir) outBase = (fh.removeBaseAndExtension(inFile.getLastBasevol()) + "_" + self.resolution + "res.mnc") outputFile = fh.createBaseName(outDir, outBase) inFile.setLastBasevol(outputFile) return(outputFile)
def registerVolume(self, targetFH, defaultDir): """create the filenames for a single registration call Two input arguments are required - a RegistrationPipeFH instance for the target volume and the default directory where the output should be placed. The output xfm is constructed based on: 1. The names of the source and target base volumes. 2. The group name (eg. base, lsq6, etc) or index (if names are not set, default is to index) 3. A counter at the end, based on the number of previous transforms. e.g. The first transform will have _0.xfm, because the length of the transforms array will be 0, the second transform will have _1.xfm etc. """ sourceFilename = fh.removeBaseAndExtension(self.getLastBasevol()) targetFilename = fh.removeBaseAndExtension(targetFH.getLastBasevol()) xfmFileName = [sourceFilename, "to", targetFilename] groupName = self.groupNames[self.currentGroupIndex] xfmsDict = self.groupedFiles[self.currentGroupIndex].transforms if xfmsDict.has_key(targetFH): numPrevXfms = len(xfmsDict[targetFH]) else: numPrevXfms = 0 xfmFileName += [str(groupName), str(numPrevXfms)] xfmFileWithExt = "_".join(xfmFileName) + ".xfm" xfmOutputDir = self.setOutputDirectory(defaultDir) # MF TO DO: Need to add in some checking for duplicate names here. outputXfm = fh.createBaseName(xfmOutputDir, xfmFileWithExt) self.addAndSetXfmToUse(targetFH, outputXfm) return(outputXfm)
def statsAndConcat(self, s, i, count, beforeAvg=True): """Construct array to common space for this timepoint. This builds upon arrays from previous calls.""" if beforeAvg: xfm = s[i].getLastXfm(s[i + 1]) else: xfm = s[i].getLastXfm(s[i - 1]) """Set this transform as last xfm from input to nlin and calculate nlin to s[i] stats""" if self.nlinFH: self.xfmToCommon.insert(0, xfm) """ Concat transforms to get xfmToCommon and calculate statistics Note that inverted transform, which is what we want, is calculated in the statistics module. """ xtc = fh.createBaseName( s[i].transformsDir, s[i].basename + "_to_" + self.commonName + ".xfm") xc = ma.xfmConcat(self.xfmToCommon, xtc, fh.logFromFile(s[i].logDir, xtc)) self.p.addStage(xc) s[i].addAndSetXfmToUse(self.nlinFH, xtc) self.statsCalculation(s[i], self.nlinFH, xfm=None, useChainStats=False) else: xtc = None """Calculate i to i+1 stats for all but final timePoint""" if count - i > 1: self.statsCalculation(s[i], s[i + 1], xfm=xtc, useChainStats=True)
def linAndNlinDisplacement(self): """The function calculates both the linear and nonlinear portions of the displacement, in order to find pure nonlinear. Input is the commonSpace, so the pure nonlinear displacement will point from input to target. This is opposite from the standard stats class, where the common space is the target """ """Calculate linear part of non-linear xfm from input to target""" lpnl = linearPartofNlin(self.inputFH, self.targetFH) self.p.addStage(lpnl) self.linearXfm = lpnl.outputFiles[0] """Invert the transform, so we get the linear xfm from target to input.""" xi = xfmInvert(self.linearXfm, FH=self.inputFH) self.p.addStage(xi) """Calculate full displacement from input to target""" self.calcFullDisplacement() """Calculate pure non-linear displacement from input to target 1. Concatenate inverse linear and full input-target xfm to get pure_nlin xfm 2. Compute mincDisplacement on this transform. """ nlinBase = fh.removeBaseAndExtension(self.xfm) + "_pure_nlin.xfm" nlinXfm = fh.createBaseName(self.inputFH.tmpDir, nlinBase) xc = xfmConcat([xi.outputFiles[0], self.xfm], nlinXfm, fh.logFromFile(self.inputFH.logDir, nlinXfm)) self.p.addStage(xc) nlinDisp = mincDisplacement(self.inputFH, self.inputFH, nlinXfm) self.p.addStage(nlinDisp) self.nlinDisp = nlinDisp.outputFiles[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 = removeBaseAndExtension(f).split(".mnc")[0] outputFile = 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
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)
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)
def linAndNlinDisplacement(self): """ The function calculates both the linear and nonlinear portions of the displacement, in order to find pure nonlinear. Common space here is the target (usually an average of some sort). We also recentre pure non linear displacement. """ """Calculate linear part of non-linear xfm from input to target""" lpnl = linearPartofNlin(self.inputFH, self.targetFH) self.p.addStage(lpnl) self.linearXfm = lpnl.outputFiles[0] """Calculate full displacement from target to input""" self.calcFullDisplacement() """Calculate pure non-linear displacement from target to input 1. Concatenate linear and inverse target to input transform to get pure_nlin xfm 2. Compute mincDisplacement on this transform. """ nlinXfm = createPureNlinXfmName(self.inputFH, self.invXfm) xc = xfmConcat([self.linearXfm, self.invXfm], nlinXfm, fh.logFromFile(self.inputFH.logDir, nlinXfm)) self.p.addStage(xc) nlinDisp = mincDisplacement(self.targetFH, self.inputFH, transform=nlinXfm) self.p.addStage(nlinDisp) self.nlinDisp = nlinDisp.outputFiles[0] """Calculate average displacement and re-center non-linear displacement if an array of input file handlers was specified on instantiation. """ if self.dispToAvg: """Calculate average inverse displacement""" avgOutput = abspath(self.targetFH.basedir) + "/" + "average_inv_pure_displacement.mnc" logBase = fh.removeBaseAndExtension(avgOutput) avgLog = fh.createLogFile(self.targetFH.basedir, logBase) avg = mincAverageDisp(self.dispToAvg, avgOutput, logFile=avgLog) self.p.addStage(avg) """Centre pure nlin displacement by subtracting average from existing""" centredBase = fh.removeBaseAndExtension(self.nlinDisp).split("_displacement")[0] centredOut = fh.createBaseName(self.inputFH.statsDir, centredBase + "_centred_displacement.mnc") cmd = ["mincmath", "-clobber", "-sub", InputFile(self.nlinDisp), InputFile(avgOutput), OutputFile(centredOut)] centredDisp = CmdStage(cmd) centredDisp.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, centredOut))) self.p.addStage(centredDisp) """Reset centred displacement to be self.nlinDisp""" self.nlinDisp = centredOut
def voxelVote(inputFH, pairwise, mask): # if we do pairwise crossing, use output labels for voting (Default) # otherwise, return inputLabels from initial atlas-input crossing useInputLabels = False if not pairwise: useInputLabels = True labels = inputFH.returnLabels(useInputLabels) out = fh.createBaseName(inputFH.labelsDir, inputFH.basename) if mask: out += "_mask.mnc" else: out += "_votedlabels.mnc" logFile = fh.logFromFile(inputFH.logDir, out) cmd = ["voxel_vote"] + [InputFile(l) for l in labels] + [OutputFile(out)] voxel = CmdStage(cmd) voxel.setLogFile(LogFile(logFile)) return (voxel)
def voxelVote(inputFH, pairwise, mask): # if we do pairwise crossing, use output labels for voting (Default) # otherwise, return inputLabels from initial atlas-input crossing useInputLabels = False if not pairwise: useInputLabels = True labels = inputFH.returnLabels(useInputLabels) out = fh.createBaseName(inputFH.labelsDir, inputFH.basename) if mask: out += "_mask.mnc" else: out += "_votedlabels.mnc" logFile = fh.logFromFile(inputFH.logDir, out) cmd = ["voxel_vote.py"] + [InputFile(l) for l in labels] + [OutputFile(out)] voxel = CmdStage(cmd) voxel.setLogFile(LogFile(logFile)) return(voxel)
def statsAndConcat(self, s, i, count, beforeAvg=True): """Construct array to common space for this timepoint. This builds upon arrays from previous calls.""" if beforeAvg: xfm = s[i].getLastXfm(s[i+1]) else: xfm = s[i].getLastXfm(s[i-1]) """Set this transform as last xfm from input to nlin and calculate nlin to s[i] stats""" if self.nlinFH: self.xfmToCommon.insert(0, xfm) """ Concat transforms to get xfmToCommon and calculate statistics Note that inverted transform, which is what we want, is calculated in the statistics module. """ xtc = fh.createBaseName(s[i].transformsDir, s[i].basename + "_to_" + self.commonName + ".xfm") xc = ma.xfmConcat(self.xfmToCommon, xtc, fh.logFromFile(s[i].logDir, xtc)) self.p.addStage(xc) s[i].addAndSetXfmToUse(self.nlinFH, xtc) self.statsCalculation(s[i], self.nlinFH, xfm=None, useChainStats=False) else: xtc=None """Calculate i to i+1 stats for all but final timePoint""" if count - i > 1: self.statsCalculation(s[i], s[i+1], xfm=xtc, useChainStats=True)
def __init__(self, xfm, FH=None, logFile=None): CmdStage.__init__(self, None) try: self.xfm = xfm if isFileHandler(FH): invXfmBase = fh.removeBaseAndExtension( self.xfm).split(".xfm")[0] self.output = fh.createBaseName(FH.transformsDir, invXfmBase + "_inverted.xfm") self.logFile = fh.logFromFile(FH.logDir, self.output) else: invXfmBase = splitext(self.xfm)[0] self.output = invXfmBase + "_inverted.xfm" if logFile: self.logFile = logFile except: print "Failed in putting together xfminvert command" print "Unexpected error: ", sys.exc_info() self.finalizeCommand() self.setName()
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)
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)
def voxelVote(inputFH, pairwise, mask): # In the main MAGeT.py code, when not only a mask is created for the # input files, the process works as follows: # # 1) the template files (library) are aligned to each input upto max_templates input files # 2) all templates (library + newly created) are aligned to each input # # That second stage contains alignments that have already run in the first stage. # And pydpiper is coded such, that this duplicated stage is not performed. In order # to get all labels for voxel voting, we need to combine atlases from both these # stages, i.e., the "initial" and the "templates". This means that we should always # get the "useInputLabels". (In the special case where there is only 1 input file # and pairwise is set to true, this is particularly important, because of the duplicate # stages, only the inputlabels will exists.) # 1) get the input templates # the True parameter will return "inputLabels" from the groupedFiles for inputFH labels = inputFH.returnLabels(True) # 2) if we do pairwise crossing, also get the output labels for voting if pairwise: # False will return "labels" from the groupedFiles for inputFH outputLabels = inputFH.returnLabels(False) # add these labels to the "initial" or input labels: labels = labels + outputLabels out = fh.createBaseName(inputFH.labelsDir, inputFH.basename) if mask: out += "_mask.mnc" else: out += "_votedlabels.mnc" logFile = fh.logFromFile(inputFH.logDir, out) cmd = ["voxel_vote"] + [InputFile(l) for l in labels] + [OutputFile(out)] voxel = CmdStage(cmd) voxel.setLogFile(LogFile(logFile)) return (voxel)
def voxelVote(inputFH, pairwise, mask): # In the main MAGeT.py code, when not only a mask is created for the # input files, the process works as follows: # # 1) the template files (library) are aligned to each input upto max_templates input files # 2) all templates (library + newly created) are aligned to each input # # That second stage contains alignments that have already run in the first stage. # And pydpiper is coded such, that this duplicated stage is not performed. In order # to get all labels for voxel voting, we need to combine atlases from both these # stages, i.e., the "initial" and the "templates". This means that we should always # get the "useInputLabels". (In the special case where there is only 1 input file # and pairwise is set to true, this is particularly important, because of the duplicate # stages, only the inputlabels will exists.) # 1) get the input templates # the True parameter will return "inputLabels" from the groupedFiles for inputFH labels = inputFH.returnLabels(True) # 2) if we do pairwise crossing, also get the output labels for voting if pairwise: # False will return "labels" from the groupedFiles for inputFH outputLabels = inputFH.returnLabels(False) # add these labels to the "initial" or input labels: labels = labels + outputLabels out = fh.createBaseName(inputFH.labelsDir, inputFH.basename) if mask: out += "_mask.mnc" else: out += "_votedlabels.mnc" logFile = fh.logFromFile(inputFH.logDir, out) cmd = ["voxel_vote"] + [InputFile(l) for l in labels] + [OutputFile(out)] voxel = CmdStage(cmd) voxel.setLogFile(LogFile(logFile)) return(voxel)
def __init__(self, xfm, FH=None, logFile=None): CmdStage.__init__(self, None) try: self.xfm = xfm if isFileHandler(FH): invXfmBase = fh.removeBaseAndExtension(self.xfm).split(".xfm")[0] self.output = fh.createBaseName(FH.transformsDir, invXfmBase + "_inverted.xfm") self.logFile = fh.logFromFile(FH.logDir, self.output) else: invXfmBase = splitext(self.xfm)[0] self.output = invXfmBase + "_inverted.xfm" if logFile: self.logFile = logFile except: print "Failed in putting together xfminvert command" print "Unexpected error: ", sys.exc_info() self.finalizeCommand() self.setName()
def calcDetAndLogDet(self, useFullDisp=False): if useFullDisp: dispToUse = self.fullDisp #absolute jacobians else: dispToUse = self.nlinDisp #relative jacobians """Insert -1 at beginning of blurs array to include the calculation of unblurred jacobians.""" self.blurs.insert(0,-1) for b in self.blurs: """Create base name for determinant calculation.""" outputBase = fh.removeBaseAndExtension(dispToUse).split("_displacement")[0] """Calculate smoothed deformation field for all blurs other than -1""" if b != -1: fwhm = "--fwhm=" + str(b) outSmooth = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_smooth_displacement_fwhm" + str(b) + ".mnc") cmd = ["smooth_vector", "--clobber", "--filter", fwhm, InputFile(dispToUse), OutputFile(outSmooth)] smoothVec = CmdStage(cmd) smoothVec.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outSmooth))) self.p.addStage(smoothVec) """Set input for determinant calculation.""" inputDet = outSmooth nameAddendum = "_fwhm" + str(b) else: inputDet = dispToUse nameAddendum = "" outputDet = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_determinant" + nameAddendum + ".mnc") outDetShift = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_det_plus1" + nameAddendum + ".mnc") if useFullDisp: #absolute jacobians outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_absolute_log_determinant" + nameAddendum + ".mnc") else: #relative jacobians outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_relative_log_determinant" + nameAddendum + ".mnc") """Calculate the determinant, then add 1 (per mincblob weirdness)""" cmd = ["mincblob", "-clobber", "-determinant", InputFile(inputDet), OutputFile(outputDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outputDet))) self.p.addStage(det) cmd = ["mincmath", "-clobber", "-2", "-const", str(1), "-add", InputFile(outputDet), OutputFile(outDetShift)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outDetShift))) self.p.addStage(det) """Calculate log determinant (jacobian) and add to statsGroup.""" cmd = ["mincmath", "-clobber", "-2", "-log", InputFile(outDetShift), OutputFile(outLogDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outLogDet))) self.p.addStage(det) if useFullDisp: self.statsGroup.absoluteJacobians[b] = outLogDet else: self.statsGroup.relativeJacobians[b] = outLogDet
def __init__(self, inputFiles, createMontage=True, montageOutPut=None, scalingFactor=20, message="lsq6"): self.p = Pipeline() self.individualImages = [] self.individualImagesLabeled = [] self.message = message if createMontage and montageOutPut == None: print("\nError: createMontage is specified in createQualityControlImages, but no output name for the montage is provided. Exiting...\n") sys.exit() # for each of the input files, run a mincpik call and create # a triplane image. for inFile in inputFiles: if isFileHandler(inFile): # create command using last base vol inputToMincpik = inFile.getLastBasevol() outputMincpik = createBaseName(inFile.tmpDir, removeBaseAndExtension(inputToMincpik) + "_QC_image.png") cmd = ["mincpik", "-clobber", "-scale", scalingFactor, "-triplanar", InputFile(inputToMincpik), OutputFile(outputMincpik)] mincpik = CmdStage(cmd) mincpik.setLogFile(LogFile(logFromFile(inFile.logDir, outputMincpik))) self.p.addStage(mincpik) self.individualImages.append(outputMincpik) # we should add a label to each of the individual images # so it will be easier for the user to identify what # which images potentially fail outputConvert = createBaseName(inFile.tmpDir, removeBaseAndExtension(inputToMincpik) + "_QC_image_labeled.png") cmdConvert = ["convert", "-label", inFile.basename, InputFile(outputMincpik), OutputFile(outputConvert)] convertAddLabel = CmdStage(cmdConvert) convertAddLabel.setLogFile(LogFile(logFromFile(inFile.logDir, outputConvert))) self.p.addStage(convertAddLabel) self.individualImagesLabeled.append(outputConvert) # if montageOutput is specified, create the overview image if createMontage: cmdmontage = ["montage", "-geometry", "+2+2"] \ + map(InputFile, self.individualImagesLabeled) + [OutputFile(montageOutPut)] montage = CmdStage(cmdmontage) montage.setLogFile(splitext(montageOutPut)[0] + ".log") message_to_print = "\n* * * * * * *\nPlease consider the following verification " message_to_print += "image, which shows a slice through all input " message_to_print += "files %s. " % self.message message_to_print += "\n%s\n" % (montageOutPut) message_to_print += "* * * * * * *\n" # the hook needs a return. Given that "print" does not return # anything, we need to encapsulate the print statement in a # function (which in this case will return None, but that's fine) def printMessageForMontage(): print(message_to_print) montage.finished_hooks.append( lambda : printMessageForMontage()) self.p.addStage(montage)
def createInvXfmName(iFH, xfm): invXfmBase = fh.removeBaseAndExtension(xfm).split(".xfm")[0] invXfm = fh.createBaseName(iFH.transformsDir, invXfmBase + "_inverted.xfm") return invXfm
def createPureNlinXfmName(iFH, xfm): nlinBase = fh.removeBaseAndExtension(xfm) + "_pure_nlin.xfm" nlinXfm = fh.createBaseName(iFH.tmpDir, nlinBase) return nlinXfm
def setDispName(iFH, xfm, defaultDir): outDir = iFH.setOutputDirectory(defaultDir) outBase = fh.removeBaseAndExtension(xfm) + "_displacement.mnc" outputFile = fh.createBaseName(outDir, outBase) return outputFile
def setOutputFile(self, inFile, defaultDir): outDir = inFile.setOutputDirectory(defaultDir) outBase = (fh.removeBaseAndExtension(self.xfm) + "_linear_part.xfm") outputFile = fh.createBaseName(outDir, outBase) return(outputFile)
def calcDetAndLogDet(self, useFullDisp=False): if useFullDisp: dispToUse = self.fullDisp #absolute jacobians else: dispToUse = self.nlinDisp #relative jacobians """Insert -1 at beginning of blurs array to include the calculation of unblurred jacobians.""" self.blurs.insert(0,-1) for b in self.blurs: """Create base name for determinant calculation.""" outputBase = fh.removeBaseAndExtension(dispToUse).split("_displacement")[0] """Calculate smoothed deformation field for all blurs other than -1""" if b != -1: fwhm = "--fwhm=" + str(b) outSmooth = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_smooth_displacement_fwhm" + str(b) + ".mnc") cmd = ["smooth_vector", "--clobber", "--filter", fwhm, InputFile(dispToUse), OutputFile(outSmooth)] smoothVec = CmdStage(cmd) smoothVec.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outSmooth))) self.p.addStage(smoothVec) """Set input for determinant calculation.""" inputDet = outSmooth nameAddendum = "_fwhm" + str(b) else: inputDet = dispToUse nameAddendum = "" outputDet = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_determinant" + nameAddendum + ".mnc") outDetShift = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_det_plus1" + nameAddendum + ".mnc") if useFullDisp: #absolute jacobians outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_absolute_log_determinant" + nameAddendum + ".mnc") else: #relative jacobians outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_relative_log_determinant" + nameAddendum + ".mnc") """Calculate the determinant, then add 1 (per mincblob weirdness)""" cmd = ["mincblob", "-clobber", "-determinant", InputFile(inputDet), OutputFile(outputDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outputDet))) self.p.addStage(det) cmd = ["mincmath", "-clobber", "-2", "-const", str(1), "-add", InputFile(outputDet), OutputFile(outDetShift)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outDetShift))) self.p.addStage(det) """Calculate log determinant (jacobian) and add to statsGroup.""" cmd = ["mincmath", "-clobber", "-2", "-log", InputFile(outDetShift), OutputFile(outLogDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outLogDet))) self.p.addStage(det) if useFullDisp: self.statsGroup.absoluteJacobians[b] = outLogDet else: self.statsGroup.relativeJacobians[b] = outLogDet
def setOutputFile(self, FH, defaultDir): # add -mask to appended file outBase = self.setOutputFileName(FH, append="mask") outDir = FH.setOutputDirectory(defaultDir) return(fh.createBaseName(outDir, outBase))
def calcDetAndLogDet(self, useFullDisp=False): #Lots of repetition here--let's see if we can't make some functions. """useFullDisp indicates whether or not to use full displacement field or non-linear component only""" if useFullDisp: dispToUse = self.fullDisp else: dispToUse = self.nlinDisp """Insert -1 at beginning of blurs array to include the calculation of unblurred jacobians.""" self.blurs.insert(0,-1) for b in self.blurs: """Calculate default output filenames and set input for determinant calculation.""" outputBase = fh.removeBaseAndExtension(dispToUse).split("_displacement")[0] inputDet = dispToUse outputDet = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_determinant.mnc") outDetShift = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_det_plus1.mnc") outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_log_determinant.mnc") outLogDetScaled = fh.createBaseName(self.inputFH.statsDir, outputBase + "_log_determinant_scaled.mnc") """Calculate smoothed deformation field for all blurs other than -1""" if b != -1: fwhm = "--fwhm=" + str(b) outSmooth = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_smooth_displacement_fwhm" + str(b) + ".mnc") cmd = ["smooth_vector", "--clobber", "--filter", fwhm, InputFile(dispToUse), OutputFile(outSmooth)] smoothVec = CmdStage(cmd) smoothVec.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outSmooth))) self.p.addStage(smoothVec) """Override file name defaults for each blur and set input for determinant calculation.""" inputDet = outSmooth outputDet = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_determinant_fwhm" + str(b) + ".mnc") outDetShift = fh.createBaseName(self.inputFH.tmpDir, outputBase + "_det_plus1_fwhm" + str(b) + ".mnc") outLogDet = fh.createBaseName(self.inputFH.statsDir, outputBase + "_log_determinant_fwhm" + str(b) + ".mnc") outLogDetScaled = fh.createBaseName(self.inputFH.statsDir, outputBase + "_log_determinant_scaled_fwhm" + str(b) + ".mnc") """Calculate the determinant, then add 1 (per mincblob weirdness)""" cmd = ["mincblob", "-clobber", "-determinant", InputFile(inputDet), OutputFile(outputDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outputDet))) self.p.addStage(det) cmd = ["mincmath", "-clobber", "-2", "-const", str(1), "-add", InputFile(outputDet), OutputFile(outDetShift)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outDetShift))) self.p.addStage(det) """Calculate log determinant (jacobian) and add to statsGroup.""" cmd = ["mincmath", "-clobber", "-2", "-log", InputFile(outDetShift), OutputFile(outLogDet)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outLogDet))) self.p.addStage(det) self.statsGroup.jacobians[b] = outLogDet """If self.linearXfm present, calculate scaled log determinant (scaled jacobian) and add to statsGroup""" if not useFullDisp: """ If self.scaleFactor is specified, then concatenate this additional transform with self.linearXfm. Typically, this will come from an LSQ12 registration, but may come from another alignment. """ if self.scalingFactor: toConcat = [self.scalingFactor, self.linearXfm] self.fullLinearXfm = fh.createBaseName(self.inputFH.transformsDir, self.inputFH.basename + "_full_linear.xfm") logFile=fh.logFromFile(self.inputFH.logDir, fh.removeBaseAndExtension(self.fullLinearXfm)) concat = xfmConcat(toConcat, self.fullLinearXfm, logFile=logFile) self.p.addStage(concat) else: self.fullLinearXfm = self.linearXfm cmd = ["scale_voxels", "-clobber", "-invert", "-log", InputFile(self.fullLinearXfm), InputFile(outLogDet), OutputFile(outLogDetScaled)] det = CmdStage(cmd) det.setLogFile(LogFile(fh.logFromFile(self.inputFH.logDir, outLogDetScaled))) self.p.addStage(det) self.statsGroup.scaledJacobians[b] = outLogDetScaled else: self.statsGroup.scaledJacobians = None
def createOutputFileName(iFH, xfm, outputDir, nameExt): outDir = iFH.setOutputDirectory(outputDir) outBase = fh.removeBaseAndExtension(xfm) + nameExt outputFile = fh.createBaseName(outDir, outBase) return outputFile
def setOutputFile(self, FH, defaultDir): # add -mask to appended file outBase = self.setOutputFileName(FH, append="mask") outDir = FH.setOutputDirectory(defaultDir) return (fh.createBaseName(outDir, outBase))
def setOutputFile(self, inFile, defaultDir): outDir = inFile.setOutputDirectory(defaultDir) outBase = (fh.removeBaseAndExtension(self.xfm) + "_linear_part.xfm") outputFile = fh.createBaseName(outDir, outBase) return(outputFile)
def setOutputFile(self, FH, defaultDir): outBase = fh.removeBaseAndExtension(self.cxfm) + "-resampled.mnc" outDir = FH.setOutputDirectory(defaultDir) return (fh.createBaseName(outDir, outBase))
def createOutputFileName(iFH, xfm, outputDir, nameExt): outDir = iFH.setOutputDirectory(outputDir) outBase = fh.removeBaseAndExtension(xfm) + nameExt outputFile = fh.createBaseName(outDir, outBase) return outputFile
def iterate(self): xfmsToAvg = {} lsq12ResampledFiles = {} for inputFH in self.inputs: """Create an array of xfms, to compute an average lsq12 xfm for each input""" xfmsToAvg[inputFH] = [] if self.maxPairs is not None: if self.maxPairs >= len(self.inputs) - 1: # -1 prevents unnecessary sampling in the case self.maxPairs = len(self.inputs) - 1 inputs = self.inputs else: random.seed( tuple(map(lambda fh: fh.inputFileName, self.inputs))) # if inputFH is included in the sample, we will register against one fewer target inputs = random.sample( filter(lambda fh: fh != inputFH, self.inputs), self.maxPairs) else: inputs = self.inputs for targetFH in inputs: if inputFH != targetFH: lsq12 = LSQ12(inputFH, targetFH, blurs=self.blurs, step=self.stepSize, gradient=self.useGradient, simplex=self.simplex, w_translations=self.w_translations) self.p.addPipeline(lsq12.p) xfmsToAvg[inputFH].append(inputFH.getLastXfm(targetFH)) """Create average xfm for inputFH using xfmsToAvg array""" avgXfmOutput = createBaseName(inputFH.transformsDir, inputFH.basename + "-avg-lsq12.xfm") cmd = ["xfmavg", "-verbose", "-clobber"] \ + map(InputFile, xfmsToAvg[inputFH]) + [OutputFile(avgXfmOutput)] #for i in range(len(xfmsToAvg[inputFH])): # cmd.append(InputFile(xfmsToAvg[inputFH][i])) # '-clobber' works around #157, but is probably better in general #cmd.append(OutputFile(avgXfmOutput)) xfmavg = CmdStage(cmd) xfmavg.setLogFile( LogFile(logFromFile(inputFH.logDir, avgXfmOutput))) self.p.addStage(xfmavg) self.lsq12AvgXfms[inputFH] = avgXfmOutput """ resample brain and add to array for mincAveraging""" if not self.likeFile: likeFile = inputFH else: likeFile = self.likeFile rslOutput = createBaseName( inputFH.resampledDir, inputFH.basename + "-resampled-lsq12.mnc") res = ma.mincresample(inputFH, inputFH, transform=avgXfmOutput, likeFile=likeFile, output=rslOutput, argArray=["-sinc"]) self.p.addStage(res) lsq12ResampledFiles[inputFH] = rslOutput """ After all registrations complete, setLastBasevol for each subject to be resampled file in lsq12 space. We can then call mincAverage on fileHandlers, as it will use the lastBasevol for each by default.""" for inputFH in self.inputs: inputFH.setLastBasevol(lsq12ResampledFiles[inputFH]) """ mincAverage all resampled brains and put in lsq12Directory""" self.lsq12Avg = abspath(self.lsq12Dir) + "/" + basename( self.lsq12Dir) + "-pairs.mnc" self.lsq12AvgFH = RegistrationPipeFH(self.lsq12Avg, basedir=self.lsq12Dir) avg = ma.mincAverage(inputs, self.lsq12AvgFH, output=self.lsq12Avg, defaultDir=self.lsq12Dir) self.p.addStage(avg)
def setOutputFile(self, FH, defaultDir): outBase = fh.removeBaseAndExtension(self.cxfm) + "-resampled.mnc" outDir = FH.setOutputDirectory(defaultDir) return(fh.createBaseName(outDir, outBase))
def setOutputFile(self, FH, defaultDir): """set name of output and add labels to appropriate likeFile labels array""" outBase = self.setOutputFileName(FH, append="labels") outDir = FH.setOutputDirectory(defaultDir) return (fh.createBaseName(outDir, outBase))
def iterate(self): if not self.maxPairs: xfmsToAvg = {} lsq12ResampledFiles = {} for inputFH in self.inputs: """Create an array of xfms, to compute an average lsq12 xfm for each input""" xfmsToAvg[inputFH] = [] for targetFH in self.inputs: if inputFH != targetFH: lsq12 = LSQ12(inputFH, targetFH, blurs=self.blurs, step=self.stepSize, gradient=self.useGradient, simplex=self.simplex, w_translations=self.w_translations) self.p.addPipeline(lsq12.p) xfmsToAvg[inputFH].append(inputFH.getLastXfm(targetFH)) """Create average xfm for inputFH using xfmsToAvg array""" cmd = ["xfmavg"] for i in range(len(xfmsToAvg[inputFH])): cmd.append(InputFile(xfmsToAvg[inputFH][i])) avgXfmOutput = createBaseName( inputFH.transformsDir, inputFH.basename + "-avg-lsq12.xfm") cmd.append(OutputFile(avgXfmOutput)) xfmavg = CmdStage(cmd) xfmavg.setLogFile( LogFile(logFromFile(inputFH.logDir, avgXfmOutput))) self.p.addStage(xfmavg) self.lsq12AvgXfms[inputFH] = avgXfmOutput """ resample brain and add to array for mincAveraging""" if not self.likeFile: likeFile = inputFH else: likeFile = self.likeFile rslOutput = createBaseName( inputFH.resampledDir, inputFH.basename + "-resampled-lsq12.mnc") res = ma.mincresample(inputFH, inputFH, transform=avgXfmOutput, likeFile=likeFile, output=rslOutput, argArray=["-sinc"]) self.p.addStage(res) lsq12ResampledFiles[inputFH] = rslOutput """ After all registrations complete, setLastBasevol for each subject to be resampled file in lsq12 space. We can then call mincAverage on fileHandlers, as it will use the lastBasevol for each by default.""" for inputFH in self.inputs: inputFH.setLastBasevol(lsq12ResampledFiles[inputFH]) """ mincAverage all resampled brains and put in lsq12Directory""" self.lsq12Avg = abspath(self.lsq12Dir) + "/" + basename( self.lsq12Dir) + "-pairs.mnc" self.lsq12AvgFH = RegistrationPipeFH(self.lsq12Avg, basedir=self.lsq12Dir) avg = ma.mincAverage(self.inputs, self.lsq12AvgFH, output=self.lsq12Avg, defaultDir=self.lsq12Dir) self.p.addStage(avg) else: print "Registration using a specified number of max pairs not yet working. Check back soon!" sys.exit()
def setOutputFile(self, FH, defaultDir): """set name of output and add labels to appropriate likeFile labels array""" outBase = self.setOutputFileName(FH, append="labels") outDir = FH.setOutputDirectory(defaultDir) return(fh.createBaseName(outDir, outBase))