Skip to content

Commit 45357bc

Browse files
authored
move calc_gridshape, docstrings, make plotarea name a property (#220)
1 parent c53b262 commit 45357bc

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

fastplotlib/layouts/_base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(
8787
# managed similar to GRAPHICS for garbage collection etc.
8888
self._selectors: List[str] = list()
8989

90-
self.name = name
90+
self._name = name
9191

9292
# need to think about how to deal with children better
9393
self.children = list()
@@ -155,6 +155,14 @@ def selectors(self) -> Tuple[BaseSelector, ...]:
155155

156156
return tuple(proxies)
157157

158+
@property
159+
def name(self) -> Any:
160+
return self._name
161+
162+
@name.setter
163+
def name(self, name: Any):
164+
self._name = name
165+
158166
def get_rect(self) -> Tuple[float, float, float, float]:
159167
"""allows setting the region occupied by the viewport w.r.t. the parent"""
160168
raise NotImplementedError("Must be implemented in subclass")

fastplotlib/layouts/_gridplot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,17 @@ def show(self, toolbar: bool = True):
291291
return VBox([self.canvas, self.toolbar.widget])
292292

293293
def close(self):
294+
"""Close the GridPlot"""
294295
self.canvas.close()
295296

296297
if self.toolbar is not None:
297298
self.toolbar.widget.close()
298299

300+
def clear(self):
301+
"""Clear all Subplots"""
302+
for subplot in self:
303+
subplot.clear()
304+
299305
def _get_iterator(self):
300306
return product(range(self.shape[0]), range(self.shape[1]))
301307

fastplotlib/layouts/_subplot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy:
129129
# only return a proxy to the real graphic
130130
return weakref.proxy(graphic)
131131

132+
@property
133+
def name(self) -> Any:
134+
return self._name
135+
136+
@name.setter
137+
def name(self, name: Any):
138+
self._name = name
139+
self.set_title(name)
140+
132141
def set_title(self, text: Any):
133142
"""Sets the name of a subplot to 'top' viewport if defined."""
134143
if text is None:

fastplotlib/plot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def show(self, autoscale: bool = True, toolbar: bool = True):
129129
return VBox([self.canvas, self.toolbar.widget])
130130

131131
def close(self):
132+
"""Close Plot"""
132133
self.canvas.close()
133134

134135
if self.toolbar is not None:

fastplotlib/utils/functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,15 @@ def make_pygfx_colors(colors, n_colors):
154154
c = Color(colors)
155155
colors_array = np.repeat(np.array([c]), n_colors, axis=0)
156156
return colors_array
157+
158+
159+
def calculate_gridshape(n_subplots: int) -> Tuple[int, int]:
160+
"""
161+
Returns ``(n_rows, n_cols)`` from given number of subplots ``n_subplots``
162+
"""
163+
sr = np.sqrt(n_subplots)
164+
165+
return (
166+
int(np.round(sr)),
167+
int(np.ceil(sr))
168+
)

fastplotlib/widgets/image.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..plot import Plot
1212
from ..layouts import GridPlot
1313
from ..graphics import ImageGraphic
14-
from ..utils import quick_min_max
14+
from ..utils import quick_min_max, calculate_gridshape
1515

1616

1717
DEFAULT_DIMS_ORDER = \
@@ -23,14 +23,6 @@
2323
}
2424

2525

26-
def _calc_gridshape(n):
27-
sr = np.sqrt(n)
28-
return (
29-
int(np.round(sr)),
30-
int(np.ceil(sr))
31-
)
32-
33-
3426
def _is_arraylike(obj) -> bool:
3527
"""
3628
Checks if the object is array-like.
@@ -248,11 +240,11 @@ def __init__(
248240
# verify that it's a list of np.ndarray
249241
if all([_is_arraylike(d) for d in data]):
250242
if grid_shape is None:
251-
grid_shape = _calc_gridshape(len(data))
243+
grid_shape = calculate_gridshape(len(data))
252244

253245
# verify that user-specified grid shape is large enough for the number of image arrays passed
254246
elif grid_shape[0] * grid_shape[1] < len(data):
255-
grid_shape = _calc_gridshape(len(data))
247+
grid_shape = calculate_gridshape(len(data))
256248
warn(f"Invalid `grid_shape` passed, setting grid shape to: {grid_shape}")
257249

258250
_ndim = [d.ndim for d in data]
@@ -821,11 +813,22 @@ def _set_slider_layout(self, *args):
821813
for mm in self.vmin_vmax_sliders:
822814
mm.layout = Layout(width=f"{w}px")
823815

824-
def _get_vmin_vmax_range(self, data: np.ndarray) -> Tuple[int, int]:
816+
def _get_vmin_vmax_range(self, data: np.ndarray) -> tuple:
817+
"""
818+
Parameters
819+
----------
820+
data
821+
822+
Returns
823+
-------
824+
Tuple[Tuple[float, float], float, float, float]
825+
(min, max), data_range, min - (data_range * 0.4), max + (data_range * 0.4)
826+
"""
827+
825828
minmax = quick_min_max(data)
826829

827830
data_range = np.ptp(minmax)
828-
data_range_40p = np.ptp(minmax) * 0.4
831+
data_range_40p = data_range * 0.4
829832

830833
_range = (
831834
minmax,

0 commit comments

Comments
 (0)