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 prepare( input_dir ): cenc_dirs = cenc.directories( input_dir) wm_lesions_dir = cenc_dirs['wmlesions']['dirs']['root'] wm_lesions_input_dir = cenc_dirs['wmlesions']['dirs']['input'] wm_lesions_lpa_dir = cenc_dirs['wmlesions']['dirs']['lpa'] # Create Input Directory if it doesn't exist and link files files = [ os.path.abspath( os.path.join( cenc_dirs['results']['dirs']['images'], 'nu.nii.gz')), os.path.abspath( os.path.join( cenc_dirs['results']['dirs']['labels'], 'mask.nii.gz')), os.path.abspath( os.path.join( cenc_dirs['cenc']['reorient'], 't2flair.nii.gz')) ] util.mkcd_dir( os.path.join( wm_lesions_input_dir ) ) util.link_inputs( files, wm_lesions_input_dir )
def methods_01_register( 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'] # Register util.mkcd_dir( [ cenc_dirs['wmlesions']['dirs']['register'] ], True) files = [ os.path.join( cenc_dirs['wmlesions']['dirs']['input'], 'nu.nii.gz'), os.path.join( cenc_dirs['wmlesions']['dirs']['input'], 't2flair.nii.gz'), os.path.join( cenc_dirs['results']['dirs']['labels'], 'mask.nii.gz') ] util.link_inputs( files, cenc_dirs['wmlesions']['dirs']['register'] ) ants_register('t2flair')
def results( input_dir): cenc_dirs = cenc.directories( input_dir) util.mkcd_dir( [ cenc_dirs['wmlesions']['dirs']['results'] ], True) # Link Files link_result_files = [ [ os.path.join( cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_t2flair.nii.gz'), os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_t2flair.nii.gz') ], [ os.path.join( cenc_dirs['wmlesions']['dirs']['input'], 'nu.nii.gz'), os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'nu.nii.gz') ], [ os.path.join( cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_pmap.nii.gz'), os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_pmap.nii.gz')], [ os.path.join( cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions.json'), os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions.json')], [os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.nii.gz'), os.path.join(cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_labels.nii.gz')], [os.path.join(cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_labels.csv'), os.path.join(cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_labels.csv')], [ os.path.join( cenc_dirs['wmlesions']['dirs']['stats'], 'wmlesions_lpa_mask.nii.gz'), os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_mask.nii.gz')], [ os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_pmap.nii.gz'), os.path.join( cenc_dirs['results']['dirs']['images'], 'wmlesions_lpa_pmap.nii.gz')], [ os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_mask.nii.gz'), os.path.join( cenc_dirs['results']['dirs']['labels'], 'wmlesions_lpa_mask.nii.gz')], [ os.path.join( cenc_dirs['wmlesions']['dirs']['results'], 'wmlesions_lpa_labels.nii.gz'), os.path.join( cenc_dirs['results']['dirs']['labels'], 'wmlesions_lpa_labels.nii.gz') ] ] for ii in link_result_files: util.force_hard_link( ii[0], ii[1])
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)