Skip to content

Commit

Permalink
DimensionLink: Write through label and unit
Browse files Browse the repository at this point in the history
Can now write through to the linked data object when setting label and
unit on RangeDimension:
- RangeDimension ticks cannot be changed.
- RangeDimension label linked to DataArray changes the DataArray label.
- RangeDimension label linked to DataFrame raises error: The column name
  of a DataFrame can't be changed since it's the name of a field of
  a compound data type, which we can't change.
- RangeDimension unit linked to DataArray changes the DataArray unit.
- RangeDimension unit linked to DataFrame changes the DataFrame column
  unit.
- SetDimension labels cannot be changed.
  • Loading branch information
achilleas-k committed Jul 16, 2020
1 parent 4cb2a78 commit ddbc727
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions nixio/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ def unit(self):
raise RuntimeError("Invalid DataObjectType attribute found in "
"DimensionLink")

@unit.setter
def unit(self, unit):
"""
Sets the unit of the linked data object.
"""
lobj = self._linked_group()
if self._data_object_type == "DataArray":
lobj.set_attr("unit", unit)
elif self._data_object_type == "DataFrame":
units = list(lobj.get_attr("units"))
units[self.index] = unit
lobj.set_attr("units", units)
else:
raise RuntimeError("Invalid DataObjectType attribute found in "
"DimensionLink")

@property
def label(self):
"""
Expand All @@ -161,6 +177,21 @@ def label(self):
raise RuntimeError("Invalid DataObjectType attribute found in "
"DimensionLink")

@label.setter
def label(self, label):
"""
Sets the label of the linked data objet.
"""
lobj = self._linked_group()
if self._data_object_type == "DataArray":
lobj.set_attr("label", label)
elif self._data_object_type == "DataFrame":
raise RuntimeError("The label of a Dimension linked to a "
"DataFrame column cannot be modified")
else:
raise RuntimeError("Invalid DataObjectType attribute found in "
"DimensionLink")

@property
def _data_object_type(self):
return self._h5group.get_attr("data_object_type")
Expand Down Expand Up @@ -233,18 +264,6 @@ def has_link(self):
return True
return False

@property
def _redirgrp(self):
"""
If the dimension links to a data object, this property returns
the H5Group of the linked DataArray or DataFrame. Otherwise, it returns
the H5Group representing the dimension.
"""
if self.has_link:
link = self._h5group.get_by_name("link")
return link.get_by_pos(0)
return self._h5group

@property
def dimension_link(self):
"""
Expand Down Expand Up @@ -392,9 +411,11 @@ def ticks(self):

@ticks.setter
def ticks(self, ticks):
# TODO: Write through to linked object
if np.any(np.diff(ticks) < 0):
raise ValueError("Ticks are not given in an ascending order.")
if self.has_link():
raise RuntimeError("The ticks of a RangeDimension linked to a "
"data object cannot be modified")
self._h5group.write_data("ticks", ticks)

@property
Expand All @@ -405,9 +426,11 @@ def label(self):

@label.setter
def label(self, label):
# TODO: Write through to linked object
util.check_attr_type(label, str)
self._redirgrp.set_attr("label", label)
if self.has_link:
self.dimension_link.label = label
else:
self._h5group.set_attr("label", label)

@property
def unit(self):
Expand All @@ -418,7 +441,10 @@ def unit(self):
@unit.setter
def unit(self, u):
util.check_attr_type(u, str)
self._redirgrp.set_attr("unit", u)
if self.has_link:
self.dimension_link.unit = u
else:
self._h5group.set_attr("unit", u)

def index_of(self, position):
"""
Expand Down Expand Up @@ -499,6 +525,8 @@ def labels(self):

@labels.setter
def labels(self, labels):
# TODO: Write through to linked object
if self.has_link:
raise RuntimeError("The labels of a SetDimension linked to a "
"data object cannot be modified")
dt = util.vlen_str_dtype
self._h5group.write_data("labels", labels, dtype=dt)

0 comments on commit ddbc727

Please sign in to comment.