FastSurferCNN.utils.mapper

Original Author: David Kuegler.

Date: Aug-19-2022

class FastSurferCNN.utils.mapper.ColorLookupTable(classes=None, color_palette=None, colormap='gist_ncar', name=None)[source]

This class provides utility in creating color palettes from colormaps.

Attributes

classes

Return the classes.

color_palette

Return the color palette if it exists.

name

Return the name of the mapper.

Methods

colormap()

Generate a Mapper object that maps classes to their corresponding colors.

get_color_by_index(index[, base])

Return the color (r, g, b, a) tuple associated with the index in the passed base.

getitem_by_index(index)

Return index, key, colors and additional values for the key.

labelname2id()

Return a mapping between the key and the value it is associated with.

labelname2index()

Return a mapping between the key and the (consecutive) index it is associated with.

colormap()[source]

Generate a Mapper object that maps classes to their corresponding colors.

get_color_by_index(index, base=1.0)[source]

Return the color (r, g, b, a) tuple associated with the index in the passed base.

getitem_by_index(index)[source]

Return index, key, colors and additional values for the key.

labelname2id()[source]

Return a mapping between the key and the value it is associated with.

Mapper[KT, Any]

Not implemented in the base class.

Raises:
RuntimeError

If no value is associated.

labelname2index()[source]

Return a mapping between the key and the (consecutive) index it is associated with.

This is the inverse of ColorLookupTable.classes.

property classes

Return the classes.

property color_palette

Return the color palette if it exists.

property name

Return the name of the mapper.

class FastSurferCNN.utils.mapper.JsonColorLookupTable(file_or_buffer, color_palette=None, colormap='gist_ncar', name=None)[source]

This class extends the ColorLookupTable to handle JSON data.

Attributes

classes

Return the classes.

color_palette

Return the color palette if it exists.

name

Return the name of the mapper.

Methods

colormap()

Generate a Mapper object that maps classes to their corresponding colors.

dataframe()

Converts the labels from the internal data dictionary to a pandas DataFrame.

get_color_by_index(index[, base])

Return the color (r, g, b, a) tuple associated with the index in the passed base.

getitem_by_index(index)

Return index, key, colors and additional values for the key.

labelname2id()

Return a mapping between the key and the value it is associated with.

labelname2index()

Return a mapping between the key and the (consecutive) index it is associated with.

dataframe()[source]

Converts the labels from the internal data dictionary to a pandas DataFrame.

labelname2id()[source]

Return a mapping between the key and the value it is associated with.

Returns:
Mapper[KT, Any]

A Mapper object that provides a mapping between label names (keys) and their corresponding IDs (values).

Raises:
RuntimeError

If no value is associated.

class FastSurferCNN.utils.mapper.Mapper(mappings, name='undefined')[source]

Map from one label space to a generic ‘label’-space.

Attributes

max_label

Return the max label.

name

Return the name of the mapper.

source_space

Return a set of labels the mapper accepts.

target_space

Return the set of labels the mapper converts to as a set of python-natives (if possible), arrays expanded to tuples.

Methods

__call__(image, label_image)

Transform a dataset from prediction to internal space for sets of image and segmentation.

chain(other_mapper)

Chain the current mapper with the other_mapper.

is_bijective()

Return, whether the Mapper is bijective.

make_classmapper(mappings[, keep_labels, ...])

Map from one label space (int) to another (also int) using a mappings function.

map(image[, out])

Forward map the labels from prediction to internal space.

reversed_dict()

Map dictionary from the target space to the source space.

update(other[, overwrite])

Merge another map into this mapper.

map_logits

map_probs

chain(other_mapper)[source]

Chain the current mapper with the other_mapper.

This effectively is an optimization to first applying this mapper and then applying the other_mapper.

Parameters:
other_mapper“Mapper[VT, T_OtherValue]”

Mapper mapping from the target-space of this mapper to a new space.

Returns:
Mapper“Mapper[KT, T_OtherValue]”

A mapper mapping from the input space of this mapper to the target-space of the other_mapper.

is_bijective()[source]

Return, whether the Mapper is bijective.

classmethod make_classmapper(mappings, keep_labels=(), compress_out_space=False, name='undefined')[source]

Map from one label space (int) to another (also int) using a mappings function.

Can also be used as a transform.

Creates a Mapper object from a mappings dictionary and a list of labels to keep.

Parameters:
mappingsDict[int, int]

A dictionary of labels from -> to mappings.

keep_labelsSequence[int]

A list of classes to keep after mapping, where all not included classes are not changed (default: empty).

compress_out_spacebool

Whether to reassign labels to reduce the maximum label (default: False).

namestr

Mame for messages (default: “undefined”).

Returns:
“Mapper[int, int]”
A Mapper object that provides a mapping from one label space to another.
Raises:
ValueError

If keep_labels contains an entry > 65535.

map(image, out=None)[source]

Forward map the labels from prediction to internal space.

Parameters:
imageAT

Data to map to internal space.

outOptional[AT]

Output array for performance. Returns an numpy.ndarray with mapped values. (Default value = None).

Returns:
AT

Data after being mapped to the internal space.

reversed_dict()[source]

Map dictionary from the target space to the source space.

update(other, overwrite=True)[source]

Merge another map into this mapper.

Parameters:
otherMapper[KT, VT]

The other Mapper object whose key-value pairs are to be added to this Mapper object.

overwritebool, default=True

Flag to overwrite value if key already exists in Mapper object (Default value = True).

Returns:
Mapper[KT, VT]

Mapper after merge.

property max_label

Return the max label.

property name

Return the name of the mapper.

property source_space

Return a set of labels the mapper accepts.

property target_space

Return the set of labels the mapper converts to as a set of python-natives (if possible), arrays expanded to tuples.

class FastSurferCNN.utils.mapper.TSVLookupTable(file_or_buffer, name=None, header=False, add_background=True)[source]

This class extends the ColorLookupTable to handle TSV (Tab Separated Values) data.

Attributes

classes

Return the classes.

color_palette

Return the color palette if it exists.

name

Return the name of the mapper.

Methods

colormap()

Generate a Mapper object that maps classes to their corresponding colors.

dataframe()

Return the raw panda data object.

get_color_by_index(index[, base])

Return the color (r, g, b, a) tuple associated with the index in the passed base.

getitem_by_index(index)

Find the Entry associated by a No.

labelname2id()

Return a Mapper between the key and the value it is associated with.

labelname2index()

Return a mapping between the key and the (consecutive) index it is associated with.

dataframe()[source]

Return the raw panda data object.

getitem_by_index(index)[source]

Find the Entry associated by a No.

Parameters:
indexint

The index Returns a tuple of the index, the label, and a tuple of the RGBA color label.

Returns:
indexint

The index of the entry.

keystr

The label name associated with the entry.

colorTuple[int, int, int, int]

The RGBA color label associated with the entry.

int

The data index associated with the entry.

labelname2id()[source]

Return a Mapper between the key and the value it is associated with.

Returns:
Mapper[KT, Any]

A Mapper object that links keys to their corresponding values based on the class and data index.

Raises:
RuntimeError

If no value is associated.

FastSurferCNN.utils.mapper.is_int(a_object)[source]

Check whether the array_or_tensor is an integer.

Parameters:
a_objectAny

The object to check its type.

Returns:
bool

Whether the data type of the object is int or not.

FastSurferCNN.utils.mapper.to_same_type(data, type_hint)[source]

Convert data to the same type as type_hint.

Parameters:
dataAny

The data to convert.

type_hintAT

Hint for the data type.

Returns:
AT

Data converted to the same type as specified by type_hint.