|
| 1 | +import plotly.graph_objs as go |
| 2 | +import numpy as np # is it fine to depend on np here? |
| 3 | + |
| 4 | +_float_types = [] |
| 5 | + |
| 6 | +# Adapted from skimage.util.dtype |
| 7 | +_integer_types = ( |
| 8 | + np.byte, |
| 9 | + np.ubyte, # 8 bits |
| 10 | + np.short, |
| 11 | + np.ushort, # 16 bits |
| 12 | + np.intc, |
| 13 | + np.uintc, # 16 or 32 or 64 bits |
| 14 | + np.int_, |
| 15 | + np.uint, # 32 or 64 bits |
| 16 | + np.longlong, |
| 17 | + np.ulonglong, |
| 18 | +) # 64 bits |
| 19 | +_integer_ranges = {t: (np.iinfo(t).min, np.iinfo(t).max) for t in _integer_types} |
| 20 | + |
| 21 | + |
| 22 | +def _vectorize_zvalue(z): |
| 23 | + if z is None: |
| 24 | + return z |
| 25 | + elif np.isscalar(z): |
| 26 | + return [z] * 3 + [1] |
| 27 | + elif len(z) == 1: |
| 28 | + return list(z) * 3 + [1] |
| 29 | + elif len(z) == 3: |
| 30 | + return list(z) + [1] |
| 31 | + elif len(z) == 4: |
| 32 | + return z |
| 33 | + else: |
| 34 | + raise ValueError( |
| 35 | + "zmax can be a scalar, or an iterable of length 1, 3 or 4. " |
| 36 | + "A value of %s was passed for zmax." % str(z) |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def _infer_zmax_from_type(img): |
| 41 | + dt = img.dtype.type |
| 42 | + if dt in _integer_types: |
| 43 | + return _integer_ranges[dt][1] |
| 44 | + else: |
| 45 | + return img[np.isfinite(img)].max() |
| 46 | + |
| 47 | + |
| 48 | +def imshow( |
| 49 | + img, zmin=None, zmax=None, origin=None, colorscale=None, showticks=True, **kwargs |
| 50 | +): |
| 51 | + """ |
| 52 | + Display an image, i.e. data on a 2D regular raster. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | +
|
| 57 | + img: array-like image |
| 58 | + The image data. Supported array shapes are |
| 59 | +
|
| 60 | + - (M, N): an image with scalar data. The data is visualized |
| 61 | + using a colormap. |
| 62 | + - (M, N, 3): an image with RGB values. |
| 63 | + - (M, N, 4): an image with RGBA values, i.e. including transparency. |
| 64 | +
|
| 65 | + zmin, zmax : scalar or iterable, optional |
| 66 | + zmin and zmax define the scalar range that the colormap covers. By default, |
| 67 | + zmin and zmax correspond to the min and max values of the datatype for integer |
| 68 | + datatypes (ie [0-255] for uint8 images, [0, 65535] for uint16 images, etc.), and |
| 69 | + to the min and max values of the image for an image of floats. |
| 70 | +
|
| 71 | + origin : str, 'upper' or 'lower' (default 'upper') |
| 72 | + position of the [0, 0] pixel of the image array, in the upper left or lower left |
| 73 | + corner. The convention 'upper' is typically used for matrices and images. |
| 74 | +
|
| 75 | + colorscale : str |
| 76 | + colormap used to map scalar data to colors (for a 2D image). This parameter is not used for |
| 77 | + RGB or RGBA images. |
| 78 | +
|
| 79 | + showticks : bool, default True |
| 80 | + if False, no tick labels are shown for pixel indices. |
| 81 | +
|
| 82 | + ** kwargs : additional arguments to be passed to the Heatmap (grayscale) or Image (RGB) trace. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + fig : graph_objects.Figure containing the displayed image |
| 87 | +
|
| 88 | + See also |
| 89 | + -------- |
| 90 | +
|
| 91 | + graph_objects.Image : image trace |
| 92 | + graph_objects.Heatmap : heatmap trace |
| 93 | + """ |
| 94 | + img = np.asanyarray(img) |
| 95 | + # Cast bools to uint8 (also one byte) |
| 96 | + if img.dtype == np.bool: |
| 97 | + img = 255 * img.astype(np.uint8) |
| 98 | + |
| 99 | + # For 2d data, use Heatmap trace |
| 100 | + if img.ndim == 2: |
| 101 | + if colorscale is None: |
| 102 | + colorscale = "gray" |
| 103 | + trace = go.Heatmap(z=img, zmin=zmin, zmax=zmax, colorscale=colorscale, **kwargs) |
| 104 | + autorange = True if origin == "lower" else "reversed" |
| 105 | + layout = dict( |
| 106 | + xaxis=dict(scaleanchor="y", constrain="domain"), |
| 107 | + yaxis=dict(autorange=autorange, constrain="domain"), |
| 108 | + ) |
| 109 | + # For 2D+RGB data, use Image trace |
| 110 | + elif img.ndim == 3 and img.shape[-1] in [3, 4]: |
| 111 | + if zmax is None and img.dtype is not np.uint8: |
| 112 | + zmax = _infer_zmax_from_type(img) |
| 113 | + zmin, zmax = _vectorize_zvalue(zmin), _vectorize_zvalue(zmax) |
| 114 | + trace = go.Image(z=img, zmin=zmin, zmax=zmax, **kwargs) |
| 115 | + layout = {} |
| 116 | + if origin == "lower": |
| 117 | + layout["yaxis"] = dict(autorange=True) |
| 118 | + else: |
| 119 | + raise ValueError( |
| 120 | + "px.imshow only accepts 2D grayscale, RGB or RGBA images. " |
| 121 | + "An image of shape %s was provided" % str(img.shape) |
| 122 | + ) |
| 123 | + fig = go.Figure(data=trace, layout=layout) |
| 124 | + if not showticks: |
| 125 | + fig.update_xaxes(showticklabels=False) |
| 126 | + fig.update_yaxes(showticklabels=False) |
| 127 | + return fig |
0 commit comments