Skip to content

Commit

Permalink
Update tests, add simpler test
Browse files Browse the repository at this point in the history
  • Loading branch information
rly committed May 28, 2020
1 parent 88c9f5e commit 0b7dab6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 25 deletions.
Empty file.
Empty file.
112 changes: 101 additions & 11 deletions src/pynwb/tests/integration/hdf5/test_events.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,106 @@
import datetime
import numpy as np

from pynwb.testing import AcquisitionH5IOMixin, TestCase
from pynwb import NWBFile, NWBHDF5IO
from pynwb.testing import AcquisitionH5IOMixin, TestCase, remove_test_file

from ndx_events import Events, LabeledEvents, TTLs, AnnotatedEvents


class TestEventsIOSimple(TestCase):
"""Simple roundtrip test for CSD."""

def setUp(self):
self.nwbfile = NWBFile(
session_description='session_description',
identifier='identifier',
session_start_time=datetime.datetime.now(datetime.timezone.utc)
)
self.path = 'test.nwb'

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

def test_roundtrip(self):
"""
Add a CSD to an "ecephys" processing module in the NWBFile, write it to file, read the file, and test that the
CSD from the file matches the original CSD.
"""

events = Events(
name='Events',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5
)
self.nwbfile.add_acquisition(events)

labeled_events = LabeledEvents(
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
label_keys=np.uint([3, 4, 3]),
labels=['', '', '', 'event1', 'event2']
)
self.nwbfile.add_acquisition(labeled_events)

ttls = TTLs(
name='TTLs',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
label_keys=np.uint([3, 4, 3]),
labels=['', '', '', 'event1', 'event2']
)
self.nwbfile.add_acquisition(ttls)

annotated_events = AnnotatedEvents(
name='AnnotatedEvents',
description='annotated events from my experiment',
resolution=1e-5
)
annotated_events.add_column(
name='extra',
description='extra metadata for each event type'
)
annotated_events.add_event_type(
label='Reward',
event_description='Times when the animal received juice reward.',
event_times=[1., 2., 3.],
extra='extra',
id=3
)
annotated_events.add_event_type(
label='Nosepoke',
event_description='Times when the animal poked its noise through the input port.',
event_times=[1., 2., 3.],
extra='extra',
id=5
)

behavior_module = self.nwbfile.create_processing_module(
name='behavior',
description='processed behavioral data'
)
behavior_module.add(annotated_events)

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(events, read_nwbfile.acquisition['Events'])
self.assertContainerEqual(labeled_events, read_nwbfile.acquisition['LabeledEvents'])
self.assertContainerEqual(ttls, read_nwbfile.acquisition['TTLs'])
self.assertContainerEqual(annotated_events, read_nwbfile.processing['behavior']['AnnotatedEvents'])


class TestEventsIO(AcquisitionH5IOMixin, TestCase):

def setUpContainer(self):
""" Return the test Events to read/write """
events = Events(
name='my_events',
name='Events',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5
Expand All @@ -23,7 +113,7 @@ class TestLabeledEventsIO(AcquisitionH5IOMixin, TestCase):
def setUpContainer(self):
""" Return the test Events to read/write """
labeled_events = LabeledEvents(
name='my_events',
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
Expand All @@ -38,7 +128,7 @@ class TestTTLs(AcquisitionH5IOMixin, TestCase):
def setUpContainer(self):
""" Return the test Events to read/write """
ttls = TTLs(
name='my_events',
name='TTLs',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
Expand All @@ -53,27 +143,27 @@ class TestAnnotatedEventsIO(AcquisitionH5IOMixin, TestCase):

def setUpContainer(self):
""" Return the test AnnotatedEvents to read/write """
events = AnnotatedEvents(
name='my_annotated_events',
annotated_events = AnnotatedEvents(
name='AnnotatedEvents',
description='annotated events from my experiment',
resolution=1e-5
)
events.add_column(
annotated_events.add_column(
name='extra',
description='extra metadata for each event type'
)
events.add_event_type(
annotated_events.add_event_type(
label='Reward',
event_description='Times when the animal received juice reward.',
event_times=[1., 2., 3.],
extra='extra',
id=3
)
events.add_event_type(
annotated_events.add_event_type(
label='Nosepoke',
event_description='Times when the animal poked its noise through the input port.',
event_times=[1., 2., 3.],
extra='extra',
id=5
)
return events
return annotated_events
Empty file.
28 changes: 14 additions & 14 deletions src/pynwb/tests/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class TestEvents(TestCase):

def test_init(self):
events = Events(
name='my_events',
name='Events',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5
)
self.assertEqual(events.name, 'my_events')
self.assertEqual(events.name, 'Events')
self.assertEqual(events.description, 'events from my experiment')
self.assertEqual(events.timestamps, [0., 1., 2.])
self.assertEqual(events.resolution, 1e-5)
Expand All @@ -26,14 +26,14 @@ class TestLabeledEvents(TestCase):

def test_init(self):
events = LabeledEvents(
name='my_events',
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
label_keys=np.uint([3, 4, 3]),
labels=['', '', '', 'event1', 'event2']
)
self.assertEqual(events.name, 'my_events')
self.assertEqual(events.name, 'LabeledEvents')
self.assertEqual(events.description, 'events from my experiment')
self.assertEqual(events.timestamps, [0., 1., 2.])
self.assertEqual(events.resolution, 1e-5)
Expand All @@ -45,7 +45,7 @@ def test_mismatch_length(self):
msg = 'Timestamps and label_keys must have the same length: 3 != 4'
with self.assertRaisesWith(ValueError, msg):
LabeledEvents(
name='my_events',
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
Expand All @@ -55,7 +55,7 @@ def test_mismatch_length(self):

def test_defaultlabels(self):
events = LabeledEvents(
name='my_events',
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
Expand All @@ -67,7 +67,7 @@ def test_none_in_labels(self):
msg = "None values are not allowed in the labels array. Please use '' for undefined label keys."
with self.assertRaisesWith(ValueError, msg):
LabeledEvents(
name='my_events',
name='LabeledEvents',
description='events from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
Expand All @@ -80,15 +80,15 @@ class TestTTLs(TestCase):

def test_init(self):
events = TTLs(
name='my_events',
description='events from my experiment',
name='TTLs',
description='ttl pulses from my experiment',
timestamps=[0., 1., 2.],
resolution=1e-5,
label_keys=np.uint([3, 4, 3]),
labels=['', '', '', 'event1', 'event2']
)
self.assertEqual(events.name, 'my_events')
self.assertEqual(events.description, 'events from my experiment')
self.assertEqual(events.name, 'TTLs')
self.assertEqual(events.description, 'ttl pulses from my experiment')
self.assertEqual(events.timestamps, [0., 1., 2.])
self.assertEqual(events.resolution, 1e-5)
self.assertEqual(events.unit, 'seconds')
Expand All @@ -100,17 +100,17 @@ class TestAnnotatedEvents(TestCase):

def test_init(self):
events = AnnotatedEvents(
name='my_annotated_events',
name='AnnotatedEvents',
description='annotated events from my experiment',
resolution=1e-5
)
self.assertEqual(events.name, 'my_annotated_events')
self.assertEqual(events.name, 'AnnotatedEvents')
self.assertEqual(events.description, 'annotated events from my experiment')
self.assertEqual(events.resolution, 1e-5)

def test_add_event_type(self):
events = AnnotatedEvents(
name='my_annotated_events',
name='AnnotatedEvents',
description='annotated events from my experiment'
)
events.add_event_type(
Expand Down

0 comments on commit 0b7dab6

Please sign in to comment.