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

Skip to content

separate array logic and graphic logic in ImageWidget #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastplotlib/widgets/image_widget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

if IMGUI:
from ._widget import ImageWidget
from ._array import ImageWidgetArray

else:

Expand Down
79 changes: 79 additions & 0 deletions fastplotlib/widgets/image_widget/_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np
from numpy.typing import NDArray
from typing import Literal, Callable


class ImageWidgetArray:
def __init__(
self,
data: NDArray,
window_functions: dict = None,
frame_apply: Callable = None,
display_dims: Literal[2, 3] = 2,
dim_names: str = "tzxy",
):
self._data = data
self._window_functions = window_functions
self._frame_apply = frame_apply
self._dim_names = dim_names

for k in self._window_functions:
if k not in dim_names:
raise KeyError

self._display_dims = display_dims

@property
def data(self) -> NDArray:
return self._data

@data.setter
def data(self, data: NDArray):
self._data = data

@property
def window_functions(self) -> dict | None:
return self._window_functions

@window_functions.setter
def window_functions(self, wf: dict | None):
self._window_functions = wf

@property
def frame_apply(self, fa: Callable | None):
self._frame_apply = fa

@frame_apply.setter
def frame_apply(self) -> Callable | None:
return self._frame_apply

def _apply_window_functions(self, array: NDArray, key):
if self.window_functions is not None:
for dim_name in self._window_functions.keys():
dim_index = self._dim_names.index(dim_name)

window_size = self.window_functions[dim_name][1]
half_window_size = int((window_size - 1) / 2)

max_bound = self._data.shape[dim_index]

window_indices = range()

else:
array = array[key]

return array

def __getitem__(self, key):
data = self._data


data = self._apply_window_functions(data, key)

if self.frame_apply is not None:
data = self.frame_apply(data)

if data.ndim != self._display_dims:
raise ValueError

return data
3 changes: 2 additions & 1 deletion fastplotlib/widgets/image_widget/_widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import Callable
from warnings import warn

Expand All @@ -11,6 +10,7 @@
from ...utils import calculate_figure_shape, quick_min_max
from ...tools import HistogramLUTTool
from ._sliders import ImageWidgetSliders
from ._array import ImageWidgetArray


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