From 999dbe87a5b9a0b138e89f82495d31e355cd2e9e Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Tue, 23 Jan 2024 12:00:15 -0800 Subject: [PATCH] Do less work in zscore constructor --- .../preprocessing/normalize_scale.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/spikeinterface/preprocessing/normalize_scale.py b/src/spikeinterface/preprocessing/normalize_scale.py index f24aff6e79..1d3730f8e8 100644 --- a/src/spikeinterface/preprocessing/normalize_scale.py +++ b/src/spikeinterface/preprocessing/normalize_scale.py @@ -272,8 +272,6 @@ def __init__( if dtype_.kind == "i": assert int_scale is not None, "For recording with dtype=int you must set dtype=float32 OR set a scale" - random_data = get_random_data_chunks(recording, **random_chunk_kwargs) - if gain is not None: assert offset is not None gain = np.asarray(gain) @@ -285,20 +283,23 @@ def __init__( if offset.ndim == 1: offset = offset[None, :] assert offset.shape[1] == n - elif mode == "median+mad": - medians = np.median(random_data, axis=0) - medians = medians[None, :] - mads = np.median(np.abs(random_data - medians), axis=0) / 0.6744897501960817 - mads = mads[None, :] - gain = 1 / mads - offset = -medians / mads else: - means = np.mean(random_data, axis=0) - means = means[None, :] - stds = np.std(random_data, axis=0) - stds = stds[None, :] - gain = 1.0 / stds - offset = -means / stds + random_data = get_random_data_chunks(recording, **random_chunk_kwargs) + + if mode == "median+mad": + medians = np.median(random_data, axis=0) + medians = medians[None, :] + mads = np.median(np.abs(random_data - medians), axis=0) / 0.6744897501960817 + mads = mads[None, :] + gain = 1 / mads + offset = -medians / mads + else: + means = np.mean(random_data, axis=0) + means = means[None, :] + stds = np.std(random_data, axis=0) + stds = stds[None, :] + gain = 1.0 / stds + offset = -means / stds if int_scale is not None: gain *= int_scale