-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDF5 read/write functionality (#303)
* Added example hdf5 reader/writer * HDF5 functionality and tests * Bumped version number * Updated pyproject.toml * Removed typing for python3.8 * Lint corrections * Windows corrections * Fixed problems with pandas documentation * Actually now making read_hdf documentation * Removed uncovered AttributeError * Updated the documentation * bumped version * Version bump * Import read_hdf * Updated docs for new location * Moved hdf functionality to anesthetic.read * Updated documentation * Bumped version * moved circular import back out again * Removed inheritance from anesthetic * fix hdf5 docstring thing * Adjusted pandas.hdf to anesthetic.read_hdf * Added back in class --------- Co-authored-by: lukashergt <[email protected]>
- Loading branch information
1 parent
61bed43
commit 59af500
Showing
12 changed files
with
173 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '2.0.0b43' | ||
__version__ = '2.0.0b44' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Anesthetic overwrites for pandas hdf functionality.""" | ||
from pandas import HDFStore as _HDFStore | ||
from pandas.io.pytables import to_hdf as _to_hdf, read_hdf as _read_hdf | ||
from anesthetic.utils import adjust_docstrings | ||
from anesthetic.samples import NestedSamples, MCMCSamples, Samples | ||
|
||
|
||
class HDFStore(_HDFStore): # noqa: D101 | ||
anesthetic_types = {x.__name__: x | ||
for x in [NestedSamples, MCMCSamples, Samples]} | ||
|
||
def get(self, key, *args, **kwargs): # noqa: D102 | ||
storer = self.get_storer(key) | ||
anesthetic_type = storer.attrs.anesthetic_type | ||
anesthetic_type = self.anesthetic_types[anesthetic_type] | ||
value = super().get(key, *args, **kwargs) | ||
value = anesthetic_type(value) | ||
_metadata = storer.attrs._metadata.keys() | ||
value._metadata = list(_metadata) | ||
for k, v in storer.attrs._metadata.items(): | ||
setattr(value, k, v) | ||
return value | ||
|
||
def put(self, key, value, *args, **kwargs): # noqa: D102 | ||
from anesthetic import __version__ | ||
super().put(key, value, *args, **kwargs) | ||
storer = self.get_storer(key) | ||
storer.attrs._metadata = { | ||
k: getattr(value, k) | ||
for k in value._metadata | ||
} | ||
storer.attrs.anesthetic_type = type(value).__name__ | ||
storer.attrs.anesthetic_version = __version__ | ||
|
||
def select(self, key, *args, **kwargs): # noqa: D102 | ||
storer = self.get_storer(key) | ||
anesthetic_type = storer.attrs.anesthetic_type | ||
anesthetic_type = self.anesthetic_types[anesthetic_type] | ||
value = super().select(key, *args, **kwargs) | ||
value = anesthetic_type(value) | ||
_metadata = storer.attrs._metadata.keys() | ||
value._metadata = list(_metadata) | ||
for k, v in storer.attrs._metadata.items(): | ||
setattr(value, k, v) | ||
return value | ||
|
||
|
||
def to_hdf(path_or_buf, key, value, mode="a", complevel=None, complib=None, | ||
*args, **kwargs): # noqa: D103 | ||
|
||
store = HDFStore(path_or_buf, mode=mode, complevel=complevel, | ||
complib=complib) | ||
store.__fspath__ = lambda: store | ||
return _to_hdf(store, key, value, *args, **kwargs) | ||
|
||
|
||
def read_hdf(path_or_buf, *args, **kwargs): # noqa: D103 | ||
store = HDFStore(path_or_buf) | ||
return _read_hdf(store, *args, **kwargs) | ||
|
||
|
||
to_hdf.__doc__ = _to_hdf.__doc__ | ||
read_hdf.__doc__ = _read_hdf.__doc__ | ||
adjust_docstrings(read_hdf, 'read_hdf', 'anesthetic.read_hdf') | ||
adjust_docstrings(read_hdf, 'DataFrame', 'pandas.DataFrame') | ||
adjust_docstrings(read_hdf, ':func:`open`', '`open`') | ||
adjust_docstrings(read_hdf, ':class:`pandas.HDFStore`', '`pandas.HDFStore`') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Anesthetic testing utilities.""" | ||
import pandas.testing | ||
import numpy.testing | ||
|
||
|
||
def assert_frame_equal(left, right, *args, **kwargs): | ||
"""Assert frames are equal, including metadata.""" | ||
check_metadata = kwargs.pop('check_metadata', True) | ||
pandas.testing.assert_frame_equal(left, right, *args, **kwargs) | ||
numpy.testing.assert_array_equal(left._metadata, right._metadata) | ||
if check_metadata: | ||
for key in left._metadata: | ||
assert getattr(left, key) == getattr(right, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters