Skip to content

Commit f4163aa

Browse files
authored
Merge pull request #1246 from NeurodataWithoutBorders/fix/elec_table
2 parents b1e4697 + b54a262 commit f4163aa

File tree

3 files changed

+138
-15
lines changed

3 files changed

+138
-15
lines changed

src/pynwb/file.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,12 @@ def add_electrode(self, **kwargs):
525525
('rel_y', 'the y coordinate within the electrode group'),
526526
('rel_z', 'the z coordinate within the electrode group'),
527527
('reference', 'Description of the reference used for this electrode.')]
528+
# add column if the arg is supplied and column does not yet exist
529+
# do not pass arg to add_row if arg is not supplied
528530
for col_name, col_doc in new_cols:
529-
if kwargs[col_name] is not None and col_name not in self.electrodes:
530-
self.electrodes.add_column(col_name, col_doc)
531+
if kwargs[col_name] is not None:
532+
if col_name not in self.electrodes:
533+
self.electrodes.add_column(col_name, col_doc)
531534
else:
532535
d.pop(col_name) # remove args from d if not set
533536

tests/integration/hdf5/test_nwbfile.py

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,86 @@ def setUpContainer(self):
444444
def addContainer(self, nwbfile):
445445
""" Add electrodes and related objects to the given NWBFile """
446446
self.dev1 = nwbfile.create_device(name='dev1')
447-
self.group = nwbfile.create_electrode_group(name='tetrode1', description='tetrode description',
448-
location='tetrode location', device=self.dev1)
447+
self.group = nwbfile.create_electrode_group(
448+
name='tetrode1',
449+
description='tetrode description',
450+
location='tetrode location',
451+
device=self.dev1
452+
)
449453

450-
nwbfile.add_electrode(id=1, x=1.0, y=2.0, z=3.0, imp=-1.0, location='CA1', filtering='none', group=self.group,
451-
group_name='tetrode1')
452-
nwbfile.add_electrode(id=2, x=1.0, y=2.0, z=3.0, imp=-2.0, location='CA1', filtering='none', group=self.group,
453-
group_name='tetrode1')
454-
nwbfile.add_electrode(id=3, x=1.0, y=2.0, z=3.0, imp=-3.0, location='CA1', filtering='none', group=self.group,
455-
group_name='tetrode1')
456-
nwbfile.add_electrode(id=4, x=1.0, y=2.0, z=3.0, imp=-4.0, location='CA1', filtering='none', group=self.group,
457-
group_name='tetrode1')
454+
nwbfile.add_electrode(
455+
id=1,
456+
x=1.0, y=2.0, z=3.0,
457+
imp=-1.0,
458+
location='CA1',
459+
filtering='none',
460+
group=self.group,
461+
group_name='tetrode1'
462+
)
463+
nwbfile.add_electrode(
464+
id=2,
465+
x=1.0, y=2.0, z=3.0,
466+
imp=-2.0,
467+
location='CA1',
468+
filtering='none',
469+
group=self.group,
470+
group_name='tetrode1'
471+
)
472+
473+
self.container = nwbfile.electrodes # override self.container which has the placeholder
474+
475+
def getContainer(self, nwbfile):
476+
""" Return the test electrodes table from the given NWBFile """
477+
return nwbfile.electrodes
478+
479+
def test_roundtrip(self):
480+
super().test_roundtrip()
481+
# When comparing the pandas dataframes for the row we drop the 'group' column since the
482+
# ElectrodeGroup object after reading will naturally have a different address
483+
pd.testing.assert_frame_equal(self.read_container[0].drop('group', axis=1),
484+
self.container[0].drop('group', axis=1))
485+
486+
487+
class TestElectrodesOptColumns(NWBH5IOMixin, TestCase):
488+
489+
def setUpContainer(self):
490+
"""
491+
Return placeholder table for electrodes. Tested electrodes are added directly to the NWBFile in addContainer
492+
"""
493+
return DynamicTable('electrodes', 'a placeholder table')
494+
495+
def addContainer(self, nwbfile):
496+
""" Add electrodes and related objects to the given NWBFile """
497+
self.dev1 = nwbfile.create_device(name='dev1')
498+
self.group = nwbfile.create_electrode_group(
499+
name='tetrode1',
500+
description='tetrode description',
501+
location='tetrode location',
502+
device=self.dev1
503+
)
504+
505+
nwbfile.add_electrode(
506+
id=1,
507+
x=1.0, y=2.0, z=3.0,
508+
imp=-1.0,
509+
location='CA1',
510+
filtering='none',
511+
group=self.group,
512+
group_name='tetrode1',
513+
rel_x=4.0, rel_y=5.0, rel_z=6.0,
514+
reference='ref1'
515+
)
516+
nwbfile.add_electrode(
517+
id=2,
518+
x=1.0, y=2.0, z=3.0,
519+
imp=-2.0,
520+
location='CA1',
521+
filtering='none',
522+
group=self.group,
523+
group_name='tetrode1',
524+
rel_x=4.0, rel_y=5.0, rel_z=6.0,
525+
reference='ref1'
526+
)
458527

459528
self.container = nwbfile.electrodes # override self.container which has the placeholder
460529

tests/unit/test_file.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,21 @@ def test_add_invalid_time_w_ts(self):
242242
timeseries=ts, tags=('hi', 'there'))
243243

244244
def test_add_electrode(self):
245-
dev1 = self.nwbfile.create_device('dev1')
246-
group = self.nwbfile.create_electrode_group('tetrode1', 'tetrode description', 'tetrode location', dev1)
247-
self.nwbfile.add_electrode(1.0, 2.0, 3.0, -1.0, 'CA1', 'none', group=group, id=1)
245+
dev1 = self.nwbfile.create_device(name='dev1')
246+
group = self.nwbfile.create_electrode_group(
247+
name='tetrode1',
248+
description='tetrode description',
249+
location='tetrode location',
250+
device=dev1
251+
)
252+
self.nwbfile.add_electrode(
253+
x=1.0, y=2.0, z=3.0,
254+
imp=-1.0,
255+
location='CA1',
256+
filtering='none',
257+
group=group,
258+
id=1
259+
)
248260
elec = self.nwbfile.electrodes[0]
249261
self.assertEqual(elec.index[0], 1)
250262
self.assertEqual(elec.iloc[0]['x'], 1.0)
@@ -254,6 +266,45 @@ def test_add_electrode(self):
254266
self.assertEqual(elec.iloc[0]['filtering'], 'none')
255267
self.assertEqual(elec.iloc[0]['group'], group)
256268

269+
def test_add_electrode_some_opt(self):
270+
dev1 = self.nwbfile.create_device(name='dev1')
271+
group = self.nwbfile.create_electrode_group(
272+
name='tetrode1',
273+
description='tetrode description',
274+
location='tetrode location',
275+
device=dev1
276+
)
277+
self.nwbfile.add_electrode(
278+
x=1.0, y=2.0, z=3.0,
279+
imp=-1.0,
280+
location='CA1',
281+
filtering='none',
282+
group=group,
283+
id=1,
284+
rel_x=4.0, rel_y=5.0, rel_z=6.0,
285+
reference='ref1'
286+
)
287+
self.nwbfile.add_electrode(
288+
x=1.0, y=2.0, z=3.0,
289+
imp=-1.0,
290+
location='CA1',
291+
filtering='none',
292+
group=group,
293+
id=2,
294+
rel_x=7.0, rel_y=8.0, rel_z=9.0,
295+
reference='ref2'
296+
)
297+
elec = self.nwbfile.electrodes[0]
298+
self.assertEqual(elec.iloc[0]['rel_x'], 4.0)
299+
self.assertEqual(elec.iloc[0]['rel_y'], 5.0)
300+
self.assertEqual(elec.iloc[0]['rel_z'], 6.0)
301+
self.assertEqual(elec.iloc[0]['reference'], 'ref1')
302+
elec = self.nwbfile.electrodes[1]
303+
self.assertEqual(elec.iloc[0]['rel_x'], 7.0)
304+
self.assertEqual(elec.iloc[0]['rel_y'], 8.0)
305+
self.assertEqual(elec.iloc[0]['rel_z'], 9.0)
306+
self.assertEqual(elec.iloc[0]['reference'], 'ref2')
307+
257308
def test_all_children(self):
258309
ts1 = TimeSeries('test_ts1', [0, 1, 2, 3, 4, 5], 'grams', timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5])
259310
ts2 = TimeSeries('test_ts2', [0, 1, 2, 3, 4, 5], 'grams', timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5])

0 commit comments

Comments
 (0)