def main(args): sys.stdout.flush() T1 = args.t1 T2 = args.t2 ATLAS = args.atlas BRAINSFit = args.BRAINSFit OUTPUT_DIR = args.output if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) T2_exists = True if (T2 == ""): T2_exists = False T1_dir = os.path.dirname(T1) T1_split = os.path.splitext(os.path.basename(T1)) if (T1_split[1] == 'gz'): T1_base = os.path.splitext(T1_split[0]) else: T1_base = T1_split[0] STX_T1 = os.path.join(OUTPUT_DIR, "".join([T1_base, "_stx.nrrd"])) if (T2_exists): T2_dir = os.path.dirname(T2) T2_split = os.path.splitext(os.path.basename(T2)) if (T2_split[1] == 'gz'): T2_base = os.path.splitext(T2_split[0]) else: T2_base = T2_split[0] STX_T2 = os.path.join(OUTPUT_DIR, "".join([T2_base, "_stx.nrrd"])) if (not os.path.isfile(STX_T1)): args=[BRAINSFit, '--fixedVolume', ATLAS, '--movingVolume', T1, '--outputVolume', STX_T1, '--useRigid',\ '--initializeTransformMode', 'useCenterOfHeadAlign', '--outputVolumePixelType', 'short'] call_and_print(args) else: print_aef("T1 image already aligned") if (T2_exists): if (not os.path.isfile(STX_T2)): args=[BRAINSFit, '--fixedVolume', STX_T1, '--movingVolume', T2, '--outputVolume', STX_T2, '--useRigid',\ '--initializeTransformMode', 'useCenterOfHeadAlign', '--outputVolumePixelType', 'short'] call_and_print(args) else: print_aef("T2 image already aligned")
def main(args): T1 = args.t1 T2 = args.t2 BrainMask = args.mask output_dir = args.output ImageMath = args.ImageMath if not os.path.exists(output_dir): os.makedirs(output_dir) if (BrainMask is not None and os.path.exists(BrainMask)): print("Skull stripping ...") if (T1 is not None and os.path.exists(T1)): T1_Base = os.path.splitext(os.path.basename(T1))[0] T1_STRIPPED = os.path.join(output_dir, T1_Base + "_stripped.nrrd") args = [ImageMath, T1, '-mask', BrainMask, '-outfile', T1_STRIPPED] call_and_print(args) if (T2 is not None and os.path.exists(T2)): T2_Base = os.path.splitext(os.path.basename(T2))[0] T2_STRIPPED = os.path.join(output_dir, T2_Base + "_stripped.nrrd") args = [ImageMath, T2, '-mask', BrainMask, '-outfile', T2_STRIPPED] call_and_print(args) else: print( "Warning! No brain mask provided or computed. The input T1/T2 will be used in the following steps..." ) if (T1 is not None and os.path.exists(T1)): T1_Base = os.path.splitext(os.path.basename(T1))[0] T1_STRIPPED = os.path.join(output_dir, T1_Base + "_stripped.nrrd") print("cp ", T1, T1_STRIPPED) copyfile(T1, T1_STRIPPED) if (T2 is not None and os.path.exists(T2)): T2_Base = os.path.splitext(os.path.basename(T2))[0] T2_STRIPPED = os.path.join(output_dir, T2_Base + "_stripped.nrrd") print("cp ", T2, T2_STRIPPED) copyfile(T2, T2_STRIPPED)
def main(args): T1 = args.t1 T2 = args.t2 ATLAS_DIR = args.at_dir BRAINSFit = args.BRAINSFit ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform ABC = args.ABC OUTPUT_DIR = args.output if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) PARENT_DIR = os.path.dirname(OUTPUT_DIR) TEMPLATE = os.path.join(ATLAS_DIR, 'template.mha') AFFINE_TF = os.path.join(OUTPUT_DIR, 'output_affine_transform.txt') TEMPLATE_AFF = os.path.join(OUTPUT_DIR, 'template_affine.nrrd') ANTs_MATRIX_NAME = os.path.join(OUTPUT_DIR, 'template_to_T1_') ANTs_WARP = ''.join([ANTs_MATRIX_NAME, 'Warp.nii.gz']) ANTs_INV_WARP = ''.join([ANTs_MATRIX_NAME, 'InverseWarp.nii.gz']) ANTs_AFFINE = ''.join([ANTs_MATRIX_NAME, 'Affine.txt']) TEMPLATE_FIN = os.path.join(OUTPUT_DIR, 'template.mha') if not (os.path.isfile(TEMPLATE) and os.path.isfile(AFFINE_TF) and os.path.isfile(TEMPLATE_AFF) and os.path.isfile(ANTs_WARP) and os.path.isfile(ANTs_INV_WARP) and os.path.isfile(ANTs_AFFINE) and os.path.isfile(TEMPLATE_FIN)): args = [ BRAINSFit, '--fixedVolume', T1, '--movingVolume', TEMPLATE, '--outputTransform', AFFINE_TF, '--useRigid', '--initializeTransformMode', 'useCenterOfHeadAlign' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_AFF, AFFINE_TF, '-R', T1 ] call_and_print(args) args = [ ANTS, '3', '-m', 'CC[' + T1 + ',' + TEMPLATE + ',1,4]', '-i', '100x50x25', '-o', ANTs_MATRIX_NAME, '-t', 'SyN[0.25]', '-r', 'Gauss[3,0]' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1 ] call_and_print(args) else: print_aef('ANTs registration already exists') for i in range(1, 6): FILE = os.path.join(ATLAS_DIR, str(i) + '.mha') FILE_FIN = os.path.join(OUTPUT_DIR, str(i) + '.mha') if not (os.path.isfile(FILE_FIN)): args = [ WarpImageMultiTransform, '3', FILE, FILE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1 ] call_and_print(args) else: print_aef(FILE_FIN + ' already exist') segfile_path = os.path.join(PARENT_DIR, 'ABCparam.xml') segfile = open(segfile_path, 'r') segfiledata = segfile.read() segfiledata = segfiledata.replace('@T1_INSEG_IMG@', T1) segfiledata = segfiledata.replace('@T2_INSEG_IMG@', T2) segfile = open(segfile_path, 'w') segfile.write(segfiledata) segfile.close() args = [ABC, segfile_path] call_and_print(args)
def main(args): T1 = args.t1 T2 = args.t2 ATLAS_DIR = args.at_dir BRAINSFit = args.BRAINSFit ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform ABC=args.ABC OUTPUT_DIR = args.output if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) PARENT_DIR = os.path.dirname(OUTPUT_DIR) TEMPLATE = os.path.join(ATLAS_DIR,'template.mha') AFFINE_TF = os.path.join(OUTPUT_DIR,'output_affine_transform.txt') TEMPLATE_AFF = os.path.join(OUTPUT_DIR,'template_affine.nrrd') ANTs_MATRIX_NAME = os.path.join(OUTPUT_DIR,'template_to_T1_') ANTs_WARP = ''.join([ANTs_MATRIX_NAME,'Warp.nii.gz']) ANTs_AFFINE = ''.join([ANTs_MATRIX_NAME,'Affine.txt']) TEMPLATE_FIN = os.path.join(OUTPUT_DIR,'template.mha') args = [BRAINSFit, '--fixedVolume', T1, '--movingVolume', TEMPLATE, '--outputTransform',AFFINE_TF, '--useRigid', '--initializeTransformMode', 'useCenterOfHeadAlign'] call_and_print(args) args = [WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_AFF, AFFINE_TF, '-R', T1] call_and_print(args) args = [ANTS, '3', '-m', 'CC['+T1+','+TEMPLATE+',1,4]', '-i', '100x50x25', '-o', ANTs_MATRIX_NAME, '-t', 'SyN[0.25]', '-r', 'Gauss[3,0]'] call_and_print(args) args = [WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1] call_and_print(args) for i in range(1,6): FILE = os.path.join(ATLAS_DIR,str(i)+'.mha') FILE_FIN = os.path.join(OUTPUT_DIR,str(i)+'.mha') args = [WarpImageMultiTransform, '3', FILE, FILE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1] call_and_print(args) args = [ABC, os.path.join(PARENT_DIR,'ABCparam.xml')] call_and_print(args)
def main(args): sys.stdout.flush() T1 = args.t1 TEMPLATE_T1_VENTRICLE = args.templateT1Ventricle TEMPLATE_INV_MASK_VENTRICLE = args.templateInvMaskVentricle SUBJECT_VENTRICLE_MASK = args.subjectVentricleMask SUBJECT_TISSUE_SEG = args.subjectTissueSeg #ANTS REGISTRATION PARAMETERS REGISTRATION_TYPE = args.registrationType TRANSFORMATION_STEP = args.transformationStep ITERATIONS = args.iterations SIM_METRIC = args.simMetric SIM_PARAMETER = args.simParameter GAUSSIAN = args.gaussian T1_WEIGHT = args.t1Weight ############################# ImageMath = args.ImageMath ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform OUTPUT_DIR = args.output if SUBJECT_TISSUE_SEG == "": tissue_seg = find_file( ".*_labels_EMS.nrrd", os.path.normpath(os.path.join(OUTPUT_DIR, "..", "ABC_Segmentation"))) if tissue_seg is not None: SUBJECT_TISSUE_SEG = tissue_seg if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) if (os.path.isfile(TEMPLATE_INV_MASK_VENTRICLE)): T1_dir = os.path.dirname(T1) T1_base = os.path.splitext(os.path.basename(T1))[0] TEMPLATE_INV_MASK_VENTRICLE_base = os.path.splitext( os.path.basename(TEMPLATE_INV_MASK_VENTRICLE))[0] TEMPLATE_INV_MASK_VENTRICLE_REG = os.path.join( OUTPUT_DIR, "".join([TEMPLATE_INV_MASK_VENTRICLE_base, "_Registered.nrrd"])) TEMPLATE_T1_VENTRICLE_base = os.path.splitext( os.path.basename(TEMPLATE_T1_VENTRICLE))[0] TEMPLATE_T1_VENTRICLE_REG = os.path.join( OUTPUT_DIR, "".join([TEMPLATE_T1_VENTRICLE_base, "_Registered.nrrd"])) SEG_WithoutVent = os.path.join( OUTPUT_DIR, "".join([T1_base, "_EMS_withoutVent.nrrd"])) ANTs_MATRIX_NAME = os.path.join(OUTPUT_DIR, T1_base) ANTs_WARP = os.path.join(OUTPUT_DIR, "".join([T1_base, "Warp.nii.gz"])) ANTs_INV_WARP = os.path.join(OUTPUT_DIR, "".join([T1_base, "Warp.nii.gz"])) ANTs_AFFINE = os.path.join(OUTPUT_DIR, "".join([T1_base, "Affine.txt"])) if not (os.path.isfile(ANTs_WARP) and os.path.isfile(ANTs_AFFINE)): args = [ ANTS, '3', '-m', SIM_METRIC + '[' + T1 + ',' + TEMPLATE_T1_VENTRICLE + ',' + T1_WEIGHT + ',' + SIM_PARAMETER + ']', '-i', ITERATIONS, '-o', ANTs_MATRIX_NAME, '-t', 'SyN[' + TRANSFORMATION_STEP + ']', '-r', 'Gauss[' + GAUSSIAN + ',0]' ] call_and_print(args) else: print('Ventricle masking : ANTs registration already exists') args = [ WarpImageMultiTransform, '3', TEMPLATE_INV_MASK_VENTRICLE, TEMPLATE_INV_MASK_VENTRICLE_REG, ANTs_WARP, ANTs_AFFINE, '-R', T1, '--use-NN' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', TEMPLATE_T1_VENTRICLE, TEMPLATE_T1_VENTRICLE_REG, ANTs_WARP, ANTs_AFFINE, '-R', T1, '--use-NN' ] call_and_print(args) if SUBJECT_TISSUE_SEG is not None and os.path.exists( SUBJECT_TISSUE_SEG): args = [ ImageMath, SUBJECT_TISSUE_SEG, '-mul', TEMPLATE_INV_MASK_VENTRICLE_REG, '-outfile', SEG_WithoutVent ] call_and_print(args) else: print( "Template inverse ventricle mask is not a file and cannot be applied" ) if (os.path.isfile(SUBJECT_VENTRICLE_MASK) and TEMPLATE_INV_MASK_VENTRICLE == ""): SUBJECT_VENTRICLE_MASK_base = os.path.splitext( os.path.basename(SUBJECT_VENTRICLE_MASK))[0] SUBJECT_VENTRICLE_MASK_INV = os.path.join( OUTPUT_DIR, SUBJECT_VENTRICLE_MASK_base + '_INV.nrrd') args = [ ImageMath, SUBJECT_VENTRICLE_MASK, '-threshold', '0,0', '-outfile', SUBJECT_VENTRICLE_MASK_INV ] call_and_print(args) SEG_WithoutVent = os.path.join( OUTPUT_DIR, "".join([T1_base, "_EMS_withoutVent.nrrd"])) args = [ ImageMath, SUBJECT_TISSUE_SEG, '-mul', SUBJECT_VENTRICLE_MASK_INV, '-outfile', SEG_WithoutVent ] call_and_print(args)
def main(args): sys.stdout.flush() T1 = args.t1 ATLAS = args.atlas #ANTS REGISTRATION PARAMETERS REGISTRATION_TYPE = args.registrationType TRANSFORMATION_STEP = args.transformationStep ITERATIONS = args.iterations SIM_METRIC = args.simMetric SIM_PARAMETER = args.simParameter GAUSSIAN = args.gaussian T1_WEIGHT = args.t1Weight ############################# ImageMath = args.ImageMath ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform TISSUE_SEG = args.tissueSeg VENT_MASK = args.ventricleMask OUTPUT_DIR = args.output if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) AUTO_EACSF_PARENT_DIR = os.path.dirname( os.path.dirname(os.path.dirname(os.path.dirname(OUTPUT_DIR)))) MASKS_DIR = os.path.join(AUTO_EACSF_PARENT_DIR, 'auto_EACSF/data/masks') VENT_REGION_MASK = os.path.join(MASKS_DIR, 'vent_region_mask_INV.nrrd') VENT_MASK_base = os.path.splitext(os.path.basename(VENT_MASK))[0] VENT_MASK_INV = os.path.join(OUTPUT_DIR, VENT_MASK_base + '_INV.nrrd') T1_dir = os.path.dirname(T1) T1_base = os.path.splitext(os.path.basename(T1))[0] OUT_VENT_MASK = os.path.join(OUTPUT_DIR, "".join([T1_base, "_AtlasToVent.nrrd"])) OUT_VENT_REGION_MASK = os.path.join( OUTPUT_DIR, "".join([T1_base, "_AtlasToVentReg.nrrd"])) SEG_WithoutVent = os.path.join(OUTPUT_DIR, "".join([T1_base, "_EMS_withoutVent.nrrd"])) ANTs_MATRIX_NAME = os.path.join(OUTPUT_DIR, T1_base) ANTs_WARP = os.path.join(OUTPUT_DIR, "".join([T1_base, "Warp.nii.gz"])) ANTs_AFFINE = os.path.join(OUTPUT_DIR, "".join([T1_base, "Affine.txt"])) args = [ ANTS, '3', '-m', SIM_METRIC + '[' + T1 + ',' + ATLAS + ',' + T1_WEIGHT + ',' + SIM_PARAMETER + ']', '-i', ITERATIONS, '-o', ANTs_MATRIX_NAME, '-t', 'SyN[' + TRANSFORMATION_STEP + ']', '-r', 'Gauss[' + GAUSSIAN + ',0]' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', VENT_REGION_MASK, OUT_VENT_REGION_MASK, ANTs_WARP, ANTs_AFFINE, '-R', T1, '--use-NN' ] call_and_print(args) args = [ ImageMath, TISSUE_SEG, '-mul', OUT_VENT_REGION_MASK, '-outfile', SEG_WithoutVent ] call_and_print(args) if (VENT_MASK != ""): VM_im = itk.imread(VENT_MASK) inv_filter = itk.InvertIntensityImageFilter.New(VM_im) itk.imwrite(inv_filter, VENT_MASK_INV) args = [ WarpImageMultiTransform, '3', VENT_MASK_INV, OUT_VENT_MASK, ANTs_WARP, ANTs_AFFINE, '-R', T1, '--use-NN' ] call_and_print(args) args = [ ImageMath, SEG_WithoutVent, '-mul', OUT_VENT_MASK, '-outfile', SEG_WithoutVent ] call_and_print(args)
def main(args): T1 = args.t1 T2 = args.t2 T2_exists = True if (T2 == ""): T2_exists = False atlases_dir = args.at_dir atlases_list = args.at_list ImageMath = args.ImageMath FSLBET = args.FSLBET convertITKformats = args.convertITKformats ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform output_dir = args.output if not os.path.exists(output_dir): os.makedirs(output_dir) atlases_list = atlases_list.split(',') l = len(atlases_list) if (l % 2 != 0): exit('Atlases list : wrong format') atlases_nb = int(l / 2) ### Convert to *.nii.gz T1_dir = os.path.dirname(T1) T1_split = os.path.splitext(os.path.basename(T1)) T1_base = T1_split[0] if (T1_split[1] != '.gz'): Input_T1_NII = os.path.join(T1_dir, "".join([T1_base, ".nii.gz"])) else: T1_base = os.path.splitext(os.path.basename(T1_base))[0] Input_T1_NII = T1 args_maj_vote = [ImageMath, Input_T1_NII, '-majorityVoting'] if (T2_exists): T2_dir = os.path.dirname(T2) T2_split = os.path.splitext(os.path.basename(T2)) T2_base = T2_split[0] if (T2_split[1] != '.gz'): Input_T2_NII = os.path.join(T2_dir, "".join([T2_base, ".nii.gz"])) else: T2_base = os.path.splitext(os.path.basename(T2_base))[0] Input_T2_NII = T2 if (T1 != Input_T1_NII): args = [convertITKformats, T1, Input_T1_NII] call_and_print(args) if (T2_exists): if (T2 != Input_T2_NII): args = [convertITKformats, T2, Input_T2_NII] call_and_print(args) T1_Only_Mask = os.path.join(T1_dir, "".join([T1_base, "_T1only"])) if (T2_exists): T2_Only_Mask = os.path.join(T2_dir, "".join([T2_base, "_T2only"])) ##--> T2 Jointly T1 (FSL BET) T2_Joint_T1_Mask = os.path.join( T1_dir, "".join([T1_base, "_T2JointT1_mask.nii.gz"])) T2_Joint_T1_Mask1 = os.path.join(T1_dir, "".join([T1_base, "_T2JointT1_tmp1"])) T2_Joint_T1_Mask2 = os.path.join(T1_dir, "".join([T1_base, "_T2JointT1_tmp2"])) args = [ FSLBET, Input_T2_NII, T2_Joint_T1_Mask1, '-f', '0.52', '-g', '0.2', '-m', '-n', '-A2', Input_T1_NII, '-R' ] call_and_print(args) args = [ FSLBET, Input_T2_NII, T2_Joint_T1_Mask2, '-f', '0.52', '-g', '0.2', '-m', '-n', '-A2', Input_T1_NII, '-R' ] call_and_print(args) T2_Joint_T1_Mask1 = ''.join([T2_Joint_T1_Mask1, "_mask.nii.gz"]) T2_Joint_T1_Mask2 = ''.join([T2_Joint_T1_Mask2, "_mask.nii.gz"]) args = [ ImageMath, T2_Joint_T1_Mask1, '-add', T2_Joint_T1_Mask2, '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-threshold', '1,2', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-dilate', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-erode', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) Input_T1_NII_255 = os.path.join(T1_dir, "".join([T1_base, "_255.nii.gz"])) Pre_SkullMask = os.path.join(T1_dir, "".join([T1_base, "_255_Skull.nii.gz"])) args = [ ImageMath, Input_T1_NII, '-rescale', '0,255', '-outfile', Input_T1_NII_255 ] call_and_print(args) args = [ ImageMath, Input_T1_NII_255, '-threshold', '0,240', '-outfile', Pre_SkullMask ] call_and_print(args) if (T2_exists): args = [ ImageMath, T2_Joint_T1_Mask, '-mul', Pre_SkullMask, '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-erode', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-dilate', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-conComp', '1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) for i in range(0, 2): args = [ ImageMath, T2_Joint_T1_Mask, '-dilate', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args = [ ImageMath, T2_Joint_T1_Mask, '-erode', '1,1', '-outfile', T2_Joint_T1_Mask ] call_and_print(args) args_maj_vote.append(T2_Joint_T1_Mask) for k in range(0, atlases_nb): atlas_base = atlases_list[2 * k] atlas_type = int(atlases_list[2 * k + 1]) if (atlas_type < 3): if (atlas_type == 1): ATLAS_suffix = '_T1.nrrd' IM_base = T1_base Input = Input_T1_NII else: ATLAS_suffix = '_T2.nrrd' IM_base = T2_base Input = Input_T2_NII ATLAS = os.path.join(atlases_dir, ''.join([atlas_base, ATLAS_suffix])) ATLAS_MASK = os.path.join(atlases_dir, ''.join([atlas_base, '_brainmask.nrrd'])) OUT_MASK = os.path.join( output_dir, ''.join([IM_base, '_', atlas_base, '_mask.nii.gz'])) ANTs_MATRIX_NAME = os.path.join( output_dir, ''.join([IM_base, '_', atlas_base, '_'])) ANTs_WARP = ''.join([ANTs_MATRIX_NAME, 'Warp.nii.gz']) ANTs_AFFINE = ''.join([ANTs_MATRIX_NAME, 'Affine.txt']) args = [ ANTS, '3', '-m', 'CC[' + Input + ',' + ATLAS + ',1,4]', '-i', '100x50x25', '-o', ANTs_MATRIX_NAME, '-t', 'SyN[0.25]', '-r', 'Gauss[3,0]' ] else: T1_ATLAS = os.path.join(atlases_dir, ''.join([atlas_base, '_T1.nrrd'])) T2_ATLAS = os.path.join(atlases_dir, ''.join([atlas_base, '_T2.nrrd'])) ATLAS_MASK = os.path.join(atlases_dir, ''.join([atlas_base, '_brainmask.nrrd'])) OUT_MASK = os.path.join( output_dir, ''.join([T2_base, '_joined_', atlas_base, '_mask.nii.gz'])) ANTs_MATRIX_NAME = os.path.join( output_dir, "".join([T2_base, '_joined_', atlas_base, '_'])) ANTs_WARP_ = ''.join([ANTs_MATRIX_NAME, 'Warp.nii.gz']) ANTs_AFFINE = ''.join([ANTs_MATRIX_NAME, 'Affine.txt']) args = [ ANTS, '3', '-m', 'CC[' + Input_T1_NII + ',' + T1_ATLAS + ',1,4]', '-m', 'CC[' + Input_T2_NII + ',' + T2_ATLAS + ',1,4]', '-i', '100x50x25', '-o', ANTs_MATRIX_NAME, '-t', 'SyN[0.25]', '-r', 'Gauss[3,0]' ] call_and_print(args) if (atlas_type == 2): args = [ WarpImageMultiTransform, '3', ATLAS_MASK, OUT_MASK, ANTs_WARP, ANTs_AFFINE, '-R', Input_T2_NII, '--use-NN' ] else: args = [ WarpImageMultiTransform, '3', ATLAS_MASK, OUT_MASK, ANTs_WARP, ANTs_AFFINE, '-R', Input_T1_NII, '--use-NN' ] call_and_print(args) args_maj_vote.append(OUT_MASK) ##--> Majority Vote Weighted_Majority_Mask = os.path.join( output_dir, "".join([T1_base, "_weightedMajority.nii.gz"])) #args=[ImageMath,Input_T1_NII,'-majorityVoting', T2_Joint_T1_Mask, T1_Only_Mask, T2_Only_Mask, COLIN_OUT_MASK, ICMB152_OUT_MASK, # BIGCSF01_OUT_MASK, BIGCSF02_OUT_MASK,'-outfile', Weighted_Majority_Mask] args_maj_vote.extend(['-outfile', Weighted_Majority_Mask]) call_and_print(args_maj_vote) TEMP_ERODE_MASK = os.path.join(output_dir, "".join([T1_base, "_TEMP_ERODE.nii.gz"])) args = [ ImageMath, Weighted_Majority_Mask, '-erode', '8,1', '-outfile', TEMP_ERODE_MASK ] call_and_print(args) FINAL_MASK = os.path.join(output_dir, "".join([T1_base, "_FinalBrainMask.nrrd"])) args = [ ImageMath, Weighted_Majority_Mask, '-dilate', '1,1', '-outfile', Weighted_Majority_Mask ] call_and_print(args) args = [ ImageMath, Weighted_Majority_Mask, '-erode', '1,1', '-outfile', FINAL_MASK ] call_and_print(args)
def main(args): ABC_TEMPLATE = "<?xml version=\"1.0\"?>\n\ <!DOCTYPE SEGMENTATION-PARAMETERS>\n\ <SEGMENTATION-PARAMETERS>\n\ <SUFFIX>EMS</SUFFIX>\n\ <ATLAS-DIRECTORY>@ATLAS_DIR@</ATLAS-DIRECTORY>\n\ <ATLAS-ORIENTATION>file</ATLAS-ORIENTATION>\n\ <OUTPUT-DIRECTORY>@OUTPUT_DIR@</OUTPUT-DIRECTORY>\n\ <OUTPUT-FORMAT>Nrrd</OUTPUT-FORMAT>\n\ @T1_INSEG_IMG@\ @T2_INSEG_IMG@\ <FILTER-ITERATIONS>10</FILTER-ITERATIONS>\n\ <FILTER-TIME-STEP>0.01</FILTER-TIME-STEP>\n\ <FILTER-METHOD>Curvature flow</FILTER-METHOD>\n\ <MAX-BIAS-DEGREE>4</MAX-BIAS-DEGREE>\n\ @PRIORS@\n\ <INITIAL-DISTRIBUTION-ESTIMATOR>robust</INITIAL-DISTRIBUTION-ESTIMATOR>\n\ <DO-ATLAS-WARP>0</DO-ATLAS-WARP>\n\ <ATLAS-WARP-FLUID-ITERATIONS>50</ATLAS-WARP-FLUID-ITERATIONS>\n\ <ATLAS-WARP-FLUID-MAX-STEP>0.1</ATLAS-WARP-FLUID-MAX-STEP>\n\ <ATLAS-LINEAR-MAP-TYPE>id</ATLAS-LINEAR-MAP-TYPE>\n\ <IMAGE-LINEAR-MAP-TYPE>id</IMAGE-LINEAR-MAP-TYPE>\n\ </SEGMENTATION-PARAMETERS>" T1 = args.t1 T2 = args.t2 ATLAS_DIR = args.at_dir BRAINSFit = args.BRAINSFit ANTS = args.ANTS WarpImageMultiTransform = args.WarpImageMultiTransform ABC = args.ABC OUTPUT_DIR = args.output OUTPUT_DIR_TISSUE_ATLAS = os.path.join(OUTPUT_DIR, "TissueSegmentationAtlas") if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) if not os.path.exists(OUTPUT_DIR_TISSUE_ATLAS): os.makedirs(OUTPUT_DIR_TISSUE_ATLAS) TEMPLATE = os.path.join(ATLAS_DIR, 'template.mha') AFFINE_TF = os.path.join(OUTPUT_DIR_TISSUE_ATLAS, 'output_affine_transform.txt') TEMPLATE_AFF = os.path.join(OUTPUT_DIR_TISSUE_ATLAS, 'template_affine.nrrd') ANTs_MATRIX_NAME = os.path.join(OUTPUT_DIR_TISSUE_ATLAS, 'template_to_T1_') ANTs_WARP = ''.join([ANTs_MATRIX_NAME, 'Warp.nii.gz']) ANTs_INV_WARP = ''.join([ANTs_MATRIX_NAME, 'InverseWarp.nii.gz']) ANTs_AFFINE = ''.join([ANTs_MATRIX_NAME, 'Affine.txt']) TEMPLATE_FIN = os.path.join(OUTPUT_DIR_TISSUE_ATLAS, 'template.mha') if not (os.path.isfile(TEMPLATE) and os.path.isfile(AFFINE_TF) and os.path.isfile(TEMPLATE_AFF) and os.path.isfile(ANTs_WARP) and os.path.isfile(ANTs_INV_WARP) and os.path.isfile(ANTs_AFFINE) and os.path.isfile(TEMPLATE_FIN)): args = [ BRAINSFit, '--fixedVolume', T1, '--movingVolume', TEMPLATE, '--outputTransform', AFFINE_TF, '--useRigid', '--initializeTransformMode', 'useCenterOfHeadAlign' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_AFF, AFFINE_TF, '-R', T1 ] call_and_print(args) args = [ ANTS, '3', '-m', 'CC[' + T1 + ',' + TEMPLATE + ',1,4]', '-i', '100x50x25', '-o', ANTs_MATRIX_NAME, '-t', 'SyN[0.25]', '-r', 'Gauss[3,0]' ] call_and_print(args) args = [ WarpImageMultiTransform, '3', TEMPLATE, TEMPLATE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1 ] call_and_print(args) else: print('ANTs registration already exists') if os.path.exists(T1): T1_INSEG_IMG = "<IMAGE>\n\t<FILE>@T1_INSEG_IMG@</FILE>\n\t<ORIENTATION>file</ORIENTATION>\n</IMAGE>\n" T1_INSEG_IMG = T1_INSEG_IMG.replace("@T1_INSEG_IMG@", T1) else: T1_INSEG_IMG = "" if os.path.exists(T2): T2_INSEG_IMG = "<IMAGE>\n\t<FILE>@T2_INSEG_IMG@</FILE>\n\t<ORIENTATION>file</ORIENTATION>\n</IMAGE>\n" T2_INSEG_IMG = T2_INSEG_IMG.replace("@T2_INSEG_IMG@", T2) else: T2_INSEG_IMG = "" predefined_priors = [1.3, 1.0, 0.7, 0.8] priors_found = find_files("\\d\\.mha", ATLAS_DIR) for pr_name in priors_found: FILE_FIN = os.path.join(OUTPUT_DIR_TISSUE_ATLAS, os.path.basename(pr_name)) if not (os.path.isfile(FILE_FIN)): args = [ WarpImageMultiTransform, '3', pr_name, FILE_FIN, ANTs_WARP, ANTs_AFFINE, '-R', T1 ] call_and_print(args) priors = [] for i in range(len(priors_found) - 1): if i < len(predefined_priors) - 1: priors.append(predefined_priors[i]) else: priors.append(1) priors.append(predefined_priors[-1]) PRIORS = "" for pr in priors: PRIORS += "<PRIOR>" + str(pr) + "</PRIOR>\n" segfiledata = ABC_TEMPLATE.replace('@T1_INSEG_IMG@', T1_INSEG_IMG) segfiledata = segfiledata.replace('@T2_INSEG_IMG@', T2_INSEG_IMG) segfiledata = segfiledata.replace('@ATLAS_DIR@', OUTPUT_DIR_TISSUE_ATLAS) segfiledata = segfiledata.replace('@PRIORS@', PRIORS) abc_params = os.path.join(OUTPUT_DIR, 'ABCparam.xml') segfile = open(abc_params, 'w') segfile.write(segfiledata) segfile.close() labels_out = os.path.join( OUTPUT_DIR, os.path.splitext(os.path.basename(T1))[0] + "_labels_EMS.nrrd") if not (os.path.isfile(labels_out)): args = [ABC, abc_params] call_and_print(args)