Skip to content

Commit

Permalink
Merge pull request #571 from jayrbolton/dev
Browse files Browse the repository at this point in the history
NWBFile#create_electrode_table_region length check
  • Loading branch information
bendichter authored Jul 30, 2018
2 parents 9cfdacc + 26ee8b5 commit 64a2143
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ def create_electrode_table_region(self, **kwargs):
msg = "no electrodes available. add electrodes before creating a region"
raise RuntimeError(msg)
region = getargs('region', kwargs)
for idx in region:
if idx < 0 or idx >= len(self.ec_electrodes):
raise IndexError('The index ' + str(idx) +
' is out of range for the ElectrodeTable of length '
+ str(len(self.ec_electrodes)))
desc = getargs('description', kwargs)
name = getargs('name', kwargs)
return ElectrodeTableRegion(self.ec_electrodes, region, desc, name)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/pynwb_tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def test_create_electrode_group(self):
self.assertEqual(elecgrp.location, loc)
self.assertIs(elecgrp.device, d)

def test_create_electrode_group_invalid_index(self):
"""
Test the case where the user creates an electrode table region with
indexes that are out of range of the amount of electrodes added.
"""
nwbfile = NWBFile('a', 'b', 'c', datetime.now())
device = nwbfile.create_device('a', 'b')
elecgrp = nwbfile.create_electrode_group('a', 'b', 'c', device=device, location='a')
for i in range(4):
nwbfile.add_electrode(i, np.nan, np.nan, np.nan, np.nan, group=elecgrp,
location='a', filtering='a', description='a')
with self.assertRaises(IndexError) as err:
nwbfile.create_electrode_table_region(list(range(6)), 'test')
self.assertTrue('out of range' in str(err.exception))

def test_epoch_tags(self):
tags1 = ['t1', 't2']
tags2 = ['t3', 't4']
Expand Down

0 comments on commit 64a2143

Please sign in to comment.