Skip to content

Commit 9250c49

Browse files
committed
Merge branch 'nixio-annotation-types'
* nixio-annotation-types: [test-nixio] Test different types of Neo annotations [nixio] Handle datetime annotations
2 parents a9ac68d + 6321149 commit 9250c49

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

neo/io/nixio.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
string_types = str
5757

5858
EMPTYANNOTATION = "EMPTYLIST"
59+
DATETIMEANNOTATION = "DATETIME"
5960
ARRAYANNOTATION = "ARRAYANNOTATION"
6061
MIN_NIX_VER = Version("1.5.0")
6162

@@ -1162,7 +1163,8 @@ def _write_property(self, section, name, v):
11621163
section.create_property(name, v.magnitude.item())
11631164
section.props[name].unit = str(v.dimensionality)
11641165
elif isinstance(v, datetime):
1165-
section.create_property(name, calculate_timestamp(v))
1166+
prop = section.create_property(name, calculate_timestamp(v))
1167+
prop.definition = DATETIMEANNOTATION
11661168
elif isinstance(v, string_types):
11671169
if len(v):
11681170
section.create_property(name, v)
@@ -1201,8 +1203,7 @@ def _write_property(self, section, name, v):
12011203
values.append(item)
12021204
section.create_property(name, values)
12031205
section.props[name].unit = unit
1204-
if definition:
1205-
section.props[name].definition = definition
1206+
section.props[name].definition = definition
12061207
elif type(v).__module__ == "numpy":
12071208
section.create_property(name, v.item())
12081209
else:
@@ -1242,6 +1243,8 @@ def _nix_attr_to_neo(nix_obj):
12421243
neo_attrs['array_annotations'][prop.name] = values
12431244
else:
12441245
neo_attrs['array_annotations'] = {prop.name: values}
1246+
elif prop.definition == DATETIMEANNOTATION:
1247+
values = datetime.fromtimestamp(values)
12451248
else:
12461249
neo_attrs[prop.name] = values
12471250
# since the 'neo_name' NIX property becomes the actual object's name,

neo/test/iotest/test_nixio.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from collections.abc import Iterable
1818
except ImportError:
1919
from collections import Iterable
20-
from datetime import datetime
20+
from datetime import date, time, datetime
2121

2222
from tempfile import mkdtemp
2323

@@ -1446,6 +1446,30 @@ def generate_complete_block():
14461446

14471447
self.write_and_compare([block_lazy])
14481448

1449+
def test_annotation_types(self):
1450+
annotations = {
1451+
"somedate": self.rdate(),
1452+
"now": datetime.now(),
1453+
"today": date.today(),
1454+
"sometime": time(13, 37, 42),
1455+
"somequantity": self.rquant(10, pq.ms),
1456+
"somestring": self.rsentence(3),
1457+
"somebytes": bytes(self.rsentence(4), "utf8"),
1458+
"npfloat": np.float(10),
1459+
"nparray": np.array([1, 2, 400]),
1460+
"emptystr": "",
1461+
}
1462+
wblock = Block("annotation_block", **annotations)
1463+
self.writer.write_block(wblock)
1464+
rblock = self.writer.read_block(neoname="annotation_block")
1465+
for k in annotations:
1466+
orig = annotations[k]
1467+
readval = rblock.annotations[k]
1468+
if isinstance(orig, np.ndarray):
1469+
np.testing.assert_almost_equal(orig, readval)
1470+
else:
1471+
self.assertEqual(annotations[k], rblock.annotations[k])
1472+
14491473

14501474
@unittest.skipUnless(HAVE_NIX, "Requires NIX")
14511475
class NixIOReadTest(NixIOTest):

0 commit comments

Comments
 (0)