From b176152858983618b83079596a7da1a9fa09dba6 Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Wed, 20 Jul 2022 17:44:20 +0200 Subject: [PATCH 1/2] Gracefully handle 0s when plotting in log scale Previously the behaviour was to add 1 before taking the log to avoid 0s causing inf's in the output, but in some cases with very small values that gives wildly incorrect results. Now we explicitly only take the log of non-zero values. --- extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py b/extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py index 03d949f3a..7bca380aa 100644 --- a/extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py +++ b/extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py @@ -129,7 +129,7 @@ def toLogScale(arr, policy=None): """Convert array result to logarithmic scale.""" ret = np.nan_to_num(arr) ret[ret < 0] = 0 - return np.log10(ret + 1) + return np.log10(ret, where=(ret > 0)) def name(self): """An identity of the PlotItem. From 23b8acf58e3a1f1bac6c92951b6684e0114f723d Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Thu, 21 Jul 2022 12:02:44 +0200 Subject: [PATCH 2/2] fixup! Gracefully handle 0s when plotting in log scale --- extra_foam/gui/plot_widgets/tests/test_plot_items.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extra_foam/gui/plot_widgets/tests/test_plot_items.py b/extra_foam/gui/plot_widgets/tests/test_plot_items.py index 90ba13d22..a58890a0a 100644 --- a/extra_foam/gui/plot_widgets/tests/test_plot_items.py +++ b/extra_foam/gui/plot_widgets/tests/test_plot_items.py @@ -92,15 +92,16 @@ def testCurvePlotItem(self, dtype): item.setData(np.arange(2).astype(dtype), np.arange(3).astype(dtype)) # test log mode + plot_bottom_right = np.log10(float(x[-1])) self._widget._plot_area._onLogXChanged(True) if dtype == float: _display() - assert item.boundingRect() == QRectF(0, 0, 1.0, 13.5) + assert item.boundingRect() == QRectF(0, 0, plot_bottom_right, 13.5) self._widget._plot_area._onLogYChanged(True) if dtype == float: _display() assert item.boundingRect().topLeft() == QPointF(0, 0) - assert item.boundingRect().bottomRight().x() == 1.0 + assert item.boundingRect().bottomRight().x() == plot_bottom_right assert 1.2 > item.boundingRect().bottomRight().y() > 1.1 # clear data