Skip to content

Commit 5458316

Browse files
authored
Merge pull request #555 from apasarkar/iw_hist_and_kwargs
Figure_shape in imagewidget and related histogram widget bug fix
2 parents 0b3d364 + 90af0d4 commit 5458316

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

fastplotlib/widgets/histogram_lut.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def __init__(
2525
----------
2626
data
2727
image_graphic
28-
nbins
29-
flank_divisor: float, default 5.0
30-
set `np.inf` for no flanks
28+
nbins: int, defaut 100.
29+
Total number of bins used in the histogram
30+
flank_divisor: float, default 5.0.
31+
Fraction of empty histogram bins on the tails of the distribution set `np.inf` for no flanks
3132
kwargs
3233
"""
3334
super().__init__(**kwargs)
@@ -161,9 +162,10 @@ def _calculate_histogram(self, data):
161162
hist, edges = np.histogram(data_ss, bins=self._nbins)
162163

163164
# used if data ptp <= 10 because event things get weird
164-
# with tiny world objects due to floating point error
165+
# with tiny world objects due to floating point error
165166
# so if ptp <= 10, scale up by a factor
166-
self._scale_factor: int = max(1, 100 * int(10 / np.ptp(data_ss)))
167+
data_interval = edges[-1] - edges[0]
168+
self._scale_factor: int = max(1, 100 * int(10 / data_interval))
167169

168170
edges = edges * self._scale_factor
169171

@@ -178,15 +180,17 @@ def _calculate_histogram(self, data):
178180
)
179181

180182
edges_flanked = np.concatenate((flank_left, edges, flank_right))
181-
np.unique(np.diff(edges_flanked))
182183

183184
hist_flanked = np.concatenate(
184185
(np.zeros(flank_nbins), hist, np.zeros(flank_nbins))
185186
)
186187

187188
# scale 0-100 to make it easier to see
188189
# float32 data can produce unnecessarily high values
189-
hist_scaled = hist_flanked / (hist_flanked.max() / 100)
190+
hist_scale_value = hist_flanked.max()
191+
if np.allclose(hist_scale_value, 0):
192+
hist_scale_value = 1
193+
hist_scaled = hist_flanked / (hist_scale_value / 100)
190194

191195
if edges_flanked.size > hist_scaled.size:
192196
# we don't care about accuracy here so if it's off by 1-2 bins that's fine

fastplotlib/widgets/image.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ def __init__(
357357
"""
358358
self._names = None
359359

360+
if figure_kwargs is None:
361+
figure_kwargs = dict()
362+
360363
# output context
361364
self._output = None
362365

@@ -368,13 +371,20 @@ def __init__(
368371
if all([_is_arraylike(d) for d in data]):
369372
# Grid computations
370373
if figure_shape is None:
371-
figure_shape = calculate_figure_shape(len(data))
374+
if "shape" in figure_kwargs:
375+
figure_shape = figure_kwargs["shape"]
376+
else:
377+
figure_shape = calculate_figure_shape(len(data))
372378

373-
# verify that user-specified figure shape is large enough for the number of image arrays passed
374-
elif figure_shape[0] * figure_shape[1] < len(data):
379+
# Regardless of how figure_shape is computed, below code
380+
# verifies that figure shape is large enough for the number of image arrays passed
381+
if figure_shape[0] * figure_shape[1] < len(data):
382+
original_shape = (figure_shape[0], figure_shape[1])
375383
figure_shape = calculate_figure_shape(len(data))
376384
warn(
377-
f"Invalid `figure_shape` passed, setting figure shape to: {figure_shape}"
385+
f"Original `figure_shape` was: {original_shape} "
386+
f" but data length is {len(data)}"
387+
f" Resetting figure shape to: {figure_shape}"
378388
)
379389

380390
self._data: list[np.ndarray] = data
@@ -500,19 +510,18 @@ def __init__(
500510
)
501511

502512
figure_kwargs_default = {"controller_ids": "sync"}
503-
if figure_kwargs is None:
504-
figure_kwargs = dict()
505513

506514
# update the default kwargs with any user-specified kwargs
507515
# user specified kwargs will overwrite the defaults
508516
figure_kwargs_default.update(figure_kwargs)
517+
figure_kwargs_default["shape"] = figure_shape
509518

510519
if graphic_kwargs is None:
511520
graphic_kwargs = dict()
512521

513522
graphic_kwargs.update({"cmap": cmap})
514523

515-
self._figure: Figure = Figure(shape=figure_shape, **figure_kwargs_default)
524+
self._figure: Figure = Figure(**figure_kwargs_default)
516525

517526
self._histogram_widget = histogram_widget
518527
for data_ix, (d, subplot) in enumerate(zip(self.data, self.figure)):

0 commit comments

Comments
 (0)