From 38fbccd9c6da1d91484a87eba27dadf8fff0b82e Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 21 Jul 2023 09:37:29 +0100 Subject: [PATCH 1/5] Don't read full cube into mem for histogramming --- src/napari_matplotlib/histogram.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index 39ad41a3..50c2bcc4 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -33,7 +33,6 @@ def draw(self) -> None: Clear the axes and histogram the currently selected layer/slice. """ layer = self.layers[0] - bins = np.linspace(np.min(layer.data), np.max(layer.data), 100) if layer.data.ndim - layer.rgb == 3: # 3D data, can be single channel or RGB @@ -42,6 +41,10 @@ def draw(self) -> None: else: data = layer.data + # Important to calculate bins after slicing 3D data, to avoid reading + # whole cube into memory. + bins = np.linspace(np.min(layer.data), np.max(layer.data), 100) + if layer.rgb: # Histogram RGB channels independently for i, c in enumerate("rgb"): From 1aa3efd13a57d0dc5ddd2b09af058bc27a125f9c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 26 Jul 2023 09:16:41 +0100 Subject: [PATCH 2/5] Actually don't read all bins into memory... --- src/napari_matplotlib/histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index 50c2bcc4..3b0c4234 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -43,7 +43,7 @@ def draw(self) -> None: # Important to calculate bins after slicing 3D data, to avoid reading # whole cube into memory. - bins = np.linspace(np.min(layer.data), np.max(layer.data), 100) + bins = np.linspace(np.min(data), np.max(data), 100) if layer.rgb: # Histogram RGB channels independently From 804934c7b5706d7d2fbd7845c3aab94584961561 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 26 Jul 2023 09:25:54 +0100 Subject: [PATCH 3/5] Read data into memory --- src/napari_matplotlib/histogram.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index 3b0c4234..a6cec3cf 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -33,6 +33,7 @@ def draw(self) -> None: Clear the axes and histogram the currently selected layer/slice. """ layer = self.layers[0] + print(layer.data.shape) if layer.data.ndim - layer.rgb == 3: # 3D data, can be single channel or RGB @@ -40,10 +41,13 @@ def draw(self) -> None: self.axes.set_title(f"z={self.current_z}") else: data = layer.data + # Read data into memory if it's a dask array + data = np.asarray(data) # Important to calculate bins after slicing 3D data, to avoid reading # whole cube into memory. bins = np.linspace(np.min(data), np.max(data), 100) + print(bins) if layer.rgb: # Histogram RGB channels independently From e84958d2b9d9a0c86f18a5b1e9281bd791dc65ce Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 26 Jul 2023 15:21:29 +0100 Subject: [PATCH 4/5] Remove print statements --- src/napari_matplotlib/histogram.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index a6cec3cf..ab098f38 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -33,7 +33,6 @@ def draw(self) -> None: Clear the axes and histogram the currently selected layer/slice. """ layer = self.layers[0] - print(layer.data.shape) if layer.data.ndim - layer.rgb == 3: # 3D data, can be single channel or RGB @@ -47,7 +46,6 @@ def draw(self) -> None: # Important to calculate bins after slicing 3D data, to avoid reading # whole cube into memory. bins = np.linspace(np.min(data), np.max(data), 100) - print(bins) if layer.rgb: # Histogram RGB channels independently From 0523d2844a7e02a9702a3e31f21bd767e8ad61f9 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 26 Jul 2023 15:23:04 +0100 Subject: [PATCH 5/5] Add changelog --- docs/changelog.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index a31a0d3a..e3b62dae 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,5 +1,16 @@ Changelog ========= +1.0.2 +----- +Bug fixes +~~~~~~~~~ +- A full dataset is no longer read into memory when using ``HistogramWidget``. + Only the current slice is loaded. + +Changes +~~~~~~~ +- Histogram bin limits are now caclualted from the slice being histogrammed, and + not the whole dataset. This is as a result of the above bug fix. 1.0.1 -----