-
Notifications
You must be signed in to change notification settings - Fork 3
Examples
To run an example based on a synthetic Multi-Atlas created with LABelsToolkit, run the testing routine with nosetests after installing the library in the virtual environment as proposed in the README.md file.
In alternative run the following code, specifying the destination where the synthetic multi-atlas will be created.
import os
from os.path import join as jph
from LABelsToolkit.tools.phantoms_generator.generate_phantom_multi_atlas import generate_multi_atlas_at_folder, \
generate_atlas_at_folder
# parameters
PATH_DIR = <path to directory where to create the synthetic multi-atlas and the target>
PATH_MULTI_ATLAS = jph(PATH_DIR, 'MultiAtlas')
PATH_TARGETS = jph(PATH_DIR, 'Targets')
MULTI_ATLAS_NAME_PREFIX = 'sj'
TARGET_NAME_SUFFIX = 'ta'
N = 8 # Number of subjects in the multi-atlas
RS = 0.3 # random parameter for the phantom shape from 0 to 1.
RN = 0.4 # random parameter for the noise added to each subject, from 0 to 1.
def generate_phantom_dataset(path_dir):
if not os.path.exists(jph(path_dir)):
print('\n\nGenerating dataset for testing: phantom multi-atlas and phantom target in {}. '
'Will take some minutes.'.format(path_dir))
os.system('mkdir {}'.format(path_dir))
os.system('mkdir {}'.format(jph(path_dir, 'MultiAtlas')))
os.system('mkdir {}'.format(jph(path_dir, 'Targets')))
generate_multi_atlas_at_folder(jph(path_dir, 'MultiAtlas'), number_of_subjects=N,
multi_atlas_root_name=MULTI_ATLAS_NAME_PREFIX,
randomness_shape=RS, randomness_noise=RN)
generate_atlas_at_folder(jph(path_dir, 'Targets'), atlas_name='{}01'.format(TARGET_NAME_SUFFIX),
randomness_shape=RS, randomness_noise=RN)
generate_phantom_dataset(PATH_DIR)
After generating the synthetic dataset, instantiate a class of SpotDS (segmentation propagation on target data structure).
spot_sj = SpotDS(atlas_pfo=PATH_MULTI_ATLAS,
target_pfo=PATH_TARGETS,
target_name='{}01'.format(TARGET_NAME_SUFFIX),
parameters_tag='MyTag')
Then modify the instance spot_sj with the parameters for the intended atlas and target.
# 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']
Select the path where the bias field corrector command niftkMTPDbc, if you intend to use the differential bias field correction in the non-rigid registration step.
bfc_corrector_cmd = '/Applications/.../niftkMTPDbc'
spot_sj.bfc_corrector_cmd = bfc_corrector_cmd
Assign the propagation options values (for debugging, some of the steps can be bypassed, setting the controller to false where needed) managing the propagation of the segmentations (see NiftyReg documentation).
# --- 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
Assign the fusion options values (for debugging, some of the steps can be bypassed, setting the controller to false where needed) managing the label fusion part (see NiftySeg documentation).
# --- 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
Once the parameters are set up, the initialisation, propagation and label fusion can be performed with
spot_sj.spot_on_target_initialise()
spot_sj.propagate()
spot_sj.fuse()
At the end of the process, in the segm folder of the Target, an automatic folder should have been created with the automatic segmentation propagation provided with the selected methods.