|
| 1 | +from ..plot import Plot |
| 2 | +from ..layouts import GridPlot |
| 3 | +from ..graphics import Image |
| 4 | +from ipywidgets.widgets import IntSlider, VBox, HBox |
| 5 | +import numpy as np |
| 6 | +from typing import * |
| 7 | +from warnings import warn |
| 8 | + |
| 9 | + |
| 10 | +DEFAULT_AXES_ORDER = \ |
| 11 | + { |
| 12 | + 2: "xy", |
| 13 | + 3: "txy", |
| 14 | + 4: "tzxy", |
| 15 | + 5: "tczxy", |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | +def calc_gridshape(n): |
| 20 | + sr = np.sqrt(n) |
| 21 | + return ( |
| 22 | + np.ceil(sr), |
| 23 | + np.round(sr) |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +class ImageWidget: |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + data: Union[np.ndarray, List[np.ndarray]], |
| 31 | + axes_order: str = None, |
| 32 | + slider_sync: bool = True, |
| 33 | + slider_axes: Union[int, str, dict] = None, |
| 34 | + frame_apply: Union[callable, dict] = None, |
| 35 | + grid_shape: Tuple[int, int] = None, |
| 36 | + ): |
| 37 | + # single image |
| 38 | + if isinstance(data, np.ndarray): |
| 39 | + self.plot_type = Plot |
| 40 | + self.data: List[np.ndarray] = [data] |
| 41 | + ndim = data[0].ndim |
| 42 | + |
| 43 | + # list of lists |
| 44 | + elif isinstance(data, list): |
| 45 | + if all([isinstance(d, np.ndarray) for d in data]): |
| 46 | + self.plot_type = GridPlot |
| 47 | + |
| 48 | + if grid_shape is None: |
| 49 | + grid_shape = calc_gridshape(len(data)) |
| 50 | + |
| 51 | + elif grid_shape[0] * grid_shape[1] < len(data): |
| 52 | + grid_shape = calc_gridshape(len(data)) |
| 53 | + warn(f"Invalid `grid_shape` passed, setting grid shape to: {grid_shape}") |
| 54 | + |
| 55 | + _ndim = [d.ndim for d in data] |
| 56 | + |
| 57 | + if not len(set(_ndim)) == 1: |
| 58 | + raise ValueError( |
| 59 | + f"Number of dimensions of all data arrays must match, your ndims are: {_ndim}" |
| 60 | + ) |
| 61 | + |
| 62 | + self.data: List[np.ndarray] = data |
| 63 | + ndim = data[0].ndim |
| 64 | + |
| 65 | + else: |
| 66 | + raise TypeError( |
| 67 | + f"`data` must be of type `numpy.ndarray` representing a single image/image sequence " |
| 68 | + f"or a list of `numpy.ndarray` representing a grid of images/image sequences" |
| 69 | + ) |
| 70 | + |
| 71 | + if axes_order is None: |
| 72 | + self.axes_order: List[str] = [DEFAULT_AXES_ORDER[ndim] for i in range(len(data))] |
| 73 | + |
| 74 | + if isinstance(slider_axes, (int)): |
| 75 | + self._slider_axes: List[int] = [slider_axes for i in range(len(data))] |
| 76 | + |
| 77 | + elif isinstance(slider_axes, str): |
| 78 | + self._slider_axes: List[int] = [self.axes_order.index(slider_axes)] |
| 79 | + |
| 80 | + self.sliders: List[IntSlider] = [ |
| 81 | + IntSlider( |
| 82 | + min=0, |
| 83 | + max=data.shape[slider_axes] - 1, |
| 84 | + value=0, |
| 85 | + step=1, |
| 86 | + description=f"slider axis: {slider_axes}" |
| 87 | + ) |
| 88 | + ] |
| 89 | + |
| 90 | + def slider_changed(self): |
| 91 | + pass |
| 92 | + |
| 93 | + def show(self): |
| 94 | + pass |
0 commit comments