From 0354d0ed384592f2bf9aee21a559a4a7b4b59d48 Mon Sep 17 00:00:00 2001 From: Moritz Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:50:03 +0200 Subject: [PATCH] [Fix] z score inplace (#592) * changed check if signal is subtype of float for inplace operations * add regression test, testing for np.float32 and np.float64 --- elephant/signal_processing.py | 2 +- elephant/test/test_signal_processing.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/elephant/signal_processing.py b/elephant/signal_processing.py index 9a09f3fa8..1e8fe8631 100644 --- a/elephant/signal_processing.py +++ b/elephant/signal_processing.py @@ -167,7 +167,7 @@ def zscore(signal, inplace=True): for sig in signal: # Perform inplace operation only if array is of dtype float. # Otherwise, raise an error. - if inplace and not np.issubdtype(float, sig.dtype): + if inplace and not np.issubdtype(sig.dtype, np.floating): raise ValueError(f"Cannot perform inplace operation as the " f"signal dtype is not float. Source: {sig.name}") diff --git a/elephant/test/test_signal_processing.py b/elephant/test/test_signal_processing.py index 303e3cdfd..83e51d4f6 100644 --- a/elephant/test/test_signal_processing.py +++ b/elephant/test/test_signal_processing.py @@ -401,7 +401,7 @@ def test_zscore_list_inplace(self): self.assertIs(result[0], signal_list[0]) self.assertIs(result[1], signal_list[1]) - def test_wrong_input(self): + def test_z_score_wrong_input(self): # wrong type self.assertRaises(TypeError, elephant.signal_processing.zscore, signal=[1, 2] * pq.uV) @@ -411,6 +411,23 @@ def test_wrong_input(self): self.assertRaises(ValueError, elephant.signal_processing.zscore, signal=[asig1, asig2]) + def test_z_score_np_float32_64(self): + """ + Regression test: Inplace operations for z_score failed when using + np.float32 or np.float64 types. + See Issue #591. + https://github.com/NeuralEnsemble/elephant/issues/591 + """ + test_types = (np.float32, np.float64) + for test_type in test_types: + with self.subTest(test_type): + signal = neo.AnalogSignal(self.test_seq1, units='mV', + t_start=0. * pq.ms, + sampling_rate=1000. * pq.Hz, + dtype=test_type) + # This should not raise a ValueError + elephant.signal_processing.zscore(signal, inplace=True) + class ButterTestCase(unittest.TestCase):