44import numpy as np
55import numpy .typing as npt
66from matplotlib .container import BarContainer
7+ from napari .layers import Image
8+ from napari .layers ._multiscale_data import MultiScaleData
79from qtpy .QtWidgets import (
810 QAbstractSpinBox ,
911 QComboBox ,
@@ -31,8 +33,9 @@ def _get_bins(data: npt.NDArray[Any]) -> npt.NDArray[Any]:
3133 step = np .ceil (np .ptp (data ) / 100 )
3234 return np .arange (np .min (data ), np .max (data ) + step , step )
3335 else :
34- # For other data types, just have 99 evenly spaced bins
35- return np .linspace (np .min (data ), np .max (data ), 100 )
36+ # For other data types, just have 100 evenly spaced bins
37+ # (and 101 bin edges)
38+ return np .linspace (np .min (data ), np .max (data ), 101 )
3639
3740
3841class HistogramWidget (SingleAxesWidget ):
@@ -112,7 +115,7 @@ def on_update_layers(self) -> None:
112115 if not self .layers :
113116 return
114117
115- # Reset to bin start, stop and step
118+ # Reset the bin start, stop and step values based on new layer data
116119 layer_data = self ._get_layer_data (self .layers [0 ])
117120 self .autoset_widget_bins (data = layer_data )
118121
@@ -189,12 +192,16 @@ def bins_num(self, num: int) -> None:
189192
190193 def _get_layer_data (self , layer : napari .layers .Layer ) -> npt .NDArray [Any ]:
191194 """Get the data associated with a given layer"""
192- if layer .data .ndim - layer .rgb == 3 :
195+ data = layer .data
196+
197+ if isinstance (layer .data , MultiScaleData ):
198+ data = data [layer .data_level ]
199+
200+ if layer .ndim - layer .rgb == 3 :
193201 # 3D data, can be single channel or RGB
194- data = layer .data [self .current_z ]
202+ # Slice in z dimension
203+ data = data [self .current_z ]
195204 self .axes .set_title (f"z={ self .current_z } " )
196- else :
197- data = layer .data
198205
199206 # Read data into memory if it's a dask array
200207 data = np .asarray (data )
@@ -205,7 +212,7 @@ def draw(self) -> None:
205212 """
206213 Clear the axes and histogram the currently selected layer/slice.
207214 """
208- layer = self .layers [0 ]
215+ layer : Image = self .layers [0 ]
209216 data = self ._get_layer_data (layer )
210217
211218 # Important to calculate bins after slicing 3D data, to avoid reading
0 commit comments