Skip to content

Commit

Permalink
Merge branch 'dev' into get_timestamps_tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
bendichter authored Apr 1, 2024
2 parents bde9d0a + af63c5f commit d3c53ac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Support `stimulus_template` as optional predefined column in `IntracellularStimuliTable`. @stephprince [#1815](https://github.com/NeurodataWithoutBorders/pynwb/pull/1815)
- Support `NWBDataInterface` and `DynamicTable` in `NWBFile.stimulus`. @rly [#1842](https://github.com/NeurodataWithoutBorders/pynwb/pull/1842)
- Added support for python 3.12 and upgraded dependency versions. This also includes infrastructure updates for developers. @mavaylon1 [#1853](https://github.com/NeurodataWithoutBorders/pynwb/pull/1853)
- Added `mock_Units` for generating Units tables. @h-mayorquin [#1875](https://github.com/NeurodataWithoutBorders/pynwb/pull/1875)

### Bug fixes
- Fix bug with reading file with linked `TimeSeriesReferenceVectorData` @rly [#1865](https://github.com/NeurodataWithoutBorders/pynwb/pull/1865)
Expand Down
19 changes: 19 additions & 0 deletions src/pynwb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,31 @@ def time_unit(self):
return self.__time_unit

def get_timestamps(self):
"""
Get the timestamps of this TimeSeries. If timestamps are not stored in this TimeSeries, generate timestamps.
"""
if self.fields.get('timestamps'):
return self.timestamps
else:
return np.arange(len(self.data)) / self.rate + self.starting_time

def get_data_in_units(self):
"""
Get the data of this TimeSeries in the specified unit of measurement, applying the conversion factor and offset:
.. math::
out = data * conversion + offset
If the field 'channel_conversion' is present, the conversion factor is applied to each channel separately:
.. math::
out_{channel} = data * conversion_{channel} + offset
Returns
-------
np.ndarray
"""
if "channel_conversion" in self.fields:
scale_factor = self.conversion * self.channel_conversion[:, np.newaxis]
else:
Expand Down
30 changes: 30 additions & 0 deletions src/pynwb/testing/mock/ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ...ecephys import ElectricalSeries, ElectrodeGroup, SpikeEventSeries
from .device import mock_Device
from .utils import name_generator
from ...misc import Units


def mock_ElectrodeGroup(
Expand Down Expand Up @@ -119,3 +120,32 @@ def mock_SpikeEventSeries(
nwbfile.add_acquisition(spike_event_series)

return spike_event_series


def mock_Units(
num_units: int = 10,
max_spikes_per_unit: int = 10,
seed: int = 0,
nwbfile: Optional[NWBFile] = None,
) -> Units:

units_table = Units()
units_table.add_column(name="unit_name", description="a readable identifier for the unit")

rng = np.random.default_rng(seed=seed)

times = rng.random(size=(num_units, max_spikes_per_unit)).cumsum(axis=1)
spikes_per_unit = rng.integers(1, max_spikes_per_unit, size=num_units)

spike_times = []
for unit_index in range(num_units):

# Not all units have the same number of spikes
spike_times = times[unit_index, : spikes_per_unit[unit_index]]
unit_name = f"unit_{unit_index}"
units_table.add_unit(spike_times=spike_times, unit_name=unit_name)

if nwbfile is not None:
nwbfile.units = units_table

return units_table
3 changes: 3 additions & 0 deletions tests/unit/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
mock_ElectrodeTable,
mock_ElectricalSeries,
mock_SpikeEventSeries,
mock_Units,
)

from pynwb.testing.mock.icephys import (
Expand Down Expand Up @@ -82,6 +83,7 @@
mock_IntracellularElectrode,
mock_CurrentClampStimulusSeries,
mock_IntracellularRecordingsTable,
mock_Units,
]


Expand Down Expand Up @@ -119,3 +121,4 @@ def test_name_generator():

assert name_generator("TimeSeries") == "TimeSeries"
assert name_generator("TimeSeries") == "TimeSeries2"

0 comments on commit d3c53ac

Please sign in to comment.