From 15763f4ba947149acb2be81d0bde6b65b74c1448 Mon Sep 17 00:00:00 2001 From: Jay R Bolton Date: Sun, 29 Jul 2018 19:17:54 -0700 Subject: [PATCH 1/4] Raise an exception when creating an electrode table region with indexes out of range of the added electrodes --- src/pynwb/file.py | 5 +++++ tests/unit/pynwb_tests/test_file.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/pynwb/file.py b/src/pynwb/file.py index 564820caf..ae2bf18e4 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 TypeError('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..73143afc1 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(TypeError) 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'] From 7a4a3f8f5fd8945c5f6a4fcb5dd850ecd4c634ca Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 30 Jul 2018 09:53:31 -0400 Subject: [PATCH 2/4] TypeError -> IndexError --- src/pynwb/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pynwb/file.py b/src/pynwb/file.py index ae2bf18e4..e14b862de 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -343,7 +343,7 @@ def create_electrode_table_region(self, **kwargs): region = getargs('region', kwargs) for idx in region: if idx < 0 or idx >= len(self.ec_electrodes): - raise TypeError('The index ' + str(idx) + + raise IndexError('The index ' + str(idx) + ' is out of range for the ElectrodeTable of length ' + str(len(self.ec_electrodes))) desc = getargs('description', kwargs) From 9f4bddae66556133e5010c6ca7f6e14c3aabe0eb Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 30 Jul 2018 09:54:02 -0400 Subject: [PATCH 3/4] TypeError -> IndexError --- tests/unit/pynwb_tests/test_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/pynwb_tests/test_file.py b/tests/unit/pynwb_tests/test_file.py index 73143afc1..8f94f10df 100644 --- a/tests/unit/pynwb_tests/test_file.py +++ b/tests/unit/pynwb_tests/test_file.py @@ -71,7 +71,7 @@ def test_create_electrode_group_invalid_index(self): 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(TypeError) as err: + with self.assertRaises(IndexError) as err: nwbfile.create_electrode_table_region(list(range(6)), 'test') self.assertTrue('out of range' in str(err.exception)) From b1dbd51030c3d0514f8188af201c00c147b48923 Mon Sep 17 00:00:00 2001 From: Jay R Bolton Date: Mon, 30 Jul 2018 10:04:10 -0700 Subject: [PATCH 4/4] flake8 lint fix --- src/pynwb/file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pynwb/file.py b/src/pynwb/file.py index e14b862de..4ba75632e 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -344,8 +344,8 @@ def create_electrode_table_region(self, **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))) + ' 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)