Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e90312

Browse files
authored
Merge pull request NeuralEnsemble#633 from lkoelman/lkmn-dev
Support signal annotations in NeoMatlabIO
2 parents 1578663 + 210f260 commit 4e90312

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

neo/io/neomatlabio.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,20 @@ def create_struct_from_obj(self, ob):
276276
struct[childname] = []
277277

278278
# attributes
279-
for i, attr in enumerate(ob._all_attrs):
279+
all_attrs = list(ob._all_attrs)
280+
if hasattr(ob, 'annotations'):
281+
all_attrs.append(('annotations', type(ob.annotations)))
282+
283+
for i, attr in enumerate(all_attrs):
280284
attrname, attrtype = attr[0], attr[1]
281285

282286
# ~ if attrname =='':
283287
# ~ struct['array'] = ob.magnitude
284288
# ~ struct['units'] = ob.dimensionality.string
285289
# ~ continue
286290

287-
if (hasattr(ob, '_quantity_attr') and ob._quantity_attr == attrname):
291+
if (hasattr(ob, '_quantity_attr') and
292+
ob._quantity_attr == attrname):
288293
struct[attrname] = ob.magnitude
289294
struct[attrname + '_units'] = ob.dimensionality.string
290295
continue
@@ -308,7 +313,7 @@ def create_struct_from_obj(self, ob):
308313

309314
def create_ob_from_struct(self, struct, classname):
310315
cl = class_by_name[classname]
311-
# check if hinerits Quantity
316+
# check if inherits Quantity
312317
# ~ is_quantity = False
313318
# ~ for attr in cl._necessary_attrs:
314319
# ~ if attr[0] == '' and attr[1] == pq.Quantity:
@@ -372,13 +377,16 @@ def create_ob_from_struct(self, struct, classname):
372377
if attrname.endswith('_units') or attrname == 'units':
373378
# linked with another field
374379
continue
375-
if (hasattr(cl, '_quantity_attr') and cl._quantity_attr == attrname):
380+
381+
if hasattr(cl, '_quantity_attr') and cl._quantity_attr == attrname:
376382
continue
377383

378384
item = getattr(struct, attrname)
379385

380-
attributes = cl._necessary_attrs + cl._recommended_attrs
381-
dict_attributes = {a[0]: a[1:] for a in attributes}
386+
attributes = cl._necessary_attrs + cl._recommended_attrs \
387+
+ (('annotations', dict),)
388+
dict_attributes = dict([(a[0], a[1:]) for a in attributes])
389+
382390
if attrname in dict_attributes:
383391
attrtype = dict_attributes[attrname][0]
384392
if attrtype == datetime:
@@ -398,6 +406,9 @@ def create_ob_from_struct(self, struct, classname):
398406
item = pq.Quantity(item, units)
399407
else:
400408
item = pq.Quantity(item, units)
409+
elif attrtype == dict:
410+
# FIXME: works but doesn't convert nested struct to dict
411+
item = {fn: getattr(item, fn) for fn in item._fieldnames}
401412
else:
402413
item = attrtype(item)
403414

neo/test/iotest/test_neomatlabio.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class TestNeoMatlabIO(BaseTestIO, unittest.TestCase):
1919
def test_write_read_single_spike(self):
2020
block1 = Block()
2121
seg = Segment('segment1')
22-
spiketrain = SpikeTrain([1] * pq.s, t_stop=10 * pq.s, sampling_rate=1 * pq.Hz)
22+
spiketrain1 = SpikeTrain([1] * pq.s, t_stop=10 * pq.s, sampling_rate=1 * pq.Hz)
23+
spiketrain1.annotate(yep='yop')
2324
block1.segments.append(seg)
24-
seg.spiketrains.append(spiketrain)
25+
seg.spiketrains.append(spiketrain1)
2526

2627
# write block
2728
filename = BaseTestIO.get_filename_path(self, 'matlabiotestfile.mat')
@@ -35,6 +36,11 @@ def test_write_read_single_spike(self):
3536
self.assertEqual(block1.segments[0].spiketrains[0],
3637
block2.segments[0].spiketrains[0])
3738

39+
# test annotations
40+
spiketrain2 = block2.segments[0].spiketrains[0]
41+
assert 'yep' in spiketrain2.annotations
42+
assert spiketrain2.annotations['yep'] == 'yop'
43+
3844

3945
if __name__ == "__main__":
4046
unittest.main()

0 commit comments

Comments
 (0)