Skip to content

Commit

Permalink
[Fix] z score inplace (#592)
Browse files Browse the repository at this point in the history
* changed check if signal is subtype of float for inplace operations
* add regression test, testing for np.float32 and np.float64
  • Loading branch information
Moritz-Alexander-Kern authored Oct 20, 2023
1 parent b332287 commit 0354d0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion elephant/signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
19 changes: 18 additions & 1 deletion elephant/test/test_signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):

Expand Down

0 comments on commit 0354d0e

Please sign in to comment.