Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6c0e8d4
remove copy statement
Moritz-Alexander-Kern Nov 19, 2024
a3fdbaa
add regression test for issue #648
Moritz-Alexander-Kern Nov 19, 2024
0635878
fix time_histogram for single spike_train
Moritz-Alexander-Kern Nov 19, 2024
710a993
update docstring
Moritz-Alexander-Kern Nov 19, 2024
64b50cb
fix neo. spike trains to neo.spiketrain objects
Moritz-Alexander-Kern Nov 28, 2024
a965d30
fix bullet point list
Moritz-Alexander-Kern Nov 28, 2024
9c06d0d
fix bullet points
Moritz-Alexander-Kern Nov 28, 2024
7fee5ad
Update elephant/test/test_statistics.py
Moritz-Alexander-Kern Jan 8, 2025
f6580e0
check if result is correct
Moritz-Alexander-Kern Jan 8, 2025
a406a79
add comments
Moritz-Alexander-Kern Jan 8, 2025
a0ebaf4
Merge branch 'master' into fix/time_histogram_648
Moritz-Alexander-Kern Jan 8, 2025
409e69e
Merge branch 'master' into fix/time_histogram_648
Moritz-Alexander-Kern Jan 14, 2025
aeb3603
Update elephant/test/test_statistics.py
Moritz-Alexander-Kern Jan 24, 2025
37387af
Merge branch 'master' into fix/time_histogram_648
CozySocksAlways Jan 30, 2026
7e2e019
fix docstring issues in time_histogram based on PR review
CozySocksAlways Feb 2, 2026
bdaec17
Merge branch 'master' into fix/time_histogram_648
CozySocksAlways Feb 2, 2026
ec77fa2
Added all output cases to time_histogram regression test
CozySocksAlways Feb 3, 2026
e826beb
Merge commit for remote branch onto local
CozySocksAlways Feb 3, 2026
5f42436
Modified docstring to fix false bold emphasis on text
CozySocksAlways Feb 3, 2026
dfab4dd
character case fix
CozySocksAlways Feb 3, 2026
959f79a
removed a '
CozySocksAlways Feb 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions elephant/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import scipy.signal
from numpy import ndarray
from scipy.special import erf
from typing import Union
from typing import List, Optional, Union

import elephant.conversion as conv
import elephant.kernels as kernels
Expand Down Expand Up @@ -1062,46 +1062,51 @@ def optimal_kernel(st):


@deprecated_alias(binsize='bin_size')
def time_histogram(spiketrains, bin_size, t_start=None, t_stop=None,
output='counts', binary=False):
def time_histogram(spiketrains: Union[List[neo.SpikeTrain], neo.SpikeTrain],
bin_size: pq.Quantity,
t_start: Optional[pq.Quantity] = None,
t_stop: Optional[pq.Quantity] = None,
output: str = 'counts',
binary: bool = False) -> neo.AnalogSignal:
"""
Time Histogram of a list of `neo.SpikeTrain` objects.
Time Histogram of a list of :class:`neo.core.SpikeTrain` objects.

Visualization of this function is covered in Viziphant:
:func:`viziphant.statistics.plot_time_histogram`.

Parameters
----------
spiketrains : list of neo.SpikeTrain
`neo.SpikeTrain`s with a common time axis (same `t_start` and `t_stop`)
spiketrains : list of :class:`neo.core.SpikeTrain` or :class:`neo.core.SpikeTrain`
`neo.SpikeTrain` objects with a common time axis (same `t_start` and `t_stop`)
bin_size : pq.Quantity
Width of the histogram's time bins.
t_start : pq.Quantity, optional
Start time of the histogram. Only events in `spiketrains` falling
between `t_start` and `t_stop` (both included) are considered in the
histogram.
If None, the maximum `t_start` of all `neo.SpikeTrain`s is used as
If None, the maximum `t_start` of all :class:`neo.core.SpikeTrain`s is used as
`t_start`.
Default: None
t_stop : pq.Quantity, optional
Stop time of the histogram. Only events in `spiketrains` falling
between `t_start` and `t_stop` (both included) are considered in the
histogram.
If None, the minimum `t_stop` of all `neo.SpikeTrain`s is used as
If None, the minimum `t_stop` of all :class:`neo.core.SpikeTrain` s is used as
`t_stop`.
Default: None
output : {'counts', 'mean', 'rate'}, optional
Normalization of the histogram. Can be one of:
* 'counts': spike counts at each bin (as integer numbers).
* 'mean': mean spike counts per spike train.
* 'rate': mean spike rate per spike train. Like 'mean', but the
counts are additionally normalized by the bin width.

- 'counts': spike counts at each bin (as integer numbers).
- 'mean': mean spike counts per spike train.
- 'rate': mean spike rate per spike train. Like 'mean', but the counts are additionally normalized
by the bin width.

Default: 'counts'
binary : bool, optional
If True, indicates whether all `neo.SpikeTrain` objects should first
If True, indicates whether all :class:`neo.core.SpikeTrain` objects should first
be binned to a binary representation (using the
`conversion.BinnedSpikeTrain` class) and the calculation of the
[:class:`elephant.conversion.BinnedSpikeTrain` class] and the calculation of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the enhancement provided by the class link, the brackets [ ... ] are not needed, and there is the missing closing parenthesis ).

histogram is based on this representation.
Note that the output is not binary, but a histogram of the converted,
binary representation.
Expand All @@ -1110,8 +1115,8 @@ def time_histogram(spiketrains, bin_size, t_start=None, t_stop=None,
Returns
-------
neo.AnalogSignal
A `neo.AnalogSignal` object containing the histogram values.
`neo.AnalogSignal[j]` is the histogram computed between
A :class:`neo.core.SpikeTrain` object containing the histogram values.
:class:`neo.core.SpikeTrain `[j]` is the histogram computed between
`t_start + j * bin_size` and `t_start + (j + 1) * bin_size`.

Raises
Expand All @@ -1129,7 +1134,7 @@ def time_histogram(spiketrains, bin_size, t_start=None, t_stop=None,

See also
--------
elephant.conversion.BinnedSpikeTrain
:func:`elephant.conversion.BinnedSpikeTrain`

Examples
--------
Expand Down Expand Up @@ -1178,17 +1183,17 @@ def time_histogram(spiketrains, bin_size, t_start=None, t_stop=None,

def _counts() -> pq.Quantity:
# 'counts': spike counts at each bin (as integer numbers).
return pq.Quantity(bin_hist, units=pq.dimensionless, copy=False)
return pq.Quantity(bin_hist, units=pq.dimensionless)

def _mean() -> pq.Quantity:
# 'mean': mean spike counts per spike train.
return pq.Quantity(bin_hist / len(spiketrains),
units=pq.dimensionless, copy=False)
return pq.Quantity(bin_hist / binned_spiketrain.shape[0],
units=pq.dimensionless)

def _rate() -> pq.Quantity:
# 'rate': mean spike rate per spike train. Like 'mean', but the
# counts are additionally normalized by the bin width.
return bin_hist / (len(spiketrains) * bin_size)
return bin_hist / (binned_spiketrain.shape[0] * bin_size)

output_mapping = {"counts": _counts, "mean": _mean, "rate": _rate}
try:
Expand Down
16 changes: 15 additions & 1 deletion elephant/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,9 +1089,23 @@ def test_annotations(self):
self.assertIn('normalization', histogram.annotations)
self.assertEqual(histogram.annotations['normalization'], output)

def test_time_histogram_regression_648_single_spiketrain(self):
# Create a single spike train
spiketrain = neo.SpikeTrain([0.1, 0.5, 1.0, 1.5, 2.0] * pq.s, t_stop=3.0 * pq.s)

# Run time_histogram with spiketrain directly and observe the incorrect result
histogram_direct = statistics.time_histogram(spiketrain, output='rate', bin_size=0.5 * pq.s)

# Wrap spiketrain in a list and run time_histogram
histogram_wrapped = statistics.time_histogram([spiketrain], output='rate', bin_size=0.5 * pq.s)
# Check if passing a single spiketrain directly vs in a list gives same result
np.testing.assert_array_equal(histogram_direct.magnitude, histogram_wrapped.magnitude)
# Check if the spike rate calculation is correct for a single spike train
np.testing.assert_array_equal(histogram_direct.magnitude.flatten(), [2., 2., 2., 2., 2., 0.]*pq.Hz)


class ComplexityTestCase(unittest.TestCase):
def test_complexity_pdf_deprecated(self):
def test_complexiy_pdf_deprecated(self):
spiketrain_a = neo.SpikeTrain(
[0.5, 0.7, 1.2, 2.3, 4.3, 5.5, 6.7] * pq.s, t_stop=10.0 * pq.s)
spiketrain_b = neo.SpikeTrain(
Expand Down
Loading