示例#1
0
def run_generate_automatic_segmentation_leave_one_out(
        modalities=('T1', 'FA'),
        suffix_result='automaticMulti',
        tag_suffix='Multi'):

    assert os.path.exists(ph.pfo_mutli_atlas_leave_one_out)

    atlas_subjects = ph.atlas_subjects

    for target_chart_name in [ph.selected_target]:

        atlas_without_one = np.sort(
            list(set(atlas_subjects) - {ph.selected_target}))

        print(
            '\n\nLeave one out segmentation [START]:\nSub-template is {0}\nTarget is {1}\n Spot target started!!\n'
            '\n'.format(atlas_without_one, target_chart_name))

        assert target_chart_name not in atlas_without_one

        spot_sj = SpotDS(atlas_pfo=ph.pfo_mutli_atlas_leave_one_out,
                         target_pfo=ph.pfo_target,
                         target_name=target_chart_name,
                         parameters_tag='CrossValidation' + tag_suffix)

        # Template parameters:
        spot_sj.atlas_name = 'Multi Atlas Newborn Rabbit'  # Multi Atlas Newborn Rabbit
        spot_sj.atlas_list_charts_names = atlas_without_one
        spot_sj.atlas_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
        spot_sj.atlas_reference_chart_name = '1305'
        spot_sj.atlas_segmentation_suffix = 'segm'

        spot_sj.arch_suffix_masks = ['roi_mask', 'reg_mask', 'reg_mask']
        spot_sj.arch_automatic_segmentations_name_folder = suffix_result

        # --- target parameters
        spot_sj.target_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.target_name = target_chart_name

        # --- Utils
        spot_sj.bfc_corrector_cmd = ph.bfc_corrector_cmd

        # --- Propagator option
        spot_sj.propagation_options['Affine_modalities'] = modalities
        spot_sj.propagation_options['Affine_reg_masks'] = (
        )  # if (), there is a single mask for all mods
        spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
        spot_sj.propagation_options[
            'N_rigid_modalities'] = modalities  # if empty, no non-rigid step.
        spot_sj.propagation_options['N_rigid_reg_masks'] = (
        )  # if [], same mask for all modalities
        spot_sj.propagation_options['N_rigid_slim_reg_mask'] = False
        spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
            'T1', )  # empty list no diff bfc. - put a comma!!
        spot_sj.propagation_options[
            'N_rigid_parameters'] = ' -be 0.5 -ln 6 -lp 4  -smooR 0.07 -smooF 0.07  '
        spot_sj.propagation_options['N_rigid_same_mask_moving'] = False
        spot_sj.propagation_options[
            'N_reg_mask_target'] = 0  # 0 roi_mask, 1 reg_mask
        spot_sj.propagation_options[
            'N_reg_mask_moving'] = 1  # 0 roi_mask, 1 reg_mask
        spot_sj.propagation_options['Final_smoothing_factor'] = 0

        # --- Propagator controller
        spot_sj.propagation_controller['Aff_alignment'] = True
        spot_sj.propagation_controller['Propagate_aff_to_segm'] = True
        spot_sj.propagation_controller['Propagate_aff_to_mask'] = True
        spot_sj.propagation_controller['Get_N_rigid_slim_mask'] = True
        spot_sj.propagation_controller['Get_differential_BFC'] = True
        spot_sj.propagation_controller['N_rigid_alignment'] = True
        spot_sj.propagation_controller['Propagate_n_rigid'] = True
        spot_sj.propagation_controller['Smooth_results'] = True
        spot_sj.propagation_controller['Stack_warps_and_segms'] = True

        # --- Fuser option
        spot_sj.fuser_options['Fusion_methods'] = ['MV', 'STEPS', 'STAPLE'
                                                   ]  # 'MV', multi atlas only.
        spot_sj.fuser_options['Tp_mod_to_stack'] = 0
        spot_sj.fuser_options['STAPLE_params'] = OrderedDict([('pr_1', None)])
        spot_sj.fuser_options['STEPS_params'] = OrderedDict([
            ('pr_{0}_{1}'.format(k, n), [k, n, 0.4]) for n in [5, 7, 9]
            for k in [5, 7, 10]
        ])
        # --- Fuser controller
        spot_sj.fuser_controller['Fuse'] = True
        spot_sj.fuser_controller['Save_results'] = True

        spot_sj.spot_on_target_initialise()
        spot_sj.propagate()
        spot_sj.fuse()

        print('\n\nAutomatic initialisation [END] for subject {0} '
              '\n Complete atlas chart was {1} \n'.format(
                  target_chart_name, atlas_without_one))
示例#2
0
def spot_a_list_of_rabbits(subjects_list):

    for sj_target in subjects_list:
        print(
            '\nAutomatic segmentation with SPOT-A-NeonatalRabbit - subject {} started.\n'
            .format(sj_target))

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

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

        pfo_target = jph(root_study_rabbits, 'A_data', study, category,
                         sj_target, 'stereotaxic')

        # Parameter tag z-SPOT_<TAG>
        if study == 'W8':
            parameters_tag = 'P3'
            multi_atlas_subjects_list = multi_atlas_W8_subjects
            pfo_sj_atlas = jph(root_study_rabbits, 'A_MultiAtlas_W8',
                               sj_target)
            root_multi_atlas = root_atlas_W8
        elif study == 'ACS' or study == 'PTB' or study == 'TestStudy':
            parameters_tag = 'P2'
            multi_atlas_subjects_list = multi_atlas_subjects
            pfo_sj_atlas = jph(root_study_rabbits, 'A_MultiAtlas', sj_target)
            root_multi_atlas = root_atlas
        else:
            raise IOError(
                'Study for subject {} not feasible.'.format(sj_target))

        # parameters_tag = 'P2'

        if sj_parameters['in_atlas']:
            # SPOT only the rabbits not already in the atlas.
            print(
                'Subject {} already in atlas. No automatic segmentation needed'
                .format(sj_target))
            pfi_T1_segm_from_atlas = jph(pfo_sj_atlas, 'segm',
                                         '{}_segm.nii.gz'.format(sj_target))
            assert os.path.exists(pfi_T1_segm_from_atlas)
            cmd0 = 'mkdir -p {}'.format(jph(pfo_target, 'segm'))
            cmd1 = 'cp {} {}'.format(
                pfi_T1_segm_from_atlas,
                jph(pfo_target, 'segm', '{}_segm.nii.gz'.format(sj_target)))
            print_and_run(cmd0)
            print_and_run(cmd1)

            return

        # --- initialise the class spot:
        spot_sj = SpotDS(atlas_pfo=root_multi_atlas,
                         target_pfo=pfo_target,
                         target_name=sj_target,
                         parameters_tag=parameters_tag)
        """
        Parameters tag -> correspondence:
        'P1' -> Mono modal T1 + BFC on T1.
        'P2' -> Multi modal T1 affine only.
        """
        # Template parameters:
        spot_sj.atlas_name = 'MANRround3'  # Multi Atlas Newborn Rabbit
        spot_sj.atlas_list_charts_names = multi_atlas_subjects_list
        spot_sj.atlas_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.atlas_list_suffix_masks = [
            'roi_mask', 'roi_reg_mask', 'brain_mask'
        ]
        spot_sj.atlas_segmentation_suffix = 'segm'

        # Architecture names - default 'automatic'.
        spot_sj.arch_automatic_segmentations_name_folder = sj_parameters[
            'names_architecture']['final_segm_strx']

        # --- target parameters
        spot_sj.target_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.target_name = sj_target

        # --- Utils
        spot_sj.bfc_corrector_cmd = bfc_corrector_cmd
        spot_sj.num_cores_run = num_cores_run

        if sj_parameters['options_brain_mask']['method'] is None:
            use_slim_mask = False
        else:
            use_slim_mask = True

        if sj_parameters['category'] == 'ex_vivo' or sj_parameters[
                'category'] == 'ex_vivo01' or sj_parameters[
                    'category'] == 'ex_vivo02':
            # --- Propagator option
            spot_sj.atlas_reference_chart_name = '1305'

            spot_sj.propagation_options['Affine_modalities'] = ('T1', 'FA')
            spot_sj.propagation_options['Affine_reg_masks'] = (
                'T1', 'S0')  # if (), there is a single mask for all modalities
            spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
            spot_sj.propagation_options['Affine_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_modalities'] = (
            )  # ('T1', 'FA')  # if empty, no non-rigid step.
            spot_sj.propagation_options['N_rigid_reg_masks'] = (
            )  #('T1', 'S0')  # if [], same mask for all modalities
            spot_sj.propagation_options[
                'N_rigid_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
                'T1',
            )  # empty list no diff bfc. - PUT A COMMA IF ONLY ONE SUBJECT!!
            spot_sj.propagation_options[
                'N_rigid_parameters'] = ' -be 0.9 -ln 6 -lp 1  -smooR 0.07 -smooF 0.07 '
            spot_sj.propagation_options['N_rigid_same_mask_moving'] = False
            spot_sj.propagation_options[
                'N_reg_mask_target'] = 0  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options[
                'N_reg_mask_moving'] = 1  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options['Final_smoothing_factor'] = 0

        elif sj_parameters['category'] == 'in_vivo':
            # --- Propagator option
            spot_sj.atlas_reference_chart_name = '1305'

            spot_sj.propagation_options['Affine_modalities'] = ('T1', )
            spot_sj.propagation_options['Affine_reg_masks'] = (
                'T1', )  # if (), there is a single mask for all modalities
            spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
            spot_sj.propagation_options['Affine_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_modalities'] = (
            )  # if empty, no non-rigid step. - first attempt with only an affine step.
            spot_sj.propagation_options['N_rigid_reg_masks'] = (
            )  # if [], same mask for all modalities
            spot_sj.propagation_options[
                'N_rigid_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
            )  # empty list no diff bfc. - PUT A COMMA IF ONLY ONE SUBJECT!!
            spot_sj.propagation_options[
                'N_rigid_parameters'] = ' -be 0.9 -ln 6 -lp 1  -smooR 0.07 -smooF 0.07 '
            spot_sj.propagation_options['N_rigid_same_mask_moving'] = False
            spot_sj.propagation_options[
                'N_reg_mask_target'] = 0  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options[
                'N_reg_mask_moving'] = 1  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options['Final_smoothing_factor'] = 1

        elif sj_parameters['category'] == 'first_trial' or sj_parameters[
                'category'] == 'second_trial' or sj_parameters[
                    'category'] == 'third_trial':
            # --- Propagator option
            spot_sj.atlas_reference_chart_name = '12503'

            spot_sj.propagation_options['Affine_modalities'] = ('T1', 'FA')
            spot_sj.propagation_options['Affine_reg_masks'] = (
                'T1', 'S0')  # if (), there is a single mask for all modalities
            spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
            spot_sj.propagation_options['Affine_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_modalities'] = (
                'T1', )  # if empty, no non-rigid step.
            spot_sj.propagation_options['N_rigid_reg_masks'] = (
                'T1', )  # if [], same mask for all modalities
            spot_sj.propagation_options[
                'N_rigid_slim_reg_mask'] = use_slim_mask
            spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
            )  # empty list no diff bfc. - PUT A COMMA EVEN IF ONLY ONE SUBJECT!!
            spot_sj.propagation_options[
                'N_rigid_parameters'] = ' -be 0.8 -vel -smooR 0.07 -smooF 0.07 '
            spot_sj.propagation_options['N_rigid_same_mask_moving'] = False
            spot_sj.propagation_options[
                'N_reg_mask_target'] = 0  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options[
                'N_reg_mask_moving'] = 1  # 0 roi_mask, 1 reg_mask
            spot_sj.propagation_options['Final_smoothing_factor'] = 0

        else:
            raise IOError(
                'Given subject category not recognised for subject {} and category {}.'
                .format(sj_target, sj_parameters['category']))

        # --- Propagator controller
        spot_sj.propagation_controller['Aff_alignment'] = True
        spot_sj.propagation_controller['Propagate_aff_to_segm'] = True
        spot_sj.propagation_controller['Propagate_aff_to_mask'] = True
        spot_sj.propagation_controller['Get_N_rigid_slim_mask'] = True
        spot_sj.propagation_controller['Get_differential_BFC'] = True
        spot_sj.propagation_controller['N_rigid_alignment'] = True
        spot_sj.propagation_controller['Propagate_n_rigid'] = True
        spot_sj.propagation_controller['Smooth_results'] = True
        spot_sj.propagation_controller['Stack_warps_and_segms'] = True

        # --- Fuser option
        spot_sj.fuser_options['Fusion_methods'] = [
            'MV',
            'STAPLE',
            'STEPS',
        ]  # ['STAPLE', 'STEPS', ]  # 'STAPLE', 'STEPS'
        spot_sj.fuser_options['STAPLE_params'] = OrderedDict([('pr1', None)])
        spot_sj.fuser_options['STEPS_params'] = OrderedDict([
            ('pr{0}.{1}'.format(k, n), [k, n, 4]) for n in [9]
            for k in [5, 11]
        ])
        # --- Fuser controller
        spot_sj.fuser_controller['Fuse'] = True
        spot_sj.fuser_controller['Save_results'] = True

        spot_sj.spot_on_target_initialise()

        t = time.time()

        spot_sj.propagate()
        spot_sj.fuse()

        elapsed = time.time() - t

        print('Time to spot subject {} is : {}'.format(sj_target, elapsed))
from collections import OrderedDict
from os.path import join as jph

from spot.tools.system_parameters import bfc_corrector_cmd
from spot.spotter import SpotDS

if __name__ == '__main__':

    root_rabbit = '/Volumes/sebastianof/rabbits'

    # --- initialise the class spot:
    target_name = '4501'
    spot_sj = SpotDS(
        atlas_pfo=jph(root_rabbit,
                      'A_atlas'),  # here path to the folder with the atlas.
        target_pfo=jph(
            root_rabbit,
            'A_data/PTB/ex_vivo/{0}/stereotaxic'.format(target_name)),
        target_name=target_name,
        parameters_tag='test')

    # Template parameters:
    spot_sj.atlas_name = 'MANRround3'  # Multi Atlas Newborn Rabbit
    spot_sj.atlas_list_charts_names = [
        '1201', '1203', '1305', '1404', '1507', '1510', '1702', '1805', '2002',
        '2502', '3301', '3404'
    ]
    spot_sj.atlas_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
    spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
    spot_sj.atlas_reference_chart_name = '1305'
    spot_sj.atlas_segmentation_suffix = 'approved_round3'
        for k in sp.fuser_controller.keys():
            f.write('{0:<40} : {1} \n'.format(k, sp.fuser_controller[k]))

        # f.write('\n--- Stereotaxic alignment options:\n')
        # for k in sp.stereotaxic_alignment_options.keys():
        #     f.write('{0:<40} : {1} \n'.format(k, sp.stereotaxic_alignment_options[k]))
        #
        # f.write('\n--- Stereotaxic alignment controller:\n')
        # for k in sp.stereotaxic_alignment_controller.keys():
        #     f.write('{0:<40} : {1} \n'.format(k, sp.stereotaxic_alignment_controller[k]))


if __name__ == '__main__':
    from spot.spotter import SpotDS
    from collections import OrderedDict
    sp = SpotDS('', '', '', '')  # dummy test!
    # extra utils:
    sp.bfc_corrector_cmd = ''
    sp.num_cores_run = 8

    # Output tagging and intermediate files
    sp.parameters_tag = 'OP1'
    sp.target_scaffoldings_folder_name = sp.arch_scaffoldings_name_folder + '_' + sp.parameters_tag
    sp.scaffoldings_pfo = '/Users/sebastiano/Desktop/test_scaffoldings'
    sp.stack_anatomies_pfi = jph(sp.scaffoldings_pfo, 'res_4d_seg.nii.gz')
    sp.stack_segmentations_pfi = jph(sp.scaffoldings_pfo, 'res_4d_warp.nii.gz')

    # Options and controller for propagation, fuser and stereotaxic alignment:
    sp.propagation_options = OrderedDict({
        'Rigid_modalities': ['T1', 'S0inT1', 'FAinT1'],
        'Rigid_masks': ['T1', 'S0inT1', 'S0inT1'],
示例#5
0
PATH_MULTI_ATLAS = jph(PATH_DIR, 'MultiAtlas')
PATH_TARGETS = jph(PATH_DIR, 'Targets')
MULTI_ATLAS_NAME_PREFIX = 'sj'
TARGET_NAME_SUFFIX = 'ta'
N = 8
RS = 0.3
RN = 0.4

if __name__ == '__main__':
    global PATH_DIR
    generate_phantom_dataset(PATH_DIR)

    # --- initialise the class spot:
    target_name = '{}01'.format(TARGET_NAME_SUFFIX)
    spot_sj = SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
                     target_pfo=PATH_TARGETS,
                     target_name=target_name,
                     parameters_tag='MyTag')

    # Template parameters:
    spot_sj.atlas_name = 'test'  # Multi Atlas Newborn Rabbit
    spot_sj.atlas_list_charts_names = [
        MULTI_ATLAS_NAME_PREFIX + str(n + 1).zfill(len(str(N)) + 1)
        for n in range(N)
    ]
    spot_sj.atlas_list_suffix_modalities = ['mod1', 'mod2']
    spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
    spot_sj.atlas_reference_chart_name = 'sj02'
    spot_sj.atlas_segmentation_suffix = 'segm_GT'

    # --- target parameters
    spot_sj.target_list_suffix_modalities = ['mod1', 'mod2']
def test_standard_experiment_with_phantom():

    global PATH_DIR
    generate_phantom_dataset(PATH_DIR)

    # # --- initialise the class spot:
    target_name = '{}01'.format(TARGET_NAME_SUFFIX)
    spot_sj = SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
                     target_pfo=PATH_TARGETS,
                     target_name=target_name,
                     parameters_tag='MyTag')

    # Template parameters:
    spot_sj.atlas_name = 'test'  # Multi Atlas Newborn Rabbit
    spot_sj.atlas_list_charts_names = [
        MULTI_ATLAS_NAME_PREFIX + str(n + 1).zfill(len(str(N)) + 1)
        for n in range(N)
    ]
    spot_sj.atlas_list_suffix_modalities = ['mod1', 'mod2']
    spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
    spot_sj.atlas_reference_chart_name = 'sj02'
    spot_sj.atlas_segmentation_suffix = 'segm_GT'

    # --- target parameters
    spot_sj.target_list_suffix_modalities = ['mod1', 'mod2']

    # --- Utils
    spot_sj.bfc_corrector_cmd = bfc_corrector_cmd

    # --- Propagator option
    spot_sj.propagation_options['Affine_modalities'] = ('mod1', 'mod2')
    spot_sj.propagation_options['Affine_reg_masks'] = (
    )  # if (), there is a single mask for all modalities
    spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
    spot_sj.propagation_options['N_rigid_modalities'] = (
        'mod1', 'mod2')  # if empty, no non-rigid step.
    spot_sj.propagation_options['N_rigid_reg_masks'] = (
    )  # if (), same mask for all modalities
    spot_sj.propagation_options['N_rigid_slim_reg_mask'] = True
    spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
        'mod2', )  # empty list no diff bfc. - put a comma!!
    spot_sj.propagation_options[
        'N_rigid_parameters'] = ' -vel -be 0.5 -ln 6 -lp 2  -smooR 1.5 -smooF 1.5 '
    spot_sj.propagation_options['N_rigid_same_mask_moving'] = True
    spot_sj.propagation_options['Final_smoothing_factor'] = 0

    # --- Propagator controller
    spot_sj.propagation_controller['Aff_alignment'] = True
    spot_sj.propagation_controller['Propagate_aff_to_segm'] = True
    spot_sj.propagation_controller['Propagate_aff_to_mask'] = True
    spot_sj.propagation_controller['Get_N_rigid_slim_mask'] = True
    spot_sj.propagation_controller['Get_differential_BFC'] = True
    spot_sj.propagation_controller['N_rigid_alignment'] = True
    spot_sj.propagation_controller['Propagate_n_rigid'] = True
    spot_sj.propagation_controller['Smooth_results'] = True
    spot_sj.propagation_controller['Stack_warps_and_segms'] = True

    # --- Fuser option
    spot_sj.fuser_options['Fusion_methods'] = ['MV', 'STAPLE', 'STEPS']
    spot_sj.fuser_options['Tp_mod_to_stack'] = 0
    spot_sj.fuser_options['STAPLE_params'] = OrderedDict([('pr1', None)])
    spot_sj.fuser_options['STEPS_params'] = OrderedDict([
        ('pr{0}_{1}'.format(k, n), [k, n, 4]) for n in [9] for k in [5, 11]
    ])
    # --- Fuser controller
    spot_sj.fuser_controller['Fuse'] = True
    spot_sj.fuser_controller['Save_results'] = True

    if not os.path.exists(
            jph(PATH_TARGETS, spot_sj.arch_segmentations_name_folder,
                spot_sj.arch_automatic_segmentations_name_folder)):
        spot_sj.spot_on_target_initialise()
        spot_sj.propagate()
        spot_sj.fuse()
    #
    pfi_ground_truth_segm = jph(
        PATH_TARGETS, spot_sj.arch_segmentations_name_folder,
        '{0}_{1}.nii.gz'.format(target_name,
                                spot_sj.atlas_segmentation_suffix))
    pri_result_MV = jph(
        PATH_TARGETS, spot_sj.arch_segmentations_name_folder,
        spot_sj.arch_automatic_segmentations_name_folder,
        '{0}_{1}_{2}.nii.gz'.format(target_name, 'MV', spot_sj.parameters_tag))
    im_segm_gt = nib.load(pfi_ground_truth_segm)
    im_segm_mv = nib.load(pri_result_MV)

    assert global_dice_score(im_segm_gt, im_segm_mv) > 0.9
def test_standard_experiments_with_phantom_broken_input_tag():

    global PATH_DIR
    generate_phantom_dataset(PATH_DIR)

    target_name = '{}01'.format(TARGET_NAME_SUFFIX)

    with npt.assert_raises(TypeError):
        SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
               target_pfo=PATH_TARGETS,
               target_name=target_name,
               parameters_tag=None)  # error here

    spot_sj = SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
                     target_pfo=PATH_TARGETS,
                     target_name=target_name,
                     parameters_tag='')  # error here

    spot_sj.spot_on_target_initialise()
    with npt.assert_raises(IOError):
        spot_sj.propagate()

    spot_sj = SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
                     target_pfo=PATH_TARGETS,
                     target_name=target_name,
                     parameters_tag='a_b')  # error here

    spot_sj.spot_on_target_initialise()
    with npt.assert_raises(IOError):
        spot_sj.propagate()
示例#8
0
def run_update_automatic_segmentation_for_atlas():

    pfo_multi_atlas_third_round = jph(path_manager.pfo_root,
                                      'C_atlas_third_round')

    assert os.path.exists(pfo_multi_atlas_third_round)

    atlas_subjects = path_manager.atlas_subjects

    # atlas flipped created with benchmarking/cross_validation/flipper
    atlas_charts_flipped = [sj + 'flip' for sj in atlas_subjects]

    atlas_chart_complete = [
        item for pair in zip(atlas_subjects, atlas_charts_flipped + [0])
        for item in pair
    ]

    for target_name in atlas_subjects:

        print(
            '\n\nAutomatic initialisation [START]: \nSub-template is {0} \n Target is {1} \n Spot target started!!\n'
            '\n'.format(atlas_chart_complete, target_name))

        atlas_pfo = pfo_multi_atlas_third_round,
        target_pfo = jph(pfo_multi_atlas_third_round, target_name)

        spot_sj = SpotDS(atlas_pfo=atlas_pfo,
                         target_pfo=target_pfo,
                         target_name=target_name,
                         parameters_tag='AutoRound3')

        # Template parameters:
        spot_sj.atlas_name = 'AutoRound3'  # Automatic round 3
        spot_sj.atlas_list_charts_names = atlas_chart_complete
        spot_sj.atlas_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
        spot_sj.atlas_reference_chart_name = '1305'
        spot_sj.atlas_segmentation_suffix = 'approved_round3'

        # --- target parameters
        spot_sj.target_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.target_name = target_name

        # --- Utils
        spot_sj.bfc_corrector_cmd = bfc_corrector_cmd

        # --- Propagator option
        spot_sj.propagation_options['Affine_modalities'] = ('T1', 'FA')
        spot_sj.propagation_options['Affine_reg_masks'] = (
            'T1', 'S0')  # if (), there is a single mask for all modalities
        spot_sj.propagation_options['Affine_parameters'] = ' -speeeeed '
        spot_sj.propagation_options['N_rigid_modalities'] = (
            'T1', 'FA')  # if empty, no non-rigid step.
        spot_sj.propagation_options['N_rigid_reg_masks'] = (
            'T1', 'S0')  # if [], same mask for all modalities
        spot_sj.propagation_options['N_rigid_slim_reg_mask'] = True
        spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = (
            'T1', )  # empty list no diff bfc. - put a comma!!
        spot_sj.propagation_options[
            'N_rigid_parameters'] = '  -be 0.5 -ln 6 -lp 4  -smooR 1.5 -smooF 1.5 '
        spot_sj.propagation_options['N_rigid_same_mask_moving'] = True
        spot_sj.propagation_options['Final_smoothing_factor'] = 0

        # --- Propagator controller
        spot_sj.propagation_controller['Aff_alignment'] = False
        spot_sj.propagation_controller['Propagate_aff_to_segm'] = False
        spot_sj.propagation_controller['Propagate_aff_to_mask'] = False
        spot_sj.propagation_controller['Get_N_rigid_slim_mask'] = True
        spot_sj.propagation_controller['Get_differential_BFC'] = True
        spot_sj.propagation_controller['N_rigid_alignment'] = True
        spot_sj.propagation_controller['Propagate_n_rigid'] = True
        spot_sj.propagation_controller['Smooth_results'] = True
        spot_sj.propagation_controller['Stack_warps_and_segms'] = True

        # --- Fuser option
        spot_sj.fuser_options['Fusion_methods'] = ['MV']  # 'MV', 'STAPLE',
        spot_sj.fuser_options['Tp_mod_to_stack'] = 0
        spot_sj.fuser_options['STAPLE_params'] = OrderedDict([('pr1', None)])
        spot_sj.fuser_options['STEPS_params'] = OrderedDict([
            ('pr{0}_{1}'.format(k, n), [k, n, 4]) for n in [9]
            for k in [5, 11]
        ])
        # --- Fuser controller
        spot_sj.fuser_controller['Fuse'] = True
        spot_sj.fuser_controller['Save_results'] = True

        spot_sj.spot_on_target_initialise()
        spot_sj.propagate()
        spot_sj.fuse()

        print('\n\nAutomatic initialisation [END] for subject {0} '
              '\n Complete atlas chart was {1} \n'.format(
                  target_name, atlas_chart_complete))
def run_generate_automatic_segmentation_leave_one_out():

    assert os.path.exists(path_manager.pfo_atlas_validation_leave_one_out)

    # atlas flipped created with benchmarking/cross_validation/flipper
    atlas_charts_flipped = [sj + 'flip' for sj in path_manager.atlas_subjects]

    atlas_chart_complete = [item
                            for pair in zip(path_manager.atlas_subjects, atlas_charts_flipped + [0]) for item in pair]

    for target_chart_name in path_manager.atlas_subjects:

        sub_atlas_charts_with_flipped = np.sort(list(set(atlas_chart_complete) -
                                                     {target_chart_name, target_chart_name + 'flip'}))

        print('\n\nLeave one out segmentation [START]:\nSub-template is {0}\nTarget is {1}\n Spot target started!!\n'
              '\n'.format(sub_atlas_charts_with_flipped, target_chart_name))

        assert target_chart_name not in sub_atlas_charts_with_flipped

        spot_sj = SpotDS(atlas_pfo=path_manager.pfo_atlas_validation_leave_one_out,
                         target_pfo=jph(path_manager.pfo_atlas_validation_leave_one_out, target_chart_name),
                         target_name=target_chart_name,
                         parameters_tag='CrossValidation')

        # Template parameters:
        spot_sj.atlas_name = 'CrossValidationWithFlipped'  # Multi Atlas Newborn Rabbit
        spot_sj.atlas_list_charts_names = sub_atlas_charts_with_flipped
        spot_sj.atlas_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.atlas_list_suffix_masks = ['roi_mask', 'roi_reg_mask']
        spot_sj.atlas_reference_chart_name = '1305'
        spot_sj.atlas_segmentation_suffix = 'segm'

        # --- target parameters
        spot_sj.target_list_suffix_modalities = ['T1', 'S0', 'V1', 'MD', 'FA']
        spot_sj.target_name = target_chart_name

        # --- Utils
        spot_sj.bfc_corrector_cmd = path_manager.bfc_corrector_cmd

        # --- Propagator option
        spot_sj.propagation_options['Affine_modalities'] = ('T1', 'FA')
        spot_sj.propagation_options['Affine_reg_masks'] = ()  # if (), there is a single mask for all mods
        spot_sj.propagation_options['Affine_parameters'] = ' '
        spot_sj.propagation_options['N_rigid_modalities'] = ('T1', 'FA')  # if empty, no non-rigid step.
        spot_sj.propagation_options['N_rigid_reg_masks'] = ()  # if [], same mask for all modalities
        spot_sj.propagation_options['N_rigid_slim_reg_mask'] = False
        spot_sj.propagation_options['N_rigid_mod_diff_bfc'] = ('T1',)  # empty list no diff bfc. - put a comma!!
        spot_sj.propagation_options['N_rigid_parameters'] = '  -vel -be 0.5 -ln 6 -lp 4  -smooR 0.07 -smooF 0.07  '
        spot_sj.propagation_options['N_rigid_same_mask_moving'] = False
        spot_sj.propagation_options['N_reg_mask_target'] = 0  # 0 roi_mask, 1 reg_mask
        spot_sj.propagation_options['N_reg_mask_moving'] = 1  # 0 roi_mask, 1 reg_mask
        spot_sj.propagation_options['Final_smoothing_factor'] = 0

        # --- Propagator controller
        spot_sj.propagation_controller['Aff_alignment']         = True
        spot_sj.propagation_controller['Propagate_aff_to_segm'] = True
        spot_sj.propagation_controller['Propagate_aff_to_mask'] = True
        spot_sj.propagation_controller['Get_N_rigid_slim_mask'] = True
        spot_sj.propagation_controller['Get_differential_BFC']  = True
        spot_sj.propagation_controller['N_rigid_alignment']     = True
        spot_sj.propagation_controller['Propagate_n_rigid']     = True
        spot_sj.propagation_controller['Smooth_results']        = True
        spot_sj.propagation_controller['Stack_warps_and_segms'] = True

        # --- Fuser option
        spot_sj.fuser_options['Fusion_methods'] = ['MV']  # 'MV', multi atlas only.
        spot_sj.fuser_options['Tp_mod_to_stack'] = 0
        spot_sj.fuser_options['STAPLE_params'] = OrderedDict([('pr_1', None)])
        spot_sj.fuser_options['STEPS_params'] = OrderedDict([('pr_{0}_{1}'.format(k, n), [k, n, 0.4])
                                                                  for n in [5, 7, 9]
                                                                  for k in [5,  11]])
        # --- Fuser controller
        spot_sj.fuser_controller['Fuse'] = True
        spot_sj.fuser_controller['Save_results'] = True

        spot_sj.spot_on_target_initialise()
        spot_sj.propagate()
        spot_sj.fuse()

        print('\n\nAutomatic initialisation [END] for subject {0} '
              '\n Complete atlas chart was {1} \n'.format(target_chart_name, sub_atlas_charts_with_flipped))