Skip to content

Commit

Permalink
first working version with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Mar 15, 2024
1 parent cef100c commit 04f546c
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 149 deletions.
50 changes: 44 additions & 6 deletions spec/ndx-binned-spikes.extensions.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
groups:
- neurodata_type_def: TetrodeSeries
neurodata_type_inc: ElectricalSeries
doc: An extension of ElectricalSeries to include the tetrode ID for each time series.
- neurodata_type_def: BinnedAlignedSpikes
neurodata_type_inc: NWBDataInterface
default_name: BinnedAlignedSpikes
doc: A data interface for binned spike data aligned to an event (e.g. a stimuli
or the beginning of a trial).
attributes:
- name: trode_id
dtype: int32
doc: The tetrode ID.
- name: bin_width_in_milliseconds
dtype: float64
doc: The lenght in milliseconds of the bins

Check failure on line 10 in spec/ndx-binned-spikes.extensions.yaml

View workflow job for this annotation

GitHub Actions / Check for spelling errors

lenght ==> length
- name: milliseconds_from_event_to_first_bin
dtype: float64
default_value: 0.0
doc: The time in milliseconds from the event (e.g. a stimuli or the beginning
of a trial),to the first bin. Note that this is a negative number if the first
bin is before the event.
required: false
- name: units
dtype:
target_type: Units
reftype: object
doc: A link to the units Table that contains the units of the data.
required: false
datasets:
- name: data
dtype: numeric
dims:
- - number_of_event_repetitions
- number_of_bins
- - number_of_event_repetitions
- number_of_bins
- num_units
shape:
- - null
- null
- - null
- null
- null
doc: TODO
- name: event_timestamps
dtype: float64
dims:
- number_of_event_repetitions
shape:
- null
doc: The timestamps at whic the event occurred.

Check failure on line 46 in spec/ndx-binned-spikes.extensions.yaml

View workflow job for this annotation

GitHub Actions / Check for spelling errors

whic ==> which
7 changes: 2 additions & 5 deletions src/pynwb/ndx_binned_spikes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
# Load the namespace
load_namespaces(str(__spec_path))

# TODO: Define your classes here to make them accessible at the package level.
# Either have PyNWB generate a class from the spec using `get_class` as shown
# below or write a custom class and register it using the class decorator
# `@register_class("TetrodeSeries", "ndx-binned-spikes")`
TetrodeSeries = get_class("TetrodeSeries", "ndx-binned-spikes")
BinnedAlignedSpikes = get_class("BinnedAlignedSpikes", "ndx-binned-spikes")


# Remove these functions from the package
del load_namespaces, get_class
121 changes: 121 additions & 0 deletions src/pynwb/tests/test_binned_aligned_spikes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Unit and integration tests for the example BinnedAlignedSpikes extension neurodata type.
TODO: Modify these tests to test your extension neurodata type.
"""

import numpy as np

from pynwb import NWBHDF5IO, NWBFile
from pynwb.testing.mock.device import mock_Device
from pynwb.testing.mock.ecephys import mock_ElectrodeGroup, mock_ElectrodeTable
from pynwb.testing.mock.file import mock_NWBFile
from pynwb.testing import TestCase, remove_test_file, NWBH5IOFlexMixin

from ndx_binned_spikes import BinnedAlignedSpikes


class TestBinnedAlignedSpikesConstructor(TestCase):
"""Simple unit test for creating a BinnedAlignedSpikes."""

def setUp(self):
"""Set up an NWB file. Necessary because BinnedAlignedSpikes requires references to electrodes."""
self.nwbfile = mock_NWBFile()

def test_constructor(self):
"""Test that the constructor for BinnedAlignedSpikes sets values as expected."""

number_of_bins = 3
number_of_event_repetitions = 4
bin_width_in_milliseconds = 20.0
milliseconds_from_event_to_first_bin = 1.0
data_list = []
for i in range(number_of_event_repetitions):
data_list.append([i] * number_of_bins)
data = np.array(data_list)
event_timestamps = np.arange(number_of_event_repetitions, dtype="float64")

binned_aligned_spikes = BinnedAlignedSpikes(
bin_width_in_milliseconds=bin_width_in_milliseconds,
milliseconds_from_event_to_first_bin=milliseconds_from_event_to_first_bin,
data=data,
event_timestamps=event_timestamps
)

np.testing.assert_array_equal(binned_aligned_spikes.data, data)
np.testing.assert_array_equal(binned_aligned_spikes.event_timestamps, event_timestamps)
self.assertEqual(binned_aligned_spikes.bin_width_in_milliseconds, bin_width_in_milliseconds)
self.assertEqual(
binned_aligned_spikes.milliseconds_from_event_to_first_bin, milliseconds_from_event_to_first_bin
)

self.assertEqual(binned_aligned_spikes.data.shape[0], number_of_event_repetitions)
self.assertEqual(binned_aligned_spikes.data.shape[1], number_of_bins)


def generate_binned_aligned_spikes(
number_of_bins=3,
number_of_event_repetitions=4,
bin_width_in_milliseconds=20.0,
milliseconds_from_event_to_first_bin=1.0,
):

data_list = []
for i in range(number_of_event_repetitions):
data_list.append([i] * number_of_bins)
data = np.array(data_list)
event_timestamps = np.arange(number_of_event_repetitions, dtype="float64")

binned_aligned_spikes = BinnedAlignedSpikes(
bin_width_in_milliseconds=bin_width_in_milliseconds,
milliseconds_from_event_to_first_bin=milliseconds_from_event_to_first_bin,
data=data,
event_timestamps=event_timestamps,
)
return binned_aligned_spikes


class TestBinnedAlignedSpikesSimpleRoundtrip(TestCase):
"""Simple roundtrip test for BinnedAlignedSpikes."""



def setUp(self):
self.nwbfile = mock_NWBFile()

self.binned_aligned_spikes = generate_binned_aligned_spikes()

self.path = "test.nwb"

def tearDown(self):
remove_test_file(self.path)

def test_roundtrip_acquisition(self):
"""
Add a BinnedAlignedSpikes to an NWBFile, write it to file, read the file, and test that the BinnedAlignedSpikes from the
file matches the original BinnedAlignedSpikes.
"""

self.nwbfile.add_acquisition(self.binned_aligned_spikes)

with NWBHDF5IO(self.path, mode="w") as io:
io.write(self.nwbfile)

with NWBHDF5IO(self.path, mode="r", load_namespaces=True) as io:
read_nwbfile = io.read()
self.assertContainerEqual(self.binned_aligned_spikes, read_nwbfile.acquisition["BinnedAlignedSpikes"])

def test_roundtrip_processing_module(self):


ecephys_processinng_module = self.nwbfile.create_processing_module(
name="ecephys", description="a description"
)
ecephys_processinng_module.add(self.binned_aligned_spikes)

with NWBHDF5IO(self.path, mode="w") as io:
io.write(self.nwbfile)

with NWBHDF5IO(self.path, mode="r", load_namespaces=True) as io:
read_nwbfile = io.read()
read_container = read_nwbfile.processing["ecephys"]["BinnedAlignedSpikes"]
self.assertContainerEqual(self.binned_aligned_spikes, read_container)
126 changes: 0 additions & 126 deletions src/pynwb/tests/test_tetrodeseries.py

This file was deleted.

Loading

0 comments on commit 04f546c

Please sign in to comment.