def methods_02_lpa( input_dir, verbose ): cenc_dirs = cenc.directories( input_dir) wm_lesions_dir = cenc_dirs['wmlesions']['dirs']['root'] wm_lesions_lpa_dir = cenc_dirs['wmlesions']['dirs']['lpa'] util.mkcd_dir( [ cenc_dirs['wmlesions']['dirs']['lpa'] ], True) util.copy_inputs( [ os.path.join( cenc_dirs['wmlesions']['dirs']['register'], 't2flair_Affine_nu__t2flair_Warped.nii.gz') ], cenc_dirs['wmlesions']['dirs']['lpa'] ) glob_files = glob.glob('*.gz') for ii in glob_files: os.system('gunzip ' + ii) os.chmod( str.replace(ii, '.gz',''), stat.S_IRUSR | stat.S_IRGRP | stat.S_IWUSR | stat.S_IWGRP) # Run Matlab matlab_run_command = "ps_LST_lpa('" + os.path.join( cenc_dirs['wmlesions']['dirs']['lpa'], 't2flair_Affine_nu__t2flair_Warped.nii') + "',''); exit;" command = [ 'matlab', '-nojvm', '-nosplash', '-nodesktop', '-r', matlab_run_command ] util.iw_subprocess( command, verbose, verbose, False) os.chdir(cenc_dirs['wmlesions']['dirs']['lpa']) for ii in glob.glob('*.nii'): os.system('gzip ' + ii) os.chmod( str.replace(ii, '.nii','.nii.gz'), stat.S_IRUSR | stat.S_IRGRP | stat.S_IWUSR | stat.S_IWGRP)
def ants_register(input_file): # antsRegistration call: #-------------------------------------------------------------------------------------- if False: # I am having problems callling the interface. Same problem of bad interface. command = ['antsRegistration', '--dimensionality', 3, '--float', 0, '--output', '[', 'mt_Affine_nu__' + input_file +'_,', 'mt_Affine_nu__' + input_file + '_Warped.nii.gz,', 'mt_Affine_nu__' + input_file + '_InverseWarped.nii.gz', ']', '--interpolation', 'Linear', '--use-histogram-matching', 0, '--winsorize-image-intensities', [0.005,0.995], '--initial-moving-transform', '[', 'nu.nii.gz,' + input_file + '.nii.gz,', 1, ']', '--transform', 'Rigid[0.1]', '--metric', 'MI[nu.nii.gz,mt_m1.nii.gz,1,32,Regular,0.25]', '--convergence', '[1000x500x250x0,1e-6,10]', '--shrink-factors', '8x4x2x1', '--smoothing-sigmas', '3x2x1x0vox', '--transform', 'Affine[0.1]', '--metric', 'MI[nu.nii.gz,' + input_file + '.nii.gz,1,32,Regular,0.25]', '--convergence', '[1000x500x250x0,1e-6,10]', '--shrink-factors', '8x4x2x1', '--smoothing-sigmas', '3x2x1x0vox' ] #-------------------------------------------------------------------------------------- # I am having problems callling the interface. Same problem of bad interface. if False: reg = Registration() reg.inputs.fixed_image = 'nu.nii.gz' reg.inputs.moving_image = input_file + '.nii.gz' reg.inputs.output_transform_prefix = "t2flair_Affine_nu__" + input_file + "_" reg.inputs.transforms = ['Affine'] reg.inputs.transform_parameters = [(2.0,), (0.25, 3.0, 0.0)] reg.inputs.number_of_iterations = [[1000,500,200,100]] reg.inputs.dimension = 3 reg.inputs.write_composite_transform = False reg.inputs.collapse_output_transforms = False reg.inputs.initialize_transforms_per_stage = False reg.inputs.metric = ['MI'] reg.inputs.metric_weight = [1] reg.inputs.radius_or_number_of_bins = [32] reg.inputs.sampling_strategy = ['Random'] reg.inputs.sampling_percentage = [0.05] reg.inputs.convergence_threshold = [1.e-10] reg.inputs.convergence_window_size = [10] reg.inputs.smoothing_sigmas = [[3,2,1,0]] reg.inputs.sigma_units = ['vox'] reg.inputs.shrink_factors = [[8,4,2,1]] reg.inputs.use_estimate_learning_rate_once = [True] reg.inputs.use_histogram_matching = [True] # This is the default reg.inputs.output_warped_image = 't2flair_Affine_nu__' + input_file + '.nii.gz' reg.inputs.terminal_output='stream' print(reg.cmdline) # reg.run() # This simple command appears to work. command = ['antsRegistrationSyNQuick.sh', '-d', '3', '-m', input_file + '.nii.gz', '-r', 'nu.nii.gz', '-f', 'nu.nii.gz', '-t', 'a', '-o', 't2flair_Affine_nu__' + input_file + '_' ] util.iw_subprocess( command, True, True, False) return
def methods_03_stats(input_dir, lower_threshold=0.5, upper_threshold=numpy.inf, min_lesion_volume_voxels=10, verbose=False): cenc_dirs = cenc.directories(input_dir) wm_lesions_dir = cenc_dirs['wmlesions']['dirs']['root'] wm_lesions_lpa_dir = cenc_dirs['wmlesions']['dirs']['lpa'] util.mkcd_dir([cenc_dirs['wmlesions']['dirs']['stats']], True) wm_lesions_stats_filename = os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.csv') wm_lesions_pmap_stats_filename = os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_pmap.csv') # Mask LPA Probability Mask and remove NaN command = [['fslmaths', os.path.join(cenc_dirs['wmlesions']['dirs']['lpa'], 'ples_lpa_mt2flair_Affine_nu__t2flair_Warped.nii.gz'), '-mas', os.path.join(cenc_dirs['wmlesions']['dirs']['input'], 'mask.nii.gz'), '-nan', os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_pmap.nii.gz') ]] for ii in command: util.iw_subprocess(ii, verbose, verbose, False) # Create labels file from LPA probability map labels.label_connected_components( os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_pmap.nii.gz'), os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), lower_threshold, upper_threshold ) # Measure properties of labels labels.properties(os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), None, False, ['volume_mm3'], wm_lesions_stats_filename, limits_volume_voxels=[min_lesion_volume_voxels, numpy.inf], limits_bb_volume_voxels=[0, numpy.inf], limits_fill_factor=[0, 1], sort='volume_mm3', verbose=verbose, verbose_nlines=20) # Keep labels greater than 10 mm^3. Limit is set above labels.keep(os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), [], wm_lesions_stats_filename, os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz') ) # 1) Create a WM Lesions mask # 2) Mask LPA Registered T2 FLAIR command = [ ['fslmaths', os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), '-bin', os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_mask.nii.gz') ], ['fslmaths', os.path.join(cenc_dirs['wmlesions']['dirs']['lpa'], 'mt2flair_Affine_nu__t2flair_Warped.nii.gz'), '-mas', os.path.join(cenc_dirs['wmlesions']['dirs']['input'], 'mask.nii.gz'), os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_t2flair.nii.gz') ] ] for ii in command: util.iw_subprocess(ii, verbose, verbose, False) # Measure Pmaps stats labels df_pmap_measure = labels.measure( os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_pmap.nii.gz')) df_pmap_measure.to_csv(wm_lesions_pmap_stats_filename, index=False) # Write out JSON file with RedCap Instrument methods_write_json_redcap_instrument(input_dir, wm_lesions_stats_filename, wm_lesions_pmap_stats_filename, verbose)