4
4
import numpy as np
5
5
import numpy .typing as npt
6
6
from matplotlib .container import BarContainer
7
+ from napari .layers import Image
8
+ from napari .layers ._multiscale_data import MultiScaleData
7
9
from qtpy .QtWidgets import (
8
10
QAbstractSpinBox ,
9
11
QComboBox ,
@@ -31,8 +33,9 @@ def _get_bins(data: npt.NDArray[Any]) -> npt.NDArray[Any]:
31
33
step = np .ceil (np .ptp (data ) / 100 )
32
34
return np .arange (np .min (data ), np .max (data ) + step , step )
33
35
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 )
36
39
37
40
38
41
class HistogramWidget (SingleAxesWidget ):
@@ -112,7 +115,7 @@ def on_update_layers(self) -> None:
112
115
if not self .layers :
113
116
return
114
117
115
- # Reset to bin start, stop and step
118
+ # Reset the bin start, stop and step values based on new layer data
116
119
layer_data = self ._get_layer_data (self .layers [0 ])
117
120
self .autoset_widget_bins (data = layer_data )
118
121
@@ -189,12 +192,16 @@ def bins_num(self, num: int) -> None:
189
192
190
193
def _get_layer_data (self , layer : napari .layers .Layer ) -> npt .NDArray [Any ]:
191
194
"""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 :
193
201
# 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 ]
195
204
self .axes .set_title (f"z={ self .current_z } " )
196
- else :
197
- data = layer .data
198
205
199
206
# Read data into memory if it's a dask array
200
207
data = np .asarray (data )
@@ -205,7 +212,7 @@ def draw(self) -> None:
205
212
"""
206
213
Clear the axes and histogram the currently selected layer/slice.
207
214
"""
208
- layer = self .layers [0 ]
215
+ layer : Image = self .layers [0 ]
209
216
data = self ._get_layer_data (layer )
210
217
211
218
# Important to calculate bins after slicing 3D data, to avoid reading
0 commit comments