|
| 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 | + rtol = 1.05 |
| 43 | + if dt in _integer_types: |
| 44 | + return _integer_ranges[dt][1] |
| 45 | + else: |
| 46 | + im_max = img[np.isfinite(img)].max() |
| 47 | + if im_max <= 1 * rtol: |
| 48 | + return 1 |
| 49 | + elif im_max <= 255 * rtol: |
| 50 | + return 255 |
| 51 | + elif im_max <= 65535 * rtol: |
| 52 | + return 65535 |
| 53 | + else: |
| 54 | + return 2 ** 32 |
| 55 | + |
| 56 | + |
| 57 | +def imshow(img, zmin=None, zmax=None, origin=None, colorscale=None): |
| 58 | + """ |
| 59 | + Display an image, i.e. data on a 2D regular raster. |
| 60 | +
|
| 61 | + Parameters |
| 62 | + ---------- |
| 63 | +
|
| 64 | + img: array-like image |
| 65 | + The image data. Supported array shapes are |
| 66 | +
|
| 67 | + - (M, N): an image with scalar data. The data is visualized |
| 68 | + using a colormap. |
| 69 | + - (M, N, 3): an image with RGB values. |
| 70 | + - (M, N, 4): an image with RGBA values, i.e. including transparency. |
| 71 | +
|
| 72 | + zmin, zmax : scalar or iterable, optional |
| 73 | + zmin and zmax define the scalar range that the colormap covers. By default, |
| 74 | + zmin and zmax correspond to the min and max values of the datatype for integer |
| 75 | + datatypes (ie [0-255] for uint8 images, [0, 65535] for uint16 images, etc.). For |
| 76 | + a multichannel image of floats, the max of the image is computed and zmax is the |
| 77 | + smallest power of 256 (1, 255, 65535) greater than this max value, |
| 78 | + with a 5% tolerance. For a single-channel image, the max of the image is used. |
| 79 | +
|
| 80 | + origin : str, 'upper' or 'lower' (default 'upper') |
| 81 | + position of the [0, 0] pixel of the image array, in the upper left or lower left |
| 82 | + corner. The convention 'upper' is typically used for matrices and images. |
| 83 | +
|
| 84 | + colorscale : str |
| 85 | + colormap used to map scalar data to colors (for a 2D image). This parameter is not used for |
| 86 | + RGB or RGBA images. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + fig : graph_objects.Figure containing the displayed image |
| 91 | +
|
| 92 | + See also |
| 93 | + -------- |
| 94 | +
|
| 95 | + plotly.graph_objects.Image : image trace |
| 96 | + plotly.graph_objects.Heatmap : heatmap trace |
| 97 | +
|
| 98 | + Notes |
| 99 | + ----- |
| 100 | +
|
| 101 | + In order to update and customize the returned figure, use |
| 102 | + `go.Figure.update_traces` or `go.Figure.update_layout`. |
| 103 | + """ |
| 104 | + img = np.asanyarray(img) |
| 105 | + # Cast bools to uint8 (also one byte) |
| 106 | + if img.dtype == np.bool: |
| 107 | + img = 255 * img.astype(np.uint8) |
| 108 | + |
| 109 | + # For 2d data, use Heatmap trace |
| 110 | + if img.ndim == 2: |
| 111 | + if colorscale is None: |
| 112 | + colorscale = "gray" |
| 113 | + trace = go.Heatmap(z=img, zmin=zmin, zmax=zmax, colorscale=colorscale) |
| 114 | + autorange = True if origin == "lower" else "reversed" |
| 115 | + layout = dict( |
| 116 | + xaxis=dict(scaleanchor="y", constrain="domain"), |
| 117 | + yaxis=dict(autorange=autorange, constrain="domain"), |
| 118 | + ) |
| 119 | + # For 2D+RGB data, use Image trace |
| 120 | + elif img.ndim == 3 and img.shape[-1] in [3, 4]: |
| 121 | + if zmax is None and img.dtype is not np.uint8: |
| 122 | + zmax = _infer_zmax_from_type(img) |
| 123 | + zmin, zmax = _vectorize_zvalue(zmin), _vectorize_zvalue(zmax) |
| 124 | + trace = go.Image(z=img, zmin=zmin, zmax=zmax) |
| 125 | + layout = {} |
| 126 | + if origin == "lower": |
| 127 | + layout["yaxis"] = dict(autorange=True) |
| 128 | + else: |
| 129 | + raise ValueError( |
| 130 | + "px.imshow only accepts 2D grayscale, RGB or RGBA images. " |
| 131 | + "An image of shape %s was provided" % str(img.shape) |
| 132 | + ) |
| 133 | + fig = go.Figure(data=trace, layout=layout) |
| 134 | + return fig |
0 commit comments