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

Added documentation example for combining InferenceData (#552) and also solving the issue of creaton of empty file for better behaviour with inference data(#486) #590

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Adding the test cases and solving the issue of creation of empty file
  • Loading branch information
Shashankjain12 committed Feb 17, 2019
commit 3949bee46bcac62783ba631b8e15f45d5eb4b7ec
24 changes: 15 additions & 9 deletions arviz/data/inference_data.py
Original file line number Diff line number Diff line change
@@ -80,15 +80,21 @@ def to_netcdf(self, filename, compress=True):
Location of netcdf file
"""
mode = "w" # overwrite first, then append
for group in self._groups:
data = getattr(self, group)
kwargs = {}
if compress:
kwargs["encoding"] = {var_name: {"zlib": True} for var_name in data.variables}
data.to_netcdf(filename, mode=mode, group=group, **kwargs)
data.close()
mode = "a"
return filename
#if the netcdf file previously exists the append the file
if self._groups:
for group in self._groups:
data = getattr(self, group)
kwargs = {}
if compress:
kwargs["encoding"] = {var_name: {"zlib": True} for var_name in data.variables}
data.to_netcdf(filename, mode=mode, group=group, **kwargs)
data.close()
mode = "a"
return filename
else: # else if the file doesnot exists previously creating the empty netcdf file
empty_file=nc.Dataset(filename,mode="w",format="NETCDF4")
empty_file.close()
return filename

def __add__(self, other):
"""Concatenate two InferenceData objects.
16 changes: 14 additions & 2 deletions arviz/tests/test_data.py
Original file line number Diff line number Diff line change
@@ -672,7 +672,7 @@ def test_io_function(self, data, eight_schools_params):
assert hasattr(inference_data3, "posterior")
os.remove(filepath)
assert not os.path.exists(filepath)

def test_io_method(self, data, eight_schools_params):
inference_data = self.get_inference_data( # pylint: disable=W0612
data, eight_schools_params
@@ -690,7 +690,19 @@ def test_io_method(self, data, eight_schools_params):
assert hasattr(inference_data2, "posterior")
os.remove(filepath)
assert not os.path.exists(filepath)


#New test case added for the empty file to be created
def test_new_file(self):
inference_data=InferenceData()
here=os.path.dirname(os.path.abspath(__file__))
data_directory=os.path.join(here,"saved_models")
filepath=os.path.join(data_directory,"io_new_file.nc")
assert not os.path.exists(filepath)
to_netcdf(inference_data,filepath)
assert os.path.exists(filepath)
assert os.path.getsize(filepath)>0
os.remove(filepath)
assert not os.path.exists(filepath)

class TestPyMC3NetCDFUtils:
@pytest.fixture(scope="class")