def mainFunc(): parser = argparse.ArgumentParser(description='Run Elastix registration protocol for all images in the directory') parser.add_argument('--refDir', '-r', dest='refDir', required = True, \ help='The directory containing the reference images.') parser.add_argument('--floatFile', '-f', dest='floatFile', required = True, \ help='Path to the floating image.') parser.add_argument('--outDir', '-o', dest='outDir', required = False, \ help='Path to store the output images/parameters (default: current dir)', default=os.getcwd()) parser.add_argument('--atlas', '-a', dest='atlas', required = False, \ help='Path to the atlas segmentation file which will be resampled with the CPP file from the registration.', default=None) args = parser.parse_args() refImgs = [join(args.refDir, File) for File in listdir(args.refDir)] refImgs = [img for img in refImgs if isfile(img) and img.endswith('.nii')] if not refImgs: print('Couldn\'t find any reference images') return if not path.isfile(args.floatFile): print('Coudln\'t find the float image') refImgs.sort(key=str.lower) refFloatPairs = [[refImg, args.floatFile] for refImg in refImgs] f3dParStr = paramListToShortString(f3d_params) aladinParStr = paramListToShortString(aladin_params) for rfPair in refFloatPairs: baseName = basename(rfPair[0])[:-4]+'_'+basename(rfPair[1])[:-4] currOutDir = join(args.outDir,baseName) mkdir(currOutDir) elastixLogPath = join(currOutDir,basename+'_LOG.txt') elastixCommand = elastixExec+' -f '+rfPair[0]+' -m '+rfPair[1]+' -p '.join(elastixParams)+' -o '+currOutDir elastixLog = '' try: elastixLog = ccall(elastixCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, elastixLogPath) with open(elastixLogPath, 'w') as f: f.write(elastixLog) transformParameterFiles = ['TransformParameters.0.txt', 'TransformParameters.1.txt'] transformParameterFiles = [join(currOutDir,tpFile) for tpFile in transformParameterFiles] for tpFilePath in transformParameterFiles: with open(tpFilePath,'r') as tpFile: tpCont = tpFile.read() tpCont = tpCont.replace('(FinalBSplineInterpolationOrder 3)', '(FinalBSplineInterpolationOrder 1)') with open(tpFilePath,'w') as tpFile: tpCont = tpFile.write(tpCont) if args.atlas is not None: atlasOutDir = join(currOutDir,'atlas') mkdir(atlasOutDir) trfxCmd = trfxExec+' -in '+args.atlas+' -out '+atlasOutDir+' tp '+transformParameterFiles[-1] try: resampleLog = ccall(trfxCmd, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, join(atlasOutDir,'ERR.txt'))
def call(args): cmd = ' '.join(flatten(args)) try: if verbose == True: print(cmd) ccall(cmd, shell=True) except: print("Errors while compiling!") sys.exit(1)
def call(args, env=None): cmd = ' '.join(flatten(args)) if env != None: env = to_env(env) try: if conf["VERBOSITY"] > 1: print(cmd) ccall(cmd, shell=True, env=env) except: print("Errors encountered while compiling!") sys.exit(1)
def try_call(args, env=None): cmd = ' '.join(flatten(args)) if env != None: env = to_env(env) try: if conf["VERBOSITY"] > 1: print(cmd) ccall(cmd, shell=True, env=env) return True except: return False
def try_call(args, env = None): cmd = ' '.join(flatten(args)) if env != None: env = to_env(env) try: if conf["VERBOSITY"] > 1: print(cmd) ccall(cmd, shell=True, env=env) return True except: return False
def call(args, env = None): cmd = ' '.join(flatten(args)) if env != None: env = to_env(env) try: if conf["VERBOSITY"] > 1: print(cmd) ccall(cmd, shell=True, env = env) except: print("Errors encountered while compiling!") sys.exit(1)
def mainFunc(): parser = argparse.ArgumentParser( description= 'Run Elastix registration protocol for all images in the directory') parser.add_argument('--refDir', '-r', dest='refDir', required = True, \ help='The directory containing the reference images.') parser.add_argument('--floatFile', '-f', dest='floatFile', required = True, \ help='Path to the floating image.') parser.add_argument('--outDir', '-o', dest='outDir', required = False, \ help='Path to store the output images/parameters (default: current dir)', default=os.getcwd()) parser.add_argument('--atlas', '-a', dest='atlas', required = False, \ help='Path to the atlas segmentation file which will be resampled with the CPP file from the registration.', default=None) args = parser.parse_args() refImgs = [join(args.refDir, File) for File in listdir(args.refDir)] refImgs = [img for img in refImgs if isfile(img) and img.endswith('.nii')] if not refImgs: print('Couldn\'t find any reference images') return if not path.isfile(args.floatFile): print('Coudln\'t find the float image') refImgs.sort(key=str.lower) refFloatPairs = [[refImg, args.floatFile] for refImg in refImgs] f3dParStr = paramListToShortString(f3d_params) aladinParStr = paramListToShortString(aladin_params) for rfPair in refFloatPairs: baseName = basename(rfPair[0])[:-4] + '_' + basename(rfPair[1])[:-4] currOutDir = join(args.outDir, baseName) mkdir(currOutDir) elastixLogPath = join(currOutDir, basename + '_LOG.txt') elastixCommand = elastixExec + ' -f ' + rfPair[0] + ' -m ' + rfPair[ 1] + ' -p '.join(elastixParams) + ' -o ' + currOutDir elastixLog = '' try: elastixLog = ccall(elastixCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, elastixLogPath) with open(elastixLogPath, 'w') as f: f.write(elastixLog) transformParameterFiles = [ 'TransformParameters.0.txt', 'TransformParameters.1.txt' ] transformParameterFiles = [ join(currOutDir, tpFile) for tpFile in transformParameterFiles ] for tpFilePath in transformParameterFiles: with open(tpFilePath, 'r') as tpFile: tpCont = tpFile.read() tpCont = tpCont.replace('(FinalBSplineInterpolationOrder 3)', '(FinalBSplineInterpolationOrder 1)') with open(tpFilePath, 'w') as tpFile: tpCont = tpFile.write(tpCont) if args.atlas is not None: atlasOutDir = join(currOutDir, 'atlas') mkdir(atlasOutDir) trfxCmd = trfxExec + ' -in ' + args.atlas + ' -out ' + atlasOutDir + ' tp ' + transformParameterFiles[ -1] try: resampleLog = ccall(trfxCmd, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, join(atlasOutDir, 'ERR.txt'))
def mainFunc(): parser = argparse.ArgumentParser( description= 'Run standard Registration protocol for all images in the directory') parser.add_argument('--refDir', '-r', dest='refDir', required = True, \ help='The directory containing the reference images.') parser.add_argument('--floatFile', '-f', dest='floatFile', required = True, \ help='Path to the floating image.') parser.add_argument('--outDir', '-o', dest='outDir', required = False, \ help='Path to store the output images/parameters (default: current dir)', default=os.getcwd()) parser.add_argument('--atlas', '-a', dest='atlas', required = False, \ help='Path to the atlas segmentation file which will be resampled with the CPP file from the registration.', default=None) parser.add_argument('--resOnly', '-m', dest='resOnly', required = False, action='store_true',\ help='Resample atlas only, relying on previous registration run.', default=False) args = parser.parse_args() refImgs = [join(args.refDir, File) for File in listdir(args.refDir)] refImgs = [ img for img in refImgs if isfile(img) and (img.endswith('.nii') or img.endswith('.nii.gz')) ] if not refImgs: print('Couldn\'t find any reference images') return if not path.isfile(args.floatFile): print('Coudln\'t find the float image') refImgs.sort(key=str.lower) refFloatPairs = [[refImg, args.floatFile] for refImg in refImgs] f3dParStr = paramListToShortString(f3d_params) aladinParStr = paramListToShortString(aladin_params) for rfPair in refFloatPairs: baseName = basename(rfPair[0])[:nameEnd(rfPair[0])] + '_' + basename( rfPair[1])[:nameEnd(rfPair[1])] cppPath = join(args.outDir, baseName + '_' + f3dParStr + '_CPP' + '.nii') affPath = join(args.outDir, baseName + '_' + aladinParStr + '_AFF' + '.txt') imgsArg = ['-ref ' + rfPair[0], '-flo ' + rfPair[1]] f3dArg = ['-cpp ' + cppPath] affArg = ['-aff ' + affPath] regAladinCommand = aladinExec + ' '.join(imgsArg + aladin_params + affArg) regF3dCommand = f3dExec + ' '.join(imgsArg + f3d_params + affArg + f3dArg) aladinLogPath = affPath[0:-4] + '_LOG.txt' f3dLogPath = cppPath[0:-4] + '_LOG.txt' aladinLog = '' f3dLog = '' if args.resOnly is False: if not path.isfile(affPath): try: aladinLog = ccall(regAladinCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, aladinLogPath) with open(aladinLogPath, 'w') as f: f.write(aladinLog) try: print(regF3dCommand) f3dLog = ccall(regF3dCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: continue writeAndDie(err.output, f3dLogPath) with open(f3dLogPath, 'w') as f: f.write(f3dLog) if args.atlas is not None: mapPath = join(args.outDir, baseName + '_' + f3dParStr + '_MAP' + '.nii.gz') resampleArgs = [ '-ref ' + rfPair[0], '-flo ' + args.atlas, '-trans ' + cppPath, '-res ' + mapPath ] resampleCommand = resampleExec + ' '.join(resampleArgs + resample_params) #print(resampleCommand) try: resampleLog = ccall(resampleCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, mapPath[0:-4] + 'ERR.txt')
def mainFunc(): parser = argparse.ArgumentParser(description='Run standard Registration protocol for all images in the directory') parser.add_argument('--refDir', '-r', dest='refDir', required = True, \ help='The directory containing the reference images.') parser.add_argument('--floatFile', '-f', dest='floatFile', required = True, \ help='Path to the floating image.') parser.add_argument('--outDir', '-o', dest='outDir', required = False, \ help='Path to store the output images/parameters (default: current dir)', default=os.getcwd()) parser.add_argument('--atlas', '-a', dest='atlas', required = False, \ help='Path to the atlas segmentation file which will be resampled with the CPP file from the registration.', default=None) parser.add_argument('--resOnly', '-m', dest='resOnly', required = False, action='store_true',\ help='Resample atlas only, relying on previous registration run.', default=False) args = parser.parse_args() refImgs = [join(args.refDir, File) for File in listdir(args.refDir)] refImgs = [img for img in refImgs if isfile(img) and (img.endswith('.nii') or img.endswith('.nii.gz'))] if not refImgs: print('Couldn\'t find any reference images') return if not path.isfile(args.floatFile): print('Coudln\'t find the float image') refImgs.sort(key=str.lower) refFloatPairs = [[refImg, args.floatFile] for refImg in refImgs] f3dParStr = paramListToShortString(f3d_params) aladinParStr = paramListToShortString(aladin_params) for rfPair in refFloatPairs: baseName = basename(rfPair[0])[:nameEnd(rfPair[0])]+'_'+basename(rfPair[1])[:nameEnd(rfPair[1])] cppPath = join(args.outDir,baseName+'_'+f3dParStr+'_CPP'+'.nii') affPath = join(args.outDir,baseName+'_'+aladinParStr+'_AFF'+'.txt') imgsArg = ['-ref '+rfPair[0],'-flo '+rfPair[1]] f3dArg = ['-cpp '+cppPath] affArg = ['-aff '+affPath] regAladinCommand = aladinExec+' '.join(imgsArg+aladin_params+affArg) regF3dCommand = f3dExec+' '.join(imgsArg+f3d_params+affArg+f3dArg) aladinLogPath = affPath[0:-4]+'_LOG.txt' f3dLogPath = cppPath[0:-4]+'_LOG.txt' aladinLog='' f3dLog='' if args.resOnly is False: if not path.isfile(affPath): try: aladinLog = ccall(regAladinCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, aladinLogPath) with open(aladinLogPath, 'w') as f: f.write(aladinLog) try: print(regF3dCommand) f3dLog = ccall(regF3dCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: continue writeAndDie(err.output, f3dLogPath) with open(f3dLogPath, 'w') as f: f.write(f3dLog) if args.atlas is not None: mapPath = join(args.outDir,baseName+'_'+f3dParStr+'_MAP'+'.nii.gz') resampleArgs = ['-ref '+rfPair[0],'-flo '+args.atlas,'-trans '+cppPath,'-res '+mapPath] resampleCommand = resampleExec+' '.join(resampleArgs+resample_params) #print(resampleCommand) try: resampleLog = ccall(resampleCommand, shell=True, stderr=STDOUT) except CalledProcessError as err: writeAndDie(err.output, mapPath[0:-4]+'ERR.txt')
def install(packagename): ccall(["pacman", "-Ss", "--noconfirm", packagename]) logging.info("installed {}".format(packagename))