macromax.utils.display package
This package contains functionality to simplify the display of complex matrices.
Submodules
macromax.utils.display.colormap module
- class macromax.utils.display.colormap.InterpolatedColorMap(name, colors, points=None)[source]
Bases:
LinearSegmentedColormapA custom colormap for use with imshow and colorbar.
Example usage:
cmap = colormap.InterpolatedColorMap( 'hsv', [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1), (1, 0, 0), (1, 1, 1)], ) cmap = colormap.InterpolatedColorMap( 'rainbow', [(0, 0, 0), (1, 0, 0), (0.75, 0.75, 0), (0, 1, 0), (0, 0.75, 0.75), (0, 0, 1), (0.75, 0, 0.75), (1, 1, 1)], points=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1], ) fig, ax = subplots(1, 1) ax.imshow(intensity_array, cmap=cmap) from matplotlib import cm fig.colorbar(cm.ScalarMappable(norm=None, cmap=cmap), ax=ax)
- __init__(name, colors, points=None)[source]
Create colormap from linear mapping segments
segmentdata argument is a dictionary with a red, green and blue entries. Each entry should be a list of x, y0, y1 tuples, forming rows in a table. Entries for alpha are optional.
Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:
cdict = {'red': [(0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)], 'green': [(0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0)], 'blue': [(0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)]}
Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1. For any input value z falling between x[i] and x[i+1], the output value of a given color will be linearly interpolated between y1[i] and y0[i+1]:
row i: x y0 y1 / / row i+1: x y0 y1
Hence y0 in the first row and y1 in the last row are never used.
See also
LinearSegmentedColormap.from_listStatic method; factory function for generating a smoothly-varying LinearSegmentedColormap.
- __call__(X, alpha=None, bytes=False)
- Parameters:
X (float or int, ~numpy.ndarray or scalar) – The data value(s) to convert to RGBA. For floats, X should be in the interval
[0.0, 1.0]to return the RGBA valuesX*100percent along the Colormap line. For integers, X should be in the interval[0, Colormap.N)to return RGBA values indexed from the Colormap with indexX.alpha (float or array-like or None) – Alpha must be a scalar between 0 and 1, a sequence of such floats with shape matching X, or None.
bytes (bool) – If False (default), the returned RGBA values will be floats in the interval
[0, 1]otherwise they will be numpy.uint8s in the interval[0, 255].
- Returns:
Tuple of RGBA values if X is scalar, otherwise an array of
RGBA values with a shape of
X.shape + (4, ).
- __copy__()
- __eq__(other)
Return self==value.
- copy()
Return a copy of the colormap.
- static from_list(name, colors, N=256, gamma=1.0)
Create a LinearSegmentedColormap from a list of colors.
- Parameters:
name (str) – The name of the colormap.
colors (list of color or list of (value, color)) – If only colors are given, they are equidistantly mapped from the range \([0, 1]\); i.e. 0 maps to
colors[0]and 1 maps tocolors[-1]. If (value, color) pairs are given, the mapping is from value to color. This can be used to divide the range unevenly.N (int) – The number of RGB quantization levels.
gamma (float)
- get_bad()
Get the color for masked values.
- get_over()
Get the color for high out-of-range values.
- get_under()
Get the color for low out-of-range values.
- is_gray()
Return whether the colormap is grayscale.
- resampled(lutsize)
Return a new colormap with lutsize entries.
- reversed(name=None)
Return a reversed instance of the Colormap.
- Parameters:
name (str, optional) – The name for the reversed colormap. If None, the name is set to
self.name + "_r".- Returns:
The reversed colormap.
- Return type:
LinearSegmentedColormap
- set_bad(color='k', alpha=None)
Set the color for masked values.
- set_extremes(*, bad=None, under=None, over=None)
Set the colors for masked (bad) values and, when
norm.clip = False, low (under) and high (over) out-of-range values.
- set_gamma(gamma)
Set a new gamma value and regenerate colormap.
- set_over(color='k', alpha=None)
Set the color for high out-of-range values.
- set_under(color='k', alpha=None)
Set the color for low out-of-range values.
- with_extremes(*, bad=None, under=None, over=None)
Return a copy of the colormap, for which the colors for masked (bad) values and, when
norm.clip = False, low (under) and high (over) out-of-range values, have been set accordingly.
- colorbar_extend
When this colormap exists on a scalar mappable and colorbar_extend is not False, colorbar creation will pick up
colorbar_extendas the default value for theextendkeyword in the matplotlib.colorbar.Colorbar constructor.
macromax.utils.display.complex2rgb module
- macromax.utils.display.complex2rgb.complex2rgb(complex_image, normalization=None, inverted=False, alpha=None, dtype=<class 'float'>, axis=-1)[source]
Converts a complex image to an RGB image.
- Parameters:
complex_image (
Union[complex,Sequence,array]) – A 2D arraynormalization (
Union[bool,float,int]) – An optional multidimensional to indicate the target magnitude of the maximum value (1.0 is saturation).inverted (
bool) – By default 0 is shown as black and amplitudes of 1 as the brightest hues. Setting this input argument to True could be useful for printing on a white background.alpha (
Optional[float]) – The maximum alpha value (0 = transparent, 1 is opaque). When specified, each pixel’s alpha value is proportional to the intensity times this value. Default: None = no alpha channel.dtype – The output data type. The value is scaled to the maximum positive numeric range for integers (np.iinfo(dtype).max). Floating point numbers are within [0, 1]. (Default: float)
axis (
int) – (default: -1) The channel axis of the output array, and also the input array if neither saturation, nor value are provided.
- Return type:
- Returns:
A real 3d-array with values between 0 and 1 if the the dtype is a float and covering the dynamic range when the dtype is an integer.
macromax.utils.display.format_image_axes module
- macromax.utils.display.format_image_axes.format_image_axes(axes, title=None, scale=0, unit='', white_background=False)[source]
Replaces the axes ticks on an image with a(n optional) scalebar and adds a(n optional) title. TODO: Integrate examples.complex_color_legend into this as well.
- Parameters:
axes – The Axes object to update.
title (
str) – An optional title to be placed in the top left corner.scale (
float|ndarray) – The width of the scale bar, typically grid.extent[0] / 8 works well. Set to 0 for no scalebar.unit (
str) – Optional unit to use. E.g. ‘m’ will translate to ‘mm’ for scales on the order of 1e-3.white_background (
bool) – If True, use white annotations, otherwise black. (default: False).
- Returns:
The modified axes.
macromax.utils.display.grid2extent module
- macromax.utils.display.grid2extent.grid2extent(*args, origin_lower=False)[source]
Utility function to determine extent values for matplotlib.pyploy.imshow
- Parameters:
args – A Grid object or monotonically increasing ranges, one per dimension (vertical, horizontal)
origin_lower (
bool) – (default: False) Set this to True when imshow has the origin set to ‘lower’ to have the vertical axis increasing upwards.
- Returns:
An nd-array with 4 numbers indicating the extent of the displayed data.
macromax.utils.display.hsv module
- macromax.utils.display.hsv.hsv2rgb(hue=None, saturation=None, value=None, alpha=None, axis=-1)[source]
Converts a hue-saturation-intensity value image to a red-green-blue image.
- Parameters:
hue (
Union[float,Sequence,ndarray]) – A 2D numpy.array with the hue. If the saturation is not provided, the first argument will be interpreted as a 3D numpy.array with the HSV image, the channel must be in the final right-hand dimension.saturation (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the saturation.value (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the intensity value.alpha (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the opaqueness alpha value.axis (
int) – (default: -1) The channel axis of the output array, and also the input array if neither saturation, nor value are provided.
- Return type:
- Returns:
rgb_image: a 3D numpy.array with the RGB image, the channel in the final right-hand dimension, or axis if provided.
- macromax.utils.display.hsv.rgb2hsv(red, green=None, blue=None, alpha=None, axis=-1)[source]
Converts a red-green-blue value image to a hue-saturation-intensity image.
- Parameters:
red (
Union[float,Sequence,ndarray]) – An 2D numpy.array with the red channel. If neither green and blue are provided, then this will be interpreted as a stack of the red, green, and blue channels.green (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the green channel.blue (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the blue channel.alpha (
Union[float,Sequence,ndarray]) – (optional) a 2D numpy.array with the opaqueness alpha value.axis – (default: -1) The channel axis of the output array, and also the input array if neither saturation, nor value are provided.
- Return type:
- Returns:
hsv_image: a 3D numpy.array with the HSV image