magmap.plot.colormaps module

Custom colormaps for MagellanMapper.

class magmap.plot.colormaps.DiscreteColormap(labels=None, alpha=150, index_direct=True, max_val=255, background=None, dup_for_neg=False, symmetric_colors=False, cmap_labels=None, **kwargs)[source]

Bases: ListedColormap

Extends :class:matplotlib.colors.ListedColormap to generate a discrete colormap and associated normalization object.

Extend ListedColormap rather than linear colormap since the number of colors should equal the number of possible vals, without requiring interpolation.

Variables:
  • cmap_labels – Tuple of N lists of RGBA values, where N is equal to the number of colors, with a discrete color for each unique value in labels.

  • norm – Normalization object, which is of type :class:matplotlib.colors.NoNorm if indexing directly or :class:matplotlib.colors.BoundaryNorm if otherwise.

  • img_labels (List[int]) – Sorted sequence of unique labels. May have more values than in labels such as mirrored negative values. None if index_direct is False.

convert_img_labels(img)[source]

Convert an image to the indices in img_labels to give a linearly scaled image.

This image can be displayed using a colormap with matplotlib.colors.NoNorm to index directly into the colormap.

Parameters:

img (np.ndarray) – Image to convert. If symmetric_colors is True, the absolute value will be taken as a workaround for likely image display resampling errors.

Returns:

Array of same shape as img with values translated to their corresponding indices within img_labels, or img unchanged if img_labels is None.

Return type:

numpy.ndarray

make_cmap()[source]

Initialize ListedColormap with stored labels rescaled to 0-1.

modified_cmap(adjust)[source]

Make a modified discrete colormap from itself.

The resulting colormap is assumed to map to the same range of label image values, using the same norm and img_labels.

Parameters:

adjust (int) – Value by which to adjust RGB (not A) values.

Return type:

DiscreteColormap

Returns:

New DiscreteColormap instance with norm pointing to first instance, img_labels, and cmap_labels incremented by adjust.

class magmap.plot.colormaps.DiscreteModes(value)[source]

Bases: Enum

Discrete colormap generation modes.

GRID = 2
RANDOMN = 1
magmap.plot.colormaps.discrete_colormap(num_colors, alpha=255, prioritize_default=True, seed=None, min_val=0, max_val=255, min_any=0, symmetric_colors=False, dup_offset=0, jitter=0, mode=DiscreteModes.RANDOMN)[source]

Make a discrete colormap using randomly generated RGB values.

Parameters:
  • num_colors (int) – Number of discrete colors to generate.

  • alpha (int, default: 255) – Transparency level, from 0-255; defaults to 255.

  • prioritize_default (Union[bool, str, Sequence[Sequence[int]]], default: True) – If True (default), the default colors from :attr:config.colors will replace the initial colormap elements. Can alternatively be a string for Matplotlib color specs, where cn = “CN” color spec, css4 = CSS4/X11 spec, and “tableau” = Tableau spec. Can also be a sequence of colors, where each colors is in r, g, b, a, ranging from 0-255.

  • seed (Optional[int], default: None) – Random number seed; defaults to None, in which case no seed will be set.

  • min_val (Union[int, float], default: 0) – Minimum value for random numbers; defaults to 0.

  • max_val (Union[int, float], default: 255) – Maximum value for random numbers; defaults to 255. For floating point ranges such as 0.0-1.0, set as a float.

  • min_any (Union[int, float], default: 0) – Minimum value above which at least one value must be in each set of RGB values; defaults to 0. If all values in an RGB set are below this value, the lowest RGB value will be scaled up by the ratio max_val:min_any. Assumes a range of min_val < min_any < max_val; defaults to 0 to ignore.

  • symmetric_colors (bool, default: False) – True to create a symmetric set of colors, assuming the first half of num_colors mirror those of the second half; defaults to False.

  • dup_offset (int, default: 0) – Amount by which to offset duplicate color values if dup_for_neg is enabled; defaults to 0.

  • jitter (int, default: 0) – In DiscreteModes.GRID mode, coordinates are randomly shifted by half this value above or below their original value; defaults to 0.

  • mode (DiscreteModes, default: <DiscreteModes.RANDOMN: 1>) – Mode given as an enumeration; defaults to DiscreteModes.RANDOMN mode.

Return type:

ndarray

Returns:

2D Numpy array in the format [[R, G, B, alpha], ...] on a scale of 0-255. This colormap will need to be converted into a Matplotlib colormap using LinearSegmentedColormap.from_list to generate a map that can be used directly in functions such as imshow.

magmap.plot.colormaps.get_borders_colormap(borders_img, labels_img, cmap_labels)[source]

Get a colormap for borders, using corresponding labels with intensity change to distinguish the borders.

If the number of labels differs from that of the original colormap, a new colormap will be generated instead.

Parameters:
  • borders_img – Borders image as a Numpy array, used to determine the number of labels required. If this image has multiple channels, a similar colormap with distinct intensity will be made for each channel.

  • labels_img – Labels image as a Numpy array, used to compare the number of labels for each channel in borders_img.

  • cmap_labels – The original colormap on which the new colormaps will be based.

Returns:

List of borders colormaps corresponding to the number of channels, or None if borders_img is None

magmap.plot.colormaps.get_cmap(cmap, n=None)[source]

Get colormap from a list of colormaps, string, or enum.

If n is given, cmap is assumed to be a list from which a colormap will be retrieved. Colormaps that are strings will be converted to the associated standard Colormap object, while enums in :class:config.Cmaps will be used to retrieve a Colormap object from :const:CMAPS, which is assumed to have been initialized.

Parameters:
  • cmap (Union[str, Cmaps]) – Colormap given as a string of Enum or list of colormaps.

  • n (Optional[int], default: None) – Index of cmap to retrieve a colormap, assuming that cmap is a sequence. Defaults to None to use cmap directly.

Return type:

Optional[LinearSegmentedColormap]

Returns:

The Colormap object, or None if no corresponding colormap is found.

magmap.plot.colormaps.get_labels_discrete_colormap(labels_img, alpha_bkgd=255, use_orig_labels=False, **kwargs)[source]

Get a default discrete colormap for a labels image.

Assumes that background is 0, and the seed is determined by :attr:config.seed.

Parameters:
  • labels_img (Optional[ndarray]) – Labels image as a Numpy array. Can be None, in which case use_orig_labels should be True.

  • alpha_bkgd (int, default: 255) – Background alpha level from 0 to 255; defaults to 255 to turn on background fully.

  • use_orig_labels (bool, default: False) – True to use original labels from config.labels_img_orig if available, falling back to labels_img. Defaults to False.

Return type:

DiscreteColormap

Returns:

A colormap with separate color for each unique value in labels_img.

magmap.plot.colormaps.make_binary_cmap(binary_colors)[source]

Make a binary discrete colormap.

Parameters:

binary_colors (List[str]) – Sequence of colors as [background, foreground].

Returns:

Discrete colormap with labels of [0, 1] mapped to binary_colors.

Return type:

DiscreteColormap

magmap.plot.colormaps.make_dark_linear_cmap(name, color)[source]

Make a linear colormap starting with black and ranging to color.

Parameters:
  • name – Name to give to colormap.

  • color – Colors will range from black to this color.

Returns:

A LinearSegmentedColormap object.

magmap.plot.colormaps.setup_cmaps()[source]

Setup default colormaps, storing them in :const:CMAPS.

magmap.plot.colormaps.setup_colormaps(num_channels)[source]

Set up colormaps based on the currently loaded main ROI profile.

Parameters:

num_channels (int) – Number of channels in the main image; if the main ROI profile does not define this many colormaps, new colormaps will be randomly generated.

magmap.plot.colormaps.setup_labels_cmap(labels_img, binary_colors=None, **kwargs)[source]

Wrapper to set up a colormap for a labels image.

Parameters:
  • labels_img (Optional[ndarray]) – Labels image.

  • binary_colors (Optional[Sequence[str]], default: None) – Sequence of colors as strings; defaults to None, in which case config.atlas_labels[config.AtlasLabels.BINARY] is used. 0 is assumed to be background, and 1 is foreground.

Return type:

DiscreteColormap

Returns:

Discrete colormap for the given labels.