Close file of InferenceData loaded using from_netcdf

Hi everyone,

I am doing some MCMC simulations in a Jupyter Notebook. I saved a trace using arviz.to_netcdf to a certain filepath, and then loaded it using arviz.from_netcdf. However, I later ran another (improved) simulation using MCMC and wanted to override the old one, i.e. I wanted to save the new simulation on the same filepath. However, if I do not close the notebook and I try to save to the same filepath from which I loaded using arviz.from_netcdf I get the following error

**OSError** : Unable to create file (unable to truncate a file which is already open)

How can I close the file opened by using arviz.from_netcdf? I did not find anything in the Arviz documentation.

Thanks a lot!

I suspect that this is either a platform-dependent problem or that something has gone wrong and interrupted the arviz process that would otherwise close the file. It may also have to do with running things is a notebook, though I am skeptical of that. Can you run the following:

import pymc as pm
import arviz as az
with pm.Model() as model:
    a = pm.Normal("a")
    b = pm.Normal("b", mu=a, sigma=1)
    idata = pm.sample()
    idata.to_netcdf("test.nc")
    idata2 = az.from_netcdf("test.nc")
1 Like