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

More informative error message for error reading InferenceData from NetCDF #1637

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Improved retrieving or pointwise log likelihood in `from_cmdstanpy`, `from_cmdstan` and `from_pystan` ([1579](https://github.com/arviz-devs/arviz/pull/1579) and [1599](https://github.com/arviz-devs/arviz/pull/1599))
* Added interactive legend to bokeh `forestplot` ([1591](https://github.com/arviz-devs/arviz/pull/1591))
* Added interactive legend to bokeh `ppcplot` ([1602](https://github.com/arviz-devs/arviz/pull/1602))
* Add more helpful error message for HDF5 problems reading `InferenceData` from NetCDF ([1637](https://github.com/arviz-devs/arviz/pull/1637))

### Maintenance and fixes
* Enforced using coordinate values as default labels ([1201](https://github.com/arviz-devs/arviz/pull/1201))
Expand Down
32 changes: 23 additions & 9 deletions arviz/data/inference_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,30 @@ def from_netcdf(filename: str) -> "InferenceData":
InferenceData object
"""
groups = {}
with nc.Dataset(filename, mode="r") as data:
data_groups = list(data.groups)
try:
with nc.Dataset(filename, mode="r") as data:
data_groups = list(data.groups)

for group in data_groups:
with xr.open_dataset(filename, group=group) as data:
if rcParams["data.load"] == "eager":
groups[group] = data.load()
else:
groups[group] = data
return InferenceData(**groups)
for group in data_groups:
with xr.open_dataset(filename, group=group) as data:
if rcParams["data.load"] == "eager":
groups[group] = data.load()
else:
groups[group] = data
return InferenceData(**groups)
except OSError as e: # pylint: disable=invalid-name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use e (instead of err or something a bit longer that pylint considers optimal) as variable to raise errors from other errors, it may be worth it to add it to .pylintrc instead of having to disable the error every time

if e.errno == -101:
raise type(e)(
str(e)
+ (
" while reading a NetCDF file. This is probably an error in HDF5, "
"which happens because your OS does not support HDF5 file locking. See "
"https://stackoverflow.com/questions/49317927/"
"errno-101-netcdf-hdf-error-when-opening-netcdf-file#49317928"
" for a possible solution."
)
)
raise e

def to_netcdf(
self, filename: str, compress: bool = True, groups: Optional[List[str]] = None
Expand Down