Skip to content

Commit

Permalink
fix larray-project#702 : fixed bug when writing metadata using HDF fo…
Browse files Browse the repository at this point in the history
…rmat
  • Loading branch information
alixdamman committed Feb 4, 2019
1 parent c2fa6d6 commit 9caa3d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/source/changes/version_0_30.rst.inc
Expand Up @@ -231,3 +231,5 @@ Fixes
of the API Reference (closes :issue:`698`).

* fixed arithmetic operations between two sessions returning a nan value for each axis and group (closes :issue:`725`).

* fixed dumping sessions with metadata using HDF format (closes :issue:`702`).
14 changes: 9 additions & 5 deletions larray/core/metadata.py
Expand Up @@ -168,11 +168,15 @@ def _convert_value(value):
return Metadata([(key, _convert_value(value)) for key, value in zip(array.axes.labels[0], array.data)])

# ---------- IO methods ----------
def to_hdf(self, hdfstore, key):
def to_hdf(self, hdfstore, key=None):
if len(self):
hdfstore.get_storer(key).attrs.metadata = self
attrs = hdfstore.get_storer(key).attrs if key is not None else hdfstore.root._v_attrs
attrs.metadata = self

@classmethod
def from_hdf(cls, hdfstore, key):
if 'metadata' in hdfstore.get_storer(key).attrs:
return hdfstore.get_storer(key).attrs.metadata
def from_hdf(cls, hdfstore, key=None):
attrs = hdfstore.get_storer(key).attrs if key is not None else hdfstore.root._v_attrs
if 'metadata' in attrs:
return attrs.metadata
else:
return None
8 changes: 5 additions & 3 deletions larray/inout/hdf.py
Expand Up @@ -143,11 +143,13 @@ def _dump_item(self, key, value, *args, **kwargs):
raise TypeError()

def _read_metadata(self):
attrs = self.handle.get_node('')._v_attrs
return attrs.metadata if 'metadata' in attrs else Metadata()
metadata = Metadata.from_hdf(self.handle)
if metadata is None:
metadata = Metadata()
return metadata

def _dump_metadata(self, metadata):
self.handle.get_node('')._v_attrs.metadata = metadata
metadata.to_hdf(self.handle)

def close(self):
self.handle.close()

0 comments on commit 9caa3d4

Please sign in to comment.