Skip to content

Examples

Sebastiano Ferraris edited this page Nov 19, 2019 · 11 revisions

Miscellaneous examples

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

import nilabels as nis

nis_app = nis.App()
nis_app.manipulate_labels.keep_one_label(
    'my_segm.nii.gz', 
    'my_segm_only203.nii.gz', 
    203
)

Given the segmentation 'my_segm.nii.gz', permute labels [1,2,3,4,5] with labels [2,1,3,5,4].

import nilabels as nis

nis_app = nis.App()
nis_app.manipulate_labels.permute_labels(
    'my_segm.nii.gz', 
     path_to_output_segmentation='my_new_segm.nii.gz', 
     permutation=[[1,2,3,4,5],[2,1,3,5,4]]
)

As in the previous example, permute also the labels descriptor text file (can be in itk-snap convention or fsl convention).

import nilabels as nis

nis_app = nis.App()
nis_app.manipulate_labels.labels_descriptor_convention = 'itk-snap'  # can be 'itk-snap' (default) or 'fsl'.

nis_app.manipulate_labels.permute_labels(
    'my_segm.nii.gz', 
    path_to_output_segmentation='my_new_segm.nii.gz', 
    permutation=[[1,2,3,4,5],[2,1,3,5,4]], 
    path_to_input_labels_descriptor='input_label_descriptor.txt',
    path_to_output_labels_descriptor='label_descriptor_after_permutation.txt'
)

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

import nilabels as nis

nis_app = nis.App()
nis_app.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:

import nilabels as nis
from nilabels.tools.aux_methods.label_descriptor_manager import LabelsDescriptorManager as LDM

nis_app = nis.App()

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])

nis_app.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
nis.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'

import nilabels as nis

nis_app = nis.App()
nis_app.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'
)