コード例 #1
0
def fuser(sp):

    print('Apply the nifty-seg fusion method {}'.format(sp.target_pfo))

    pfo_tmp = sp.scaffoldings_pfo
    assert os.path.exists(pfo_tmp)

    if sp.fuser_controller['Fuse']:
        print('- Fuse {} '.format(sp.target_name))

        # file-names and file-paths of the resulting stacks:
        fin_stack_segm = '{0}_stack_warp_segm_{1}.nii.gz'.format(
            sp.target_name, sp.parameters_tag)
        fin_stack_warp = '{0}_stack_warp_mod_{1}.nii.gz'.format(
            sp.target_name, sp.parameters_tag)

        pfi_4d_stack_warp_segm = jph(pfo_tmp, fin_stack_segm)
        pfi_4d_stack_warp_mod = jph(pfo_tmp, fin_stack_warp)

        assert os.path.exists(pfi_4d_stack_warp_segm)
        assert os.path.exists(pfi_4d_stack_warp_mod)

        # ------------ MV ------------
        pfi_output_MV = jph(
            pfo_tmp, 'result_{0}_MV_{1}.nii.gz'.format(sp.target_name,
                                                       sp.parameters_tag))
        if 'MV' in sp.fuser_options['Fusion_methods']:
            cmd_mv = 'seg_LabFusion -in {0} -out {1} -MV'.format(
                pfi_4d_stack_warp_segm, pfi_output_MV)
            print_and_run(cmd_mv, short_path_output=False)
            assert check_path_validity(pfi_output_MV, timeout=5000, interval=5)

        # -------- STAPLE ------------
        if 'STAPLE' in sp.fuser_options['Fusion_methods']:
            for key_staple in sp.fuser_options['STAPLE_params'].keys():
                pfi_output_STAPLE = jph(
                    pfo_tmp, 'result_{0}_STAPLE_{1}_{2}.nii.gz'.format(
                        sp.target_name, key_staple, sp.parameters_tag))
                beta = sp.fuser_options['STAPLE_params'][key_staple]
                if beta is None:
                    cmd_staple = 'seg_LabFusion -in {0} -STAPLE -out {1}'.format(
                        pfi_4d_stack_warp_segm, pfi_output_STAPLE)
                else:
                    cmd_staple = 'seg_LabFusion -in {0} -STAPLE -out {1} -MRF_beta {2}'.format(
                        pfi_4d_stack_warp_segm, pfi_output_STAPLE, beta)

                print_and_run(cmd_staple, short_path_output=False)
                assert check_path_validity(pfi_output_STAPLE,
                                           timeout=5000,
                                           interval=5)

        # ------------ STEPS ------------
        if 'STEPS' in sp.fuser_options['Fusion_methods']:
            # select the path to target:
            if len(sp.propagation_options['N_rigid_modalities']) > 0:
                if len(sp.propagation_options['N_rigid_mod_diff_bfc']) > 0:
                    pfi_target = jph(
                        pfo_tmp, 'target_nrigid_{}_mod_BFC.nii.gz'.format(
                            sp.target_name))
                else:
                    pfi_target = jph(
                        pfo_tmp,
                        'target_nrigid_{}_mod.nii.gz'.format(sp.target_name))
            else:
                pfi_target = sp.propagation_options[
                    'N_rigid_modalities'] = jph(
                        pfo_tmp,
                        'target_aff_{}_mod.nii.gz'.format(sp.target_name))

            assert os.path.exists(pfi_target)

            for key_steps in sp.fuser_options['STEPS_params'].keys():
                pfi_output_STEPS = jph(
                    pfo_tmp, 'result_{0}_STEPS_{1}_{2}.nii.gz'.format(
                        sp.target_name, key_steps, sp.parameters_tag))
                k, n, beta = sp.fuser_options['STEPS_params'][key_steps]
                if beta is None:
                    cmd_steps = 'seg_LabFusion -in {0} -out {1} -STEPS {2} {3} {4} {5} -prop_update'.format(
                        pfi_4d_stack_warp_segm, pfi_output_STEPS, k, n,
                        pfi_target, pfi_4d_stack_warp_mod)
                else:
                    cmd_steps = 'seg_LabFusion -in {0} -out {1} -STEPS {2} {3} {4} {5} -MRF_beta {5}  -prop_update '.\
                        format(pfi_4d_stack_warp_segm,
                               pfi_output_STEPS,
                               k,
                               n,
                               pfi_target,
                               pfi_4d_stack_warp_mod,
                               str(beta))
                print_and_run(cmd_steps, short_path_output=False)
                assert check_path_validity(pfi_output_STEPS,
                                           timeout=1000,
                                           interval=10)

    if sp.fuser_controller['Save_results']:

        print('- save result for target subject {}'.format(sp.target_name))

        pfo_automatic_segmentations_results = jph(
            sp.target_pfo, sp.arch_segmentations_name_folder,
            sp.arch_automatic_segmentations_name_folder)

        cmd0 = 'mkdir -p {}'.format(
            jph(sp.target_pfo, sp.arch_segmentations_name_folder))
        cmd1 = 'mkdir -p {}'.format(pfo_automatic_segmentations_results)
        print_and_run(cmd0)
        print_and_run(cmd1)

        for filename in os.listdir(pfo_tmp):
            if filename.startswith('result_'):
                cmd = 'cp {0} {1}'.format(
                    jph(pfo_tmp, filename),
                    jph(pfo_automatic_segmentations_results,
                        filename.replace('result_', '')))
                print_and_run(cmd)
コード例 #2
0
def process_MSME_per_subject(sj, controller):
    print('\nProcessing MSME, subject {} started.\n'.format(sj))

    if sj not in list_all_subjects(pfo_subjects_parameters):
        raise IOError(
            'Subject {} does not have a subject parameter ready.'.format(sj))

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

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

    pfo_input_sj = jph(root_study_rabbits, '02_nifti', study, category, sj)
    pfo_output_sj = jph(root_study_rabbits, 'A_data', study, category, sj)

    if not os.path.exists(pfo_input_sj):
        raise IOError('Input folder Subject does not exist.')

    # -- 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_MSME')

    print_and_run('mkdir -p {}'.format(pfo_output_sj))
    print_and_run('mkdir -p {}'.format(pfo_mod))
    print_and_run('mkdir -p {}'.format(pfo_segm))
    print_and_run('mkdir -p {}'.format(pfo_mask))
    print_and_run('mkdir -p {}'.format(pfo_tmp))

    pfi_msme_nifti = jph(pfo_input_sj, sj + '_MSME', sj + '_MSME.nii.gz')

    if not os.path.exists(pfi_msme_nifti):
        print(
            'MSME modality not given in the input folder after Nifti conversion. Bypass methods involving MSME'
        )
        return

    if controller['squeeze']:
        print('- Processing MSME: squeeze {}'.format(sj))

        assert os.path.exists(pfi_msme_nifti)
        pfi_msme = jph(pfo_tmp, sj + '_MSME.nii.gz')
        squeeze_image_from_path(pfi_msme_nifti, pfi_msme, copy_anyway=True)

    if controller['orient_to_standard']:
        print('- Processing MSME: orient to standard {}'.format(sj))
        pfi_msme = jph(pfo_tmp, sj + '_MSME.nii.gz')
        assert check_path_validity(pfi_msme)
        cmd = 'fslreorient2std {0} {0}'.format(pfi_msme)
        print_and_run(cmd)
        set_translational_part_to_zero(pfi_msme, pfi_msme)

    if controller['extract_first_timepoint']:
        print('- Processing MSME: extract first layers {}'.format(sj))
        pfi_msme = jph(pfo_tmp, sj + '_MSME.nii.gz')
        assert os.path.exists(pfi_msme)
        pfi_msme_original_first_layer = jph(pfo_tmp, sj + '_MSME_tp0.nii.gz')
        cmd0 = 'seg_maths {0} -tp 0 {1}'.format(pfi_msme,
                                                pfi_msme_original_first_layer)
        print_and_run(cmd0)

    if controller['register_tp0_to_S0']:

        print('- Processing MSME: create ficticious original MSME mask {}'.
              format(sj))
        pfi_msme_original_first_layer = jph(pfo_tmp, sj + '_MSME_tp0.nii.gz')

        print('- Processing MSME: register tp0 to S0 {}'.format(sj))
        pfi_s0 = jph(pfo_mod, sj + '_S0.nii.gz')
        pfi_s0_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz')
        pfi_msme_tp0 = jph(pfo_tmp, sj + '_MSME_tp0.nii.gz')
        assert os.path.exists(pfi_s0)
        assert os.path.exists(pfi_s0_mask)
        assert check_path_validity(pfi_msme_tp0)
        pfi_transf_msme_on_s0 = jph(pfo_tmp, sj + '_msme_on_S0_rigid.txt')
        pfi_warped_msme_on_s0 = jph(pfo_tmp, sj + '_MSME_tp0_up.nii.gz')
        cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -aff {3} -res {4} -omp {5} -rigOnly'.format(
            pfi_s0, pfi_s0_mask, pfi_msme_tp0, pfi_transf_msme_on_s0,
            pfi_warped_msme_on_s0, num_cores_run)
        print_and_run(cmd)

    if controller['register_msme_to_S0']:
        pfi_s0 = jph(pfo_mod, sj + '_S0.nii.gz')
        pfi_msme = jph(pfo_tmp, sj + '_MSME.nii.gz')
        pfi_transf_msme_on_s0 = jph(pfo_tmp, sj + '_msme_on_S0_rigid.txt')
        assert os.path.exists(pfi_s0)
        assert os.path.exists(pfi_msme)
        assert check_path_validity(pfi_transf_msme_on_s0)
        pfi_msme_upsampled = jph(pfo_tmp, sj + '_MSMEinS0.nii.gz')
        cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 1'.format(
            pfi_s0, pfi_msme, pfi_transf_msme_on_s0, pfi_msme_upsampled)
        print_and_run(cmd)

    if controller['get_mask_for_original_msme']:
        print('- Processing MSME: get mask for original msme:')
        # Start from the S0 mask (this will be saved as an extra mask as MSMEinS0 mask).
        pfi_s0_mask = jph(pfo_mask, '{}_S0_roi_mask.nii.gz'.format(sj))
        pfi_warped_msme_on_s0 = jph(pfo_tmp, sj + '_MSME_tp0_up.nii.gz')
        assert os.path.exists(pfi_s0_mask), pfi_s0_mask
        assert os.path.exists(pfi_warped_msme_on_s0), pfi_warped_msme_on_s0
        pfi_msme_up_lesion_mask = jph(
            pfo_tmp, '{}_msme_up_lesions_mask.nii.gz'.format(sj))
        pfi_msme_up_reg_mask = jph(pfo_tmp,
                                   '{}_msme_up_reg_mask.nii.gz'.format(sj))
        # Crop it properly to the MSMEinS0.
        percentile_lesion_mask_extractor(
            im_input_path=pfi_warped_msme_on_s0,
            im_output_path=pfi_msme_up_lesion_mask,
            im_mask_foreground_path=pfi_s0_mask,
            percentiles=(10, 98),
            safety_on=False)
        cmd = 'seg_maths {0} -sub {1} {2}'.format(pfi_s0_mask,
                                                  pfi_msme_up_lesion_mask,
                                                  pfi_msme_up_reg_mask)
        print_and_run(cmd)
        # Register MSMEinS0 with an approximative new mask over the original with no mask.
        pfi_msme_original_first_layer = jph(pfo_tmp,
                                            '{}_MSME_tp0.nii.gz'.format(sj))
        pfi_msme_original_pre_mask = jph(
            pfo_tmp, '{}_MSME_tp0_pre_mask.nii.gz'.format(sj))
        percentile_lesion_mask_extractor_only_from_image_path(
            pfi_msme_original_first_layer,
            pfi_msme_original_pre_mask,
            percentile_range=(30, 95))  # TODO, personal attribute?

        pfi_warped_msme_on_s0_back_on_msme = jph(
            pfo_tmp, '{}_warped_msme_on_s0_back_on_msme.nii.gz'.format(sj))
        pfi_affine_msme_on_s0_back_on_msme = jph(
            pfo_tmp, '{}_affine_msme_on_s0_back_on_msme.txt'.format(sj))
        cmd = 'reg_aladin -ref {0} -rmask {1} -flo {2} -fmask {3} -res {4} -aff {5} -omp {6} -rigOnly'.format(
            pfi_msme_original_first_layer, pfi_msme_original_pre_mask,
            pfi_warped_msme_on_s0, pfi_s0_mask,
            pfi_warped_msme_on_s0_back_on_msme,
            pfi_affine_msme_on_s0_back_on_msme, num_cores_run)
        print_and_run(cmd)
        # Apply transformation to the S0 roi_mask and to S0 reg_mask

        for ma in ['roi_mask', 'reg_mask']:
            S0_mask = jph(pfo_mask, '{0}_S0_{1}.nii.gz'.format(sj, ma))
            assert os.path.exists(S0_mask)
            MSME_mask = jph(pfo_mask, '{0}_MSME_{1}.nii.gz'.format(sj, ma))
            cmd = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format(
                pfi_msme_original_first_layer, S0_mask,
                pfi_affine_msme_on_s0_back_on_msme, MSME_mask)
            print_and_run(cmd)

        # print('back-propagate the S0 mask on the MSME:')
        # pfi_aff = jph(pfo_tmp, sj + '_msme_on_S0_rigid.txt')
        # assert os.path.exists(pfi_aff)
        # # this very same transformation must be used to back propagate the segmentations!
        # pfi_inv_aff = jph(pfo_tmp, sj + '_S0_on_msmse_rigid.txt')
        # cmd0 = 'reg_transform -invAff {0} {1}'.format(pfi_aff, pfi_inv_aff)
        # print_and_run(cmd0)
        # pfi_S0_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz')
        # pfi_msme = jph(pfo_tmp, sj + '_MSME.nii.gz')
        # assert os.path.exists(pfi_S0_mask)
        # assert os.path.exists(pfi_msme)
        # pfi_mask_on_msme = jph(pfo_mask, sj + '_MSME_roi_mask.nii.gz')
        # cmd1 = 'reg_resample -ref {0} -flo {1} -trans {2} -res {3} -inter 0'.format(
        #     pfi_msme, pfi_S0_mask, pfi_inv_aff, pfi_mask_on_msme)
        # print_and_run(cmd1)
        # print('Dilate:')
        # assert check_path_validity(pfi_mask_on_msme)
        # dil_factor = 0
        # cmd0 = 'seg_maths {0} -dil {1} {2}'.format(pfi_mask_on_msme, dil_factor, pfi_mask_on_msme)
        # print_and_run(cmd0)

    if controller['bfc']:
        print('- get bfc correction each slice:')
        pfi_msme_original = jph(pfo_tmp, sj + '_MSME.nii.gz')
        assert check_path_validity(pfi_msme_original)
        print('-- un-pack slices')
        im = nib.load(pfi_msme_original)
        tps = im.shape[-1]
        for tp in range(tps):
            pfi_tp = jph(pfo_tmp, sj + '_MSME_tp{}.nii.gz'.format(tp))
            cmd0 = 'seg_maths {0} -tp {1} {2}'.format(pfi_msme_original, tp,
                                                      pfi_tp)
            print_and_run(cmd0)
        print('-- bias-field correct the first slice')
        # bfc_param = subject[sj][3]
        bfc_param = [0.001, (50, 50, 50, 50), 0.15, 0.01, 400, (4, 4, 4), 3]
        pfi_tp0 = jph(pfo_tmp, sj + '_MSME_tp0.nii.gz')
        pfi_tp0_bfc = jph(pfo_tmp, sj + '_MSME_tp0_bfc.nii.gz')
        pfi_mask_on_msme = jph(pfo_mask, sj + '_MSME_roi_mask.nii.gz')
        assert os.path.exists(pfi_mask_on_msme)
        bias_field_correction(pfi_tp0,
                              pfi_tp0_bfc,
                              pfi_mask=pfi_mask_on_msme,
                              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)
        print('-- get the bias field from the bfc corrected')
        bias_field = jph(pfo_tmp, sj + '_bfc.nii.gz')
        cmd0 = 'seg_maths {0} -div {1} {2}'.format(pfi_tp0_bfc, pfi_tp0,
                                                   bias_field)
        cmd1 = 'seg_maths {0} -removenan {0}'.format(bias_field)
        print_and_run(cmd0)
        print_and_run(cmd1)
        assert check_path_validity(bias_field)
        print('-- correct all the remaining slices')
        for tp in range(1, tps):
            pfi_tp = jph(pfo_tmp, sj + '_MSME_tp{}.nii.gz'.format(tp))
            pfi_tp_bfc = jph(pfo_tmp, sj + '_MSME_tp{}_bfc.nii.gz'.format(tp))
            cmd0 = 'seg_maths {0} -mul {1} {2}'.format(pfi_tp, bias_field,
                                                       pfi_tp_bfc)
            print_and_run(cmd0)
            check_path_validity(pfi_tp_bfc)
        print('-- pack together all the images in a stack')
        cmd = 'seg_maths {0} -merge	{1} {2} '.format(pfi_tp0_bfc, tps - 1, 4)
        for tp in range(1, tps):
            pfi_tp_bfc = jph(pfo_tmp, sj + '_MSME_tp{}_bfc.nii.gz'.format(tp))
            cmd += pfi_tp_bfc + ' '
        pfi_stack = jph(pfo_tmp, sj + '_MSME_BFC.nii.gz')
        cmd += pfi_stack

        print_and_run(cmd)

    if controller['bfc_up']:
        print('- get bfc correction each slice:')
        pfi_msme_upsampled = jph(pfo_tmp, sj + '_MSMEinS0.nii.gz')
        assert check_path_validity(pfi_msme_upsampled)
        print('-- un-pack slices')
        im = nib.load(pfi_msme_upsampled)
        tps = im.shape[-1]
        for tp in range(tps):
            pfi_up_tp = jph(pfo_tmp, sj + '_MSMEinS0_tp{}.nii.gz'.format(tp))
            cmd0 = 'seg_maths {0} -tp {1} {2}'.format(pfi_msme_upsampled, tp,
                                                      pfi_up_tp)
            print_and_run(cmd0)
        print('-- bias-field correct the first slice')
        # bfc_param = subject[sj][3]
        bfc_param = [0.001, (50, 50, 50, 50), 0.15, 0.01, 400, (4, 4, 4), 3]
        pfi_up_tp0 = jph(pfo_tmp, sj + '_MSMEinS0_tp0.nii.gz')
        pfi_up_tp0_bfc = jph(pfo_tmp, sj + '_MSMEinS0_tp0_BFC.nii.gz')
        pfi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz')
        bias_field_correction(pfi_up_tp0,
                              pfi_up_tp0_bfc,
                              pfi_mask=pfi_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)
        print('-- get the bias field from the bfc corrected')
        bias_field_up = jph(pfo_tmp, sj + '_bfc_up.nii.gz')
        cmd0 = 'seg_maths {0} -div {1} {2}'.format(pfi_up_tp0_bfc, pfi_up_tp0,
                                                   bias_field_up)
        cmd1 = 'seg_maths {0} -removenan {0}'.format(bias_field_up)
        print_and_run(cmd0)
        print_and_run(cmd1)
        assert check_path_validity(bias_field_up)
        print('-- correct all the remaining slices')
        for tp in range(1, tps):
            pfi_up_tp = jph(pfo_tmp, sj + '_MSMEinS0_tp{}.nii.gz'.format(tp))
            pfi_up_tp_bfc = jph(pfo_tmp,
                                sj + '_MSMEinS0_tp{}_bfc.nii.gz'.format(tp))
            cmd0 = 'seg_maths {0} -mul {1} {2}'.format(pfi_up_tp,
                                                       bias_field_up,
                                                       pfi_up_tp_bfc)
            print_and_run(cmd0)
            check_path_validity(pfi_up_tp_bfc)
        print('-- pack together all the images in a stack')
        cmd_merge = 'seg_maths {0} -merge	{1} {2} '.format(
            pfi_up_tp0_bfc, tps - 1, 4)
        for tp in range(1, tps):
            pfi_up_tp_bfc = jph(pfo_tmp,
                                sj + '_MSMEinS0_tp{}_bfc.nii.gz'.format(tp))
            cmd_merge += pfi_up_tp_bfc + ' '
        pfi_stack_up = jph(pfo_tmp, sj + '_MSMEinS0_BFC.nii.gz')
        cmd_merge += pfi_stack_up

        print_and_run(cmd_merge)

    if controller['save_results']:
        print(
            'save results -  save only the bias field corrected as they proved to have the same results.'
        )
        pfi_msme_nifti = jph(pfo_tmp, sj + '_MSME.nii.gz')  # original
        pfi_msme_bfc = jph(pfo_tmp, sj + '_MSME_BFC.nii.gz')  # original bfc
        pfi_msme_up = jph(pfo_tmp, sj + '_MSMEinS0.nii.gz')  # up
        pfi_msme_up_bfc = jph(pfo_tmp, sj + '_MSMEinS0_BFC.nii.gz')  # up bfc
        assert check_path_validity(pfi_msme_nifti)
        assert check_path_validity(pfi_msme_bfc)
        assert check_path_validity(pfi_msme_up)
        assert check_path_validity(pfi_msme_up_bfc)
        # pfi_final        = jph(pfo_mod, sj + '_MSME.nii.gz')
        pfi_final_bfc = jph(pfo_mod, sj + '_MSME.nii.gz')
        # pfi_final_up     = jph(pfo_mod, sj + '_MSMEinS0.nii.gz')
        pfi_final_bfc_up = jph(pfo_mod, sj + '_MSMEinS0.nii.gz')
        # cmd0 = 'cp {0} {1}'.format(pfi_msme_nifti, pfi_final)
        # cmd2 = 'cp {0} {1}'.format(pfi_msme_up, pfi_final_up)
        cmd1 = 'cp {0} {1}'.format(pfi_msme_bfc, pfi_final_bfc)
        cmd3 = 'cp {0} {1}'.format(pfi_msme_up_bfc, pfi_final_bfc_up)
        # print_and_run(cmd0)
        # print_and_run(cmd2)
        print_and_run(cmd1)
        print_and_run(cmd3)

    if controller['save_results_tp0']:
        print('save results - timepoint zero, only the bfc')
        # pfi_msme_nifti  = jph(pfo_tmp, sj + '_MSME.nii.gz')  # original
        # pfi_msme_up     = jph(pfo_tmp, sj + '_MSMEinS0.nii.gz')  # up
        pfi_msme_bfc = jph(pfo_tmp, sj + '_MSME_BFC.nii.gz')  # original bfc
        pfi_msme_up_bfc = jph(pfo_tmp, sj + '_MSMEinS0_BFC.nii.gz')  # up bfc
        # assert check_path_validity(pfi_msme_nifti)
        # assert check_path_validity(pfi_msme_up)
        assert check_path_validity(pfi_msme_bfc)
        assert check_path_validity(pfi_msme_up_bfc)
        pfo_tp0 = jph(pfo_mod, 'MSME_tp0')
        # pfi_final_tp0        = jph(pfo_tp0, sj + '_MSME_tp0.nii.gz')
        # pfi_final_up_tp0     = jph(pfo_tp0, sj + '_MSMEinS0_tp0.nii.gz')
        pfi_final_bfc_tp0 = jph(pfo_tp0, sj + '_MSME_tp0.nii.gz')
        pfi_final_bfc_up_tp0 = jph(pfo_tp0, sj + '_MSMEinS0_tp0.nii.gz')
        cmd0 = 'mkdir -p {}'.format(pfo_tp0)
        # cmd1 = 'seg_maths {0} -tp 0 {1}'.format(pfi_msme_nifti, pfi_final_tp0)
        # cmd3 = 'seg_maths {0} -tp 0 {1}'.format(pfi_msme_up, pfi_final_up_tp0)
        cmd2 = 'seg_maths {0} -tp 0 {1}'.format(pfi_msme_bfc,
                                                pfi_final_bfc_tp0)

        cmd4 = 'seg_maths {0} -tp 0 {1}'.format(pfi_msme_up_bfc,
                                                pfi_final_bfc_up_tp0)
        print_and_run(cmd0)
        # print_and_run(cmd1)
        # print_and_run(cmd3)
        print_and_run(cmd2)
        print_and_run(cmd4)
コード例 #3
0
def test_check_path_validity_root():
    assert check_path_validity(root_dir)
コード例 #4
0
def process_T2_map_per_subject(sj, controller):

    print('\nProcessing T2 map {} 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_input_sj_MSME = jph(root_study_rabbits, '02_nifti', study, category, sj, sj + '_MSME')
    pfo_output_sj     = jph(root_study_rabbits, 'A_data', study, category, sj)
    pfo_mod           = jph(pfo_output_sj, 'mod')

    # input sanity check:
    if not os.path.exists(pfo_input_sj_MSME):
        print('MSME modality not given in the input folder after Nifti conversion. Bypass methods involving MSME')
        return
    if not os.path.exists(pfo_output_sj):
        raise IOError('Output folder MSME does not exist.')
    if not os.path.exists(pfo_mod):
        raise IOError('Output folder MSME does not exist.')

    # --  Generate intermediate and output folder
    pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_T2map')

    print_and_run('mkdir -p {}'.format(pfo_tmp))

    suffix = ['', 'inS0']

    if controller['get_acquisition_echo_time']:
        pfi_visu_pars = jph(pfo_input_sj_MSME, sj + '_MSME_visu_pars.npy')
        assert check_path_validity(pfi_visu_pars)
        pfi_echo_times = jph(pfo_tmp, sj + '_echo_times.txt')
        visu_pars_dict = np.load(pfi_visu_pars)
        np.savetxt(fname=pfi_echo_times, X=visu_pars_dict.item().get('VisuAcqEchoTime'), fmt='%10.2f', newline=' ')

    if controller['process_each_MSME_input']:
        pfi_echo_times = jph(pfo_tmp, '{}_echo_times.txt'.format(sj))
        assert os.path.exists(pfi_echo_times)
        TE = np.loadtxt(pfi_echo_times)
        echo_delta = TE[2] - TE[1]
        # original
        for s in suffix:
            pfi_original_MSME = jph(pfo_mod, '{}_MSME{}.nii.gz'.format(sj, s))
            check_path_validity(pfi_original_MSME)
            pfi_T2map = jph(pfo_tmp, '{}_T2map{}.nii.gz'.format(sj, s))
            cmd1 = root_fit_apps + 'fit_qt2 -source {0} -TE {1} -t2map {2}'.format(pfi_original_MSME,
                                                                                   echo_delta, pfi_T2map)
            print cmd1
            print_and_run(cmd1)

    if controller['correct_origin']:  # some versions of niftyfit for fit_qt2 are dividing by 0 in the origin.
        for s in suffix:
            pfi_T2map = jph(pfo_tmp, sj + '_T2map{}.nii.gz'.format(s))
            check_path_validity(pfi_T2map)
            pfi_T2map_corrected = jph(pfo_tmp, sj + '_corrected_T2map{}.nii.gz'.format(s))
            # clean upper outliers (Mean + 2 * StandardDeviation) ... They are more than outliers!
            im_s = nib.load(pfi_T2map)
            places_not_outliers = im_s.get_data() < 1000  # np.mean(im_s.get_data()) + 2 * np.std(im_s.get_data())
            im_s.get_data()[:] = places_not_outliers * im_s.get_data()
            im_corrected = set_new_data(im_s, places_not_outliers * im_s.get_data())
            nib.save(im_corrected, pfi_T2map_corrected)

    if controller['save_results']:
        # Save the bias field corrected '', and '_up' in the name _T2map and _T2map_up
        for s in suffix:
            pfi_source    = jph(pfo_tmp, sj + '_corrected_T2map{}.nii.gz'.format(s))
            pfi_destination    = jph(pfo_mod, sj + '_T2map.nii.gz')
            cmd = 'cp {0} {1}'.format(pfi_source, pfi_destination)
            print_and_run(cmd)
コード例 #5
0
def test_check_path_validity_for_a_nifti_image():
    assert check_path_validity(jph(pfo_tmp_test, 'dummy_image.nii.gz'))
コード例 #6
0
def test_check_path_validity_not_existing_path():
    with assert_raises(IOError):
        check_path_validity('/Spammer/path_to_spam')
コード例 #7
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
コード例 #8
0
def process_g_ratio_per_subject(sj, controller):

    print('\nProcessing g-ratio {} started.\n'.format(sj))

    if sj not in list_all_subjects(pfo_subjects_parameters):
        raise IOError('Subject parameters not known. Subject {}'.format(sj))

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

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

    DWI_suffix = sj_parameters['names_architecture']['DWI']  # default is DWI

    pfo_input_sj_DWI = jph(root_study_rabbits, '02_nifti', study, category, sj,
                           '{}_{}'.format(sj, DWI_suffix))
    pfo_input_sj_MSME = jph(root_study_rabbits, '02_nifti', study, category,
                            sj, sj + '_MSME')
    pfo_output_sj = jph(root_study_rabbits, 'A_data', study, category, sj)

    # input sanity check:
    if not os.path.exists(pfo_input_sj_DWI):
        print(
            'DWI modality not given in the input folder after Nifti conversion. Bypass methods involving DWI'
        )
        return
    if not os.path.exists(pfo_input_sj_MSME):
        print(
            'MSME modality not given in the input folder after Nifti conversion. Bypass methods involving MSME'
        )
        return
    if not os.path.exists(pfo_output_sj):
        raise IOError(
            'Output folder subject {} does not exist.'.format(pfo_output_sj))

    # --  Generate intermediate and output folder

    pfo_mod = jph(pfo_output_sj, 'mod')
    pfo_segm = jph(pfo_output_sj, 'segm')
    pfo_mask = jph(pfo_output_sj, 'masks')
    pfo_tmp = jph(pfo_output_sj, 'z_tmp', 'z_gr{}'.format(DWI_suffix))

    print_and_run('mkdir -p {}'.format(pfo_output_sj))
    print_and_run('mkdir -p {}'.format(pfo_mod))
    print_and_run('mkdir -p {}'.format(pfo_segm))
    print_and_run('mkdir -p {}'.format(pfo_mask))
    print_and_run('mkdir -p {}'.format(pfo_tmp))

    # --

    if controller['transpose_bvals_bvects']:
        print('- Transpose b-vals and b-vects')
        pfi_bvals = jph(pfo_input_sj_DWI,
                        '{}_{}_DwEffBval.txt'.format(sj, DWI_suffix))
        pfi_bvects = jph(pfo_input_sj_DWI,
                         '{}_{}_DwGradVec.txt'.format(sj, DWI_suffix))
        assert check_path_validity(pfi_bvals)
        assert check_path_validity(pfi_bvects)
        pfi_transposed_bvals = jph(
            pfo_tmp, '{}_{}_DwEffBval_T.txt'.format(sj, DWI_suffix))
        pfi_transposed_vects = jph(
            pfo_tmp, '{}_{}_DwGradVec_T.txt'.format(sj, DWI_suffix))
        m = np.loadtxt(pfi_bvals)
        np.savetxt(fname=pfi_transposed_bvals,
                   X=m.T,
                   delimiter=' ',
                   newline=' ',
                   fmt='%10.8f')
        m = np.loadtxt(pfi_bvects)
        np.savetxt(fname=pfi_transposed_vects, X=m.T, fmt='%10.8f')

    if controller['get_acquisition_echo_time']:
        pfi_visu_pars = jph(pfo_input_sj_MSME, sj + '_MSME_visu_pars.npy')
        assert check_path_validity(pfi_visu_pars), pfi_visu_pars
        pfi_echo_times = jph(pfo_tmp, sj + '_echo_times.txt')
        visu_pars_dict = np.load(pfi_visu_pars)
        np.savetxt(fname=pfi_echo_times,
                   X=visu_pars_dict.item().get('VisuAcqEchoTime'),
                   fmt='%10.2f',
                   newline=' ')

    if controller['noddi']:
        print('- Noddi execution')
        # check if there is a DWI already processed in the TMP folder of the same subject:
        pfo_tmp_dwi = jph(pfo_output_sj, 'z_tmp', 'z_' + DWI_suffix)
        pfi_dwi_eddy_corrected = jph(
            pfo_tmp_dwi, '{}_{}_eddy.nii.gz'.format(sj, DWI_suffix))

        pfi_roi_mask = jph(pfo_mask, '{}_S0_roi_mask.nii.gz'.format(sj))

        pfi_transposed_bvals = jph(
            pfo_tmp, '{}_{}_DwEffBval_T.txt'.format(sj, DWI_suffix))
        pfi_transposed_vects = jph(
            pfo_tmp, '{}_{}_DwGradVec_T.txt'.format(sj, DWI_suffix))
        pfi_echo_times = jph(pfo_tmp, '{}_echo_times.txt'.format(sj))

        assert check_path_validity(
            pfi_dwi_eddy_corrected), 'Need to run process_DWI first?'
        assert check_path_validity(pfi_transposed_bvals)
        assert check_path_validity(pfi_transposed_vects)
        assert check_path_validity(pfi_roi_mask)
        assert check_path_validity(pfi_echo_times)
        pfi_output_noddi = jph(pfo_tmp, '{}_nod.nii.gz'.format(sj))
        cmd = root_fit_apps + 'fit_dwi -source {0} -mask {1} -bval {2} -bvec {3} -TE {4} -mcmap {5} -nod'.format(
            pfi_dwi_eddy_corrected, pfi_roi_mask, pfi_transposed_bvals,
            pfi_transposed_vects, pfi_echo_times, pfi_output_noddi)
        print_and_run(cmd)

    if controller['save_T2times']:
        if sj_parameters['category'] == 'ex_vivo':
            t2_times = (8, 50, 60)  # (15, 80, 110) 30, 160, 200 - 14, 70, 100
        elif sj_parameters['category'] == 'in_vivo':
            t2_times = (10, 60, 80)
        else:
            t2_times = (10, 60, 80)
        pfi_T2_times = jph(pfo_tmp, sj + '_t2_times.txt')
        np.savetxt(fname=pfi_T2_times,
                   X=np.array(t2_times),
                   fmt='%10.10f',
                   newline=' ')

    if controller['fit_msme']:
        pfi_msme_inS0 = jph(pfo_mod, sj + '_MSMEinS0.nii.gz')
        pfi_roi_mask = jph(pfo_mask, sj + '_S0_roi_mask.nii.gz')
        pfi_echo_times = jph(pfo_tmp, sj + '_echo_times.txt')
        pfi_T2_times = jph(pfo_tmp, sj + '_t2_times.txt')
        assert check_path_validity(
            pfi_msme_inS0), 'Need to run process_MSME first?'
        assert check_path_validity(pfi_roi_mask)
        assert check_path_validity(pfi_echo_times)
        assert check_path_validity(pfi_T2_times)
        pfi_mwf = jph(pfo_tmp, sj + '_vmvf.nii.gz')
        cmd = root_fit_apps + 'fit_qt2 -source {0} -mask {1} -nc 3 -TElist {2} -T2list {3} -mwf {4}'.format(
            pfi_msme_inS0, pfi_roi_mask, pfi_echo_times, pfi_T2_times, pfi_mwf)
        print cmd
        print_and_run(cmd)
        assert check_path_validity(pfi_mwf)
        if not os.path.exists(pfi_mwf):
            raise IOError('Something went wrong in using fit_qt2...')

    if controller['extract_first_tp_noddi']:
        pfi_noddi = jph(pfo_tmp, sj + '_nod.nii.gz')
        assert check_path_validity(pfi_noddi)
        pfi_vin = jph(pfo_tmp, sj + '_vin.nii.gz')
        cmd = 'seg_maths {0} -tp 0 {1}'.format(pfi_noddi, pfi_vin)
        print_and_run(cmd)

    if controller['compute_gratio']:
        pfi_mwf = jph(pfo_tmp, sj + '_vmvf.nii.gz')
        pfi_vin = jph(pfo_tmp, sj + '_vin.nii.gz')
        assert check_path_validity(pfi_mwf)
        assert check_path_validity(pfi_vin)
        pfi_tmp = jph(pfo_tmp, sj + '_tmp_g_ratio.nii.gz')
        pfi_g_ratio = jph(pfo_tmp, sj + '_g_ratio.nii.gz')
        cmd1 = 'seg_maths {0} -mul -1. {1}'.format(pfi_mwf, pfi_tmp)
        cmd2 = 'seg_maths {0} -add 1.0 {0}'.format(pfi_tmp)
        cmd3 = 'seg_maths {0} -mul {1} {0}'.format(pfi_tmp, pfi_vin)
        cmd4 = 'seg_maths {0} -div {1} {1}'.format(pfi_mwf, pfi_tmp)
        cmd5 = 'seg_maths {0} -add 1.0 {0}'.format(pfi_tmp)
        cmd6 = 'seg_maths {0} -recip {0}'.format(pfi_tmp)
        cmd7 = 'seg_maths {0} -sqrt {0}'.format(pfi_tmp)
        cmd8 = 'seg_maths {0} -uthr 0.999999999 {1}'.format(
            pfi_tmp, pfi_g_ratio)
        print_and_run(cmd1)
        print_and_run(cmd2)
        print_and_run(cmd3)
        print_and_run(cmd4)
        print_and_run(cmd5)
        print_and_run(cmd6)
        print_and_run(cmd7)
        print_and_run(cmd8)

    if controller['save_results']:
        pfi_g_ratio = jph(pfo_tmp, sj + '_g_ratio.nii.gz')
        assert check_path_validity(pfi_g_ratio)
        pfi_g_ratio_final = jph(pfo_mod, sj + '_g_ratio.nii.gz')
        cmd = 'cp {} {} '.format(pfi_g_ratio, pfi_g_ratio_final)
        print_and_run(cmd)
コード例 #9
0
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