Skip to content

Sync Subplots by Name #267

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

Closed
wants to merge 7 commits into from
Closed
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
190 changes: 69 additions & 121 deletions examples/notebooks/gridplot_simple.ipynb

Large diffs are not rendered by default.

58 changes: 45 additions & 13 deletions fastplotlib/layouts/_gridplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def to_array(a) -> np.ndarray:
if not isinstance(a, list):
raise TypeError("must pass list or numpy array")

return np.array(a)
return np.array(a, dtype=object)


valid_cameras = ["2d", "2d-big", "3d", "3d-big"]
Expand All @@ -38,6 +38,7 @@ def __init__(
shape: Tuple[int, int],
cameras: Union[np.ndarray, str] = "2d",
controllers: Union[np.ndarray, str] = None,
names: Union[np.ndarray, str] = None,
canvas: Union[str, WgpuCanvas, pygfx.Texture] = None,
renderer: pygfx.WgpuRenderer = None,
size: Tuple[int, int] = (500, 300),
Expand Down Expand Up @@ -92,11 +93,49 @@ def __init__(
self.shape
)

if isinstance(controllers, str):
if controllers == "sync":
controllers = np.zeros(
self.shape[0] * self.shape[1], dtype=int
).reshape(self.shape)
if names is not None:
self.names = to_array(names)
# Check if shape of names kwarg is same as gridplot shape
if self.names.shape != self.shape:
raise ValueError(f"subplot names: {self.names} must be in gridplot shape: {self.shape}")
# Check if all elements in names array are strings
if not all(isinstance(self.names[i], str) for i in product(range(self.shape[0]), range(self.shape[1]))):
raise ValueError(f"subplot names: {self.names} must all be strings")
else:
self.names = None

if controllers is not None:
# If value in controllers is 'sync', set all subplots to same controller
if isinstance(controllers, str):
if controllers == "sync":
controllers = np.zeros(
self.shape[0] * self.shape[1], dtype=int
).reshape(self.shape)
else:
c = to_array(controllers)
# Confirm number of elements in controller array is same as number in gridplot shape
if (c.shape[0]*c.shape[1]) != (self.shape[0]*self.shape[1]):
raise ValueError(f"number of controllers: {len(controllers)} must be the same as number of elements"
f" in gridplot shape {self.shape}")
# Confirm controllers array doesn't have multiple dtypes
if any(isinstance(c[i], str) for i in product(range(c.shape[0]), range(c.shape[1]))) and \
any(not isinstance(c[i], str) for i in product(range(c.shape[0]), range(c.shape[1]))):
raise ValueError(f"controllers: {controllers} must all be the same type")
# Check if names kwarg is given, and if all elements in controller array are strings
if self.names is not None:
if all(isinstance(c[i], str) for i in product(range(c.shape[0]), range(c.shape[1]))):
controllers = np.zeros(
self.shape[0] * self.shape[1], dtype=int
).reshape(self.shape)
# For each value in names array, find corresponding index in controller array
for positions in product(range(self.shape[0]), range(self.shape[1])):
controller_idx = np.argwhere(c == self.names[positions])
# If name exists in controller array, set controller for item to the row number
if len(controller_idx) != 0:
controllers[positions] = controller_idx[0][0]
else:
raise ValueError(f"string names in controllers: {c} must be the same as "
f"string names in names: {self.names}")

if controllers is None:
controllers = np.arange(self.shape[0] * self.shape[1]).reshape(self.shape)
Expand Down Expand Up @@ -144,13 +183,6 @@ def __init__(
if renderer is None:
renderer = pygfx.renderers.WgpuRenderer(canvas)

if "names" in kwargs.keys():
self.names = to_array(kwargs["names"])
if self.names.shape != self.shape:
raise ValueError
else:
self.names = None

self.canvas = canvas
self.renderer = renderer

Expand Down