Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd502e4

Browse files
committed
start separating iw plotting and array logic
1 parent 72a3f02 commit dd502e4

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

fastplotlib/widgets/image_widget/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
if IMGUI:
44
from ._widget import ImageWidget
5+
from ._array import ImageWidgetArray
56

67
else:
78

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import numpy as np
2+
from numpy.typing import NDArray
3+
from typing import Literal, Callable
4+
5+
6+
class ImageWidgetArray:
7+
def __init__(
8+
self,
9+
data: NDArray,
10+
window_functions: dict = None,
11+
frame_apply: Callable = None,
12+
display_dims: Literal[2, 3] = 2,
13+
dim_names: str = "tzxy",
14+
):
15+
self._data = data
16+
self._window_functions = window_functions
17+
self._frame_apply = frame_apply
18+
self._dim_names = dim_names
19+
20+
for k in self._window_functions:
21+
if k not in dim_names:
22+
raise KeyError
23+
24+
self._display_dims = display_dims
25+
26+
@property
27+
def data(self) -> NDArray:
28+
return self._data
29+
30+
@data.setter
31+
def data(self, data: NDArray):
32+
self._data = data
33+
34+
@property
35+
def window_functions(self) -> dict | None:
36+
return self._window_functions
37+
38+
@window_functions.setter
39+
def window_functions(self, wf: dict | None):
40+
self._window_functions = wf
41+
42+
@property
43+
def frame_apply(self, fa: Callable | None):
44+
self._frame_apply = fa
45+
46+
@frame_apply.setter
47+
def frame_apply(self) -> Callable | None:
48+
return self._frame_apply
49+
50+
def _apply_window_functions(self, array: NDArray, key):
51+
if self.window_functions is not None:
52+
for dim_name in self._window_functions.keys():
53+
dim_index = self._dim_names.index(dim_name)
54+
55+
window_size = self.window_functions[dim_name][1]
56+
half_window_size = int((window_size - 1) / 2)
57+
58+
max_bound = self._data.shape[dim_index]
59+
60+
window_indices = range()
61+
62+
else:
63+
array = array[key]
64+
65+
return array
66+
67+
def __getitem__(self, key):
68+
data = self._data
69+
70+
71+
data = self._apply_window_functions(data, key)
72+
73+
if self.frame_apply is not None:
74+
data = self.frame_apply(data)
75+
76+
if data.ndim != self._display_dims:
77+
raise ValueError
78+
79+
return data

fastplotlib/widgets/image_widget/_widget.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from copy import deepcopy
21
from typing import Callable
32
from warnings import warn
43

@@ -11,6 +10,7 @@
1110
from ...utils import calculate_figure_shape, quick_min_max
1211
from ...tools import HistogramLUTTool
1312
from ._sliders import ImageWidgetSliders
13+
from ._array import ImageWidgetArray
1414

1515

1616
# Number of dimensions that represent one image/one frame
@@ -289,6 +289,7 @@ def _get_n_scrollable_dims(self, curr_arr: np.ndarray, rgb: bool) -> list[int]:
289289
def __init__(
290290
self,
291291
data: np.ndarray | list[np.ndarray],
292+
array_types: ImageWidgetArray | list[ImageWidgetArray] = ImageWidgetArray,
292293
window_funcs: dict[str, tuple[Callable, int]] = None,
293294
frame_apply: Callable | dict[int, Callable] = None,
294295
figure_shape: tuple[int, int] = None,

0 commit comments

Comments
 (0)