Skip to content

Examples

Sebastiano Ferraris edited this page Aug 20, 2018 · 11 revisions

Miscellaneous examples

Erase all the labels of the segmentation 'my_segm.nii.gz' but the label 203:

from LABelsToolkit.main import LABelsToolkit as LaB

la = LaB()
la.manipulate_labels.keep_one_label('my_segm.nii.gz', 'my_segm_only203.nii.gz', 203)

Given the segmentation ''

Get a report with the numbers of connected components of the segmentation 'my_segm.nii.gz' in the log text file 'log.txt':

from LABelsToolkit.main import LABelsToolkit as LaB

la = LaB()
la.check.number_connected_components_per_label('my_segm.nii.gz',
                                               where_to_save_the_log_file='log.txt')

Given the segmentation 'my_segm.nii.gz' provided with a label descriptor under 'my_labels_descriptor.txt' clean it removing all the connected components of each label, that are not connected with the largest connected component:

from LABelsToolkit.main import LABelsToolkit as LaB
from LABelsToolkit.tools.aux_methods.label_descriptor_manager import LabelsDescriptorManager as LDM


la = LaB()
ldm = LDM('my_labels_descriptor.txt')

labels_from_labels_descriptor = ldm.get_dict_itk_snap().keys()

correspondences_lab_comps  = []
for lab in labels_from_labels_descriptor:
    if lab == 203:
        # Admit 3 connected components for label 203.
        correspondences_lab_comps.append([lab, 3])
    else:
        # Admit only 1 connected component for all the other labels.
        correspondences_lab_comps.append([lab, 1])

la.manipulate_labels.clean_segmentation('my_segm.nii.gz', 'my_output_cleaned_segm.nii.gz',
                                        labels_to_clean=correspondences_lab_comps, force_overwriting=False)

# get a report of the connected components after cleaning
lt.check.number_connected_components_per_label('my_output_cleaned_segm.nii.gz',
                                               where_to_save_the_log_file='log.txt')

Given a segmentation of the left side of a symmetric anatomy, create the other half through flipping and rigid registration:

The input anatomy is 'my_anatomy.nii.gz' and its segmentation is 'my_segm.nii.gz' labels in the centre are [1, 2, 3, 4, 5], labels in the left side are [6, 8, 10, 12, 14] and corresponding labels on the other side are [7, 9, 11, 13, 15]. Output is saved under 'my_segm_SYM.nii.gz' and intermediate files are saved in the folder 'z_tmp'

from LABelsToolkit.main import LABelsToolkit as LaB

la = LaB()
la.symmetrize.symmetrise_with_registration('my_anatomy.nii.gz',
                                           'my_segm.nii.gz',
                                           labels_sym_left=[1, 2, 3, 4, 5] + [6, 8, 10, 12, 14],
                                           'my_segm_SYM.nii.gz',
                                           results_folder_path=z_tmp,
                                           list_labels_transformed=[1, 2, 3, 4, 5] + [7, 9, 11, 13, 15],
                                           coord='z')
Clone this wiki locally