Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ImageWidget fix frame_apply(), add grid_plot_kwargs(), reset_vmin_vmax() #148

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

Merged
merged 3 commits into from
Mar 9, 2023
Merged
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
42 changes: 26 additions & 16 deletions 42 fastplotlib/layouts/_gridplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def __init__(
# create the array representing the views for each subplot in the grid
cameras = np.array([cameras] * self.shape[0] * self.shape[1]).reshape(self.shape)

if controllers == "sync":
controllers = np.zeros(self.shape[0] * self.shape[1], dtype=int).reshape(self.shape)
if isinstance(controllers, str):
if controllers == "sync":
controllers = np.zeros(self.shape[0] * self.shape[1], dtype=int).reshape(self.shape)

if controllers is None:
controllers = np.arange(self.shape[0] * self.shape[1]).reshape(self.shape)
Expand All @@ -82,13 +83,31 @@ def __init__(
if controllers.shape != self.shape:
raise ValueError

self._controllers = np.empty(shape=cameras.shape, dtype=object)

cameras = to_array(cameras)

if cameras.shape != self.shape:
raise ValueError

if not np.all(np.sort(np.unique(controllers)) == np.arange(np.unique(controllers).size)):
raise ValueError("controllers must be consecutive integers")
# create controllers if the arguments were integers
if np.issubdtype(controllers.dtype, np.integer):
if not np.all(np.sort(np.unique(controllers)) == np.arange(np.unique(controllers).size)):
raise ValueError("controllers must be consecutive integers")

for controller in np.unique(controllers):
cam = np.unique(cameras[controllers == controller])
if cam.size > 1:
raise ValueError(
f"Controller id: {controller} has been assigned to multiple different camera types")

self._controllers[controllers == controller] = create_controller(cam[0])
# else assume it's a single pygfx.Controller instance or a list of controllers
else:
if isinstance(controllers, pygfx.Controller):
self._controllers = np.array([controllers] * shape[0] * shape[1]).reshape(shape)
else:
self._controllers = np.array(controllers).reshape(shape)

if canvas is None:
canvas = WgpuCanvas()
Expand All @@ -111,18 +130,9 @@ def __init__(
self._subplots: np.ndarray[Subplot] = np.ndarray(shape=(nrows, ncols), dtype=object)
# self.viewports: np.ndarray[Subplot] = np.ndarray(shape=(nrows, ncols), dtype=object)

self._controllers: List[pygfx.PanZoomController] = [
pygfx.PanZoomController() for i in range(np.unique(controllers).size)
]

self._controllers = np.empty(shape=cameras.shape, dtype=object)

for controller in np.unique(controllers):
cam = np.unique(cameras[controllers == controller])
if cam.size > 1:
raise ValueError(f"Controller id: {controller} has been assigned to multiple different camera types")

self._controllers[controllers == controller] = create_controller(cam[0])
# self._controllers: List[pygfx.PanZoomController] = [
# pygfx.PanZoomController() for i in range(np.unique(controllers).size)
# ]

for i, j in self._get_iterator():
position = (i, j)
Expand Down
57 changes: 49 additions & 8 deletions 57 fastplotlib/widgets/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def __init__(
vmin_vmax_sliders: bool = False,
grid_shape: Tuple[int, int] = None,
names: List[str] = None,
grid_plot_kwargs: dict = None,
**kwargs
):
"""
Expand Down Expand Up @@ -227,6 +228,9 @@ def __init__(
grid_shape: Optional[Tuple[int, int]]
manually provide the shape for a gridplot, otherwise a square gridplot is approximated.

grid_plot_kwargs: dict, optional
passed to `GridPlot`

names: Optional[str]
gives names to the subplots

Expand Down Expand Up @@ -471,12 +475,12 @@ def __init__(

if vmin_vmax_sliders:
data_range = np.ptp(minmax)
data_range_30p = np.ptp(minmax) * 0.3
data_range_40p = np.ptp(minmax) * 0.3

minmax_slider = FloatRangeSlider(
value=minmax,
min=minmax[0] - data_range_30p,
max=minmax[1] + data_range_30p,
min=minmax[0] - data_range_40p,
max=minmax[1] + data_range_40p,
step=data_range / 150,
description=f"min-max",
readout=True,
Expand All @@ -494,11 +498,15 @@ def __init__(
kwargs["vmin"], kwargs["vmax"] = minmax

frame = self._process_indices(self.data[0], slice_indices=self._current_index)
frame = self._process_frame_apply(frame, 0)

self.image_graphics: List[ImageGraphic] = [self.plot.add_image(data=frame, name="image", **kwargs)]

elif self._plot_type == "grid":
self._plot: GridPlot = GridPlot(shape=grid_shape, controllers="sync")
if grid_plot_kwargs is None:
grid_plot_kwargs = {"controllers": "sync"}

self._plot: GridPlot = GridPlot(shape=grid_shape, **grid_plot_kwargs)

self.image_graphics = list()
for data_ix, (d, subplot) in enumerate(zip(self.data, self.plot)):
Expand All @@ -513,12 +521,12 @@ def __init__(

if vmin_vmax_sliders:
data_range = np.ptp(minmax)
data_range_30p = np.ptp(minmax) * 0.4
data_range_40p = np.ptp(minmax) * 0.4

minmax_slider = FloatRangeSlider(
value=minmax,
min=minmax[0] - data_range_30p,
max=minmax[1] + data_range_30p,
min=minmax[0] - data_range_40p,
max=minmax[1] + data_range_40p,
step=data_range / 150,
description=f"mm: {name_slider}",
readout=True,
Expand All @@ -539,6 +547,7 @@ def __init__(
_kwargs = kwargs

frame = self._process_indices(d, slice_indices=self._current_index)
frame = self._process_frame_apply(frame, data_ix)
ig = ImageGraphic(frame, name="image", **_kwargs)
subplot.add_graphic(ig)
subplot.name = name
Expand Down Expand Up @@ -767,11 +776,17 @@ def _get_window_indices(self, data_ix, dim, indices_dim):
return indices_dim

def _process_frame_apply(self, array, data_ix) -> np.ndarray:
if callable(self.frame_apply):
return self.frame_apply(array)

if data_ix not in self.frame_apply.keys():
return array
if self.frame_apply[data_ix] is not None:

elif self.frame_apply[data_ix] is not None:
return self.frame_apply[data_ix](array)

return array

def _slider_value_changed(
self,
dimension: str,
Expand Down Expand Up @@ -801,6 +816,32 @@ def _set_slider_layout(self, *args):
for mm in self.vmin_vmax_sliders:
mm.layout = Layout(width=f"{w}px")

def _get_vmin_vmax_range(self, data: np.ndarray) -> Tuple[int, int]:
minmax = quick_min_max(data)

data_range = np.ptp(minmax)
data_range_40p = np.ptp(minmax) * 0.4

_range = (
minmax,
data_range,
minmax[0] - data_range_40p,
minmax[1] + data_range_40p
)

return _range

def reset_vmin_vmax(self):
"""
Reset the vmin and vmax w.r.t. the currently displayed image(s)
"""
for i, ig in enumerate(self.image_graphics):
mm = self._get_vmin_vmax_range(ig.data())
self.vmin_vmax_sliders[i].min = mm[2]
self.vmin_vmax_sliders[i].max = mm[3]
self.vmin_vmax_sliders[i].step = mm[1] / 150
self.vmin_vmax_sliders[i].value = mm[0]

def show(self):
"""
Show the widget
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.