diff --git a/fastplotlib/widgets/image_widget/_widget.py b/fastplotlib/widgets/image_widget/_widget.py index 2773470c..79330104 100644 --- a/fastplotlib/widgets/image_widget/_widget.py +++ b/fastplotlib/widgets/image_widget/_widget.py @@ -38,7 +38,7 @@ def _is_arraylike(obj) -> bool: Checks if the object is array-like. For now just checks if obj has `__getitem__()` """ - for attr in ["__getitem__", "shape", "ndim"]: + for attr in ["__getitem__", "shape"]: if not hasattr(obj, attr): return False @@ -199,7 +199,7 @@ def current_index(self, index: dict[str, int]): return try: - self._reentrant_block = True # block re-execution until current_index has *fully* completed execution + self._reentrant_block = True # block re-execution until current_index has *fully* completed execution if not set(index.keys()).issubset(set(self._current_index.keys())): raise KeyError( f"All dimension keys for setting `current_index` must be present in the widget sliders. " @@ -210,7 +210,9 @@ def current_index(self, index: dict[str, int]): if not isinstance(val, int): raise TypeError("Indices for all dimensions must be int") if val < 0: - raise IndexError("negative indexing is not supported for ImageWidget") + raise IndexError( + "negative indexing is not supported for ImageWidget" + ) if val > self._dims_max_bounds[k]: raise IndexError( f"index {val} is out of bounds for dimension '{k}' " @@ -367,6 +369,11 @@ def __init__( if isinstance(data, list): # verify that it's a list of np.ndarray if all([_is_arraylike(d) for d in data]): + + for i, d in enumerate(data): + if not hasattr(d, "ndim"): + data[i] = np.asarray(d) + # Grid computations if figure_shape is None: if "shape" in figure_kwargs: @@ -908,6 +915,11 @@ def set_data( ) # check all arrays for i, (new_array, current_array) in enumerate(zip(new_data, self._data)): + + # if we don't know the ndim, we can get it from the shape + if not hasattr(new_array, "ndim"): + new_array.ndim = len(new_array.shape) + if new_array.ndim != current_array.ndim: raise ValueError( f"new data ndim {new_array.ndim} at index {i} "