def normal_lesion_mask_extractor(im_input_path, im_output_path, im_mask_foreground_path, safety_on=False): low_thr, up_thr = get_normal_interval_range(im_input_path) cmd = '''seg_maths {0} -thr {3} {1}; seg_maths {1} -uthr {4} {1}; seg_maths {1} -bin {1}; seg_maths {1} -add {2} {1}; seg_maths {1} -replace 2 0 {1}; seg_maths {1} -dil 0.5 {1}; seg_maths {1} -ero 0.5 {1}; seg_maths {1} -mul {2} {1}; '''.format(im_input_path, im_output_path, im_mask_foreground_path, low_thr, up_thr) print cmd if not safety_on: print_and_run(cmd)
def simple_lesion_mask_extractor_path(im_input_path, im_output_path, im_mask_foreground_path, safety_on=False): cmd = '''seg_maths {0} -thr 500 {1}; seg_maths {1} -bin {1}; seg_maths {1} -add {2} {1}; seg_maths {1} -replace 2 0 {1}; seg_maths {1} -fill {1}; seg_maths {1} -dil 2 {1}; seg_maths {1} -ero 2 {1}; seg_maths {1} -smol 1.2 {1}; seg_maths {1} -dil 1 {1}; seg_maths {1} -mul {2} {1}; '''.format(im_input_path, im_output_path, im_mask_foreground_path) print cmd if not safety_on: print_and_run(cmd)
def percentile_lesion_mask_extractor(im_input_path, im_output_path, im_mask_foreground_path, percentiles, safety_on=False, median_filter=False, pfo_tmp=None, verbose=True): if median_filter: im_input_name = os.path.basename(im_input_path).split('.')[0] im_input_path_filtered = os.path.join( pfo_tmp, '{}_filtered.nii.gz'.format(im_input_name)) im = nib.load(im_input_path) im_fil = set_new_data(im, medfilt(im.get_data())) nib.save(im_fil, im_input_path_filtered) im_input_path = im_input_path_filtered low_thr, up_thr = get_percentiles_range(im_input_path, percentiles=percentiles, pfi_im_mask=None) cmd = '''seg_maths {0} -thr {3} {1}; seg_maths {1} -uthr {4} {1}; seg_maths {1} -bin {1}; seg_maths {1} -add {2} {1}; seg_maths {1} -replace 2 0 {1}; seg_maths {1} -dil 2 {1}; seg_maths {1} -ero 1 {1}; seg_maths {1} -mul {2} {1}; '''.format(im_input_path, im_output_path, im_mask_foreground_path, low_thr, up_thr) if verbose: print('\nLower {}-percentile: {} '.format(percentiles[0], low_thr)) print('Upper {}-percentile: {} \n'.format(percentiles[1], up_thr)) print(cmd) if not safety_on: print_and_run(cmd)
def send_data_to_hannes_from_list(subj_list, records_only=False, erase_source=False, erase_destination=False, send_excel_table=False): root_data = jph(root_study_rabbits, 'A_data') if os.path.exists(root_shared_records): # copy excel file if send_excel_table: if os.path.exists(pfi_excel_table_all_data): cmd1 = 'cp {} {} '.format( pfi_excel_table_all_data, jph(root_shared_records, 'REoP_Data.xlsx')) print_and_run(cmd1) print(cmd1) for sj in subj_list: sj_parameters = pickle.load( open(jph(pfo_subjects_parameters, sj), 'r')) study = sj_parameters['study'] category = sj_parameters['category'] pfo_source = jph(root_data, study, category) assert os.path.exists(pfo_source) pfo_destination = jph(root_shared_records, study, category) send_or_erase(sj, pfo_source, pfo_destination, records_only=records_only, erase_source=erase_source, erase_destination=erase_destination)
def test_lesion_masks_extractor_for_simple_input(): # Test for filter_connected_components. cmd = 'mkdir -p {0}'.format(os.path.join(root_dir, 'output')) print_and_run(cmd) dims = [105, 20, 20] im_data = np.zeros([dims[1], dims[2], 1]) intervals = [50, 10, 50, 20, 25] weights = [12, 2, 3, 4, 5] for i in range(len(intervals)): slice = weights[i] * np.ones([dims[1], dims[2], intervals[i]]) im_data = np.concatenate((im_data, slice), axis=2) im = nib.Nifti1Image(im_data, np.eye(4)) nib.save(im, os.path.join(root_dir, 'output/test.nii.gz')) im_filtered = filter_connected_components_by_volume(im, num_cc_to_filter=3) # for the moment visual assessment test! nib.save(im_filtered, os.path.join(root_dir, 'output/test_fil.nii.gz')) print_and_run('open {}'.format( os.path.join(root_dir, 'output/test_fil.nii.gz')))
def percentile_lesion_mask_extractor_only_from_image_path( pfi_input_anatomical_image, pfi_output_segmentation, percentile_range=(5, 95)): """ :param pfi_input_anatomical_image: :param pfi_output_segmentation: :param percentile_range: :return: """ im = nib.load(pfi_input_anatomical_image) new_data_2 = percentile_lesion_mask_extractor_only_from_image( im.get_data(), percentile_range=percentile_range) new_im_2 = set_new_data(im, new_data=new_data_2, new_dtype=np.int16) nib.save(new_im_2, filename=pfi_output_segmentation) cmd = '''seg_maths {0} -fill {0}; seg_maths {0} -ero 1 {0}; seg_maths {0} -dil 1 {0}; seg_maths {0} -smol 2 {0}; '''.format(pfi_output_segmentation) print_and_run(cmd)
def lesion_masks_extractor_cc_based_path(im_input_path, im_output_path, im_mask_foreground_path, safety_on=False): """ Lesion masks are extracted before filtering for connected components. :return: """ save_intermediate = True cmd1 = '''seg_maths {0} -thr 500 {1}; seg_maths {1} -bin {1}; seg_maths {1} -add {2} {1}; seg_maths {1} -replace 2 0 {1}; seg_maths {1} -dil 2 {1}; seg_maths {1} -ero 2 {1}; seg_maths {1} -concomp6 {1}; '''.format(im_input_path, im_output_path, im_mask_foreground_path) print cmd1 if not safety_on: print_and_run(cmd1) print "filtering connected components" filter_connected_components_by_volume_path(im_input_path, im_output_path, num_cc_to_filter=10) if save_intermediate: im_output_path_intermediate = os.path.join( os.path.dirname(im_output_path), 'z_intermediate.nii.gz') cmd_mid = 'cp {0} {1}'.format(im_output_path, im_output_path_intermediate) print_and_run(cmd_mid) cmd2 = '''seg_maths {0} -smol 1.2 {0}; seg_maths {0} -dil 1 {0}; seg_maths {0} -mul {1} {0}; '''.format(im_output_path, im_mask_foreground_path) print cmd2 if not safety_on: print_and_run(cmd2) pass
def get_brain_mask_per_subject(sj, sj_parameters): """ From subject id, retrieves the subject parameters and creates its brain mask based upon them. :param sj: subject id. :param sj_parameters: subject parameters filtered from caller function. :return: brain of subject sj. """ print('\nGet brain mask per subject {} started.\n'.format(sj)) study = sj_parameters['study'] category = sj_parameters['category'] options_brain_mask = sj_parameters['options_brain_mask'] # Target folder in stereotaxic coordinates: pfo_sj = jph(defs.root_study_rabbits, 'A_data', study, category, sj) pfo_sc_sj = jph(pfo_sj, 'stereotaxic') pfo_tmp = jph(pfo_sc_sj, 'z_tmp_generate_brain_mask') cmd = 'mkdir -p {}'.format(pfo_tmp) print_and_run(cmd) # FINAL Output pfi_brain_mask_sj = jph(pfo_sc_sj, 'masks', '{}_brain_mask.nii.gz'.format(sj)) # IF subject is in multi-atlas just copy the brain_mask to the new destination. if sj in defs.multi_atlas_subjects: # Input pfi_brain_mask_sj_atlas = jph(defs.root_atlas, sj, 'masks', '{}_brain_mask.nii.gz'.format(sj)) assert os.path.exists(pfi_brain_mask_sj_atlas), pfi_brain_mask_sj_atlas # Command cmd = 'cp {} {}'.format(pfi_brain_mask_sj_atlas, pfi_brain_mask_sj) print_and_run(cmd) elif sj in defs.multi_atlas_BT_subjects: # Input pfi_brain_mask_sj_atlas = jph(defs.root_atlas_BT, '{}_brain_mask.nii.gz'.format(sj)) assert os.path.exists(pfi_brain_mask_sj_atlas), pfi_brain_mask_sj_atlas # Command cmd = 'cp {} {}'.format(pfi_brain_mask_sj_atlas, pfi_brain_mask_sj) print_and_run(cmd) # ELSE: get the list of subjects of the atlas else: if options_brain_mask['method'] == 'MA': mutli_atlas_subject_list = defs.multi_atlas_subjects elif options_brain_mask['method'] == 'BTMA': mutli_atlas_subject_list = defs.multi_atlas_BT_subjects elif options_brain_mask['method'] == 'BTMA_MA' or options_brain_mask[ 'method'] == 'MA_BTMA': mutli_atlas_subject_list = defs.multi_atlas_BT_subjects + defs.multi_atlas_subjects else: raise IOError('option for brain mask not existent.') print('\nMulti atlas for brain mask selected is {} .\n'.format( mutli_atlas_subject_list)) # THEN: get the output brain mask from extenal function btm.extract_brain_tissue_from_multi_atlas_list_stereotaxic( sj, mutli_atlas_subject_list, pfo_tmp, pfi_output_brain_mask=pfi_brain_mask_sj, options=options_brain_mask) # Print end-of-procedure message: print('\nBrain mask per subject {} saved in {}.\n'.format( sj, pfi_brain_mask_sj))
def propagate_segmentation_in_original_space_per_subject(sj, controller): """ Movign from the stereotaxic oriented chart to the original chart. The stereotaxic can be directly retrieved from the multi-atlas if sj is in the template or it can be a chart in the multi-atlas, or it can be retrived from the 'stereotaxic' folder in the subject folder (under A_data), after the :param sj: :param controller: :return: """ print('\nfrom Stereotaxic orientation to original space - SUBJECT {} started.\n'.format(sj)) sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) study = sj_parameters['study'] category = sj_parameters['category'] folder_selected_segmentation = sj_parameters['names_architecture']['final_segm_strx'] # default 'automatic' suffix_selected_segmentation = sj_parameters['names_architecture']['suffix_segm'] # default 'MV_P2' pfo_root_sj_orig = jph(root_study_rabbits, 'A_data', study, category, sj) pfo_root_sj_strx = jph(root_study_rabbits, 'A_data', study, category, sj, 'stereotaxic') pfo_mod_orig = jph(pfo_root_sj_orig, 'mod') pfo_mask_orig = jph(pfo_root_sj_orig, 'masks') pfo_segm_orig = jph(pfo_root_sj_orig, 'segm') pfo_mod_strx = jph(pfo_root_sj_strx, 'mod') pfo_mask_strx = jph(pfo_root_sj_strx, 'masks') pfo_segm_strx = jph(pfo_root_sj_strx, 'segm') pfo_tmp = jph(pfo_root_sj_orig, 'z_tmp', 'z_propagator') print_and_run('mkdir -p {}'.format(pfo_segm_orig)) print_and_run('mkdir -p {}'.format(pfo_tmp)) # recover the source in stereotaxic coordinates (strx): if sj_parameters['in_atlas']: if study == 'W8': pfo_sj_atlas = jph(root_study_rabbits, 'A_MultiAtlas_W8', sj) elif study == 'ACS' or study == 'PTB' or study == 'TestStudy': pfo_sj_atlas = jph(root_study_rabbits, 'A_MultiAtlas', sj) else: raise IOError('Study for subject {} not feasible.'.format(sj)) pfi_T1_strx = jph(pfo_sj_atlas, 'mod', '{}_T1.nii.gz'.format(sj)) pfi_T1_reg_mask_strx = jph(pfo_sj_atlas, 'masks', '{}_reg_mask.nii.gz'.format(sj)) pfi_T1_segm_strx = jph(pfo_sj_atlas, 'segm', '{}_segm.nii.gz'.format(sj)) else: pfi_T1_strx = jph(pfo_mod_strx, '{}_T1.nii.gz'.format(sj)) pfi_T1_reg_mask_strx = jph(pfo_mask_strx, '{}_T1_reg_mask.nii.gz'.format(sj)) if folder_selected_segmentation == 'automatic': pfo_segmentation_strx = jph(pfo_segm_strx, 'automatic') else: pfo_segmentation_strx = pfo_segm_strx pfi_T1_segm_strx = jph(pfo_segmentation_strx, '{}_{}.nii.gz'.format(sj, suffix_selected_segmentation)) for p in [pfi_T1_strx, pfi_T1_reg_mask_strx, pfi_T1_segm_strx]: assert os.path.exists(p), p # --> INTRA-modal segmentation: if controller['Header_alignment_T1strx_to_T1orig']: print('-> Align header T1strx to origin') # output header orientati: pfi_T1_strx_hdo = jph(pfo_tmp, '{}_T1_strx_hdo.nii.gz'.format(sj)) pfi_T1_reg_mask_strx_hdo = jph(pfo_tmp, '{}_T1_reg_mask_strx_hdo.nii.gz'.format(sj)) pfi_T1_segm_strx_hdo = jph(pfo_tmp, '{}_T1_segm_strx_hdo.nii.gz'.format(sj)) angles = sj_parameters['angles'] if isinstance(angles[0], list): pitch_theta = angles[0][1] else: pitch_theta = angles[1] for strx, strx_hdo in zip([pfi_T1_strx, pfi_T1_reg_mask_strx, pfi_T1_segm_strx], [pfi_T1_strx_hdo, pfi_T1_reg_mask_strx_hdo, pfi_T1_segm_strx_hdo]): cmd = 'cp {0} {1}'.format(strx, strx_hdo) print_and_run(cmd) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(strx_hdo, strx_hdo, angle=pitch_theta, principal_axis='pitch') if controller['Rigid_T1strx_to_T1orig']: print('-> Align T1strx_hdo (header oriented) to origin') # fixed pfi_T1_origin = jph(pfo_mod_orig, '{}_T1.nii.gz'.format(sj)) pfi_T1_reg_mask_origin = jph(pfo_mask_orig, '{}_T1_reg_mask.nii.gz'.format(sj)) # moving pfi_T1_strx_hdo = jph(pfo_tmp, '{}_T1_strx_hdo.nii.gz'.format(sj)) pfi_T1_reg_mask_strx_hdo = jph(pfo_tmp, '{}_T1_reg_mask_strx_hdo.nii.gz'.format(sj)) assert os.path.exists(pfi_T1_origin), pfi_T1_origin assert os.path.exists(pfi_T1_reg_mask_origin), pfi_T1_reg_mask_origin assert os.path.exists(pfi_T1_strx_hdo), pfi_T1_strx_hdo assert os.path.exists(pfi_T1_reg_mask_strx_hdo), pfi_T1_reg_mask_strx_hdo pfi_transformation = jph(pfo_tmp, 'T1_strx_to_origin.txt') pfi_warped_T1_rigid = jph(pfo_tmp, 'T1_strx_to_origin_warped.nii.gz') cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -rigOnly '.format( # pfi_T1_origin, pfi_T1_reg_mask_origin, pfi_T1_strx_hdo, pfi_T1_reg_mask_strx_hdo, pfi_transformation, pfi_warped_T1_rigid) print_and_run(cmd) if controller['Propagate_T1_segm']: pfi_T1_origin = jph(pfo_mod_orig, '{}_T1.nii.gz'.format(sj)) pfi_T1_segm_strx_hdo = jph(pfo_tmp, '{}_T1_segm_strx_hdo.nii.gz'.format(sj)) pfi_transformation = jph(pfo_tmp, 'T1_strx_to_origin.txt') assert os.path.exists(pfi_T1_origin), pfi_T1_origin assert os.path.exists(pfi_T1_segm_strx_hdo), pfi_T1_segm_strx_hdo assert os.path.exists(pfi_transformation), pfi_transformation pfi_warped_T1_segm = jph(pfo_segm_orig, '{}_T1_segm.nii.gz'.format(sj)) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -omp {4} -inter 0'.format( pfi_T1_origin, pfi_T1_segm_strx_hdo, pfi_transformation, pfi_warped_T1_segm, num_cores_run) print_and_run(cmd) # --> INTER-modal segmentation propagation: -- from here we are only working with the original chart. if controller['Inter_modal_reg_S0']: # T1 to S0 - Reference is S0. Floating is T1 # references: pfi_S0_origin = jph(pfo_mod_orig, '{}_S0.nii.gz'.format(sj)) pfi_S0_reg_mask_origin = jph(pfo_mask_orig, '{}_S0_reg_mask.nii.gz'.format(sj)) angles = sj_parameters['angles'] if isinstance(angles[0], list): # Both S0 and T1 angles are specified: [[y,p,r], [y,p,r]] pitch_theta_T1 = angles[0][1] pitch_theta_S0 = angles[1][1] pitch_theta = pitch_theta_S0 - pitch_theta_T1 # floating oriented header: pfi_T1_strx_hdoS0 = jph(pfo_tmp, '{}_T1_strx_hdoS0.nii.gz'.format(sj)) pfi_T1_reg_mask_strx_hdoS0 = jph(pfo_tmp, '{}_T1_reg_mask_strx_hdoS0.nii.gz'.format(sj)) pfi_T1_segm_strx_hdoS0 = jph(pfo_tmp, '{}_T1_segm_strx_hdoS0.nii.gz'.format(sj)) for strx, strx_hdo in zip([pfi_T1_strx, pfi_T1_reg_mask_strx, pfi_T1_segm_strx], [pfi_T1_strx_hdoS0, pfi_T1_reg_mask_strx_hdoS0, pfi_T1_segm_strx_hdoS0]): cmd = 'cp {0} {1}'.format(strx, strx_hdo) print_and_run(cmd) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(strx_hdo, strx_hdo, angle=pitch_theta, principal_axis='pitch') pfi_transformation = jph(pfo_tmp, 'T1origin_to_S0origin.txt') pfi_warped_T1toS0_rigid = jph(pfo_tmp, 'T1origin_to_S0origin_warped.nii.gz') cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} -'.format( # rigOnly pfi_S0_origin, pfi_S0_reg_mask_origin, pfi_T1_strx_hdoS0, pfi_T1_reg_mask_strx_hdoS0, pfi_transformation, pfi_warped_T1toS0_rigid, num_cores_run ) print_and_run(cmd) # result: pfi_S0_segm = jph(pfo_segm_orig, '{}_S0_segm.nii.gz'.format(sj)) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_S0_origin, pfi_T1_segm_strx_hdoS0, pfi_transformation, pfi_S0_segm) print_and_run(cmd) else: # floating not oriented header: pfi_transformation = jph(pfo_tmp, 'T1origin_to_S0origin.txt') pfi_warped_T1toS0_rigid = jph(pfo_tmp, 'T1origin_to_S0origin_warped.nii.gz') cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} '.format( # -rigOnly pfi_S0_origin, pfi_S0_reg_mask_origin, pfi_T1_strx, pfi_T1_reg_mask_strx, pfi_transformation, pfi_warped_T1toS0_rigid, num_cores_run ) print_and_run(cmd) # result: pfi_S0_segm = jph(pfo_segm_orig, '{}_S0_segm.nii.gz'.format(sj)) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_S0_origin, pfi_T1_segm_strx, pfi_transformation, pfi_S0_segm) print_and_run(cmd) if controller['Inter_modal_reg_MSME']: # MSMEinS0 to MSME: reference is MSME, floating is MSMEinS0 same as S0. # (same strategy used as before - again hardcoded) # references: pfi_MSME_origin = jph(pfo_mod_orig, 'MSME_tp0', '{}_MSME_tp0.nii.gz'.format(sj)) pfi_MSME_reg_mask_origin = jph(pfo_mask_orig, '{}_MSME_reg_mask.nii.gz'.format(sj)) # for safety orient the input to standard: orient2std(pfi_MSME_origin, pfi_MSME_origin) orient2std(pfi_MSME_reg_mask_origin, pfi_MSME_reg_mask_origin) assert os.path.exists(pfi_MSME_origin) assert os.path.exists(pfi_MSME_reg_mask_origin) # floating pfi_MSMEinS0 = jph(pfo_mod_orig, 'MSME_tp0', '{}_MSMEinS0_tp0.nii.gz'.format(sj)) pfi_MSMEinS0_reg_mask = jph(pfo_mask_orig, '{}_S0_reg_mask.nii.gz'.format(sj)) pfi_MSMEinS0_segm = jph(pfo_segm_orig, '{}_S0_segm.nii.gz'.format(sj)) angles = sj_parameters['angles'] if isinstance(angles[0], list) and len(angles) == 3: # all angles specified: [[y,p,r], [y,p,r], [y,p,r]] pitch_theta_S0 = angles[1][1] pitch_theta_MSME = angles[2][1] pitch_theta = pitch_theta_MSME - pitch_theta_S0 # floating oriented header: pfi_MSMEinS0_hdoMSME = jph(pfo_tmp, '{}_MSMEinS0_strx_hdoS0.nii.gz'.format(sj)) pfi_MSMEinS0_reg_mask_hdoMSME = jph(pfo_tmp, '{}_MSMEinS0_reg_mask_strx_hdoS0.nii.gz'.format(sj)) pfi_MSMEinS0_segm_hdoMSME = jph(pfo_tmp, '{}_MSMEinS0_segm_strx_hdoS0.nii.gz'.format(sj)) for strx, strx_hdo in zip([pfi_MSMEinS0, pfi_MSMEinS0_reg_mask, pfi_MSMEinS0_segm], [pfi_MSMEinS0_hdoMSME, pfi_MSMEinS0_reg_mask_hdoMSME, pfi_MSMEinS0_segm_hdoMSME]): cmd = 'cp {0} {1}'.format(strx, strx_hdo) print_and_run(cmd) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(strx_hdo, strx_hdo, angle=pitch_theta, principal_axis='pitch') pfi_transformation = jph(pfo_tmp, 'MSMEinS0_to_MSME.txt') pfi_warped_MSMEinS0_to_MSME_rigid = jph(pfo_tmp, 'MSMEinS0_to_MSME_warped.nii.gz') cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} '.format( # -rigOnly pfi_MSME_origin, pfi_MSME_reg_mask_origin, pfi_MSMEinS0_hdoMSME, pfi_MSMEinS0_reg_mask_hdoMSME, pfi_transformation, pfi_warped_MSMEinS0_to_MSME_rigid, num_cores_run ) print_and_run(cmd) # result: pfi_MSME_segm = jph(pfo_segm_orig, '{}_MSME_segm.nii.gz'.format(sj)) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_MSME_origin, pfi_MSMEinS0_segm_hdoMSME, pfi_transformation, pfi_MSME_segm) print_and_run(cmd) else: pfi_transformation = jph(pfo_tmp, 'MSMEinS0_to_MSME.txt') pfi_warped_MSMEinS0_to_MSME_rigid = jph(pfo_tmp, 'MSMEinS0_to_MSME_warped.nii.gz') cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} '.format( # -rigOnly pfi_MSME_origin, pfi_MSME_reg_mask_origin, pfi_MSMEinS0, pfi_MSMEinS0_reg_mask, pfi_transformation, pfi_warped_MSMEinS0_to_MSME_rigid, num_cores_run ) print_and_run(cmd) # result: pfi_MSME_segm = jph(pfo_segm_orig, '{}_MSME_segm.nii.gz'.format(sj)) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_MSME_origin, pfi_MSMEinS0_segm, pfi_transformation, pfi_MSME_segm) print_and_run(cmd)
def send_or_erase(sj, pfo_source, pfo_destination, records_only=False, erase_source=False, erase_destination=False): # copy records template if any folder_destination_reports = jph(pfo_destination, sj) cmd0 = 'mkdir -p {}'.format(folder_destination_reports) print_and_run(cmd0) if sj is not None: if erase_source: cmd = 'rm -r {}'.format(jph(pfo_source, sj)) print_and_run(cmd) print(cmd) elif erase_destination: cmd = 'rm -r {}'.format(jph(pfo_destination, sj)) print_and_run(cmd) print(cmd) else: # copy records folder_source_reports = jph(pfo_source, sj, 'records') folder_destination_reports = jph(pfo_destination, sj) cmd0 = 'mkdir -p {}'.format(folder_destination_reports) print_and_run(cmd0) if os.path.exists(folder_source_reports): cmd1 = 'cp -r {} {} '.format(folder_source_reports, folder_destination_reports) print_and_run(cmd1) print(cmd1) else: print('REPORTS for subject {} not present'.format(sj)) return folder_source_reports = jph(pfo_source, sj, 'records_template') if os.path.exists(folder_source_reports): cmd1 = 'cp -r {} {} '.format(folder_source_reports, folder_destination_reports) print_and_run(cmd1) print(cmd1) if not records_only: # copy mod folder_source_mod = jph(pfo_source, sj, 'mod') folder_destination_mod = jph(pfo_destination, sj) cmd0 = 'mkdir -p {}'.format(folder_destination_mod) print_and_run(cmd0) if os.path.exists(folder_source_mod): cmd1 = 'cp -r {} {} '.format(folder_source_mod, folder_destination_mod) print_and_run(cmd1) print(cmd1) else: print('MOD for subject {} not present'.format(sj)) # copy segm folder_source_segm = jph(pfo_source, sj, 'segm') folder_destination_segm = jph(pfo_destination, sj) cmd0 = 'mkdir -p {}'.format(folder_destination_segm) print_and_run(cmd0) if os.path.exists(folder_source_segm): cmd1 = 'cp -r {} {} '.format(folder_source_segm, folder_destination_segm) print_and_run(cmd1) print(cmd1) else: print('REPORTS for subject {} not present'.format(sj))
def move_to_stereotaxic_coordinate_per_subject(sj, controller): print('\nProcessing T1 {} started.\n'.format(sj)) # parameters file sanity check: if sj not in list_all_subjects(pfo_subjects_parameters): raise IOError('Subject parameters not known. Subject {}'.format(sj)) sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) study = sj_parameters['study'] category = sj_parameters['category'] pfo_sj = jph(root_study_rabbits, 'A_data', study, category, sj) pfo_sj_mod = jph(pfo_sj, 'mod') pfo_sj_masks = jph(pfo_sj, 'masks') if study == 'W8': pfo_atlas = root_atlas_W8 options = {'Template_chart_path' : jph(root_atlas_W8, '12503'), # TODO 'Template_name' : '12503'} elif study == 'ACS' or study == 'PTB' or study == 'TestStudy': pfo_atlas = root_atlas options = {'Template_chart_path' : jph(root_atlas, '1305'), 'Template_name' : '1305'} else: raise IOError('Study for subject {} not feasible.'.format(sj)) assert os.path.exists(pfo_sj_mod) assert os.path.exists(pfo_sj_masks) # reference atlas main modality and mask pfi_mod_reference_atlas = jph(options['Template_chart_path'], 'mod', '{0}_{1}.nii.gz'.format( options['Template_name'], 'T1')) pfi_reg_mask_reference_atlas = jph(options['Template_chart_path'], 'masks', '{0}_reg_mask.nii.gz'.format( options['Template_name'])) assert os.path.exists(pfi_mod_reference_atlas), pfi_mod_reference_atlas assert os.path.exists(pfi_reg_mask_reference_atlas), pfi_reg_mask_reference_atlas pfo_tmp = jph(pfo_sj, 'z_tmp', 'z_sc_aligment'.format(sj)) pfo_sc_sj = jph(pfo_sj, 'stereotaxic') pfo_sc_sj_mod = jph(pfo_sc_sj, 'mod') pfo_sc_sj_masks = jph(pfo_sc_sj, 'masks') subject_is_in_atlas = False # sj_parameters['in_atlas'] # TODO momentary bypass - correct this part! if subject_is_in_atlas: pfo_sj_mod_in_atlas = jph(pfo_atlas, sj, 'mod') pfo_sj_masks_in_atlas = jph(pfo_atlas, sj, 'masks') pfo_sj_segm_in_atlas = jph(pfo_atlas, sj, 'segm') assert os.path.exists(pfo_sj_mod_in_atlas), pfo_sj_mod_in_atlas assert os.path.exists(pfo_sj_masks_in_atlas), pfo_sj_masks_in_atlas assert os.path.exists(pfo_sj_segm_in_atlas), pfo_sj_segm_in_atlas print_and_run('mkdir -p {}'.format(pfo_sc_sj)) cmd = 'cp -r {} {}'.format(pfo_sj_mod_in_atlas, pfo_sc_sj) print_and_run(cmd) cmd = 'cp -r {} {}'.format(pfo_sj_masks_in_atlas, pfo_sc_sj) print_and_run(cmd) cmd = 'cp -r {} {}'.format(pfo_sj_segm_in_atlas, pfo_sc_sj) print_and_run(cmd) return # Initialise folder structure in stereotaxic coordinates if controller['Initialise_sc_folder']: print_and_run('mkdir -p {}'.format(pfo_tmp)) print_and_run('mkdir -p {}'.format(pfo_sc_sj)) print_and_run('mkdir -p {}'.format(pfo_sc_sj_mod)) print_and_run('mkdir -p {}'.format(pfo_sc_sj_masks)) if controller['Register_T1']: print('Orient header histological T1 and reg-mask:') angles = sj_parameters['angles'] if isinstance(angles[0], list): pitch_theta = -1 * angles[0][1] else: pitch_theta = -1 * angles[1] pfi_original_T1 = jph(pfo_sj_mod, '{0}_{1}.nii.gz'.format(sj, 'T1')) assert os.path.exists(pfi_original_T1), pfi_original_T1 pfi_T1_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}.nii.gz'.format(sj, 'T1')) print_and_run('cp {} {}'.format(pfi_original_T1, pfi_T1_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_T1_reoriented, pfi_T1_reoriented, angle=pitch_theta, principal_axis='pitch') pfi_original_roi_mask_T1 = jph(pfo_sj_masks, '{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'roi_mask')) assert os.path.exists(pfi_original_roi_mask_T1), pfi_original_roi_mask_T1 pfi_roi_mask_T1_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'roi_mask')) print_and_run('cp {} {}'.format(pfi_original_roi_mask_T1, pfi_roi_mask_T1_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_roi_mask_T1_reoriented, pfi_roi_mask_T1_reoriented, angle=pitch_theta, principal_axis='pitch') pfi_original_reg_mask_T1 = jph(pfo_sj_masks, '{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'reg_mask')) assert os.path.exists(pfi_original_reg_mask_T1), pfi_original_reg_mask_T1 pfi_reg_mask_T1_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'reg_mask')) print_and_run('cp {} {}'.format(pfi_original_reg_mask_T1, pfi_reg_mask_T1_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_reg_mask_T1_reoriented, pfi_reg_mask_T1_reoriented, angle=pitch_theta, principal_axis='pitch') del nis_app print('Rigid registration T1:') pfi_transformation_T1_over_T1 = jph(pfo_tmp, 'trans_{0}_over_{1}_mod_{2}_rigid.txt'.format( sj, options['Template_name'], 'T1')) pfi_resampled_T1 = jph(pfo_sc_sj_mod, '{0}_T1.nii.gz'.format(sj)) # RESULT # THIS MUST BE A RIGID REGISTRATION! cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} -rigOnly '.format( pfi_mod_reference_atlas, pfi_reg_mask_reference_atlas, pfi_T1_reoriented, pfi_reg_mask_T1_reoriented, pfi_transformation_T1_over_T1, pfi_resampled_T1, num_cores_run) print_and_run(cmd) del angles, pitch_theta, pfi_original_T1, pfi_T1_reoriented, pfi_original_reg_mask_T1, \ pfi_reg_mask_T1_reoriented, pfi_transformation_T1_over_T1, pfi_resampled_T1, cmd if controller['Propagate_T1_masks']: print('Propagate T1 mask:') pfi_T1_in_sc = jph(pfo_sc_sj_mod, '{0}_T1.nii.gz'.format(sj)) pfi_roi_mask_T1_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'roi_mask')) pfi_reg_mask_T1_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'T1', 'reg_mask')) pfi_transformation_T1_over_T1 = jph(pfo_tmp, 'trans_{0}_over_{1}_mod_{2}_rigid.txt'.format( sj, options['Template_name'], 'T1')) assert os.path.exists(pfi_T1_in_sc), pfi_T1_in_sc assert os.path.exists(pfi_roi_mask_T1_reoriented), pfi_roi_mask_T1_reoriented assert os.path.exists(pfi_reg_mask_T1_reoriented), pfi_reg_mask_T1_reoriented assert os.path.exists(pfi_transformation_T1_over_T1), pfi_transformation_T1_over_T1 pfi_final_roi_mask_T1 = jph(pfo_sc_sj_masks, '{}_roi_mask.nii.gz'.format(sj)) # RESULT cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_T1_in_sc, pfi_roi_mask_T1_reoriented, pfi_transformation_T1_over_T1, pfi_final_roi_mask_T1) print_and_run(cmd) pfi_final_reg_mask_T1 = jph(pfo_sc_sj_masks, '{0}_{1}_reg_mask.nii.gz'.format(sj, 'T1')) cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_T1_in_sc, pfi_reg_mask_T1_reoriented, pfi_transformation_T1_over_T1, pfi_final_reg_mask_T1) print_and_run(cmd) del pfi_T1_in_sc, pfi_reg_mask_T1_reoriented, pfi_transformation_T1_over_T1 if controller['Register_S0']: print('Orient header histological S0 and its reg-mask:') angles = sj_parameters['angles'] if isinstance(angles[0], list): pitch_theta = -1 * angles[1][1] else: pitch_theta = -1 * angles[1] pfi_original_S0 = jph(pfo_sj_mod, '{0}_{1}.nii.gz'.format(sj, 'S0')) assert os.path.exists(pfi_original_S0), pfi_original_S0 pfi_S0_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}.nii.gz'.format(sj, 'S0')) print_and_run('cp {} {}'.format(pfi_original_S0, pfi_S0_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_S0_reoriented, pfi_S0_reoriented, angle=pitch_theta, principal_axis='pitch') pfi_original_reg_mask_S0 = jph(pfo_sj_masks, '{0}_{1}_{2}.nii.gz'.format(sj, 'S0', 'reg_mask')) assert os.path.exists(pfi_original_reg_mask_S0), pfi_original_reg_mask_S0 pfi_reg_mask_S0_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'S0', 'reg_mask')) print_and_run('cp {} {}'.format(pfi_original_reg_mask_S0, pfi_reg_mask_S0_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_reg_mask_S0_reoriented, pfi_reg_mask_S0_reoriented, angle=pitch_theta, principal_axis='pitch') del nis_app print('Rigid registration S0:') pfi_transformation_S0_over_T1 = jph(pfo_tmp, 'trans_{0}_over_{1}_mod_{2}_rigid.txt'.format( sj, options['Template_name'], 'S0')) pfi_resampled_S0 = jph(pfo_sc_sj_mod, '{0}_S0.nii.gz'.format(sj)) # RESULT # Try the non rigid and full mask -rigOnly cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} '.format( #-rigOnly pfi_mod_reference_atlas, pfi_reg_mask_reference_atlas, pfi_S0_reoriented, pfi_reg_mask_S0_reoriented, pfi_transformation_S0_over_T1, pfi_resampled_S0, num_cores_run) print_and_run(cmd) del angles, pitch_theta, pfi_original_S0, pfi_S0_reoriented, pfi_original_reg_mask_S0, \ pfi_reg_mask_S0_reoriented, pfi_transformation_S0_over_T1, pfi_resampled_S0, cmd if controller['Propagate_S0_related_mods_and_mask']: angles = sj_parameters['angles'] if isinstance(angles[0], list): pitch_theta = -1 * angles[1][1] else: pitch_theta = -1 * angles[1] pfi_S0_in_sc = jph(pfo_sc_sj_mod, '{0}_S0.nii.gz'.format(sj)) pfi_transformation_S0_over_T1 = jph(pfo_tmp, 'trans_{0}_over_{1}_mod_{2}_rigid.txt'.format( sj, options['Template_name'], 'S0')) for mod in ['FA', 'MD', 'V1']: print('Orient header histological {}:'.format(mod)) pfi_original_MOD = jph(pfo_sj_mod, '{0}_{1}.nii.gz'.format(sj, mod)) assert os.path.exists(pfi_original_MOD), pfi_original_MOD pfi_MOD_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}.nii.gz'.format(sj, mod)) print_and_run('cp {} {}'.format(pfi_original_MOD, pfi_MOD_reoriented)) if pitch_theta != 0: nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_MOD_reoriented, pfi_MOD_reoriented, angle=pitch_theta, principal_axis='pitch') del nis_app print('Resampling {}:'.format(mod)) pfi_final_MOD = jph(pfo_sc_sj_mod, '{0}_{1}.nii.gz'.format(sj, mod)) # RESULT cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 1'.format( pfi_S0_in_sc, pfi_MOD_reoriented, pfi_transformation_S0_over_T1, pfi_final_MOD) print_and_run(cmd) pfi_reg_mask_S0_reoriented = jph(pfo_tmp, 'histo_header_{0}_{1}_{2}.nii.gz'.format(sj, 'S0', 'reg_mask')) assert os.path.exists(pfi_reg_mask_S0_reoriented), pfi_reg_mask_S0_reoriented pfi_final_reg_mask_S0_reoriented = jph(pfo_sc_sj_masks, '{0}_{1}_reg_mask.nii.gz'.format(sj, 'S0')) # RESULT cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_S0_in_sc, pfi_reg_mask_S0_reoriented, pfi_transformation_S0_over_T1, pfi_final_reg_mask_S0_reoriented) print_and_run(cmd) if controller['Adjustments']: # work only on the sc newly generated chart: pfi_sc_T1 = jph(pfo_sc_sj_mod, '{0}_T1.nii.gz'.format(sj)) pfi_sc_S0 = jph(pfo_sc_sj_mod, '{0}_S0.nii.gz'.format(sj)) pfi_sc_FA = jph(pfo_sc_sj_mod, '{0}_FA.nii.gz'.format(sj)) pfi_sc_MD = jph(pfo_sc_sj_mod, '{0}_MD.nii.gz'.format(sj)) pfi_sc_V1 = jph(pfo_sc_sj_mod, '{0}_V1.nii.gz'.format(sj)) assert os.path.exists(pfi_sc_T1), pfi_sc_T1 assert os.path.exists(pfi_sc_S0), pfi_sc_S0 assert os.path.exists(pfi_sc_FA), pfi_sc_FA assert os.path.exists(pfi_sc_MD), pfi_sc_MD assert os.path.exists(pfi_sc_V1), pfi_sc_V1 print_and_run('seg_maths {0} -thr 0 {0} '.format(pfi_sc_T1)) print_and_run('seg_maths {0} -thr 0 {0} '.format(pfi_sc_S0)) print_and_run('seg_maths {0} -thr 0 {0} '.format(pfi_sc_FA)) print_and_run('seg_maths {0} -thr 0 {0} '.format(pfi_sc_MD)) print_and_run('seg_maths {0} -abs {0} '.format(pfi_sc_V1)) pfi_sc_roi_mask = jph(pfo_sc_sj_masks, '{}_roi_mask.nii.gz'.format(sj)) assert os.path.exists(pfi_sc_roi_mask), pfi_sc_roi_mask # create mask for V1: pfi_V1_mask = jph(pfo_tmp, 'roi_mask_V1_{}.nii.gz'.format(sj)) stack_a_list_of_images_from_list_pfi( [pfi_sc_roi_mask for _ in range(3)], pfi_V1_mask) if sj_parameters['options_T1']['crop_roi']: print_and_run('seg_maths {0} -mul {1} {0} '.format(pfi_sc_T1, pfi_sc_roi_mask)) print_and_run('seg_maths {0} -mul {1} {0} '.format(pfi_sc_S0, pfi_sc_roi_mask)) print_and_run('seg_maths {0} -mul {1} {0} '.format(pfi_sc_FA, pfi_sc_roi_mask)) print_and_run('seg_maths {0} -mul {1} {0} '.format(pfi_sc_MD, pfi_sc_roi_mask)) print_and_run('seg_maths {0} -mul {1} {0} '.format(pfi_sc_V1, pfi_V1_mask))
def process_MSME_per_subject(sj, pfo_input_sj_MSME, pfo_output_sj, controller): print('\nProcessing MSME {} started.\n'.format(sj)) # input sanity check: all_sj = list_all_subjects(pfo_subjects_parameters) if sj not in all_sj: raise IOError('Subject parameters not known') if not os.path.exists(pfo_input_sj_MSME): raise IOError('Input folder T1 does not exist.') # -- Generate intermediate and output folder pfo_mod = jph(pfo_output_sj, 'mod') pfo_segm = jph(pfo_output_sj, 'segm') pfo_mask = jph(pfo_output_sj, 'masks') pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_MSME') print_and_run('mkdir -p {}'.format(pfo_output_sj)) print_and_run('mkdir -p {}'.format(pfo_mod)) print_and_run('mkdir -p {}'.format(pfo_segm)) print_and_run('mkdir -p {}'.format(pfo_mask)) print_and_run('mkdir -p {}'.format(pfo_tmp)) # -- if controller['transpose b-vals b-vects']: pfi_bvals = '' pfi_vects = '' assert os.path.exists(pfi_bvals) assert os.path.exists(pfi_vects) pfi_transpose_bvals = '' pfi_transpose_vects = '' m = np.loadtxt(pfi_bvals) np.savetxt(fname=pfi_transpose_bvals, X=m.T) m = np.loadtxt(pfi_vects) np.savetxt(fname=pfi_transpose_vects, X=m.T) if controller['noddi']: pfi_dwi = '' pfi_mask = '' pfi_bval_in_row = '' pfi_bvec_in_row = '' pfi_output_noddi = '' cmd = 'fit_dwi -source {0} -mask {1} -bval {2} -bvec {3} -mcmap {4} -nod'.format( pfi_dwi, pfi_mask, pfi_bval_in_row, pfi_bvec_in_row, pfi_output_noddi) print_and_run(cmd) if controller['save T2_times']: sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) if sj_parameters['category'] == 'ex_vivo': t2_times = (0, 0, 0) # myelin, GM, CSF default elif sj_parameters['category'] == 'in_vivo': t2_times = (0, 0, 0) else: t2_times = (0, 0, 0) pfi_T2_times = '' np.savetxt(fname=pfi_T2_times, X=np.array(t2_times)) if controller['fit msme']: pfi_msme_up = '' pfi_masks = '' pfi_echo_times = '' pfi_T2_times = '' pfi_output_mwf = '' cmd = 'fit_qt2 -source {0} -mask {1} -nc 3 -TElist {2} -T2list {3} -mwf {4}'.format( pfi_msme_up, pfi_masks, pfi_echo_times, pfi_T2_times, pfi_output_mwf) print_and_run(cmd) if controller['extract first tp noddi']: pfi_noddi = '' assert os.path.exists(pfi_noddi) pfi_vin = '' cmd = 'seg_maths {0} -tp 0 {1}'.format(pfi_noddi, pfi_vin) print_and_run(cmd) if controller['compute g-ratio']: pfi_mwf = '' pfi_vin = '' assert os.path.exists(pfi_mwf) assert os.path.exists(pfi_vin) pfi_tmp = '' pfi_g_ratio = '' cmd1 = 'fit_maths {0} -mul -1. {1}'.format(pfi_mwf, pfi_tmp) cmd2 = 'fit_maths {0} -add 1.0 {0}'.format(pfi_tmp) cmd3 = 'fit_maths {0} -mul {1} {0}'.format(pfi_tmp, pfi_vin) cmd4 = 'fit_maths {0} -div {1} {1}'.format(pfi_mwf, pfi_tmp) cmd5 = 'fit_maths {0} -add 1.0 {0}'.format(pfi_tmp) cmd6 = 'fit_maths {0} -recip {0}'.format(pfi_tmp) cmd7 = 'fit_maths {0} -sqrt {0}'.format(pfi_tmp) cmd8 = 'seg_maths {0} -uthr 0.999999999 {1}'.format( pfi_tmp, pfi_g_ratio) print_and_run(cmd1) print_and_run(cmd2) print_and_run(cmd3) print_and_run(cmd4) print_and_run(cmd5) print_and_run(cmd6) print_and_run(cmd7) print_and_run(cmd8)
def process_DWI_per_subject(sj, controller): print('\nProcessing DWI, subject {} started.\n'.format(sj)) if sj not in list_all_subjects(pfo_subjects_parameters): raise IOError('Subject parameters not known. Subject {}'.format(sj)) sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) DWI_suffix = sj_parameters['names_architecture']['DWI'] # default is DWI study = sj_parameters['study'] category = sj_parameters['category'] pfo_input_sj_DWI = jph(root_study_rabbits, '02_nifti', study, category, sj, '{}_{}'.format(sj, DWI_suffix)) pfo_output_sj = jph(root_study_rabbits, 'A_data', study, category, sj) if not os.path.exists(pfo_input_sj_DWI): print( 'DWI modality not given in the input folder after Nifti conversion. ' 'Bypass methods involving DWI for subject {}'.format(sj)) return if not os.path.exists(pfo_output_sj): raise IOError( 'Output folder DWI does not exist. Subject {}'.format(sj)) # -- Generate intermediate and output folders: pfo_mod = jph(pfo_output_sj, 'mod') pfo_segm = jph(pfo_output_sj, 'segm') pfo_mask = jph(pfo_output_sj, 'masks') pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_' + DWI_suffix) print_and_run('mkdir -p {}'.format(pfo_output_sj)) print_and_run('mkdir -p {}'.format(pfo_mod)) print_and_run('mkdir -p {}'.format(pfo_segm)) print_and_run('mkdir -p {}'.format(pfo_mask)) print_and_run('mkdir -p {}'.format(pfo_tmp)) if controller['squeeze']: print('- squeeze {}'.format(sj)) pfi_dwi = jph(pfo_input_sj_DWI, '{}_{}.nii.gz'.format(sj, DWI_suffix)) assert os.path.exists(pfi_dwi) squeeze_image_from_path(pfi_dwi, pfi_dwi) del pfi_dwi if controller['orient_to_standard']: print('- orient to standard {}'.format(sj)) # DWI pfi_dwi_original = jph(pfo_input_sj_DWI, '{}_{}.nii.gz'.format(sj, DWI_suffix)) assert check_path_validity(pfi_dwi_original) pfi_dwi_std = jph(pfo_tmp, '{}_{}_to_std.nii.gz'.format(sj, DWI_suffix)) orient2std(pfi_dwi_original, pfi_dwi_std) # S0 if sj_parameters['b0_level'] == 0: pfi_S0_original = jph(pfo_input_sj_DWI, '{}_{}_S0.nii.gz'.format(sj, DWI_suffix)) else: # create the time-point t and save its path under pfi_S0_original tp = sj_parameters['b0_level'] pfi_DWI_original = jph(pfo_input_sj_DWI, '{}_{}.nii.gz'.format(sj, DWI_suffix)) assert check_path_validity(pfi_DWI_original) pfi_S0_original = jph( pfo_tmp, '{0}_{1}_S0_tp{2}.nii.gz'.format(sj, DWI_suffix, tp)) grab_a_timepoint_path(pfi_DWI_original, pfi_S0_original, tp) assert check_path_validity(pfi_S0_original) pfi_S0_std = jph(pfo_tmp, '{}_{}_S0_to_std.nii.gz'.format(sj, DWI_suffix)) orient2std(pfi_S0_original, pfi_S0_std) if sj_parameters['DWI_squashed']: scale_y_value_and_trim(pfi_dwi_std, pfi_dwi_std, squeeze_factor=2.218074656188605) scale_y_value_and_trim(pfi_S0_std, pfi_S0_std, squeeze_factor=2.218074656188605) del pfi_dwi_original, pfi_dwi_std, pfi_S0_original, pfi_S0_std if controller['create_roi_masks']: # Ideal pipeline uses the T1_roi_mask, that has been created before. # Not ideally some data have different orientation for each modality. # not ideally T1 has not been computed yet - not working in this case. # Path T1 mask pfi_sj_T1_roi_mask = jph(pfo_mask, '{}_T1_roi_mask.nii.gz'.format(sj)) assert os.path.exists( pfi_sj_T1_roi_mask ), 'Mask {} missing. Run T1 pipeline before'.format(pfi_sj_T1_roi_mask) # Conditional flags: mask creation options # can_resample_T1 = False # Different modalities are in the same space. It is enough to resample T1. # need_to_register_mask = False # Different modalities in different spaces. Pure resampling is not working! # # if isinstance(sj_parameters['angles'][0], list): # need_to_register_mask = True # else: # can_resample_T1 = True # # process: # if can_resample_T1: # print('- Create roi masks {}'.format(sj)) # pfi_S0 = jph(pfo_tmp, '{}_{}_S0_to_std.nii.gz'.format(sj, DWI_suffix)) # pfi_affine_identity = jph(pfo_tmp, 'id.txt') # np.savetxt(pfi_affine_identity, np.eye(4), fmt='%d') # pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') # cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( # pfi_S0, # pfi_sj_T1_roi_mask, # pfi_affine_identity, # pfi_roi_mask) # print_and_run(cmd) # del pfi_S0, pfi_affine_identity, pfi_roi_mask, cmd # # elif need_to_register_mask: print('- Register roi masks {}'.format(sj)) pfi_S0 = jph(pfo_tmp, '{}_{}_S0_to_std.nii.gz'.format(sj, DWI_suffix)) assert os.path.exists(pfi_S0), pfi_S0 pfi_T1 = jph(pfo_mod, '{}_T1.nii.gz'.format(sj)) pfi_T1_roi_mask = jph(pfo_mask, '{}_T1_roi_mask.nii.gz'.format(sj)) assert os.path.exists(pfi_T1), pfi_T1 assert os.path.exists(pfi_T1_roi_mask), pfi_T1_roi_mask pfi_T1_hd_oriented = jph(pfo_tmp, '{}_T1_hd_oriented_to_S0.nii.gz'.format(sj)) pfi_T1_roi_mask_hd_oriented = jph( pfo_tmp, '{}_T1_roi_mask_hd_oriented_to_S0.nii.gz'.format(sj)) # check if the orientation angles are different for each modality: if isinstance(sj_parameters['angles'][0], list): # re-orient the T1 and the T1-mask on the S0 to better initialise the mask propagation. angles = sj_parameters['angles'][1] angle_parameter = angles[1] nis_app = nis.App() nis_app.header.apply_small_rotation(pfi_T1, pfi_T1_hd_oriented, angle=angle_parameter, principal_axis='pitch') nis_app.header.apply_small_rotation(pfi_T1_roi_mask, pfi_T1_roi_mask_hd_oriented, angle=angle_parameter, principal_axis='pitch') else: cmd1 = 'cp {0} {1}'.format(pfi_T1, pfi_T1_hd_oriented) cmd2 = 'cp {0} {1}'.format(pfi_T1_roi_mask, pfi_T1_roi_mask_hd_oriented) os.system(cmd1) os.system(cmd2) assert check_path_validity(pfi_T1_hd_oriented) assert check_path_validity(pfi_T1_roi_mask_hd_oriented) pfi_affine_transformation_ref_on_subject = jph( pfo_tmp, 'aff_ref_on_' + sj + '_S0.txt') pfi_3d_warped_ref_on_subject = jph(pfo_tmp, 'warp_ref_on_' + sj + '_S0.nii.gz') cmd0 = 'reg_aladin -ref {0} -flo {1} -fmask {2} -aff {3} -res {4} -omp {5} -rigOnly; '.format( pfi_S0, pfi_T1_hd_oriented, pfi_T1_roi_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_3d_warped_ref_on_subject, num_cores_run) print_and_run(cmd0) print('- propagate roi masks {}'.format(sj)) assert check_path_validity(pfi_affine_transformation_ref_on_subject) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') cmd1 = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_S0, pfi_T1_roi_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_roi_mask) print_and_run(cmd1) del pfi_S0, pfi_3d_warped_ref_on_subject, pfi_T1, pfi_T1_hd_oriented, pfi_T1_roi_mask, \ pfi_T1_roi_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_roi_mask, cmd0, cmd1 if controller['adjust_mask']: print('- adjust mask {}'.format(sj)) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') assert check_path_validity(pfi_roi_mask) pfi_roi_mask_dil = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') dil_factor = sj_parameters['options_S0']['mask_dilation'] cmd = 'seg_maths {0} -dil {1} {2}'.format(pfi_roi_mask, dil_factor, pfi_roi_mask_dil) print_and_run(cmd) del pfi_roi_mask, pfi_roi_mask_dil, dil_factor, cmd if controller['cut_mask_dwi']: print('- cut mask dwi {}'.format(sj)) pfi_dwi = jph(pfo_tmp, '{}_{}_to_std.nii.gz'.format(sj, DWI_suffix)) pfi_roi_mask = jph(pfo_mask, '{}_S0_roi_mask.nii.gz'.format(sj)) assert check_path_validity(pfi_dwi) assert check_path_validity(pfi_roi_mask) pfi_dwi_cropped = jph(pfo_tmp, '{}_{}_cropped.nii.gz'.format(sj, DWI_suffix)) cut_dwi_image_from_first_slice_mask_path(pfi_dwi, pfi_roi_mask, pfi_dwi_cropped) print('cutting done. Input DWI {0}. Output cropped {1}.\n\n'.format( pfi_dwi, pfi_dwi_cropped)) del pfi_dwi, pfi_roi_mask, pfi_dwi_cropped if controller['cut_mask_S0']: print('- cut mask S0 {}'.format(sj)) pfi_S0 = jph(pfo_tmp, '{}_{}_S0_to_std.nii.gz'.format(sj, DWI_suffix)) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') assert check_path_validity(pfi_S0) assert check_path_validity(pfi_roi_mask) pfi_S0_cropped = jph(pfo_tmp, sj + '_S0_cropped.nii.gz') cmd = 'seg_maths {0} -mul {1} {2}'.format(pfi_S0, pfi_roi_mask, pfi_S0_cropped) print_and_run(cmd) del pfi_S0, pfi_roi_mask, pfi_S0_cropped, cmd if controller['correct_slope']: print('- correct slope {}'.format(sj)) # -- pfi_dwi_cropped = jph(pfo_tmp, '{}_{}_cropped.nii.gz'.format(sj, DWI_suffix)) pfi_slope_txt = jph(pfo_input_sj_DWI, '{}_{}_slope.txt'.format(sj, DWI_suffix)) assert check_path_validity(pfi_dwi_cropped), pfi_dwi_cropped assert check_path_validity(pfi_slope_txt), pfi_slope_txt pfi_dwi_slope_corrected = jph( pfo_tmp, '{}_{}_slope_corrected.nii.gz'.format(sj, DWI_suffix)) slopes = np.loadtxt(pfi_slope_txt) slope_corrector_path(slopes, pfi_dwi_cropped, pfi_dwi_slope_corrected) # -- pfi_S0_cropped = jph(pfo_tmp, sj + '_S0_cropped.nii.gz') assert check_path_validity(pfi_S0_cropped) pfi_S0_slope_corrected = jph(pfo_tmp, sj + '_S0_slope_corrected.nii.gz') slope_corrector_path(slopes[0], pfi_S0_cropped, pfi_S0_slope_corrected) del pfi_dwi_cropped, pfi_slope_txt, pfi_dwi_slope_corrected, slopes, pfi_S0_cropped, pfi_S0_slope_corrected if controller['eddy_current']: print('- eddy current {}'.format(sj)) pfi_dwi_slope_corrected = jph( pfo_tmp, '{}_{}_slope_corrected.nii.gz'.format(sj, DWI_suffix)) assert check_path_validity(pfi_dwi_slope_corrected) pfi_dwi_eddy_corrected = jph( pfo_tmp, '{}_{}_eddy.nii.gz'.format(sj, DWI_suffix)) cmd = 'eddy_correct {0} {1} 0 '.format(pfi_dwi_slope_corrected, pfi_dwi_eddy_corrected) print_and_run(cmd) del pfi_dwi_slope_corrected, pfi_dwi_eddy_corrected, cmd elif controller['fsl_tensor_fitting']: pfi_dwi_slope_corrected = jph( pfo_tmp, '{}_{}_slope_corrected.nii.gz'.format(sj, DWI_suffix)) pfi_dwi_eddy_corrected = jph( pfo_tmp, '{}_{}_eddy.nii.gz'.format(sj, DWI_suffix)) cmd = 'cp {0} {1} '.format(pfi_dwi_slope_corrected, pfi_dwi_eddy_corrected) print_and_run(cmd) del pfi_dwi_slope_corrected, pfi_dwi_eddy_corrected, cmd else: pass if controller['fsl_tensor_fitting']: print('- fsl tensor fitting {}'.format(sj)) pfi_dwi_eddy_corrected = jph( pfo_tmp, '{}_{}_eddy.nii.gz'.format(sj, DWI_suffix)) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') pfi_bvals = jph(pfo_input_sj_DWI, '{}_{}_DwEffBval.txt'.format(sj, DWI_suffix)) pfi_bvects = jph(pfo_input_sj_DWI, '{}_{}_DwGradVec.txt'.format(sj, DWI_suffix)) if isinstance(sj_parameters['b0_to_use_in_fsldti'], list): b0_tps_to_keep = sj_parameters['b0_to_use_in_fsldti'] tag = str(b0_tps_to_keep).replace('[', '').replace(']', '').replace( ',', '').replace(' ', '_') # out of the initial n uses only the one at the selected timepoint. bvals = np.loadtxt(pfi_bvals) bvects = np.loadtxt(pfi_bvects) num_bzeros = np.sum(bvals == bvals[0]) assert len(b0_tps_to_keep) < num_bzeros bvals_new = np.concatenate([[ bvals[0], ] * len(b0_tps_to_keep), bvals[num_bzeros:]]) bvect_new = np.vstack([ bvects[0, :].reshape(1, -1), ] * len(b0_tps_to_keep) + [bvects[num_bzeros:, :]]) pfi_bvals_new = jph( pfo_tmp, '{}_{}_DwEffBval_s0tp{}.txt'.format(sj, DWI_suffix, tag)) pfi_bvects_new = jph( pfo_tmp, '{}_{}_DwGradVec_s0tp{}.txt'.format(sj, DWI_suffix.tag)) np.savetxt(pfi_bvals_new, bvals_new) np.savetxt(pfi_bvects_new, bvect_new) im_eddy_corrected = nib.load(pfi_dwi_eddy_corrected) x, y, z, t = im_eddy_corrected.shape data_only_one_tp = np.concatenate([ im_eddy_corrected.get_data()[..., b0_tps_to_keep].reshape( x, y, z, -1), im_eddy_corrected.get_data()[..., num_bzeros:] ], axis=3) np.testing.assert_array_equal( data_only_one_tp.shape, [x, y, z, t - num_bzeros + len(b0_tps_to_keep)]) im_eddy_corrected_only_one_b0_tp = set_new_data( im_eddy_corrected, data_only_one_tp) pfi_dwi_eddy_corrected_new = jph( pfo_tmp, '{}_{}_eddy_s0tp{}.nii.gz'.format(sj, DWI_suffix, tag)) nib.save(im_eddy_corrected_only_one_b0_tp, pfi_dwi_eddy_corrected_new) assert check_path_validity(pfi_dwi_eddy_corrected) assert os.path.exists(pfi_bvals) assert os.path.exists(pfi_bvects) assert check_path_validity(pfi_roi_mask) pfi_analysis_fsl = jph(pfo_tmp, 'fsl_fit_' + sj) here = os.getcwd() cmd0 = 'cd {}'.format(pfo_tmp) cmd1 = 'dtifit -k {0} -b {1} -r {2} -m {3} ' \ '-w --save_tensor -o {4}'.format(pfi_dwi_eddy_corrected_new, pfi_bvals_new, pfi_bvects_new, pfi_roi_mask, pfi_analysis_fsl) cmd2 = 'cd {}'.format(here) print_and_run(cmd0) print_and_run(cmd1) print_and_run(cmd2) else: assert check_path_validity(pfi_dwi_eddy_corrected) assert os.path.exists(pfi_bvals) assert os.path.exists(pfi_bvects) assert check_path_validity(pfi_roi_mask) pfi_analysis_fsl = jph(pfo_tmp, 'fsl_fit_' + sj) here = os.getcwd() cmd0 = 'cd {}'.format(pfo_tmp) cmd1 = 'dtifit -k {0} -b {1} -r {2} -m {3} ' \ '-w --save_tensor -o {4}'.format(pfi_dwi_eddy_corrected, pfi_bvals, pfi_bvects, pfi_roi_mask, pfi_analysis_fsl) cmd2 = 'cd {}'.format(here) print_and_run(cmd0) print_and_run(cmd1) print_and_run(cmd2) del pfi_dwi_eddy_corrected, pfi_bvals, pfi_bvects, pfi_roi_mask, pfi_analysis_fsl, cmd0, cmd1, cmd2 if controller['adjust_dtibased_mod']: print('- adjust dti-based modalities {}'.format(sj)) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') pfi_roi_mask_4d = jph(pfo_mask, sj + '_S0_roi_mask_4d.nii.gz') pfi_v1 = jph(pfo_tmp, 'fsl_fit_' + sj + '_V1.nii.gz') pfi_s0 = jph(pfo_tmp, 'fsl_fit_' + sj + '_S0.nii.gz') pfi_FA = jph(pfo_tmp, 'fsl_fit_' + sj + '_FA.nii.gz') pfi_MD = jph(pfo_tmp, 'fsl_fit_' + sj + '_MD.nii.gz') cmd0, cmd1, cmd2 = None, None, None for pfi_mod in [pfi_v1, pfi_s0, pfi_FA, pfi_MD]: assert check_path_validity(pfi_mod) if 'V1' in pfi_mod: cmd0 = 'seg_maths {} -abs {}'.format(pfi_mod, pfi_mod) print_and_run(cmd0) reproduce_slice_fourth_dimension_path(pfi_roi_mask, pfi_roi_mask_4d, num_slices=3) cmd1 = 'seg_maths {0} -mul {1} {0}'.format( pfi_mod, pfi_roi_mask_4d, pfi_mod) print_and_run(cmd1) else: cmd0 = 'seg_maths {0} -mul {1} {0}'.format( pfi_mod, pfi_roi_mask, pfi_mod) print_and_run(cmd0) cmd0 = 'seg_maths {0} -removenan {0}'.format(pfi_mod) print_and_run(cmd0) cmd1 = 'seg_maths {0} -thr {1} {0}'.format(pfi_mod, '0') print_and_run(cmd0) del pfi_roi_mask, pfi_roi_mask_4d, pfi_v1, pfi_s0, pfi_FA, pfi_MD, cmd0, cmd1, cmd2 if controller['bfc_S0']: print('- bfc S0 {}'.format(sj)) pfi_s0 = jph(pfo_tmp, 'fsl_fit_' + sj + '_S0.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') assert check_path_validity(pfi_s0) assert check_path_validity(pfi_roi_mask) set_new_data_path(pfi_target_im=pfi_s0, pfi_image_where_the_new_data=pfi_roi_mask, pfi_result=pfi_roi_mask) bfc_param = sj_parameters['bias_field_parameters'] pfi_s0_bfc = jph(pfo_tmp, 'fsl_fit_' + sj + '_S0_bfc.nii.gz') bias_field_correction(pfi_s0, pfi_s0_bfc, pfi_mask=pfi_roi_mask, prefix='', convergenceThreshold=bfc_param[0], maximumNumberOfIterations=bfc_param[1], biasFieldFullWidthAtHalfMaximum=bfc_param[2], wienerFilterNoise=bfc_param[3], numberOfHistogramBins=bfc_param[4], numberOfControlPoints=bfc_param[5], splineOrder=bfc_param[6], print_only=False) del pfi_s0, pfi_roi_mask, bfc_param, pfi_s0_bfc if controller['create_lesion_mask']: print('- create lesion mask {}'.format(sj)) pfi_s0_bfc = jph(pfo_tmp, 'fsl_fit_' + sj + '_S0_bfc.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') assert check_path_validity(pfi_s0_bfc) assert os.path.exists(pfi_roi_mask) pfi_lesion_mask = jph(pfo_mask, sj + '_S0_lesion_mask.nii.gz') percentile = sj_parameters['options_S0']['window_percentile'] percentile_lesion_mask_extractor(im_input_path=pfi_s0_bfc, im_output_path=pfi_lesion_mask, im_mask_foreground_path=pfi_roi_mask, percentiles=percentile, safety_on=False) del pfi_s0_bfc, pfi_roi_mask, pfi_lesion_mask if controller['create_reg_masks']: print('- create reg masks {}'.format(sj)) pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') pfi_lesion_mask = jph(pfo_mask, sj + '_S0_lesion_mask.nii.gz') assert os.path.exists(pfi_roi_mask) assert check_path_validity(pfi_lesion_mask) pfi_registration_mask = jph(pfo_mask, sj + '_S0_reg_mask.nii.gz') cmd = 'seg_maths {0} -sub {1} {2} '.format( pfi_roi_mask, pfi_lesion_mask, pfi_registration_mask) # until here seems correct. print_and_run(cmd) del pfi_roi_mask, pfi_lesion_mask, pfi_registration_mask, cmd if controller['save_results']: print('- save results {}'.format(sj)) pfi_v1 = jph(pfo_tmp, 'fsl_fit_' + sj + '_V1.nii.gz') pfi_s0 = jph(pfo_tmp, 'fsl_fit_' + sj + '_S0.nii.gz') pfi_FA = jph(pfo_tmp, 'fsl_fit_' + sj + '_FA.nii.gz') pfi_MD = jph(pfo_tmp, 'fsl_fit_' + sj + '_MD.nii.gz') for p in [pfi_v1, pfi_s0, pfi_FA, pfi_MD]: assert check_path_validity(p) pfi_v1_new = jph(pfo_mod, sj + '_V1.nii.gz') pfi_s0_new = jph(pfo_mod, sj + '_S0.nii.gz') pfi_FA_new = jph(pfo_mod, sj + '_FA.nii.gz') pfi_MD_new = jph(pfo_mod, sj + '_MD.nii.gz') for a, b in zip([pfi_v1, pfi_s0, pfi_FA, pfi_MD], [pfi_v1_new, pfi_s0_new, pfi_FA_new, pfi_MD_new]): cmd = 'cp {0} {1}'.format(a, b) print_and_run(cmd) del pfi_v1, pfi_s0, pfi_FA, pfi_MD, pfi_v1_new, pfi_s0_new, pfi_FA_new, pfi_MD_new
def process_g_ratio_per_subject(sj, controller): print('\nProcessing g-ratio {} started.\n'.format(sj)) if sj not in list_all_subjects(pfo_subjects_parameters): raise IOError('Subject parameters not known. Subject {}'.format(sj)) sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) study = sj_parameters['study'] category = sj_parameters['category'] DWI_suffix = sj_parameters['names_architecture']['DWI'] # default is DWI pfo_input_sj_DWI = jph(root_study_rabbits, '02_nifti', study, category, sj, '{}_{}'.format(sj, DWI_suffix)) pfo_input_sj_MSME = jph(root_study_rabbits, '02_nifti', study, category, sj, sj + '_MSME') pfo_output_sj = jph(root_study_rabbits, 'A_data', study, category, sj) # input sanity check: if not os.path.exists(pfo_input_sj_DWI): print( 'DWI modality not given in the input folder after Nifti conversion. Bypass methods involving DWI' ) return if not os.path.exists(pfo_input_sj_MSME): print( 'MSME modality not given in the input folder after Nifti conversion. Bypass methods involving MSME' ) return if not os.path.exists(pfo_output_sj): raise IOError( 'Output folder subject {} does not exist.'.format(pfo_output_sj)) # -- Generate intermediate and output folder pfo_mod = jph(pfo_output_sj, 'mod') pfo_segm = jph(pfo_output_sj, 'segm') pfo_mask = jph(pfo_output_sj, 'masks') pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_gr{}'.format(DWI_suffix)) print_and_run('mkdir -p {}'.format(pfo_output_sj)) print_and_run('mkdir -p {}'.format(pfo_mod)) print_and_run('mkdir -p {}'.format(pfo_segm)) print_and_run('mkdir -p {}'.format(pfo_mask)) print_and_run('mkdir -p {}'.format(pfo_tmp)) # -- if controller['transpose_bvals_bvects']: print('- Transpose b-vals and b-vects') pfi_bvals = jph(pfo_input_sj_DWI, '{}_{}_DwEffBval.txt'.format(sj, DWI_suffix)) pfi_bvects = jph(pfo_input_sj_DWI, '{}_{}_DwGradVec.txt'.format(sj, DWI_suffix)) assert check_path_validity(pfi_bvals) assert check_path_validity(pfi_bvects) pfi_transposed_bvals = jph( pfo_tmp, '{}_{}_DwEffBval_T.txt'.format(sj, DWI_suffix)) pfi_transposed_vects = jph( pfo_tmp, '{}_{}_DwGradVec_T.txt'.format(sj, DWI_suffix)) m = np.loadtxt(pfi_bvals) np.savetxt(fname=pfi_transposed_bvals, X=m.T, delimiter=' ', newline=' ', fmt='%10.8f') m = np.loadtxt(pfi_bvects) np.savetxt(fname=pfi_transposed_vects, X=m.T, fmt='%10.8f') if controller['get_acquisition_echo_time']: pfi_visu_pars = jph(pfo_input_sj_MSME, sj + '_MSME_visu_pars.npy') assert check_path_validity(pfi_visu_pars), pfi_visu_pars pfi_echo_times = jph(pfo_tmp, sj + '_echo_times.txt') visu_pars_dict = np.load(pfi_visu_pars) np.savetxt(fname=pfi_echo_times, X=visu_pars_dict.item().get('VisuAcqEchoTime'), fmt='%10.2f', newline=' ') if controller['noddi']: print('- Noddi execution') # check if there is a DWI already processed in the TMP folder of the same subject: pfo_tmp_dwi = jph(pfo_output_sj, 'z_tmp', 'z_' + DWI_suffix) pfi_dwi_eddy_corrected = jph( pfo_tmp_dwi, '{}_{}_eddy.nii.gz'.format(sj, DWI_suffix)) pfi_roi_mask = jph(pfo_mask, '{}_S0_roi_mask.nii.gz'.format(sj)) pfi_transposed_bvals = jph( pfo_tmp, '{}_{}_DwEffBval_T.txt'.format(sj, DWI_suffix)) pfi_transposed_vects = jph( pfo_tmp, '{}_{}_DwGradVec_T.txt'.format(sj, DWI_suffix)) pfi_echo_times = jph(pfo_tmp, '{}_echo_times.txt'.format(sj)) assert check_path_validity( pfi_dwi_eddy_corrected), 'Need to run process_DWI first?' assert check_path_validity(pfi_transposed_bvals) assert check_path_validity(pfi_transposed_vects) assert check_path_validity(pfi_roi_mask) assert check_path_validity(pfi_echo_times) pfi_output_noddi = jph(pfo_tmp, '{}_nod.nii.gz'.format(sj)) cmd = root_fit_apps + 'fit_dwi -source {0} -mask {1} -bval {2} -bvec {3} -TE {4} -mcmap {5} -nod'.format( pfi_dwi_eddy_corrected, pfi_roi_mask, pfi_transposed_bvals, pfi_transposed_vects, pfi_echo_times, pfi_output_noddi) print_and_run(cmd) if controller['save_T2times']: if sj_parameters['category'] == 'ex_vivo': t2_times = (8, 50, 60) # (15, 80, 110) 30, 160, 200 - 14, 70, 100 elif sj_parameters['category'] == 'in_vivo': t2_times = (10, 60, 80) else: t2_times = (10, 60, 80) pfi_T2_times = jph(pfo_tmp, sj + '_t2_times.txt') np.savetxt(fname=pfi_T2_times, X=np.array(t2_times), fmt='%10.10f', newline=' ') if controller['fit_msme']: pfi_msme_inS0 = jph(pfo_mod, sj + '_MSMEinS0.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz') pfi_echo_times = jph(pfo_tmp, sj + '_echo_times.txt') pfi_T2_times = jph(pfo_tmp, sj + '_t2_times.txt') assert check_path_validity( pfi_msme_inS0), 'Need to run process_MSME first?' assert check_path_validity(pfi_roi_mask) assert check_path_validity(pfi_echo_times) assert check_path_validity(pfi_T2_times) pfi_mwf = jph(pfo_tmp, sj + '_vmvf.nii.gz') cmd = root_fit_apps + 'fit_qt2 -source {0} -mask {1} -nc 3 -TElist {2} -T2list {3} -mwf {4}'.format( pfi_msme_inS0, pfi_roi_mask, pfi_echo_times, pfi_T2_times, pfi_mwf) print cmd print_and_run(cmd) assert check_path_validity(pfi_mwf) if not os.path.exists(pfi_mwf): raise IOError('Something went wrong in using fit_qt2...') if controller['extract_first_tp_noddi']: pfi_noddi = jph(pfo_tmp, sj + '_nod.nii.gz') assert check_path_validity(pfi_noddi) pfi_vin = jph(pfo_tmp, sj + '_vin.nii.gz') cmd = 'seg_maths {0} -tp 0 {1}'.format(pfi_noddi, pfi_vin) print_and_run(cmd) if controller['compute_gratio']: pfi_mwf = jph(pfo_tmp, sj + '_vmvf.nii.gz') pfi_vin = jph(pfo_tmp, sj + '_vin.nii.gz') assert check_path_validity(pfi_mwf) assert check_path_validity(pfi_vin) pfi_tmp = jph(pfo_tmp, sj + '_tmp_g_ratio.nii.gz') pfi_g_ratio = jph(pfo_tmp, sj + '_g_ratio.nii.gz') cmd1 = 'seg_maths {0} -mul -1. {1}'.format(pfi_mwf, pfi_tmp) cmd2 = 'seg_maths {0} -add 1.0 {0}'.format(pfi_tmp) cmd3 = 'seg_maths {0} -mul {1} {0}'.format(pfi_tmp, pfi_vin) cmd4 = 'seg_maths {0} -div {1} {1}'.format(pfi_mwf, pfi_tmp) cmd5 = 'seg_maths {0} -add 1.0 {0}'.format(pfi_tmp) cmd6 = 'seg_maths {0} -recip {0}'.format(pfi_tmp) cmd7 = 'seg_maths {0} -sqrt {0}'.format(pfi_tmp) cmd8 = 'seg_maths {0} -uthr 0.999999999 {1}'.format( pfi_tmp, pfi_g_ratio) print_and_run(cmd1) print_and_run(cmd2) print_and_run(cmd3) print_and_run(cmd4) print_and_run(cmd5) print_and_run(cmd6) print_and_run(cmd7) print_and_run(cmd8) if controller['save_results']: pfi_g_ratio = jph(pfo_tmp, sj + '_g_ratio.nii.gz') assert check_path_validity(pfi_g_ratio) pfi_g_ratio_final = jph(pfo_mod, sj + '_g_ratio.nii.gz') cmd = 'cp {} {} '.format(pfi_g_ratio, pfi_g_ratio_final) print_and_run(cmd)
def process_T1_per_subject(sj, steps): print('\nProcessing T1 {} started.\n'.format(sj)) if sj not in list_all_subjects(pfo_subjects_parameters): raise IOError('Subject parameters not known. Subject {}'.format(sj)) sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r')) study = sj_parameters['study'] category = sj_parameters['category'] options_T1 = sj_parameters['options_T1'] suffix_T1_architecture = sj_parameters['names_architecture'][ 'T1'] # default is 3D pfo_input_sj_3D = jph(root_study_rabbits, '02_nifti', study, category, sj, sj + '_' + suffix_T1_architecture) pfo_output_sj = jph(root_study_rabbits, 'A_data', study, category, sj) # select appropriate atlas: if study == 'ACS' or study == 'PTB' or study == 'TestStudy': pfo_atlas = root_atlas elif study == 'W8': pfo_atlas = root_atlas_W8 else: raise IOError('Parameter **study** not included in the current logic.') # input sanity check: if not os.path.exists(pfo_input_sj_3D): raise IOError('Input folder nifti data T1 does not exist. {}'.format( pfo_input_sj_3D)) # -- Generate intermediate and output folder pfo_mod = jph(pfo_output_sj, 'mod') pfo_segm = jph(pfo_output_sj, 'segm') pfo_mask = jph(pfo_output_sj, 'masks') pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_T1' + suffix_T1_architecture) print_and_run('mkdir -p {}'.format(pfo_output_sj)) print_and_run('mkdir -p {}'.format(pfo_mod)) print_and_run('mkdir -p {}'.format(pfo_segm)) print_and_run('mkdir -p {}'.format(pfo_mask)) print_and_run('mkdir -p {}'.format(pfo_tmp)) reference_subject = options_T1['pivot'] # If the element is in template retrieve the original roi mask and reg mask if sj_parameters['in_atlas']: reference_subject = sj # turn off all the not useful stuff: options_T1['roi_mask'] = '' steps['adjust_mask'] = False steps['create_lesion_maks'] = False if steps['orient_to_standard']: print('- orient to standard {}'.format(sj)) pfi_input_original = jph(pfo_input_sj_3D, sj + '_3D.nii.gz') assert check_path_validity(pfi_input_original) pfi_std = jph(pfo_tmp, sj + '_to_std.nii.gz') orient2std(pfi_input_original, pfi_std) del pfi_input_original, pfi_std if steps['create_roi_masks']: pfi_std = jph(pfo_tmp, '{}_to_std.nii.gz'.format(sj)) assert check_path_validity(pfi_std) # --- Get the reference masks from the stereotaxic orientation --- # reference subject: pfi_sj_ref_coord_system = jph(pfo_atlas, reference_subject, 'mod', '{}_T1.nii.gz'.format(reference_subject)) # original mask pfi_reference_roi_mask = jph( pfo_atlas, reference_subject, 'masks', '{}_roi_mask.nii.gz'.format(reference_subject)) pfi_reference_reg_mask = jph( pfo_atlas, reference_subject, 'masks', '{}_reg_mask.nii.gz'.format(reference_subject)) assert check_path_validity(pfi_sj_ref_coord_system) assert check_path_validity(pfi_reference_roi_mask) assert check_path_validity(pfi_reference_reg_mask) # --- Get the angle difference from histological (template) to bicommissural (data) and orient header --- if isinstance(sj_parameters['angles'][0], list): angles = sj_parameters['angles'][0] else: angles = sj_parameters['angles'] angle_parameter = angles[1] print('Get initial roi mask using the pivot.') pfi_sj_ref_coord_system_hd_oriented = jph( pfo_tmp, 'reference_for_mask_registration.nii.gz') pfi_reference_roi_mask_hd_oriented = jph( pfo_tmp, 'reference_for_mask_roi_mask.nii.gz') pfi_reference_reg_mask_hd_oriented = jph( pfo_tmp, 'reference_for_mask_reg_mask.nii.gz') nis_app = nis.App() nis_app.header.apply_small_rotation( pfi_sj_ref_coord_system, pfi_sj_ref_coord_system_hd_oriented, angle=angle_parameter, principal_axis='pitch') nis_app.header.apply_small_rotation(pfi_reference_roi_mask, pfi_reference_roi_mask_hd_oriented, angle=angle_parameter, principal_axis='pitch') nis_app.header.apply_small_rotation(pfi_reference_reg_mask, pfi_reference_reg_mask_hd_oriented, angle=angle_parameter, principal_axis='pitch') # set translational part to zero nis_app.header.modify_translational_part( pfi_sj_ref_coord_system_hd_oriented, pfi_sj_ref_coord_system_hd_oriented, np.array([0, 0, 0])) nis_app.header.modify_translational_part( pfi_reference_roi_mask_hd_oriented, pfi_reference_roi_mask_hd_oriented, np.array([0, 0, 0])) nis_app.header.modify_translational_part( pfi_reference_reg_mask_hd_oriented, pfi_reference_reg_mask_hd_oriented, np.array([0, 0, 0])) assert check_path_validity(pfi_sj_ref_coord_system_hd_oriented) assert check_path_validity(pfi_reference_roi_mask_hd_oriented) pfi_affine_transformation_ref_on_subject = jph( pfo_tmp, 'aff_ref_on_' + sj + '.txt') pfi_3d_warped_ref_on_subject = jph(pfo_tmp, 'warp_ref_on_' + sj + '.nii.gz') cmd = 'reg_aladin -ref {0} -flo {1} -fmask {2} -aff {3} -res {4} -omp {5} -speeeeed '.format( # -rigOnly pfi_std, pfi_sj_ref_coord_system_hd_oriented, pfi_reference_roi_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_3d_warped_ref_on_subject, num_cores_run) print_and_run(cmd) print('- propagate affine registration T1 to roi masks {}'.format(sj)) pfi_roi_mask_not_adjusted = jph( pfo_tmp, sj + '_T1_roi_mask_not_adjusted.nii.gz') cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_std, pfi_reference_roi_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_roi_mask_not_adjusted) print_and_run(cmd) print('- propagate affine registration T1 to reg masks {}'.format(sj)) pfi_reg_mask_not_adjusted = jph( pfo_tmp, sj + '_T1_reg_mask_not_adjusted.nii.gz') cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format( pfi_std, pfi_reference_reg_mask_hd_oriented, pfi_affine_transformation_ref_on_subject, pfi_reg_mask_not_adjusted) print_and_run(cmd) if sj_parameters['in_atlas']: # if sj is in multi-atlas just copy the masks as they are in the destination, and then manipulate pfi_roi_mask = jph(pfo_mask, '{}_T1_roi_mask.nii.gz'.format(sj)) # pfi_reg_mask = jph(pfo_mask, '{}_T1_reg_mask.nii.gz'.format(sj)) cmd = 'cp {} {}'.format(pfi_roi_mask_not_adjusted, pfi_roi_mask) print_and_run(cmd) # cmd = 'cp {} {}'.format(pfi_reg_mask_not_adjusted, pfi_reg_mask) # print_and_run(cmd) del pfi_std, pfi_sj_ref_coord_system, pfi_reference_roi_mask, \ angle_parameter, angles, pfi_sj_ref_coord_system_hd_oriented, pfi_reference_roi_mask_hd_oriented, \ pfi_affine_transformation_ref_on_subject, pfi_3d_warped_ref_on_subject, cmd if steps['adjust_mask']: print('- adjust mask {}'.format(sj)) # Input: pfi_roi_mask_not_adjusted = jph( pfo_tmp, sj + '_T1_roi_mask_not_adjusted.nii.gz') assert os.path.exists( pfi_roi_mask_not_adjusted), pfi_roi_mask_not_adjusted # Parameters: dilation_param = options_T1['mask_dilation'] # Main output: pfi_roi_mask = jph(pfo_mask, '{}_T1_roi_mask.nii.gz'.format(sj)) if dilation_param < 0: # if negative, erode. cmd = 'seg_maths {0} -ero {1} {2}'.format( pfi_roi_mask_not_adjusted, -1 * dilation_param, pfi_roi_mask) elif dilation_param > 0: cmd = 'seg_maths {0} -dil {1} {2}'.format( pfi_roi_mask_not_adjusted, dilation_param, pfi_roi_mask) else: cmd = 'cp {} {}'.format(pfi_roi_mask_not_adjusted, pfi_roi_mask) del dilation_param, pfi_roi_mask_not_adjusted print_and_run(cmd) del pfi_roi_mask, cmd if steps['cut_masks']: if options_T1['crop_roi']: print('- cut masks {}'.format(sj)) pfi_std = jph(pfo_tmp, sj + '_to_std.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_T1_roi_mask.nii.gz') assert check_path_validity(pfi_std) assert check_path_validity(pfi_roi_mask) pfi_3d_cropped_roi = jph(pfo_tmp, sj + '_cropped.nii.gz') cmd = 'seg_maths {0} -mul {1} {2}'.format(pfi_std, pfi_roi_mask, pfi_3d_cropped_roi) print '\nCutting newly-created ciccione mask on the subject: subject {0}.\n'.format( sj) print_and_run(cmd) del pfi_std, pfi_roi_mask, pfi_3d_cropped_roi, cmd else: pfi_std = jph(pfo_tmp, sj + '_to_std.nii.gz') assert check_path_validity(pfi_std) pfi_3d_cropped_roi = jph(pfo_tmp, sj + '_cropped.nii.gz') cmd = 'cp {0} {1}'.format(pfi_std, pfi_3d_cropped_roi) print_and_run(cmd) if steps['step_bfc']: print('- step bfc {}'.format(sj)) pfi_3d_cropped_roi = jph(pfo_tmp, sj + '_cropped.nii.gz') assert check_path_validity(pfi_3d_cropped_roi) pfi_3d_bias_field_corrected = jph(pfo_tmp, sj + '_bfc.nii.gz') bfc_param = sj_parameters['bias_field_parameters'] pfi_roi_mask = jph(pfo_mask, sj + '_T1_roi_mask.nii.gz') bias_field_correction(pfi_3d_cropped_roi, pfi_3d_bias_field_corrected, pfi_mask=pfi_roi_mask, prefix='', convergenceThreshold=bfc_param[0], maximumNumberOfIterations=bfc_param[1], biasFieldFullWidthAtHalfMaximum=bfc_param[2], wienerFilterNoise=bfc_param[3], numberOfHistogramBins=bfc_param[4], numberOfControlPoints=bfc_param[5], splineOrder=bfc_param[6], print_only=False) del pfi_3d_cropped_roi, pfi_3d_bias_field_corrected, bfc_param, pfi_roi_mask if steps['create_lesion_maks']: print('Extract lesion mask: Subject {}'.format(sj)) # output: pfi_lesion_mask = jph(pfo_mask, sj + '_T1_lesion_mask.nii.gz') if options_T1['lesion_mask_method'] == 0: percentile = sj_parameters['options_T1']['window_percentile'] median_filter = sj_parameters['options_T1']['median_filter'] print( 'remove percentiles, values added manually: percentile {}, median filter {}' .format(percentile, median_filter)) pfi_3d_bias_field_corrected = jph(pfo_tmp, sj + '_bfc.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_T1_roi_mask.nii.gz') assert check_path_validity(pfi_3d_bias_field_corrected) assert check_path_validity(pfi_roi_mask) percentile_lesion_mask_extractor( im_input_path=pfi_3d_bias_field_corrected, im_output_path=pfi_lesion_mask, im_mask_foreground_path=pfi_roi_mask, percentiles=percentile, safety_on=False, median_filter=median_filter, pfo_tmp=pfo_tmp) elif options_T1['lesion_mask_method'] > 0: K = options_T1['lesion_mask_method'] print( 'remove the first (not background) and the last gaussians after MoG fitting, with K = {}.' .format(K)) pfi_3d_bias_field_corrected = jph(pfo_tmp, sj + '_bfc.nii.gz') pfi_roi_mask = jph(pfo_mask, sj + '_T1_roi_mask.nii.gz') assert os.path.exists(pfi_3d_bias_field_corrected) assert check_path_validity(pfi_roi_mask) pfi_mog_segm = jph(pfo_tmp, '{}_mog_segm.nii.gz'.format(sj)) pfi_T1_bfc = nib.load(pfi_3d_bias_field_corrected) pfi_roi_mask = nib.load(pfi_roi_mask) im_T1_bfc = nib.load(pfi_T1_bfc) im_roi_mask = nib.load(pfi_T1_bfc) c_array, p_array = MoG_array(im_T1_bfc.get_data(), K=K, pre_process_median_filter=True, mask_array=im_roi_mask.get_data(), pre_process_only_interquartile=True) c = set_new_data(im_T1_bfc, c_array) p = set_new_data(im_T1_bfc, p_array) nib.save(c, '/Users/sebastiano/Desktop/zzz.nii.gz') old_labels = list(range(K)) # [0, 1, 2, 3, 4] new_labels = [ 1, ] * len(old_labels) new_labels[0], new_labels[1], new_labels[-1] = 0, 0, 0 im_crisp = set_new_data(c, np.copy( relabeller(c.get_data(), old_labels, new_labels)), new_dtype=np.uint8) nib.save(im_crisp, pfi_mog_segm) # final tuning: cmd0 = 'seg_maths {0} -ero 3 {0}'.format(pfi_mog_segm, pfi_mog_segm) cmd1 = 'seg_maths {0} -fill {0}'.format(pfi_mog_segm) cmd2 = 'seg_maths {0} -dil 3 {0}'.format(pfi_mog_segm) cmd3 = 'seg_maths {0} -fill {0}'.format(pfi_mog_segm) print_and_run(cmd0) print_and_run(cmd1) print_and_run(cmd2) print_and_run(cmd3) # pfi_lesion_mask IS pfi_roi_mask - pfi_mog_segm pfi_mog_segm_and_roi = jph(pfo_tmp, '{}_T1_MoGsegm_and_roi.nii.gz') cmd11 = 'seg_maths {0} -mul {1} {2}'.format( pfi_roi_mask, pfi_mog_segm, pfi_mog_segm_and_roi) print_and_run(cmd11) cmd22 = 'seg_maths {0} -sub {1} {2}'.format( pfi_roi_mask, pfi_mog_segm_and_roi, pfi_lesion_mask) print_and_run(cmd22) if steps['create_reg_mask']: if sj_parameters['in_atlas']: print('Get registration mask: Subject {} is in atlas'.format(sj)) pfi_reg_mask_not_adjusted = jph( pfo_tmp, '{}_T1_reg_mask_not_adjusted.nii.gz'.format(sj)) assert os.path.exists(pfi_reg_mask_not_adjusted), \ 'Processing T1, subject {}. Run the step create_roi_mask first'.format(sj) pfi_reg_mask = jph(pfo_mask, '{}_T1_reg_mask.nii.gz'.format(sj)) cmd = 'cp {} {}'.format(pfi_reg_mask_not_adjusted, pfi_reg_mask) print_and_run(cmd) else: print('Create registration mask: Subject {}'.format(sj)) # output: pfi_reg_mask = jph(pfo_mask, sj + '_T1_reg_mask.nii.gz') # registration mask is defined as the difference between roi_mask and lesion_mask pfi_roi_mask = jph(pfo_mask, sj + '_T1_roi_mask.nii.gz') pfi_lesion_mask = jph(pfo_mask, sj + '_T1_lesion_mask.nii.gz') assert os.path.exists(pfi_roi_mask), pfi_roi_mask assert os.path.exists(pfi_lesion_mask), pfi_lesion_mask pfi_roi_mask_and_lesion_mask = jph( pfo_tmp, '{}_T1_ROI_and_LM.nii.gz'.format(sj)) cmd11 = 'seg_maths {0} -mul {1} {2}'.format( pfi_roi_mask, pfi_lesion_mask, pfi_roi_mask_and_lesion_mask) print_and_run(cmd11) cmd22 = 'seg_maths {0} -sub {1} {2}'.format( pfi_roi_mask, pfi_roi_mask_and_lesion_mask, pfi_reg_mask) print_and_run(cmd22) if steps['save_results']: print('- save results {}'.format(sj)) pfi_3d_bias_field_corrected = jph(pfo_tmp, sj + '_bfc.nii.gz') assert check_path_validity(pfi_3d_bias_field_corrected) pfi_3d_final_destination = jph(pfo_mod, sj + '_T1.nii.gz') cmd = 'cp {0} {1}'.format(pfi_3d_bias_field_corrected, pfi_3d_final_destination) print_and_run(cmd) del pfi_3d_bias_field_corrected, pfi_3d_final_destination, cmd