Skip to content

Commit

Permalink
[nixio] Special cases for annotations
Browse files Browse the repository at this point in the history
Empty lists and tuples: If an annotation's value is an empty iterable,
the property value is set to empty string and the definition is used to
signify that the original value was an EMPTY LIST. On read, this is used
to set the annotation to [].
Fixes #500

Lists of strings: Multidimensional data are not supported. Lists of
strings were being detected as multidimensional because strings are
iterables. Items in an iterable are now checked against string types
before checking if they're any other type of iterable.
  • Loading branch information
achilleas-k committed Mar 21, 2018
1 parent d90f14d commit fbb8feb
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions neo/io/nixio.py
Expand Up @@ -47,6 +47,8 @@
except NameError:
string_types = str

EMPTYANNOTATION = "EMPTYLIST"


def stringify(value):
if value is None:
Expand Down Expand Up @@ -1033,13 +1035,23 @@ def _write_property(self, section, name, v):
elif isinstance(v, Iterable):
values = []
unit = None
if hasattr(v, "ndim") and v.ndim == 0:
definition = None
if len(v) == 0:
# empty list can't be saved in NIX property
# but we can store an empty string and use the
# definition to signify that it should be restored
# as an iterable (list)
values = ""
definition = EMPTYANNOTATION
elif hasattr(v, "ndim") and v.ndim == 0:
values = v.item()
if isinstance(v, pq.Quantity):
unit = str(v.dimensionality)
else:
for item in v:
if isinstance(item, pq.Quantity):
if isinstance(item, string_types):
item = nix.Value(item)
elif isinstance(item, pq.Quantity):
unit = str(item.dimensionality)
item = nix.Value(item.magnitude.item())
elif isinstance(item, Iterable):
Expand All @@ -1052,6 +1064,8 @@ def _write_property(self, section, name, v):
values.append(item)
section[name] = values
section.props[name].unit = unit
if definition:
section.props[name].definition = definition
elif type(v).__module__ == "numpy":
section[name] = nix.Value(v.item())
else:
Expand All @@ -1075,15 +1089,15 @@ def _nix_attr_to_neo(nix_obj):
neo_attrs["description"] = stringify(nix_obj.definition)
if nix_obj.metadata:
for prop in nix_obj.metadata.props:
values = prop.values
values = list(v.value for v in values)
values = list(v.value for v in prop.values)
if prop.unit:
units = prop.unit
values = create_quantity(values, units)
if len(values) == 1:
neo_attrs[prop.name] = values[0]
else:
neo_attrs[prop.name] = values
values = values[0]
if values == "" and prop.definition == EMPTYANNOTATION:
values = list()
neo_attrs[prop.name] = values
neo_attrs["name"] = stringify(neo_attrs.get("neo_name"))

if "file_datetime" in neo_attrs:
Expand Down

0 comments on commit fbb8feb

Please sign in to comment.