Skip to content

Commit

Permalink
ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Aug 21, 2024
1 parent 997dd13 commit 6fd3f3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ binned_aligned_spikes = BinnedAlignedSpikes(
As with the previous example this can be then added to a processing module in an NWB file and then written to disk using exactly the same code as before.

### Storing data from multiple conditions (i.e. multiple stimuli)
`BinnedAlignedSpikes` can also be used to store data that is aggregated across multiple conditions while at the same time keeping track of which condition each set of counts corresponds to. This is useful when you want to store the spike counts around multiple conditions (e.g., different stimuli, behavioral events, etc.) in a single structure. For this purpose, the `BinnedAlignedSpikes` object is used. Since each condition may not occur the same number of times (e.g. different stimuli do not appear in the same frequency), an homogeneous data structure is not possible. Therefore an extra variable, `condition_indices`, is used to indicate which condition each set of counts corresponds to.
`BinnedAlignedSpikes` can also be used to store data that is aggregated across multiple conditions while at the same time keeping track of which condition each set of counts corresponds to. This is useful when you want to store the spike counts around multiple conditions (e.g., different stimuli, behavioral events, etc.) in a single structure. Since each condition may not occur the same number of times (e.g. different stimuli do not appear in the same frequency), an homogeneous data structure is not possible. Therefore an extra variable, `condition_indices`, is used to indicate which condition each set of counts corresponds to.


```python
Expand Down
41 changes: 15 additions & 26 deletions src/pynwb/tests/test_binned_aligned_spikes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Unit and integration tests for the example BinnedAlignedSpikes extension neurodata type.
"""
"""Unit and integration tests for the example BinnedAlignedSpikes extension neurodata type."""

import numpy as np

Expand Down Expand Up @@ -62,7 +61,6 @@ def test_constructor(self):

def test_constructor_units_region(self):


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

Expand Down Expand Up @@ -99,15 +97,15 @@ def test_constructor_units_region(self):

def test_constructor_inconsistent_timestamps_and_data_error(self):
shorter_timestamps = self.timestamps[:-1]

with self.assertRaises(ValueError):
BinnedAlignedSpikes(
bin_width_in_milliseconds=self.bin_width_in_milliseconds,
milliseconds_from_event_to_first_bin=self.milliseconds_from_event_to_first_bin,
data=self.data,
timestamps=shorter_timestamps,
)


class TestBinnedAlignedSpikesMultipleConditions(TestCase):
"""Simple unit test for creating a BinnedAlignedSpikes with multiple conditions."""
Expand Down Expand Up @@ -160,23 +158,21 @@ def setUp(self):
self.timestamps_first_condition = [5.0, 15.0]
self.timestamps_second_condition = [0.0, 10.0, 20.0]

self.condition_indices = np.concatenate(
[
np.full(event_data.shape[1], condition_index)
for condition_index, event_data in enumerate([self.data_for_first_condition, self.data_for_second_condition])
]
)

data_list = [self.data_for_first_condition, self.data_for_second_condition]
self.data = np.concatenate(data_list, axis=1)

indices_list = [np.full(data.shape[1], condition_index) for condition_index, data in enumerate(data_list)]
self.condition_indices = np.concatenate(indices_list)

self.data = np.concatenate([self.data_for_first_condition, self.data_for_second_condition], axis=1)
self.timestamps = np.concatenate([self.timestamps_first_condition, self.timestamps_second_condition])

self.sorted_indices = np.argsort(self.timestamps)

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


# Test error if the timestamps are not aligned

# Test error if the timestamps are not aligned
with self.assertRaises(ValueError):
BinnedAlignedSpikes(
bin_width_in_milliseconds=self.bin_width_in_milliseconds,
Expand All @@ -185,14 +181,13 @@ def test_constructor(self):
timestamps=self.timestamps,
condition_indices=self.condition_indices,
)



data, timestamps, condition_indices = BinnedAlignedSpikes.sort_data_by_timestamps(
self.data,
self.timestamps,
self.condition_indices,
)

aggregated_binnned_align_spikes = BinnedAlignedSpikes(
bin_width_in_milliseconds=self.bin_width_in_milliseconds,
milliseconds_from_event_to_first_bin=self.milliseconds_from_event_to_first_bin,
Expand All @@ -218,7 +213,6 @@ def test_constructor(self):

def test_get_single_condition_data_methods(self):


data, timestamps, condition_indices = BinnedAlignedSpikes.sort_data_by_timestamps(
self.data,
self.timestamps,
Expand Down Expand Up @@ -246,15 +240,12 @@ def test_get_single_condition_data_methods(self):
np.testing.assert_allclose(timestamps_condition2, self.timestamps_second_condition)




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

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


self.path = "test.nwb"

def tearDown(self):
Expand All @@ -265,8 +256,8 @@ 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.
"""
# Testing here

# Testing here
self.binned_aligned_spikes = mock_BinnedAlignedSpikes(number_of_conditions=0)

self.nwbfile.add_acquisition(self.binned_aligned_spikes)
Expand Down Expand Up @@ -305,12 +296,10 @@ def test_roundtrip_with_units_table(self):
binned_aligned_spikes_with_region = mock_BinnedAlignedSpikes(units_region=units_region)
self.nwbfile.add_acquisition(binned_aligned_spikes_with_region)


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.acquisition["BinnedAlignedSpikes"]
self.assertContainerEqual(binned_aligned_spikes_with_region, read_container)

0 comments on commit 6fd3f3b

Please sign in to comment.