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:
ListedColormapExtends :class:
matplotlib.colors.ListedColormapto generate a discrete colormap and associated normalization object.Extend
ListedColormaprather 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.NoNormif indexing directly or :class:matplotlib.colors.BoundaryNormif otherwise.img_labels (List[int]) – Sorted sequence of unique labels. May have more values than in
labelssuch as mirrored negative values. None ifindex_directis False.
- convert_img_labels(img)[source]#
Convert an image to the indices in
img_labelsto give a linearly scaled image.This image can be displayed using a colormap with
matplotlib.colors.NoNormto index directly into the colormap.- Parameters:
img (
np.ndarray) – Image to convert. Ifsymmetric_colorsis True, the absolute value will be taken as a workaround for likely image display resampling errors.- Returns:
Array of same shape as
imgwith values translated to their corresponding indices withinimg_labels, orimgunchanged ifimg_labelsis None.- Return type:
- 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
normandimg_labels.- Parameters:
adjust (
int) – Value by which to adjust RGB (not A) values.- Return type:
- Returns:
New
DiscreteColormapinstance withnormpointing to first instance,img_labels, andcmap_labelsincremented byadjust.
- class magmap.plot.colormaps.DiscreteModes(value)[source]#
Bases:
EnumDiscrete 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.colorswill 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 inr, 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 ratiomax_val:min_any. Assumes a range ofmin_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 ofnum_colorsmirror those of the second half; defaults to False.dup_offset (
int, default:0) – Amount by which to offset duplicate color values ifdup_for_negis enabled; defaults to 0.jitter (
int, default:0) – InDiscreteModes.GRIDmode, 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 toDiscreteModes.RANDOMNmode.
- Return type:
- 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 usingLinearSegmentedColormap.from_listto generate a map that can be used directly in functions such asimshow.
- 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_imgis None
- magmap.plot.colormaps.get_cmap(cmap, n=None)[source]#
Get colormap from a list of colormaps, string, or enum.
If
nis given,cmapis 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.Cmapswill be used to retrieve a Colormap object from :const:CMAPS, which is assumed to have been initialized.- Parameters:
cmap – Colormap given as a string of Enum or list of colormaps.
n (default:
None) – Index of cmap to retrieve a colormap, assuming that cmap is a sequence. Defaults to None to use cmap directly.
- Returns:
The
Colormapobject, 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 caseuse_orig_labelsshould 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 fromconfig.labels_img_origif available, falling back tolabels_img. Defaults to False.
- Return type:
- 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 tobinary_colors.- Return type:
- 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_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.