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

Commit e79e5fa

Browse filesBrowse files
committed
Updates LUT to deal with divide by zero errors and updates figure shape logic in imagewidget to deal with multiple shape values in constructor
1 parent 9ff92a7 commit e79e5fa
Copy full SHA for e79e5fa

File tree

2 files changed

+25
-11
lines changed
Filter options

2 files changed

+25
-11
lines changed

‎fastplotlib/widgets/histogram_lut.py

Copy file name to clipboardExpand all lines: fastplotlib/widgets/histogram_lut.py
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def __init__(
2525
----------
2626
data
2727
image_graphic
28-
nbins
29-
flank_divisor: float, default 5.0
28+
nbins: int, defaut 100. Total number of bins used in the histogram
29+
flank_divisor: float, default 5.0. Fraction of empty histogram bins on the tails of the distribution
3030
set `np.inf` for no flanks
3131
kwargs
3232
"""
@@ -161,9 +161,10 @@ def _calculate_histogram(self, data):
161161
hist, edges = np.histogram(data_ss, bins=self._nbins)
162162

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

168169
edges = edges * self._scale_factor
169170

@@ -178,15 +179,17 @@ def _calculate_histogram(self, data):
178179
)
179180

180181
edges_flanked = np.concatenate((flank_left, edges, flank_right))
181-
np.unique(np.diff(edges_flanked))
182182

183183
hist_flanked = np.concatenate(
184184
(np.zeros(flank_nbins), hist, np.zeros(flank_nbins))
185185
)
186186

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

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

‎fastplotlib/widgets/image.py

Copy file name to clipboardExpand all lines: fastplotlib/widgets/image.py
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,25 @@ def __init__(
368368
if all([_is_arraylike(d) for d in data]):
369369
# Grid computations
370370
if figure_shape is None:
371-
figure_shape = calculate_figure_shape(len(data))
371+
if figure_kwargs is not None:
372+
if 'shape' in figure_kwargs:
373+
figure_shape = figure_kwargs['shape']
374+
else:
375+
figure_shape = calculate_figure_shape(len(data))
372376

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):
377+
# Regardless of how figure_shape is computed, below code
378+
# verifies that figure shape is large enough for the number of image arrays passed
379+
if figure_shape[0] * figure_shape[1] < len(data):
380+
original_shape = (figure_shape[0], figure_shape[1])
375381
figure_shape = calculate_figure_shape(len(data))
376382
warn(
377-
f"Invalid `figure_shape` passed, setting figure shape to: {figure_shape}"
383+
f"Original `figure_shape` was: {original_shape} "
384+
f" but data length is {len(data)}"
385+
f" Resetting figure shape to: {figure_shape}"
378386
)
379387

388+
389+
380390
self._data: list[np.ndarray] = data
381391

382392
# Establish number of image dimensions and number of scrollable dimensions for each array
@@ -506,13 +516,14 @@ def __init__(
506516
# update the default kwargs with any user-specified kwargs
507517
# user specified kwargs will overwrite the defaults
508518
figure_kwargs_default.update(figure_kwargs)
519+
figure_kwargs_default['shape'] = figure_shape
509520

510521
if graphic_kwargs is None:
511522
graphic_kwargs = dict()
512523

513524
graphic_kwargs.update({"cmap": cmap})
514525

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

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.