firelight.visualizers package

firelight.visualizers.base module

class firelight.visualizers.base.BaseVisualizer(input=None, input_mapping=None, colorize=True, cmap=None, background_label=None, background_color=None, opacity=1.0, colorize_jointly=None, value_range=None, verbose=False, scaling_options=None, **super_kwargs)[source]

Bases: firelight.utils.dim_utils.SpecFunction

Base class for all visualizers. If you want to use outputs of other visualizers, derive from ContainerVisualizer instead.

Parameters
  • input (list or None) – If the visualizer has one input only, this can be used to specify which state to pass (in the format of a value in input_mapping).

  • input_mapping (dict or list) – Dictionary specifying slicing and renaming of states for visualization (see apply_slice_mapping()).

  • colorize (bool) – If False, the addition/rescaling of a ‘Color’ dimension to RGBA in [0,1] is suppressed.

  • cmap (str or callable) –

    If string, specifies the name of the matplotlib colormap to be used for colorization.

    If callable, must be a mapping from a [Batch x Pixels] to [Batch x Pixels x Color] numpy.ndarray used for colorization.

  • background_label (int or float) – If specified, pixels with this value (after visualize()) will be colored with background_color.

  • background_color (float or list) – Specifies the color for the background_label. Will be interpreted as grey-value if float, and RGB or RGBA if list of length 3 or 4 respectively.

  • opacity (float) – Opacity of visualization, see colorization.py.

  • colorize_jointly (list of str) –

    A list containing names of dimensions. Sets of data points separated only in these dimensions will be scaled equally at colorization (such that they lie in [0, 1]). Not used if ‘value_range’ is specified.

    Default: ['W', 'H', 'D'] (standing for Width, Height, Depth)

    Examples:

    • color_jointly = ['W', 'H'] : Scale each image separately

    • color_jointly = ['B', 'W', 'H'] : Scale images corresponding to different samples in the batch equally, such that their intensities are comparable

  • value_range (List) –

    If specified, the automatic scaling for colorization is overridden. Has to have 2 elements. The interval [value_range[0], value_range[1]] will be mapped to [0, 1] by a linear transformation.

    Examples:

    • If your network has the sigmoid function as a final layer, the data does not need to be scaled further. Hence value_range = [0, 1] should be specified.

    • If your network produces outputs normalized between -1 and 1, you could set value_range = [-1, 1].

  • verbose (bool) – If true, information about the state dict will be printed during visualization.

  • **super_kwargs – Arguments passed to the constructor of SpecFunction, above all the dimension names of inputs and output of visualize()

__call__(return_spec=False, **states)[source]

Visualizes the data specified in the state dictionary, following these steps:

  • Apply the input mapping (using apply_input_mapping()),

  • Reshape the states needed for visualization as specified by in_specs at initialization. Extra dimensions are ‘put into’ the batch dimension, missing dimensions are added (This is handled in the base class, firelight.utils.dim_utils.SpecFunction)

  • Apply visualize(),

  • Reshape the result, with manipulations applied on the input in reverse,

  • If not disabled by setting colorize=False, colorize the result, leading to RGBA output with values in \([0, 1]\).

Parameters
  • return_spec (bool) – If true, a list containing the dimension names of the output is returned additionally

  • states (dict) – Dictionary including the states to be visualized.

Returns

result (torch.Tensor or (torch.Tensor, list)) – Either only the resulting visualization, or a tuple of the visualization and the corresponding spec (depending on the value of return_spec).

internal(*args, **kwargs)[source]

Function that is being wrapped.

visualize(**states)[source]

Main visualization function that all subclasses have to implement.

Parameters

states (dict) – Dictionary containing states used for visualization. The states in in_specs (specified at initialization) will have dimensionality and order of dimensions as specified there.

Returns

torch.Tensor

class firelight.visualizers.base.ContainerVisualizer(visualizers, in_spec, out_spec, extra_in_specs=None, input_mapping=None, equalize_visualization_shapes=True, colorize=False, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Base Class for visualizers combining the outputs of other visualizers.

Parameters
  • visualizers (List of BaseVisualizer) – Child visualizers whose outputs are to be combined.

  • in_spec (List of str) – List of dimension names. The outputs of all the child visualizers will be brought in this shape to be combined (in combine()).

  • out_spec (List of str) – List of dimension names of the output of combine().

  • extra_in_specs (dict) – Dictionary containing lists of dimension names for inputs of combine that are directly taken from the state dictionary and are not the output of a child visualizer.

  • input_mapping (dict) – Dictionary specifying slicing and renaming of states for visualization (see apply_slice_mapping()).

  • equalize_visualization_shapes (bool) – If true (as per default), the shapes of the outputs of child visualizers will be equalized by repeating along dimensions with shape mismatches. Only works if the maximum size of each dimension is divisible by the sizes of all the child visualizations in that dimension.

  • colorize (bool) – If False, the addition/rescaling of a ‘Color’ dimension to RGBA in [0,1] is suppressed.

  • **super_kwargs – Dictionary specifying other arguments of BaseVisualizer.

__call__(return_spec=False, **states)[source]

Like call in BaseVisualizer, but computes visualizations for all child visualizers first, which will be passed to combine() (equivalent of visualize for BaseVisualizer).

Parameters
  • return_spec (bool) – If true, a list containing the dimension names of the output is returned additionally

  • states (dict) – Dictionary including the states to be visualized.

Returns

torch.Tensor or (torch.Tensor, list), depending on the value of return_spec.

combine(*visualizations, **extra_states)[source]

Main visualization function that all subclasses have to implement.

Parameters
  • visualizations (list of torch.Tensor) – List containing the visualizations from the child visualizers. Their dimensionality and order of dimensions will be as specified in in_spec at initialization.

  • extra_states (dict) – Dictionary containing extra states (not outputs of child visualizers) used for visualization. The states in extra_in_specs (specified at initialization) will have dimensionality and order of dimensions as specified there.

Returns

torch.Tensor

internal(**states)[source]

Function that is being wrapped.

firelight.visualizers.base.DEFAULT_SPECS = {3: ['B', 'H', 'W'], 4: ['B', 'C', 'H', 'W'], 5: ['B', 'C', 'D', 'H', 'W'], 6: ['B', 'C', 'T', 'D', 'H', 'W']}

The default ways to label the dimensions depending on dimensionality.

  • 3 Axes : \((B, H, W)\)

  • 4 Axes : \((B, C, H, W)\)

  • 5 Axes : \((B, C, D, H, W)\)

  • 6 Axes : \((B, C, T, D, H, W)\)

Type

dict

firelight.visualizers.base.apply_slice_mapping(mapping, states, include_old_states=True)[source]

Add/Replace tensors in the dictionary ‘states’ as specified with the dictionary ‘mapping’. Each key in mapping corresponds to a state in the resulting dictionary, and each value describes:

  • from which tensors in states this state is grabbed (e.g. ['prediction'])

  • if a list of tensors is grabbed: which list index should be used (e.g '[index': 0])

  • what slice of the grabbed tensor should be used (e.g ['B': '0', 'C': '0:3']). For details see parse_named_slicing().

  • what function in torch.nn.functional should be applied to the tensor after the slicing (e.g. ['pre': 'sigmoid']). See parse_pre_func() for details.

These arguments can be specified in one dictionary or a list of length one dictionaries.

Parameters
  • mapping (dict) – Dictionary describing the mapping of states

  • states (dict) – Dictionary of states to be mapped. Values must be either tensors, or tuples of the form (tensor, spec).

  • include_old_states (bool) – Whether or not to include states in the ouput dictionary, on which no operations were performed.

Returns

dict – Dictionary of mapped states

firelight.visualizers.base.get_single_key_value_pair(d)[source]

Get the key and value of a length one dictionary.

Parameters

d (dict) – Single element dictionary to split into key and value.

Returns

tuple – of length 2, containing the key and value

Examples

>>> d = dict(key='value')
>>> get_single_key_value_pair(d)
('key', 'value')
firelight.visualizers.base.list_of_dicts_to_dict(list_of_dicts)[source]

Convert a list of one element dictionaries to one dictionary.

Parameters

list_of_dicts (list of dict) – List of one element dictionaries to merge.

Returns

dict

Examples

>>> list_of_dicts_to_dict([{'a': 1}, {'b': 2}])
{'a': 1, 'b': 2}
firelight.visualizers.base.parse_named_slicing(slicing, spec)[source]

Parse a slicing as a list of slice objects.

Parameters
  • slicing (str or list or dict) –

    Specifies the slicing that is to be applied. Depending on the type:

    • str: slice strings joined by ‘,’. In this case, spec will be ignored. (e.g. '0, 1:4')

    • list: has to be list of one element dictionaries, that will be converted to one dict with list_of_dicts_to_dict()

    • dict: keys are dimension names, values corresponding slices (as strings) (e.g. {'B': '0', 'C': '1:4'})

  • spec (list) – List of names of dimensions of the tensor that is to be sliced.

Returns

list – List of slice objects

Examples

Three ways to encode the same slicing:

>>> parse_named_slicing(':5, :, 1', ['A', 'B', 'C'])
[slice(None, 5, None), slice(None, None, None), slice(1, 2, None)]
>>> parse_named_slicing({'A': ':5', 'C': '1'}, ['A', 'B', 'C'])
[slice(None, 5, None), slice(None, None, None), slice(1, 2, None)]
>>> parse_named_slicing([{'A': ':5'}, {'C': '1'}], ['A', 'B', 'C'])
[slice(None, 5, None), slice(None, None, None), slice(1, 2, None)]
firelight.visualizers.base.parse_pre_func(pre_info)[source]

Parse the pre-processing function for an input to a visualizer (as given by the ‘pre’ key in the input_mapping).

Parameters

pre_info (list, dict or str) –

Depending on the type:

  • str: Name of function in torch, torch.nn.functional, or dotted path to function.

  • list: List of functions to be applied in succession. Each will be parsed by this function.

  • dict: Has to have length one. The key is the name of a function (see str above), the value specifies additional arguments supplied to that function (apart from the tensor that will be transformed). Either positional arguments can be specified as a list, or keyword arguments as a dictionary.

Examples:

  • pre_info = 'sigmoid'

  • pre_info = {'softmax': [1]}}

  • pre_info = {'softmax': {dim: 0}}}

Returns

Callable – The parsed pre-processing function.

firelight.visualizers.base.parse_slice(slice_string)[source]

Parse a slice given as a string.

Parameters

slice_string (str) – String describing the slice. Format as in fancy indexing: ‘start:stop:end’.

Returns

slice

Examples

Everything supported in fancy indexing works here, too:

>>> parse_slice('5')
slice(5, 6, None)
>>> parse_slice(':5')
slice(None, 5, None)
>>> parse_slice('5:')
slice(5, None, None)
>>> parse_slice('2:5')
slice(2, 5, None)
>>> parse_slice('2:5:3')
slice(2, 5, 3)
>>> parse_slice('::3')
slice(None, None, 3)

firelight.visualizers.colorization module

class firelight.visualizers.colorization.Colorize(background_label=None, background_color=None, opacity=1.0, value_range=None, cmap=None, colorize_jointly=None, scaling_options=None)[source]

Bases: firelight.utils.dim_utils.SpecFunction

Constructs a function used for the colorization / color normalization of tensors. The output tensor has a length 4 RGBA output dimension labeled ‘Color’.

If the input tensor is continuous, a color dimension will be added if not present already. Then, it will be scaled to \([0, 1]\). How exactly the scaling is performed can be influenced by the parameters below.

If the tensor consists of only ones and zeros, the ones will become black and the zeros transparent white.

If the input tensor is discrete including values different to zero and one, it is assumed to be a segmentation and randomly colorized.

Parameters
  • background_label (int or tuple, optional) – Value of input tensor that will be colored with background color.

  • background_color (int or tuple, optional) – Color that will be assigned to regions of the input having the value background_label.

  • opacity (float, optional) – Multiplier that will be applied to alpha channel. Useful to blend images with OverlayVisualizer.

  • value_range (tuple, optional) – Range the input data will lie in (e.g. \([-1, 1]\) for l2-normalized vectors). This range will be mapped linearly to the unit interval \([0, 1]\). If not specified, the output data will be scaled to use the full range \([0, 1]\).

  • cmap (str or callable or None, optional) –

    If str, has to be the name of a matplotlib colormap, to be used to color grayscale data.

    If callable, has to be function that adds a RGBA color dimension at the end, to an input numpy.ndarray with values between 0 and 1.

    If None, the output will be grayscale with the intensity in the opacity channel.

  • colorize_jointly (list, optional) –

    List of the names of dimensions that should be colored jointly. Default: ['W', 'H', 'D'].

    Data points separated only in these dimensions will be scaled equally. See StackVisualizer for an example usage.

add_alpha(img)[source]
internal(tensor)[source]

If not present, add a color channel to tensor. Scale the colors using Colorize.normalize_colors().

normalize_colors(tensor)[source]

Scale each color channel individually to use the whole extend of \([0, 1]\). Uses ScaleTensor.

class firelight.visualizers.colorization.ScaleTensor(invert=False, value_range=None, scale_robust=False, quantiles=(0.05, 0.95), keep_centered=False)[source]

Bases: firelight.utils.dim_utils.SpecFunction

Parameters
  • invert (bool) – Whether the input should be multiplied with -1.

  • value_range ([float, float] or None, optional) – If specified, tensor will be scaled by a linear map that maps value_range[0] will be mapped to 0, and value_range[1] will be to 1.

  • scale_robust (bool, optional) –

    Whether outliers in the input should be ignored in the scaling.

    Has no effect if value_range is specified.

  • quantiles ((float, float), optional) –

    Values under the first and above the second quantile are considered outliers for robust scaling.

    Ignored if scale_robust is False or value_range is specified.

  • keep_centered (bool, optional) –

    Whether the scaling should be symmetric in the sense that (if the scaling function is \(f\)):

    \[f(-x) = 0.5 - f(x)\]

    This can be useful in combination with diverging colormaps.

internal(tensor)[source]

Scales the input tensor to the interval \([0, 1]\).

quantile_scale(tensor, quantiles=None, return_params=False)[source]

Scale tensor linearly, such that the quantiles[i]-quantile ends up on quantiles[i].

scale_tails(tensor)[source]

Scale the tails (the elements below self.quantiles[0] and the ones above self.quantiles[1]) linearly to make all values lie in \([0, 1]\).

firelight.visualizers.colorization.add_alpha(img)[source]

Adds a totally opaque alpha channel to a tensor, whose last axis corresponds to RGB color.

Parameters

img (torch.Tensor) – The RGB image.

Returns

torch.Tensor – The resulting RGBA image.

firelight.visualizers.colorization.colorize_segmentation(seg, ignore_label=None, ignore_color=(0, 0, 0))[source]

Randomly colorize a segmentation with a set of distinct colors.

Parameters
  • seg (numpy.ndarray) – Segmentation to be colorized. Can have any shape, but data type must be discrete.

  • ignore_label (int) – Label of segment to be colored with ignore_color.

  • ignore_color (tuple) – RGB color of segment labeled with ignore_label.

Returns

numpy.ndarray – The randompy colored segmentation. The RGB channels are in the last axis.

firelight.visualizers.colorization.from_matplotlib_cmap(cmap)[source]

Converts the name of a matplotlib colormap to a colormap function that can be applied to a numpy.ndarray.

Parameters

cmap (str) – Name of the matplotlib colormap

Returns

callable – A function that maps greyscale arrays to RGBA.

firelight.visualizers.colorization.get_distinct_colors(n, min_sat=0.5, min_val=0.5)[source]

Generates a list of distinct colors, evenly separated in HSV space.

Parameters
  • n (int) – Number of colors to generate.

  • min_sat (float) – Minimum saturation.

  • min_val (float) – Minimum brightness.

Returns

numpy.ndarray – Array of shape (n, 3) containing the generated colors.

firelight.visualizers.colorization.hsv_to_rgb(h, s, v)[source]

Converts a color from HSV to RGB

Parameters
Returns

numpy.ndarray – The converted color in RGB space.

firelight.visualizers.container_visualizers module

class firelight.visualizers.container_visualizers.ColumnVisualizer(*super_args, **super_kwargs)[source]

Bases: firelight.visualizers.container_visualizers.ImageGridVisualizer

Visualizer that arranges outputs of child visualizers in a grid of images, with different child visualizations stacked horizontally (side by side). For more options, see ImageGridVisualizer

Parameters
  • *super_args

  • **super_kwargs

class firelight.visualizers.container_visualizers.ImageGridVisualizer(row_specs=('H', 'C', 'V'), column_specs=('W', 'D', 'T', 'B'), pad_width=1, pad_value=0.5, upsampling_factor=1, *super_args, **super_kwargs)[source]

Bases: firelight.visualizers.base.ContainerVisualizer

Visualizer that arranges outputs of child visualizers in a grid of images.

Parameters
  • row_specs (list) –

    List of dimension names. These dimensions of the outputs of child visualizers will be put into the height dimension of the resulting image, according to the order in the list.

    In other words, data points only separated in dimensions at the beginning of this list will be right next to each other, while data points separated in dimensions towards the back will be further away from each other in the output image.

    A special dimension name is ‘V’ (for visualizers). It stands for the dimension differentiating between the child visualizers.

    Example: Given the tensor [[1,  2 , 3 ], [10, 20, 30]] with shape (2, 3) and dimension names ['A', 'B'], this is the order of the rows, depending on the specified row_specs (suppose column_specs = []):

    • If row_specs = ['B', 'A'], the output will be [1, 2, 3, 10, 20, 30]

    • If row_specs = ['A', 'B'], the output will be [1, 10, 2, 20, 3, 30]

  • column_specs (list) – As row_specs but for columns of resulting image. Each dimension of child visualizations has to either occur in row_specs or column_specs. The intersection of row_specs and column specs has to be empty.

  • pad_width (int or dict) –

    Determines the width of padding when concatenating images. Depending on type:

    • int: Padding will have this width for concatenations along all dimensions, apart from H and W (no

      padding between adjacent pixels in image)

    • dict: Keys are dimension names, values the padding width when concatenating along them. Special key

      ’rest’ determines default value if given (otherwise no padding is used as default).

  • pad_value (int or dict) – Determines the color of padding when concatenating images. Colors can be given as floats (gray values) or list of RGB / RGBA values. If dict, interpreted as pad_width

  • upsampling_factor (int) – The whole resulting image grid will be upsampled by this factor. Useful when visualizing small images in tensorboard, but can lead to unnecessarily big file sizes.

  • *super_args (list) –

  • **super_kwargs (dict) –

get_pad_kwargs(spec)[source]
internal(*args, return_spec=False, **states)[source]

Function that is being wrapped.

visualization_to_image(visualization, spec)[source]
class firelight.visualizers.container_visualizers.OverlayVisualizer(*super_args, **super_kwargs)[source]

Bases: firelight.visualizers.base.ContainerVisualizer

Visualizer that overlays the outputs of its child visualizers on top of each other, using transparency based on the alpha channel. The output of the first child visualizer will be on the top, the last on the bottom.

Parameters
  • *super_args

  • **super_kwargs

combine(*visualizations, **_)[source]

Main visualization function that all subclasses have to implement.

Parameters
  • visualizations (list of torch.Tensor) – List containing the visualizations from the child visualizers. Their dimensionality and order of dimensions will be as specified in in_spec at initialization.

  • extra_states (dict) – Dictionary containing extra states (not outputs of child visualizers) used for visualization. The states in extra_in_specs (specified at initialization) will have dimensionality and order of dimensions as specified there.

Returns

torch.Tensor

class firelight.visualizers.container_visualizers.RiffleVisualizer(riffle_dim='C', *super_args, **super_kwargs)[source]

Bases: firelight.visualizers.base.ContainerVisualizer

Riffles the outputs of its child visualizers along specified dimension.

For a way to also scale target and prediction equally, have a look at StackVisualizer (if the range of values is known, you can also just use value_range: [a, b] for the child visualizers

Parameters
  • riffle_dim (str) – Name of dimension which is to be riffled

  • *super_args

  • **super_kwargs

Examples

Riffle the channels of a multidimensional target and prediction, such that corresponding images are closer spatially. A possible configuration file would look like this:

RiffleVisualizer:
    riffle_dim: 'C'
    visualizers:
        - ImageVisualizer:
            input_mapping:
                image: 'target'
        - ImageVisualizer:
            input_mapping:
                image: 'prediction'
combine(*visualizations, **_)[source]

Main visualization function that all subclasses have to implement.

Parameters
  • visualizations (list of torch.Tensor) – List containing the visualizations from the child visualizers. Their dimensionality and order of dimensions will be as specified in in_spec at initialization.

  • extra_states (dict) – Dictionary containing extra states (not outputs of child visualizers) used for visualization. The states in extra_in_specs (specified at initialization) will have dimensionality and order of dimensions as specified there.

Returns

torch.Tensor

class firelight.visualizers.container_visualizers.RowVisualizer(*super_args, **super_kwargs)[source]

Bases: firelight.visualizers.container_visualizers.ImageGridVisualizer

Visualizer that arranges outputs of child visualizers in a grid of images, with different child visualizations stacked vertically. For more options, see ImageGridVisualizer

Parameters
  • *super_args

  • **super_kwargs

class firelight.visualizers.container_visualizers.StackVisualizer(stack_dim='S', *super_args, **super_kwargs)[source]

Bases: firelight.visualizers.base.ContainerVisualizer

Stacks the outputs of its child visualizers along specified dimension.

Parameters
  • stack_dim (str) – Name of new dimension along which the child visualizations will be stacked. None of the child visualizations should have this dimension.

  • *super_args

  • **super_kwargs

Example

Stack a multidimensional target and prediction along an extra dimension, e.g. ‘TP’. In order to make target and prediction images comparable, disable colorization in the child visualizers and colorize only in the StackVisualizer, jointly coloring along ‘TP’, thus scaling target and prediction images by the same factors. The config would look like this:

StackVisualizer:
    stack_dim: 'TP'
    colorize: True
    color_jointly: ['H', 'W', 'TP']  # plus other dimensions you want to scale equally, e.g. D = depth
    visualizers:
        - ImageVisualizer:
            input_mapping:
                image: 'target'
            colorize = False
        - ImageVisualizer:
            input_mapping:
                image: 'target'
            colorize = True
combine(*visualizations, **_)[source]

Main visualization function that all subclasses have to implement.

Parameters
  • visualizations (list of torch.Tensor) – List containing the visualizations from the child visualizers. Their dimensionality and order of dimensions will be as specified in in_spec at initialization.

  • extra_states (dict) – Dictionary containing extra states (not outputs of child visualizers) used for visualization. The states in extra_in_specs (specified at initialization) will have dimensionality and order of dimensions as specified there.

Returns

torch.Tensor

firelight.visualizers.visualizers module

class firelight.visualizers.visualizers.CrackedEdgeVisualizer(width=1, connective_dims=('H', 'W'), **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Visualize the boundaries of a segmentation.

Parameters
  • width (int, optional) – width of the boundary in every direction

  • connective_dims (tuple, optional) –

    Tuple of axis names. Edges in those axes will be shown.

    E.g. use ('D', 'H', 'W') to visualize edges in 3D.

  • **super_kwargs

make_pad_slice_tuples()[source]
visualize(segmentation, **_)[source]
class firelight.visualizers.visualizers.DiagonalSplitVisualizer(offset=0, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Combine two input images, displaying one above and one below the diagonal.

Parameters
  • offset (int, optional) – The diagonal along which the image will be split is shifted by offset.

  • **super_kwargs

visualize(upper_right_image, lower_left_image, **_)[source]
class firelight.visualizers.visualizers.IdentityVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Visualizer that returns the tensor passed to it. Useful to visualize each channel of a tensor as a separate image.

visualize(tensor, **_)[source]
class firelight.visualizers.visualizers.ImageVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Same as IdentityVisualizer, but acting on ‘image’.

visualize(image, **_)[source]
class firelight.visualizers.visualizers.InputVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Same as IdentityVisualizer, but acting on ‘input’.

visualize(input, **_)[source]
class firelight.visualizers.visualizers.MSEVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Visualize the Mean Squared Error (MSE) between two tensors (e.g. prediction and target).

visualize(prediction, target, **_)[source]
class firelight.visualizers.visualizers.MaskVisualizer(mask_label, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Returns a mask that is 1 where the input image equals the mask label passed at initialization, and 0 elsewhere

Parameters
  • mask_label (float) – Label to be used for the construction of the mask

  • **super_kwargs

visualize(tensor, **states)[source]
class firelight.visualizers.visualizers.MaskedPcaVisualizer(ignore_label=None, n_components=3, background_label=0, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Version of PcaVisualizer that allows for an ignore mask. Data points which are labeled with ignore_label in the segmentation are ignored in the PCA analysis.

Parameters
  • ignore_label (int or float, optional) – Data points with this label in the segmentation are ignored.

  • n_components (int, optional) – Number of components for PCA. Has to be divisible by 3, such that a whole number of RGB images can be returned.

  • background_label (float, optional) – As in BaseVisualizer, here used by default to color the ignored region.

  • **super_kwargs

visualize(embedding, segmentation, **_)[source]
class firelight.visualizers.visualizers.NormVisualizer(order=2, dim='C', **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Visualize the norm of a tensor, along a given direction (by default over the channels).

Parameters
  • order (int, optional) – Order of the norm (Default is 2, euclidean norm).

  • dim (str, optional) – Name of the dimension in which the norm is computed.

  • **super_kwargs

visualize(tensor, **_)[source]
class firelight.visualizers.visualizers.PcaVisualizer(n_components=3, joint_specs=('D', 'H', 'W'), **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

PCA Visualization of high dimensional embedding tensor. An arbitrary number of channels is reduced to a multiple of 3 which are interpreted as sets RGB images.

Parameters
  • n_components (int, optional) – Number of components to use. Must be divisible by 3.

  • joint_specs (tuple of str, optional) –

    Entries only separated along these axis are treated jointly.

    Defaults to spatial dimensions.

    Use e.g. ('B', 'H', 'W') to run PCA jointly on all images of the batch. #TODO: make this example work. Right now, all dimensions except ‘B’ work.

  • **super_kwargs

visualize(embedding, **_)[source]
class firelight.visualizers.visualizers.PredictionVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Same as IdentityVisualizer, but acting on ‘prediction’.

visualize(prediction, **_)[source]
class firelight.visualizers.visualizers.RGBVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Visualize the input tensor as RGB images. If the input has n * 3 channels, n color images will be returned.

visualize(tensor, **_)[source]
class firelight.visualizers.visualizers.SegmentationVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Same as IdentityVisualizer, but acting on ‘segmentation’.

visualize(segmentation, **_)[source]
class firelight.visualizers.visualizers.TargetVisualizer(**super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Same as IdentityVisualizer, but acting on ‘target’.

visualize(target, **_)[source]
class firelight.visualizers.visualizers.ThresholdVisualizer(threshold, mode='greater_equal', **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Returns a mask resulting from thresholding the input tensor.

Parameters
MODES = ['greater', 'smaller', 'greater_equal', 'smaller_equal']
visualize(tensor, **_)[source]
class firelight.visualizers.visualizers.TsneVisualizer(joint_dims=None, n_components=3, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

tSNE Visualization of high dimensional embedding tensor. An arbitrary number of channels is reduced to a multiple of 3 which are interpreted as sets RGB images.

Parameters
  • n_components (int, optional) – Number of components to use. Must be divisible by 3.

  • joint_dims (tuple of str, optional) –

    Entries only separated along these axis are treated jointly.

    Defaults to spatial dimensions.

  • **super_kwargs

visualize(embedding, **_)[source]
class firelight.visualizers.visualizers.UmapVisualizer(joint_dims=None, n_components=3, n_neighbors=15, min_dist=0.1, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

UMAP Visualization of high dimensional embedding tensor. An arbitrary number of channels is reduced to 3 which are interpreted as RGB.

For a detailed discussion of parameters, see https://umap-learn.readthedocs.io/en/latest/parameters.html.

Parameters
  • joint_dims (tuple of str, optional) –

    Entries only separated along these axis are treated jointly.

    Defaults to spatial dimensions.

  • n_components (int, optional) – Number of components to use. Must be divisible by 3.

  • n_neighbors (int, optional) – controls how many neighbors are considered for distance estimation on the manifold. Low number focuses on local distance, large numbers more on global structure, default 15.

  • min_dist (float, optional) – minimum distance of points after dimension reduction, default 0.1.

  • **super_kwargs

visualize(embedding, **_)[source]
class firelight.visualizers.visualizers.UpsamplingVisualizer(specs, shape=None, factors=None, **super_kwargs)[source]

Bases: firelight.visualizers.base.BaseVisualizer

Upsample a tensor along a list of axis (specified via specs) to a specified shape, by a list of specified factors or the shape of a reference tensor (given as an optional argument to visualize).

Parameters
  • specs (list of str) – Specs of the axes to upsample along.

  • shape (None or int or list, optional) – Shape after upsampling.

  • factors (None or int or list, optional) – Factors to upsample by.

  • **super_kwargs

visualize(tensor, reference=None, **_)[source]
firelight.visualizers.visualizers.pca(embedding, output_dimensions=3, reference=None, center_data=False)[source]

Principal component analysis wrapping sklearn.decomposition.PCA. Dimension 1 of the input embedding is reduced

Parameters
  • embedding (torch.Tensor) – Embedding whose dimensions will be reduced.

  • output_dimensions (int, optional) – Number of dimension to reduce to.

  • reference (torch.Tensor, optional) – Optional tensor that will be used to train PCA on.

  • center_data (bool, optional) – Whether to subtract the mean before PCA.

Returns

torch.Tensor