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:
LinearSegmentedColormap
A 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_list
Static method; factory function for generating a smoothly-varying LinearSegmentedColormap.
- __call__(X, alpha=None, bytes=False)
- Parameters:
X (float or int, 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*100
percent 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 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 (array-like of colors or array-like 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.
- reversed(name=None)
Return a reversed instance of the Colormap.
- Parameters:
name (str, optional) – The name for the reversed colormap. If it’s None the name will be the name of the parent colormap + “_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_extend
as the default value for theextend
keyword 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
,None
]) – 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.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
,None
]) – 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
,None
]) – (optional) a 2D numpy.array with the saturation.value (
Union
[float
,Sequence
,ndarray
,None
]) – (optional) a 2D numpy.array with the intensity value.alpha (
Union
[float
,Sequence
,ndarray
,None
]) – (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
,None
]) – (optional) a 2D numpy.array with the green channel.blue (
Union
[float
,Sequence
,ndarray
,None
]) – (optional) a 2D numpy.array with the blue channel.alpha (
Union
[float
,Sequence
,ndarray
,None
]) – (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