def divide_taxonomical_intra_rater_tst_retest():
    taxonomical_old_labels = []
    taxonomical_new_labels = []

    for k in nomenclature_taxonomical.keys():
        taxonomical_old_labels += nomenclature_taxonomical[k][1]
        taxonomical_new_labels += [k] * len(nomenclature_taxonomical[k][1])

    # divide auto
    nis_app = nis.App()
    nis_app.manipulate_labels.relabel(ph.pfi_segm_auto,
                                      ph.pfi_segm_auto_divided_taxonomical,
                                      list_old_labels=taxonomical_old_labels,
                                      list_new_labels=taxonomical_new_labels)
    del nis_app

    # divide man1
    nis_app = nis.App()
    nis_app.manipulate_labels.relabel(ph.pfi_segm_man1,
                                      ph.pfi_segm_man1_divided_taxonomical,
                                      list_old_labels=taxonomical_old_labels,
                                      list_new_labels=taxonomical_new_labels)
    del nis_app

    # divide man2
    nis_app = nis.App()
    nis_app.manipulate_labels.relabel(ph.pfi_segm_man2,
                                      ph.pfi_segm_man2_divided_taxonomical,
                                      list_old_labels=taxonomical_old_labels,
                                      list_new_labels=taxonomical_new_labels)
    del nis_app
def prepare_slim_mask_from_path_to_stack(pfi_target_reg_mask_input,
                                         pfi_brain_mask,
                                         pfi_target_reg_mask_output):
    """
    Very simple for the moment:
    slim_mask = reg_mask * brain_mask
    :param pfi_target_reg_mask_input:
    :param pfi_brain_mask:
    :param pfi_target_reg_mask_output:
    :return:
    """
    nis_app = nis.App()
    nis_app.manipulate_shape.cut_4d_volume_with_a_1_slice_mask(
        pfi_target_reg_mask_input, pfi_brain_mask, pfi_target_reg_mask_output)
def create_eroded_segmentations_if_not_already_created(pfi_segm_non_eroded, pfi_contour, pfi_segm_eroded):

    if os.path.exists(pfi_segm_eroded):
        print('eroded segmentation for {} already created.'.format(pfi_segm_eroded))
    else:

        print('Getting contour segmentation {}'.format(pfi_segm_non_eroded))

        nis_app = nis.App()
        nis_app.manipulate_intensities.get_contour_from_segmentation(pfi_segm_non_eroded, pfi_contour, verbose=1)

        cmd = 'seg_maths {} -sub {} {}'.format(pfi_segm_non_eroded, pfi_contour, pfi_segm_eroded)

        print('Getting eroded segmentation {}'.format(pfi_segm_non_eroded))
        print(cmd)
        os.system(cmd)
def flipper(pfo_atlas, atlas_charts_names, sufix_atlas, labels_descriptor):

    for chart in atlas_charts_names:
        print
        print('Flipping chart {}'.format(chart))

        pfo_chart = jph(pfo_atlas, chart)
        pfo_chart_flipped = jph(pfo_atlas, chart + sufix_atlas)

        cmd = 'cp -r {0} {1}'.format(pfo_chart, pfo_chart_flipped)

        os.system(cmd)

        for root, dirs, files in os.walk(pfo_chart_flipped):
            for name in files:
                if name.endswith(('.nii', '.nii.gz')):
                    name_flipped = name.replace(chart, chart + sufix_atlas)
                    pfi_nii = jph(root, name)
                    pfi_nii_flipped = jph(root, name_flipped)

                    print 'old : {}'.format(pfi_nii)
                    print 'new : {}'.format(pfi_nii_flipped)

                    flip_left_right_from_path(pfi_nii, pfi_nii_flipped)

                    os.system('rm {}'.format(pfi_nii))

                    if '_segm' in name_flipped:
                        # substitute labels left-right:
                        ld_dict = labels_descriptor.get_dict()

                        left_labels = [
                            l for l in ld_dict.keys()
                            if 'left' in ld_dict[l][2].lower()
                        ]
                        right_labels = [l + 1 for l in left_labels]

                        nis_app = nis.App()
                        nis_app.manipulate_labels.relabel(
                            pfi_nii_flipped,
                            pfi_nii_flipped,
                            list_old_labels=left_labels + right_labels,
                            list_new_labels=right_labels + left_labels)
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)
Exemple #6
0
assert os.path.exists(pfi_input_anatomy), pfi_input_anatomy
assert os.path.exists(
    pfi_input_segmentation_noisy), pfi_input_segmentation_noisy
assert os.path.exists(pfo_output_folder), pfo_output_folder

# Output
log_file_before_cleaning = jph(pfo_output_folder, 'log_before_cleaning.txt')
pfi_output_cleaned_segmentation = jph(pfo_output_folder,
                                      'ellipsoids_segm_cleaned.nii.gz')
log_file_after_cleaning = jph(pfo_output_folder, 'log_after_cleaning.txt')
pfi_differece_cleaned_non_cleaned = jph(
    pfo_output_folder, 'difference_half_cleaned_uncleaned.nii.gz')

# ---- PROCESS ----

nl = nil.App()

# get the report before cleaning
nl.check.number_connected_components_per_label(
    pfi_input_segmentation_noisy,
    where_to_save_the_log_file=log_file_before_cleaning)

print('Wanted final number of components per label:')
im_input_segmentation_noisy = nib.load(pfi_input_segmentation_noisy)
correspondences_labels_components = [
    [k, 1] for k in range(np.max(im_input_segmentation_noisy.get_data()) + 1)
]
print(correspondences_labels_components)

# get the cleaned segmentation
nl.manipulate_labels.clean_segmentation(
Exemple #7
0
def extract_brain_tissue_from_multi_atlas_list_stereotaxic(target_name,
                                                           multi_atlas_list,
                                                           pfo_tmp,
                                                           pfi_output_brain_mask,
                                                           options):
    """
    :param target_name:
    :param multi_atlas_list:
    :param pfo_tmp:
    :param pfi_output_brain_mask:
    :param options:
    :return:
    """
    # controller:
    affine_only = True
    nrig_options = ' -be 0.98 -jl 0.5 '
    steps = {'register'    : True,
             'propagate'   : True,
             'stack'       : True,
             'fuse'        : True}

    assert os.path.exists(pfo_tmp)

    # parameters target:
    sj_parameters = pickle.load(open(jph(defs.pfo_subjects_parameters, target_name), 'r'))

    study    = sj_parameters['study']
    category = sj_parameters['category']

    # input target:
    root_target_sj = jph(defs.root_study_rabbits, 'A_data', study, category, target_name)

    pfi_target_sj_T1        = jph(root_target_sj, 'stereotaxic', 'mod', '{}_T1.nii.gz'.format(target_name))
    pfi_target_sj_reg_mask  = jph(root_target_sj, 'stereotaxic', 'masks', '{}_T1_reg_mask.nii.gz'.format(target_name))

    assert os.path.exists(pfi_target_sj_T1), pfi_target_sj_T1
    assert os.path.exists(pfi_target_sj_reg_mask), pfi_target_sj_reg_mask

    # list of final intermediate output:
    list_pfi_brain_mask_registered_on_target = []
    list_pfi_T1_registered_on_target         = []

    for atlas_sj in multi_atlas_list:

        # input selected atlas_sj
        if atlas_sj in defs.multi_atlas_subjects:
            root_atlas_sj = jph(defs.root_atlas, atlas_sj)
            pfi_atlas_sj_T1         = jph(root_atlas_sj, 'mod', '{}_T1.nii.gz'.format(atlas_sj))
            pfi_atlas_sj_reg_mask   = jph(root_atlas_sj, 'masks', '{}_reg_mask.nii.gz'.format(atlas_sj))
            pfi_atlas_sj_brain_mask = jph(root_atlas_sj, 'masks', '{}_brain_mask.nii.gz'.format(atlas_sj))
        elif atlas_sj in defs.multi_atlas_BT_subjects:
            root_atlas_name_BT = jph(defs.root_atlas_BT, atlas_sj)
            pfi_atlas_sj_T1         = jph(root_atlas_name_BT, '{}_T1.nii.gz'.format(atlas_sj))
            pfi_atlas_sj_reg_mask   = jph(root_atlas_name_BT, '{}_reg_mask.nii.gz'.format(atlas_sj))
            pfi_atlas_sj_brain_mask = jph(root_atlas_name_BT, '{}_brain_mask.nii.gz'.format(atlas_sj))
        else:
            raise IOError('Subject {} is not in a known or provided multi-atlas.'.format(atlas_sj))

        assert os.path.exists(pfi_atlas_sj_T1),         pfi_atlas_sj_T1
        assert os.path.exists(pfi_atlas_sj_reg_mask),   pfi_atlas_sj_reg_mask
        assert os.path.exists(pfi_atlas_sj_brain_mask), pfi_atlas_sj_brain_mask

        # intermediate output: AFFINE
        pfi_affine_transformation_ref_on_subject = jph(pfo_tmp, 'target{}_floating{}_aff_transformation.txt'.format(target_name, atlas_sj))
        pfi_affine_warped_ref_on_subject         = jph(pfo_tmp, 'target{}_floating{}_aff_warped.nii.gz'.format(target_name, atlas_sj))

        # AFFINE step
        cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -res {5} -omp {6} -speeeeed '.format(
            pfi_target_sj_T1, pfi_target_sj_reg_mask, pfi_atlas_sj_T1, pfi_atlas_sj_reg_mask,
            pfi_affine_transformation_ref_on_subject,
            pfi_affine_warped_ref_on_subject,
            defs.num_cores_run)
        if steps['register']:
            print_and_run(cmd)

        pfi_final_transformation = pfi_affine_transformation_ref_on_subject

        # intermediate output: NON-RIGID
        pfi_nrig_cpp_ref_on_subject    = jph(pfo_tmp, 'target{}_floating{}_nrigid_cpp.nii.gz'.format(target_name, atlas_sj))
        pfi_nrig_warped_ref_on_subject = jph(pfo_tmp, 'target{}_floating{}_nrigid_warped.nii.gz'.format(target_name, atlas_sj))

        # NON-RIGID step
        if not affine_only:
            cmd = 'reg_f3d -ref {0} -rmask {1} -flo {2} -fmask {3} -aff {4} -cpp {5} -res {6} {7} -omp {8}'.format(
                pfi_target_sj_T1, pfi_target_sj_reg_mask,
                pfi_atlas_sj_T1, pfi_atlas_sj_reg_mask,
                pfi_affine_transformation_ref_on_subject,
                pfi_nrig_cpp_ref_on_subject,
                pfi_nrig_warped_ref_on_subject,
                nrig_options,
                defs.num_cores_run)
            if steps['register']:
                print_and_run(cmd)

            pfi_final_transformation = pfi_nrig_cpp_ref_on_subject

        print('- Propagate registration to brain tissue mask, subject {0} over the target {1}'.format(
            atlas_sj, target_name))

        # Output brain tissue after affine trasformation, must include the atlas_sj name in the naming.
        pfi_brain_tissue_from_multi_atlas_sj = jph(pfo_tmp, 'target{0}_floating{1}_final_warped_brain_mask.nii.gz'.format(
            target_name, atlas_sj))
        cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format(
            pfi_target_sj_T1,
            pfi_atlas_sj_brain_mask,
            pfi_final_transformation,
            pfi_brain_tissue_from_multi_atlas_sj)
        if steps['propagate']:
            print_and_run(cmd)

        # Append path to output files ot the respective lists.
        list_pfi_brain_mask_registered_on_target.append(pfi_brain_tissue_from_multi_atlas_sj)

        if affine_only:
            list_pfi_T1_registered_on_target.append(pfi_affine_warped_ref_on_subject)
        else:
            list_pfi_T1_registered_on_target.append(pfi_nrig_warped_ref_on_subject)

    print('\n- Create stack of the brain masks warped over the target {} and merge with MV. '.format(target_name))

    pfi_stack_brain_mask = jph(pfo_tmp, 'a_stack_brain_tissues_target{0}_multiAtlas{1}.nii.gz'.format(
        target_name, options['method']))
    pfi_stack_T1 = jph(pfo_tmp, 'a_stack_T1_target{0}_multiAtlas{1}.nii.gz'.format(
        target_name, options['method']))

    if steps['stack']:
        # Create stack of warped brain mask
        nis_app = nis.App()
        nis_app.manipulate_shape.stack_list_pfi_images(list_pfi_brain_mask_registered_on_target, pfi_stack_brain_mask)
        del nis_app

        # Create stack of warped T1
        nis_app = nis.App()
        nis_app.manipulate_shape.stack_list_pfi_images(list_pfi_T1_registered_on_target, pfi_stack_T1)
        del nis_app

    if steps['fuse']:
        print('\n\n-Labels Fusion')
        # merge the roi masks in one (Majority voting for now):
        cmd = 'seg_LabFusion -in {0} -out {1} -MV '.format(pfi_stack_brain_mask, pfi_output_brain_mask)
        # cmd = 'seg_LabFusion -in {0} -STEPS 3 5 {1} {2} -out {3}'.format(pfi_stack_brain_mask, pfi_target_sj_T1,
        #                                                                  pfi_stack_T1, pfi_output_brain_mask)
        print_and_run(cmd)
    else:
        raise IOError

    # ----------------------------------------------------
    # ----------  START ----------------------------------
    # ----------------------------------------------------

    if control['prepare_data']:

        pfi_input_T1W = jph(pfo_brainweb, 'A_nifti', subject_id, '{}_T1W.nii.gz'.format(subject_id))
        pfi_input_crisp = jph(pfo_brainweb, 'A_nifti', subject_id, '{}_CRISP.nii.gz'.format(subject_id))
        assert os.path.exists(pfi_input_T1W), pfi_input_T1W
        assert os.path.exists(pfi_input_crisp), pfi_input_crisp

        # get mask with only the selected label_brain
        nis_app = nis.App()
        nis_app.manipulate_labels.assign_all_other_labels_the_same_value(
            pfi_input_crisp, pfi_brain_tissue_mask, labels_brain_to_keep, 0
        )
        nis_app.manipulate_labels.relabel(
            pfi_brain_tissue_mask, pfi_brain_tissue_mask, labels_brain_to_keep, [1, ] * len(labels_brain_to_keep)
        )

        # skull strip
        nis_app.math.prod(pfi_brain_tissue_mask, pfi_input_T1W, pfi_skull_stripped)

        # get a slice in PNG
        im_skull_stripped = nib.load(pfi_skull_stripped)
        scipy.misc.toimage(
            im_skull_stripped.get_data()[x_lim[0]:x_lim[1], y_slice, :].T
        ).save(pfi_coronal_slice)
Exemple #9
0
def collect_measurements_tst_retest():

    man1_man2 = True
    auto_man1 = True
    auto_man2 = True

    if man1_man2:
        print '\n==================='
        print '\n\nTaxonomical division inter-rater test-re-test'
        print '==================='
        print 'Compare man1 with man2 all labels'
        nis_app = nis.App()
        nis_app.measure.verbose = True
        man1_man2_measures_divide_taxonomical = nis_app.measure.dist(
            ph.pfi_segm_man1,
            ph.pfi_segm_man2,
            labels_list=taxonomy_abbreviations.keys(),
            labels_names=taxonomy_abbreviations.keys(),
            metrics=(dist.dice_score, dist.covariance_distance,
                     dist.hausdorff_distance,
                     dist.normalised_symmetric_contour_distance),
            where_to_save=ph.pfi_pickled_scoring_man1_man2_all)

        print '---------------'
        print 'Compare man1 with man2 all labels'
        print man1_man2_measures_divide_taxonomical

        del nis_app

    if auto_man1:
        print '\n\n==================='
        print 'Compare auto with man1 all labels'
        nis_app = nis.App()
        nis_app.measure.verbose = True
        auto_man1_measures_divide_taxonomical = nis_app.measure.dist(
            ph.pfi_segm_auto,
            ph.pfi_segm_man1,
            labels_list=taxonomy_abbreviations.keys(),
            labels_names=taxonomy_abbreviations.keys(),
            metrics=(dist.dice_score, dist.covariance_distance,
                     dist.hausdorff_distance,
                     dist.normalised_symmetric_contour_distance),
            where_to_save=ph.pfi_pickled_scoring_auto_man1_all)

        print '---------------'
        print 'Compare auto with man1 all labels'
        print auto_man1_measures_divide_taxonomical

        del nis_app

    if auto_man2:
        print '\n\n==================='
        print 'Compare auto with man2  all labels'
        nis_app = nis.App()
        nis_app.measure.verbose = True
        auto_man2_measures_divide_taxonomical = nis_app.measure.dist(
            ph.pfi_segm_auto,
            ph.pfi_segm_man2,
            labels_list=taxonomy_abbreviations.keys(),
            labels_names=taxonomy_abbreviations.keys(),
            metrics=(dist.dice_score, dist.covariance_distance,
                     dist.hausdorff_distance,
                     dist.normalised_symmetric_contour_distance),
            where_to_save=ph.pfi_pickled_scoring_auto_man2_all)

        print '---------------'
        print 'Compare auto with man2 all labels'
        print auto_man2_measures_divide_taxonomical

        del nis_app
            # Segm:
            for se in os.listdir(pfo_segm):
                if ch in se:
                    pfi_se = jph(pfo_segm, se)
                    adjust_nifti_translation_path(pfi_se, c_m, pfi_se)
                    adjust_nifti_image_type_path(pfi_se,
                                                 np.uint16,
                                                 pfi_se,
                                                 update_description='')

            # for T1 and S0 apply the re-normalisation based on the registration mask.
            pfi_reg_mask = jph(pfo_masks, '{}_roi_reg_mask.nii.gz'.format(ch))
            pfi_T1 = jph(pfo_mod, '{}_T1.nii.gz'.format(ch))
            pfi_S0 = jph(pfo_mod, '{}_S0.nii.gz'.format(ch))

            nis_app = nis.App(pfo_mod)
            nis_app.manipulate_intensities.normalise_below_label(
                pfi_T1,
                pfi_T1,
                pfi_segm,
            )
            nis_app.manipulate_intensities.normalise_below_label(
                pfi_S0,
                pfi_S0,
                pfi_segm,
            )
    ''' set the origin in the center of mass of the segmentation of a chart of the atlas '''
    if False:

        for fi in os.listdir(pfo_multi_atlas):
            if fi.endswith('.nii.gz'):
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))
Exemple #12
0
def run_comparison_inter_rater():
    # -- CONTROLLER

    label_hippocampus = 31
    pfo_root_experiments_input = path_manager.pfo_test_retest_inter_rater_input
    pfo_root_experiments_output = path_manager.pfo_test_retest_inter_rater_elaborations

    controller = {
        'compute_data': True,
        'plot_data_table': True,
        'see_segmentations': True
    }

    # -- DATA

    pfi_segm_raterH = jph(pfo_root_experiments_input, 'H.nii.gz')
    pfi_segm_raterL = jph(pfo_root_experiments_input, 'L.nii.gz')
    pfi_segm_auto = jph(pfo_root_experiments_input, 'auto.nii.gz')

    assert os.path.exists(pfi_segm_raterH)
    assert os.path.exists(pfi_segm_raterL)
    assert os.path.exists(pfi_segm_auto)

    # -- WORKFLOW

    if controller['compute_data']:
        raterH_vs_raterL = np.zeros(4)
        raterH_vs_auto = np.zeros(4)
        raterL_vs_auto = np.zeros(4)

        im_raterH = nib.load(pfi_segm_raterH)
        im_raterL = nib.load(pfi_segm_raterL)
        im_auto = nib.load(pfi_segm_auto)

        # dice score:
        raterH_vs_raterL[0] = dice_score_one_label(im_raterH, im_raterL,
                                                   label_hippocampus)
        raterH_vs_auto[0] = dice_score_one_label(im_raterH, im_auto,
                                                 label_hippocampus)
        raterL_vs_auto[0] = dice_score_one_label(im_raterL, im_auto,
                                                 label_hippocampus)

        # Covariance distance:
        raterH_vs_raterL[1] = covariance_distance(im_raterH,
                                                  im_raterL, [
                                                      label_hippocampus,
                                                  ], [
                                                      label_hippocampus,
                                                  ],
                                                  factor=10)[label_hippocampus]
        raterH_vs_auto[1] = covariance_distance(im_raterH,
                                                im_auto, [
                                                    label_hippocampus,
                                                ], [
                                                    label_hippocampus,
                                                ],
                                                factor=10)[label_hippocampus]
        raterL_vs_auto[1] = covariance_distance(im_raterL,
                                                im_auto, [
                                                    label_hippocampus,
                                                ], [
                                                    label_hippocampus,
                                                ],
                                                factor=10)[label_hippocampus]

        # Hausdorff distance:
        raterH_vs_raterL[2] = hausdorff_distance_one_label(im_raterH,
                                                           im_raterL,
                                                           label_hippocampus,
                                                           return_mm3=True)
        raterH_vs_auto[2] = hausdorff_distance_one_label(im_raterH,
                                                         im_auto,
                                                         label_hippocampus,
                                                         return_mm3=True)
        raterL_vs_auto[2] = hausdorff_distance_one_label(im_raterL,
                                                         im_auto,
                                                         label_hippocampus,
                                                         return_mm3=True)

        # NSCD:
        raterH_vs_raterL[3] = symmetric_contour_distance_one_label(
            im_raterH, im_raterL, label_hippocampus, return_mm3=True)
        raterH_vs_auto[3] = symmetric_contour_distance_one_label(
            im_raterH, im_auto, label_hippocampus, return_mm3=True)
        raterL_vs_auto[3] = symmetric_contour_distance_one_label(
            im_raterL, im_auto, label_hippocampus, return_mm3=True)

        print raterH_vs_raterL
        print raterH_vs_auto
        print raterL_vs_auto

        d = OrderedDict({
            'raterH_vs_raterL': raterH_vs_raterL,
            'raterH_vs_auto': raterH_vs_auto,
            'raterL_vs_auto': raterL_vs_auto
        })
        df = pa.DataFrame(data=d, index=['Dice', 'CovDist', 'HD', 'NSCD'])

        df.T.to_pickle(jph(pfo_root_experiments_output, 'results.pickle'))

    if controller['compute_data']:
        df = pa.read_pickle(jph(pfo_root_experiments_output, 'results.pickle'))
        print df.to_latex()
        df.to_latex(jph(pfo_root_experiments_output, 'result_comparison.tex'))

    if controller['see_segmentations']:
        """
        Colourig idea:
        Primary colors: segmentations
        - RED      7   auto
        - YELLOW   1   L (rater 2)
        - BLUE     4   H (rater 1)

        Secondary colours: intersections
        - Orange 11   auto + H
        - Purple 8    auto + L
        - Green 5    L + H

        Axial slice suggested 181
        """

        for f, f_label in zip(['L', 'H', 'auto'], [1, 4, 7]):
            # get contours
            pfi_segm = jph(pfo_root_experiments_input, '{}.nii.gz'.format(f))
            assert os.path.exists(pfi_segm)
            pfi_contour = jph(pfo_root_experiments_output,
                              '{}_contour.nii.gz'.format(f))

            nis_app = nis.App()
            nis_app.manipulate_intensities.get_contour_from_segmentation(
                pfi_segm, pfi_contour, omit_axis='y', verbose=1)
            # change labels values:
            os.system('seg_maths {0} -bin {0}'.format(pfi_contour))
            os.system('seg_maths {0} -mul {1} {0}'.format(
                pfi_contour, f_label))

        # sum contour together within a single segmentation:
        pfi_final_sum = jph(pfo_root_experiments_output, 'sum_contours.nii.gz')
        os.system('cp {0} {1}'.format(
            jph(pfo_root_experiments_output, '{}_contour.nii.gz'.format('L')),
            pfi_final_sum))
        for f in ['H', 'auto']:
            os.system('seg_maths {0} -add {1} {0}'.format(
                pfi_final_sum,
                jph(pfo_root_experiments_output,
                    '{}_contour.nii.gz'.format(f))))

        # open them: (anonymised and randomly selected subject 1203, hidden from the rater)
        sj = '1203'
        pfi_anatomy = jph(path_manager.pfo_multi_atlas, sj, 'mod',
                          '{}_T1.nii.gz'.format(sj))
        assert os.path.exists(pfi_anatomy)
        pfi_labels_descriptor = jph(pfo_root_experiments_input,
                                    'label_descriptor.txt')
        cmd = 'itksnap -g {0} -s {1} -l {2}'.format(pfi_anatomy, pfi_final_sum,
                                                    pfi_labels_descriptor)
        os.system(cmd)
def collect_measurements_intra_rater_tst_retest():
    print '\n==================='
    print '\n\nTaxonomical division inter-rater test-re-test'
    print '==================='
    print 'Compare man1 with man2 taxonomical'
    nis_app = nis.App()
    nis_app.measure.verbose = True
    man1_man2_measures_divide_taxonomical = nis_app.measure.dist(
        ph.pfi_segm_man1_divided_taxonomical,
        ph.pfi_segm_man2_divided_taxonomical,
        labels_list=nomenclature_taxonomical.keys(),
        labels_names=nomenclature_taxonomical.keys(),
        metrics=(dist.dice_score, dist.covariance_distance,
                 dist.hausdorff_distance,
                 dist.normalised_symmetric_contour_distance),
        where_to_save=ph.pfi_pickled_scoring_man1_man2_taxo)

    print '---------------'
    print 'Compare man1 with man2 divided taxonomical'
    print man1_man2_measures_divide_taxonomical

    del nis_app

    print '\n\n==================='
    print 'Compare auto with man1 taxonomical'
    nis_app = nis.App()
    nis_app.measure.verbose = True
    auto_man1_measures_divide_taxonomical = nis_app.measure.dist(
        ph.pfi_segm_auto_divided_taxonomical,
        ph.pfi_segm_man1_divided_taxonomical,
        labels_list=nomenclature_taxonomical.keys(),
        labels_names=nomenclature_taxonomical.keys(),
        metrics=(dist.dice_score, dist.covariance_distance,
                 dist.hausdorff_distance,
                 dist.normalised_symmetric_contour_distance),
        where_to_save=ph.pfi_pickled_scoring_auto_man1_taxo)

    print '---------------'
    print 'Compare auto with man1 divided taxonomical'
    print auto_man1_measures_divide_taxonomical

    del nis_app

    print '\n\n==================='
    print 'Compare auto with man2  taxonomical'
    nis_app = nis.App()
    nis_app.measure.verbose = True
    auto_man2_measures_divide_taxonomical = nis_app.measure.dist(
        ph.pfi_segm_auto_divided_taxonomical,
        ph.pfi_segm_man2_divided_taxonomical,
        labels_list=nomenclature_taxonomical.keys(),
        labels_names=nomenclature_taxonomical.keys(),
        metrics=(dist.dice_score, dist.covariance_distance,
                 dist.hausdorff_distance,
                 dist.normalised_symmetric_contour_distance),
        where_to_save=ph.pfi_pickled_scoring_auto_man2_taxo)

    print '---------------'
    print 'Compare auto with man2 divided taxonomical'
    print auto_man2_measures_divide_taxonomical

    del nis_app
Exemple #14
0
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
Exemple #15
0
def get_selection_dataset(subset, get_other_tissue_probability=True, keep_fuzzy_uint8=False):
    """
    From the dataset converted, it will provide a new dataset with a selection of segmentations.
    It will provide 3 images in the subject folder:
    > T1
    > CRIPS
    > 4d volumes with the FUZZY probability of the required subset and the other tissues in one label if required.
    :param subset: selection of segmentation as a sublist of names_tissues
    :param get_other_tissue_probability: [True] will merge all the other tissue in one label and provide this tissue
    probability as well as the others in the stack.
    :param keep_fuzzy_uint8: [False] if True fuzzy will be between 0 and 255, if False, result will be between 0 and 1.
    :return:
    """

    other_tissue = set(bw.names_tissues) - set(subset + ['bck'])

    old_labels = [0, ] + [bw.names_tissues.index(k) for k in subset] + [bw.names_tissues.index(k) for k in other_tissue]
    new_labels = [0, ] + list(range(1, len(subset) + 1))
    labels_mask = [0, ] + [1, ] * len(subset) + [0, ] * len(other_tissue)

    if get_other_tissue_probability:
        labels_plus_one = np.max(new_labels) + 1
        new_labels = new_labels + [labels_plus_one, ] * len(other_tissue)
    else:
        new_labels = new_labels + [0, ] * len(other_tissue)

    os.system('mkdir {}'.format(bw.pfo_data))

    for sj in bw.subjects_num_list:

        print('Subject {}'.format(sj))

        pfo_sj_nifti = os.path.join(bw.pfo_nifti_in_root, 'BW{}'.format(sj))

        pfo_sj_nifti_new = os.path.join(bw.pfo_data, 'BW{}'.format(sj))

        # os.system('mkdir {}'.format(pfo_sj_nifti_new))

        pfi_nifti_T1    = os.path.join(pfo_sj_nifti, 'BW{}_{}.nii.gz'.format(sj, bw.name_T1.upper()))
        pfi_nifti_CRISP = os.path.join(pfo_sj_nifti, 'BW{}_{}.nii.gz'.format(sj, bw.name_crisp.upper()))

        pfi_nifti_T1_new    = os.path.join(pfo_sj_nifti_new, 'BW{}_{}.nii.gz'.format(sj, bw.name_T1.upper()))
        pfi_nifti_CRISP_new = os.path.join(pfo_sj_nifti_new, 'BW{}_{}.nii.gz'.format(sj, bw.name_crisp.upper()))

        os.system('cp {} {}'.format(pfi_nifti_T1, pfi_nifti_T1_new))
        os.system('cp {} {}'.format(pfi_nifti_CRISP, pfi_nifti_CRISP_new))

        nis_app = nis.App()
        nis_app.manipulate_labels.relabel(pfi_nifti_CRISP_new, pfi_nifti_CRISP_new, old_labels, new_labels)

        pfi_nifti_MASK = os.path.join(pfo_sj_nifti_new, 'BW{}_MASK.nii.gz'.format(sj))

        os.system('cp {} {}'.format(pfi_nifti_CRISP_new, pfi_nifti_MASK))

        nis_app.manipulate_labels.relabel(pfi_nifti_MASK, pfi_nifti_MASK, new_labels, labels_mask)

        list_nib_fuzzy_segm = []

        for s in subset:
            pfi_output_nifti_fuzzy = os.path.join(pfo_sj_nifti, 'BW{}_{}.nii.gz'.format(sj, s))
            list_nib_fuzzy_segm.append(nib.load(pfi_output_nifti_fuzzy))

        if get_other_tissue_probability:
            array_other_tissue = np.zeros_like(list_nib_fuzzy_segm[0].get_data(), dtype=np.int32)
            for s_other in other_tissue:
                pfi_output_nifti_fuzzy = os.path.join(pfo_sj_nifti, 'BW{}_{}.nii.gz'.format(sj, s_other))
                array_other_tissue += nib.load(pfi_output_nifti_fuzzy).get_data()

        sum_fuzzy = np.zeros_like(list_nib_fuzzy_segm[0].get_data(), dtype=np.int32)

        for a in list_nib_fuzzy_segm:
            sum_fuzzy += a.get_data()

        if get_other_tissue_probability:
            sum_fuzzy += array_other_tissue

        if keep_fuzzy_uint8:

            if get_other_tissue_probability:
                new_data_no_bkg = np.stack([a.get_data().astype(np.uint16) for a in list_nib_fuzzy_segm] + [array_other_tissue], axis=3)
            else:
                new_data_no_bkg = np.stack([a.get_data().astype(np.uint16) for a in list_nib_fuzzy_segm], axis=3)
            sum_probabilities = np.sum(new_data_no_bkg, axis=3)
            excedence_matrix = np.zeros_like(new_data_no_bkg)
            X, Y, Z = np.where(sum_probabilities > 255)
            for x, y, z in zip(X, Y, Z):
                pos_max = np.argmax(new_data_no_bkg[x, y, z, :])
                excedence_matrix[x, y, z, pos_max] = sum_probabilities[x, y, z] - 255

            new_data_no_bkg = new_data_no_bkg - excedence_matrix

            sum_fuzzy = np.sum(new_data_no_bkg, axis=3)

            assert np.max(sum_fuzzy) <= 255

            bkg_data = 255 - sum_fuzzy

            new_data = np.stack([bkg_data] + [new_data_no_bkg[...,p] for p in range(len(subset))], axis=3).astype(np.uint8)

            new_image = nib.Nifti1Image(new_data, list_nib_fuzzy_segm[0].affine, header=list_nib_fuzzy_segm[0].header)

            pfi_nifti_fuzzy_new = os.path.join(pfo_sj_nifti_new, 'BW{}_FUZZY.nii.gz'.format(sj))
            nib.save(new_image, pfi_nifti_fuzzy_new)

        else:

            excedence_matrix = (sum_fuzzy > 255) * (sum_fuzzy - 255)

            normaliser_single_slice = 255 * np.ones_like(sum_fuzzy) + excedence_matrix
            if get_other_tissue_probability:
                new_data_no_bkg = np.nan_to_num(np.stack([a.get_data().astype(np.float64)/normaliser_single_slice for a in list_nib_fuzzy_segm] + [array_other_tissue.astype(np.float64)/normaliser_single_slice], axis=3))
            else:
                new_data_no_bkg = np.nan_to_num(np.stack([a.get_data().astype(np.float64) / normaliser_single_slice for a in list_nib_fuzzy_segm], axis=3))

            assert np.max(new_data_no_bkg) <= 1.0
            assert np.min(new_data_no_bkg) >= .0

            bkg_data = 1 - np.sum(new_data_no_bkg, axis=3)
            new_data = np.stack([bkg_data] + [new_data_no_bkg[..., p] for p in range(new_data_no_bkg.shape[-1])], axis=3).astype(
                np.float64)

            s = np.sum(new_data, axis=3)
            np.testing.assert_array_almost_equal(s, np.ones_like(s), decimal=10)

            new_image = nib.Nifti1Image(new_data, list_nib_fuzzy_segm[0].affine)

            pfi_nifti_fuzzy_new = os.path.join(pfo_sj_nifti_new, 'BW{}_FUZZY.nii.gz'.format(sj))
            nib.save(new_image, pfi_nifti_fuzzy_new)
def open_subject(sj, coordinates, check_dice_if_in_atlas=True):

    print('Quality control subject {}, coordinates {}'.format(sj, coordinates))

    sj_parameters = pickle.load(open(jph(pfo_subjects_parameters, sj), 'r'))

    study = sj_parameters['study']
    category = sj_parameters['category']

    in_atlas = sj_parameters['in_atlas']

    segm_suffix = sj_parameters['names_architecture']['suffix_segm']

    if study == 'W8':
        pfo_atlas = root_atlas_W8
        options = {
            'Template_chart_path': jph(root_atlas_W8, '12503'),
            '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))

    root_subject = jph(root_study_rabbits, 'A_data', study, category, sj)

    if coordinates == 'original':

        pfo_sj_mod = jph(root_subject, 'mod')
        pfo_sj_segm = jph(root_subject, 'segm')

        if not os.path.exists(pfo_sj_mod):
            print('\n\n Not existing {}!!'.format(pfo_sj_mod))
        if not os.path.exists(pfo_sj_segm):
            print('\n\n Not existing {}!!'.format(pfo_sj_mod))

        # open T1 in one frame:
        cmd = 'itksnap -g {} -o '.format(
            jph(pfo_sj_mod, '{}_T1.nii.gz'.format(sj)))
        cmd += ' -s {} '.format(
            jph(pfo_sj_segm, '{}_T1_segm.nii.gz'.format(sj)))
        cmd += ' -l {}'.format(pfi_labels_descriptor)
        os.system(cmd)

        # open other modalities in the other frame:
        cmd = 'itksnap -g {} -o '.format(
            jph(pfo_sj_mod, '{}_S0.nii.gz'.format(sj)))
        for m in ['FA', 'MD', 'V1']:
            cmd += ' {} '.format(jph(pfo_sj_mod, '{}_{}.nii.gz'.format(sj, m)))
        cmd += ' -s {} '.format(
            jph(pfo_sj_segm, '{}_S0_segm.nii.gz'.format(sj)))
        cmd += ' -l {}'.format(pfi_labels_descriptor)
        os.system(cmd)

    elif coordinates == 'stereotaxic':

        pfo_sj_mod = jph(root_subject, 'stereotaxic', 'mod')
        pfo_sj_segm = jph(root_subject, 'stereotaxic', 'segm')

        if not os.path.exists(pfo_sj_mod):
            print('\n\n Not existing {}!!'.format(pfo_sj_mod))
        if not os.path.exists(pfo_sj_segm):
            print('\n\n Not existing {}!!'.format(pfo_sj_mod))

        print pfo_sj_mod
        print pfo_sj_segm

        pfi_segm = jph(pfo_sj_segm, '{}_segm.nii.gz'.format(sj))
        if not os.path.exists(pfi_segm):
            pfi_segm = jph(pfo_sj_segm, 'automatic',
                           '{}_{}.nii.gz'.format(sj, segm_suffix))

        cmd = 'itksnap -g {} -o '.format(
            jph(pfo_sj_mod, '{}_T1.nii.gz'.format(sj)))
        for m in ['FA', 'MD', 'V1', 'S0']:
            cmd += ' {} '.format(jph(pfo_sj_mod, '{}_{}.nii.gz'.format(sj, m)))
        cmd += ' -s {} '.format(pfi_segm)
        cmd += ' -l {}'.format(pfi_labels_descriptor)
        os.system(cmd)

        if in_atlas and check_dice_if_in_atlas:

            pfi_segm_strx = jph(root_subject, 'stereotaxic', 'segm',
                                '{}_segm.nii.gz'.format(sj))
            pfi_segm_from_atlas = jph(pfo_atlas, sj, 'segm',
                                      '{}_segm.nii.gz'.format(sj))

            if not os.path.exists(pfi_segm_strx):
                print('\n\n Not existing {}!!'.format(pfi_segm_strx))
            if not os.path.exists(pfi_segm_from_atlas):
                print('\n\n Not existing {}!!'.format(pfi_segm_from_atlas))

            nis_app = nis.App()
            glob_dc = nis_app.measure.global_dist(
                pfi_segm_strx,
                pfi_segm_from_atlas,
                global_metrics=(global_dice_score, ))

            print(
                'Subject {} in atlas, has a segmentation aligned with the ground truth. Dice as measure of overlap: '
            )
            print(glob_dc)
            if glob_dc[0] < 0.95:
                print('WARNING!!! Possible bugs!')
    else:
        raise IOError(
            'Input variable - corrdinates - can be only original or stereotaxic. Had {}'
            .format(coordinates))
def denoise_mif_for_subject(sj, controller):

    pfi_DWI  = jph(root_intermediate, '{}_DWI_slope_corrected_to_std.nii.gz'.format(sj))
    pfi_brain_mask = jph(root_MASKs, '{}_brain.nii.gz'.format(sj))

    assert os.path.exists(pfi_DWI)
    assert os.path.exists(pfi_brain_mask)

    if controller['Denoise']:
        pfi_DWI_denoised = jph(root_DWIs_corrected, '{}_DWI.nii.gz'.format(sj))
        pfi_DWI_noise    = jph(root_intermediate, '{}_noise.nii.gz'.format(sj))
        cmd = 'dwidenoise {} {} -noise {} -mask {}'.format(
            pfi_DWI, pfi_DWI_denoised, pfi_DWI_noise, pfi_brain_mask)
        print_and_run(cmd)

    if controller['Get_differences']:
        pfi_DWI_denoised = jph(root_DWIs_corrected, '{}_DWI.nii.gz'.format(sj))
        pfi_DWI_noise = jph(root_intermediate, '{}_noise.nii.gz'.format(sj))
        assert os.path.exists(pfi_DWI_denoised)
        assert os.path.exists(pfi_DWI_noise)
        pfi_residual = jph(root_intermediate, '{}_residual.nii.gz'.format(sj))
        cmd = 'mrcalc {} {} -subtract {}'.format(pfi_DWI, pfi_DWI_denoised, pfi_residual)
        print_and_run(cmd)

    if controller['Quality_control']:
        pfi_residual = jph(root_intermediate, '{}_residual.nii.gz'.format(sj))
        assert os.path.exists(pfi_residual)
        cmd = 'mrview {}'.format(pfi_residual)
        print_and_run(cmd)

    if controller['Eddy_correct']:
        print('- eddy current {}'.format(sj))
        pfi_DWI_denoised = jph(root_DWIs_corrected, '{}_DWI.nii.gz'.format(sj))
        assert os.path.exists(pfi_DWI_denoised)
        pfi_DWI_denoised_eddy = jph(root_DWIs_corrected, '{}_DWI_eddy.nii.gz'.format(sj))
        cmd = 'eddy_correct {0} {1} 0 '.format(pfi_DWI_denoised, pfi_DWI_denoised_eddy)
        print_and_run(cmd)

    if controller['Quality_control_Eddy']:
        pfi_DWI_denoised = jph(root_DWIs_corrected, '{}_DWI.nii.gz'.format(sj))
        pfi_DWI_denoised_eddy = jph(root_DWIs_corrected, '{}_DWI_eddy.nii.gz'.format(sj))
        assert os.path.exists(pfi_DWI_denoised), pfi_DWI_denoised
        assert os.path.exists(pfi_DWI_denoised_eddy), pfi_DWI_denoised_eddy
        cmd = 'mrview -load {0} -load {1}'.format(pfi_DWI_denoised, pfi_DWI_denoised_eddy)
        print_and_run(cmd)

    if controller['Create_multi_timepoints_mask']:
        im_dwi = nib.load(pfi_DWI)
        pfi_brain_mask_multi_timepoint = jph(root_tmp, '{}_brain_multi_timepoints.nii.gz'.format(sj))
        nis_app = nis.App()
        nis_app.manipulate_shape.extend_slice_new_dimension(pfi_brain_mask, pfi_output=pfi_brain_mask_multi_timepoint,
                                                        new_axis=3, num_slices=im_dwi.shape[-1])

        del im_dwi, nis_app

    if controller['Grafting_denoised']:
        print('- grafting')
        pfi_DWI_denoised = jph(root_DWIs_corrected, '{}_DWI.nii.gz'.format(sj))
        pfi_residual = jph(root_intermediate, '{}_residual.nii.gz'.format(sj))
        pfi_brain_mask_multi_timepoint = jph(root_tmp, '{}_brain_multi_timepoints.nii.gz'.format(sj))
        assert os.path.exists(pfi_DWI_denoised)
        assert os.path.exists(pfi_residual)
        assert os.path.exists(pfi_brain_mask_multi_timepoint)

        pfi_DWI_denoised_grafted = jph(root_DWIs_corrected, '{}_DWIg.nii.gz'.format(sj))
        nis_app = nis.App()
        nis_app.manipulate_intensities.get_grafting(pfi_residual, pfi_DWI_denoised, pfi_DWI_denoised_grafted,
                                                    pfi_brain_mask_multi_timepoint)

    if controller['Grafting_denoised_Eddy']:
        print('- grafting eddy')
        pfi_DWI_denoised_eddy = jph(root_DWIs_corrected, '{}_DWI_eddy.nii.gz'.format(sj))
        pfi_residual = jph(root_intermediate, '{}_residual.nii.gz'.format(sj))
        pfi_brain_mask_multi_timepoint = jph(root_tmp, '{}_brain_multi_timepoints.nii.gz'.format(sj))
        assert os.path.exists(pfi_DWI_denoised_eddy)
        assert os.path.exists(pfi_residual)
        assert os.path.exists(pfi_brain_mask_multi_timepoint)
        pfi_DWI_denoised_grafted_eddy = jph(root_DWIs_corrected, '{}_DWIg_eddy.nii.gz'.format(sj))
        nis_app = nis.App()
        nis_app.manipulate_intensities.get_grafting(pfi_residual, pfi_DWI_denoised_eddy, pfi_DWI_denoised_grafted_eddy,
                                                    pfi_brain_mask_multi_timepoint)
Exemple #18
0
def run_separate_brain_regions(controller):

    # --- loop over subjects id ----

    for sj in path_manager.atlas_subjects:
        pfo_elaborations = jph(path_manager.pfo_atlas_validation_leave_one_out,
                               sj, 'segm', 'elaborations')
        os.system('mkdir -p {}'.format(pfo_elaborations))
        pfi_segm_manual = jph(path_manager.pfo_atlas_validation_leave_one_out,
                              sj, 'segm', '{}_segm.nii.gz'.format(sj))
        assert os.path.exists(pfi_segm_manual), pfi_segm_manual

        if controller['verbose']:
            print '\n\n==================='
            print 'Grouping segmentation according to given subdivision {}'.format(
                sj)
            print '===================\n'

        # -------------- get the selected nomenclature labels correspondence -----------

        nomenclature_old_labels = []
        nomenclature_new_labels = []

        if controller['Nomenclature'] == 'anatomical':
            nomenclature = nomenclature_anatomical
        elif controller['Nomenclature'] == 'taxonomical':
            nomenclature = nomenclature_taxonomical

        for k in nomenclature.keys():
            nomenclature_old_labels += nomenclature[k][1]
            nomenclature_new_labels += [k] * len(nomenclature[k][1])

        # -------------- create ground segmentation divided by selected nomenclature --------------

        if controller['Divide_manual_segm']:
            pfi_segm_manual_divided_nomenclature = jph(
                pfo_elaborations,
                sj + '_segm_{}.nii.gz'.format(controller['Nomenclature']))

            if controller['verbose']:
                print '\n==================='
                print '\n\nDivide_manual_segm nomenclature {}, subject {}'.format(
                    controller['Nomenclature'], sj)
                print 'From {0} to {1}'.format(
                    pfi_segm_manual, pfi_segm_manual_divided_nomenclature)
            nis_app = nis.App()
            nis_app.manipulate_labels.relabel(
                pfi_segm_manual,
                pfi_segm_manual_divided_nomenclature,
                list_old_labels=nomenclature_old_labels,
                list_new_labels=nomenclature_new_labels)

        # -------------- create automatic segmentations divided Taxonomical --------------

        if controller['Divide_automatic_segm']:

            for segm_suffix in param.segm_suffix:
                name_segm = '{0}_{1}_{2}'.format(sj, segm_suffix,
                                                 param.segm_tags)
                pfi_segm_automatic = jph(
                    path_manager.pfo_atlas_validation_leave_one_out, sj,
                    'segm', 'automatic', '{}.nii.gz'.format(name_segm))
                assert os.path.exists(pfi_segm_automatic), pfi_segm_automatic

                pfi_segm_automatic_divided_by_nomenclature = jph(
                    pfo_elaborations,
                    '{}_{}.nii.gz'.format(name_segm,
                                          controller['Nomenclature']))

                if controller['verbose']:
                    print('\n===================')
                    print(
                        '\nDivide_automatic_segm nomenclature {}, subject {}'.
                        format(controller['Nomenclature'], sj))
                    print 'From {0} to {1}'.format(
                        pfi_segm_automatic,
                        pfi_segm_automatic_divided_by_nomenclature)
                    nis_app = nis.App()
                    nis_app.manipulate_labels.relabel(
                        pfi_segm_automatic,
                        pfi_segm_automatic_divided_by_nomenclature,
                        list_old_labels=nomenclature_old_labels,
                        list_new_labels=nomenclature_new_labels)
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