Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressing #77 #78

Merged
merged 3 commits into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# gneiss changelog

## Version 0.2.0

### Features
* Added filehandle support for write and read io in RegressionResults object [#72](https://github.com/biocore/gneiss/issues/77)


## Version 0.1.3

### Features
Expand Down
19 changes: 12 additions & 7 deletions gneiss/regression/_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def read_pickle(self, filename):

Parameters
----------
filename : str
filename : str or filehandle
Input file to unpickle.

Returns
Expand All @@ -283,18 +283,23 @@ def read_pickle(self, filename):
Warning: Loading pickled data received from untrusted
sources can be unsafe. See: https://wiki.python.org/moin/UsingPickle
"""
with open(filename, 'rb') as fh:
res = pickle.load(fh)
if isinstance(filename, str):
with open(filename, 'rb') as fh:
res = pickle.load(fh)
else:
res = pickle.load(filename)
return res

def write_pickle(self, filename):
""" Writes RegressionResults object to pickle file.

Parameters
----------
filename : str
filename : str or filehandle
Output file to store pickled object.
"""

with open(filename, 'wb') as fh:
pickle.dump(self, fh)
if isinstance(filename, str):
with open(filename, 'wb') as fh:
pickle.dump(self, fh)
else:
pickle.dump(self, filename)
11 changes: 11 additions & 0 deletions gneiss/regression/tests/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ def test_read_write(self):
self.assertEqual(str(res.tree), str(exp.tree))
pdt.assert_frame_equal(res.pvalues, exp.pvalues)

def test_read_write_handle(self):
with open(self.pickle_fname, 'wb') as wfh:
exp = RegressionResults(self.results, tree=self.tree)
exp.write_pickle(wfh)

with open(self.pickle_fname, 'rb') as rfh:
res = RegressionResults.read_pickle(rfh)

self.assertEqual(str(res.tree), str(exp.tree))
pdt.assert_frame_equal(res.pvalues, exp.pvalues)


if __name__ == "__main__":
unittest.main()