diff --git a/src/pynwb/file.py b/src/pynwb/file.py index 564820caf..4ba75632e 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -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) diff --git a/tests/unit/pynwb_tests/test_file.py b/tests/unit/pynwb_tests/test_file.py index e80e16036..8f94f10df 100644 --- a/tests/unit/pynwb_tests/test_file.py +++ b/tests/unit/pynwb_tests/test_file.py @@ -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']