From 7e6a70dcad5dc0ee8948d5b93d7e2bf7a3fbabd6 Mon Sep 17 00:00:00 2001 From: felixp8 Date: Tue, 3 Oct 2023 11:23:52 +0000 Subject: [PATCH 1/3] modify binning behavior in compute_smoothed_firing_rate --- nwbwidgets/analysis/spikes.py | 3 ++- test/test_analysis_spikes.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/test_analysis_spikes.py diff --git a/nwbwidgets/analysis/spikes.py b/nwbwidgets/analysis/spikes.py index e7c2c471..f853f8e2 100644 --- a/nwbwidgets/analysis/spikes.py +++ b/nwbwidgets/analysis/spikes.py @@ -17,7 +17,8 @@ def compute_smoothed_firing_rate(spike_times, tt, sigma_in_secs): if len(spike_times) < 2: return np.zeros_like(tt) binned_spikes = np.zeros_like(tt) - binned_spikes[np.searchsorted(tt, spike_times)] += 1 + spike_idx = np.searchsorted(tt, spike_times, side='right') - 1 + np.add.at(binned_spikes, spike_idx, 1) dt = np.diff(tt[:2])[0] sigma_in_samps = sigma_in_secs / dt smooth_fr = scipy.ndimage.gaussian_filter1d(binned_spikes, sigma_in_samps) / dt diff --git a/test/test_analysis_spikes.py b/test/test_analysis_spikes.py new file mode 100644 index 00000000..8ff5b76f --- /dev/null +++ b/test/test_analysis_spikes.py @@ -0,0 +1,14 @@ +import numpy as np +from nwbwidgets.analysis.spikes import compute_smoothed_firing_rate + + +def test_compute_smoothed_firing_rate(): + spike_times = np.array([1.0, 2.0, 5.0, 5.5, 7.0, 7.5, 8.0]) + tt = np.arange(10, dtype=float) + expected_binned_spikes = np.array([0.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 2.0, 1.0, 0.0]) + expected_smoothed_spikes = np.array([0.35438556, 0.64574827, 0.64991247, 0.40421249, 0.55136343, + 0.91486675, 1.02201074, 1.14797447, 0.89644961, 0.41307621]) + binned_spikes = compute_smoothed_firing_rate(spike_times, tt, 0.001) + smoothed_spikes = compute_smoothed_firing_rate(spike_times, tt, 1.0) + np.testing.assert_allclose(expected_binned_spikes, binned_spikes) + np.testing.assert_allclose(expected_smoothed_spikes, smoothed_spikes) \ No newline at end of file From f0654194c8e966277855a2f418f4f443a9f69b43 Mon Sep 17 00:00:00 2001 From: felixp8 Date: Tue, 3 Oct 2023 13:18:12 +0000 Subject: [PATCH 2/3] add additional checks --- nwbwidgets/analysis/spikes.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nwbwidgets/analysis/spikes.py b/nwbwidgets/analysis/spikes.py index f853f8e2..bc343f38 100644 --- a/nwbwidgets/analysis/spikes.py +++ b/nwbwidgets/analysis/spikes.py @@ -14,11 +14,16 @@ def compute_smoothed_firing_rate(spike_times, tt, sigma_in_secs): Returns: Gaussian smoothing evaluated at array t """ - if len(spike_times) < 2: + if len(spike_times) < 1: return np.zeros_like(tt) binned_spikes = np.zeros_like(tt) + # find bin indices for spike times spike_idx = np.searchsorted(tt, spike_times, side='right') - 1 + # filter out negative spike idxs, though there shouldn't be any + spike_idx = spike_idx[spike_idx >= 0].astype(int) + # increment binned spike count at bin indices np.add.at(binned_spikes, spike_idx, 1) + # smooth data dt = np.diff(tt[:2])[0] sigma_in_samps = sigma_in_secs / dt smooth_fr = scipy.ndimage.gaussian_filter1d(binned_spikes, sigma_in_samps) / dt From fd255d4474ecf2a46450e8cc020a9a1b73c3a01d Mon Sep 17 00:00:00 2001 From: felixp8 Date: Tue, 3 Oct 2023 13:30:54 +0000 Subject: [PATCH 3/3] pre-commit --- nwbwidgets/analysis/spikes.py | 2 +- test/test_analysis_spikes.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/nwbwidgets/analysis/spikes.py b/nwbwidgets/analysis/spikes.py index bc343f38..7a13a858 100644 --- a/nwbwidgets/analysis/spikes.py +++ b/nwbwidgets/analysis/spikes.py @@ -18,7 +18,7 @@ def compute_smoothed_firing_rate(spike_times, tt, sigma_in_secs): return np.zeros_like(tt) binned_spikes = np.zeros_like(tt) # find bin indices for spike times - spike_idx = np.searchsorted(tt, spike_times, side='right') - 1 + spike_idx = np.searchsorted(tt, spike_times, side="right") - 1 # filter out negative spike idxs, though there shouldn't be any spike_idx = spike_idx[spike_idx >= 0].astype(int) # increment binned spike count at bin indices diff --git a/test/test_analysis_spikes.py b/test/test_analysis_spikes.py index 8ff5b76f..082b79a7 100644 --- a/test/test_analysis_spikes.py +++ b/test/test_analysis_spikes.py @@ -1,4 +1,5 @@ import numpy as np + from nwbwidgets.analysis.spikes import compute_smoothed_firing_rate @@ -6,9 +7,21 @@ def test_compute_smoothed_firing_rate(): spike_times = np.array([1.0, 2.0, 5.0, 5.5, 7.0, 7.5, 8.0]) tt = np.arange(10, dtype=float) expected_binned_spikes = np.array([0.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 2.0, 1.0, 0.0]) - expected_smoothed_spikes = np.array([0.35438556, 0.64574827, 0.64991247, 0.40421249, 0.55136343, - 0.91486675, 1.02201074, 1.14797447, 0.89644961, 0.41307621]) + expected_smoothed_spikes = np.array( + [ + 0.35438556, + 0.64574827, + 0.64991247, + 0.40421249, + 0.55136343, + 0.91486675, + 1.02201074, + 1.14797447, + 0.89644961, + 0.41307621, + ] + ) binned_spikes = compute_smoothed_firing_rate(spike_times, tt, 0.001) smoothed_spikes = compute_smoothed_firing_rate(spike_times, tt, 1.0) np.testing.assert_allclose(expected_binned_spikes, binned_spikes) - np.testing.assert_allclose(expected_smoothed_spikes, smoothed_spikes) \ No newline at end of file + np.testing.assert_allclose(expected_smoothed_spikes, smoothed_spikes)