Skip to content

Commit

Permalink
Don't allow bins lower than 0 if dtype is unisgned
Browse files Browse the repository at this point in the history
  • Loading branch information
p-j-smith committed Jan 11, 2024
1 parent 6bb2747 commit 2ec6e7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import napari

viewer = napari.Viewer()
viewer.open_sample("napari", "kidney")
viewer.open_sample("napari", "coins")

viewer.window.add_plugin_dock_widget(
plugin_name="napari-matplotlib", widget_name="Histogram"
Expand Down
19 changes: 12 additions & 7 deletions src/napari_matplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
bins_start.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
bins_start.setRange(-1e10, 1e10)
bins_start.setValue(0)
bins_start.setWrapping(True)
bins_start.setWrapping(False)
bins_start.setKeyboardTracking(False)
bins_start.setDecimals(2)

Expand All @@ -55,6 +55,7 @@ def __init__(
bins_stop.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
bins_stop.setRange(-1e10, 1e10)
bins_stop.setValue(100)
bins_start.setWrapping(False)
bins_stop.setKeyboardTracking(False)
bins_stop.setDecimals(2)

Expand Down Expand Up @@ -148,13 +149,17 @@ def on_update_layers(self) -> None:
self.autoset_widget_bins(data=layer_data)

# Only allow integer bins for integer data
# And only allow values greater than 0 for unsigned integers
n_decimals = 0 if np.issubdtype(layer_data.dtype, np.integer) else 2
self.findChild(QDoubleSpinBox, name="bins start").setDecimals(
n_decimals
)
self.findChild(QDoubleSpinBox, name="bins stop").setDecimals(
n_decimals
)
is_unsigned = layer_data.dtype.kind == "u"
minimum_value = 0 if is_unsigned else -1e10

bins_start = self.findChild(QDoubleSpinBox, name="bins start")
bins_stop = self.findChild(QDoubleSpinBox, name="bins stop")
bins_start.setDecimals(n_decimals)
bins_stop.setDecimals(n_decimals)
bins_start.setMinimum(minimum_value)
bins_stop.setMinimum(minimum_value)

def draw(self) -> None:
"""
Expand Down

0 comments on commit 2ec6e7b

Please sign in to comment.