Closed avivajpeyi closed 2 years ago
tic_entry.save_data(inference_data=inference_data)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
/var/folders/qt/rxjvm_j566v9qn7g754s1v9hzb3p7f/T/ipykernel_10253/3890135057.py in <module>
----> 1 tic_entry.save_data(inference_data=inference_data)
2 summary(inference_data)
~/Documents/projects/tess/tess-atlas/src/tess_atlas/data/tic_entry.py in save_data(self, inference_data)
138 if inference_data is not None:
139 self.inference_data = inference_data
--> 140 save_inference_data(inference_data, self.outdir)
141 logger.info(f"Saved data in {self.outdir}")
~/Documents/projects/tess/tess-atlas/src/tess_atlas/data/inference_data_tools.py in save_inference_data(inference_data, outdir)
32 def save_inference_data(inference_data, outdir: str):
33 fname = get_idata_fname(outdir)
---> 34 az.to_netcdf(inference_data, filename=fname)
35 save_samples(inference_data, outdir)
36
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/arviz/data/io_netcdf.py in to_netcdf(data, filename, group, coords, dims)
50 """
51 inference_data = convert_to_inference_data(data, group=group, coords=coords, dims=dims)
---> 52 file_name = inference_data.to_netcdf(filename)
53 return file_name
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/arviz/data/inference_data.py in to_netcdf(self, filename, compress, groups)
390 if compress:
391 kwargs["encoding"] = {var_name: {"zlib": True} for var_name in data.variables}
--> 392 data.to_netcdf(filename, mode=mode, group=group, **kwargs)
393 data.close()
394 mode = "a"
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/dataset.py in to_netcdf(self, path, mode, format, group, engine, encoding, unlimited_dims, compute, invalid_netcdf)
1898 from ..backends.api import to_netcdf
1899
-> 1900 return to_netcdf(
1901 self,
1902 path,
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/api.py in to_netcdf(dataset, path_or_file, mode, format, group, engine, encoding, unlimited_dims, compute, multifile, invalid_netcdf)
1075 # TODO: allow this work (setting up the file for writing array data)
1076 # to be parallelized with dask
-> 1077 dump_to_store(
1078 dataset, store, writer, encoding=encoding, unlimited_dims=unlimited_dims
1079 )
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/api.py in dump_to_store(dataset, store, writer, encoder, encoding, unlimited_dims)
1122 variables, attrs = encoder(variables, attrs)
1123
-> 1124 store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
1125
1126
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/common.py in store(self, variables, attributes, check_encoding_set, writer, unlimited_dims)
260 writer = ArrayWriter()
261
--> 262 variables, attributes = self.encode(variables, attributes)
263
264 self.set_attributes(attributes)
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/common.py in encode(self, variables, attributes)
349 # All NetCDF files get CF encoded by default, without this attempting
350 # to write times, for example, would fail.
--> 351 variables, attributes = cf_encoder(variables, attributes)
352 variables = {k: self.encode_variable(v) for k, v in variables.items()}
353 attributes = {k: self.encode_attribute(v) for k, v in attributes.items()}
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/conventions.py in cf_encoder(variables, attributes)
851
852 # add encoding for time bounds variables if present.
--> 853 _update_bounds_encoding(variables)
854
855 new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/conventions.py in _update_bounds_encoding(variables)
431 is_datetime_type = np.issubdtype(
432 v.dtype, np.datetime64
--> 433 ) or contains_cftime_datetimes(v)
434
435 if (
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/common.py in contains_cftime_datetimes(var)
1839 def contains_cftime_datetimes(var) -> bool:
1840 """Check if an xarray.Variable contains cftime.datetime objects"""
-> 1841 return _contains_cftime_datetimes(var.data)
1842
1843
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/variable.py in data(self)
342 return self._data
343 else:
--> 344 return self.values
345
346 @data.setter
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/variable.py in values(self)
515 def values(self):
516 """The variable's data as a numpy.ndarray"""
--> 517 return _as_array_or_item(self._data)
518
519 @values.setter
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/variable.py in _as_array_or_item(data)
257 TODO: remove this (replace with np.asarray) once these issues are fixed
258 """
--> 259 data = np.asarray(data)
260 if data.ndim == 0:
261 if data.dtype.kind == "M":
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/indexing.py in __array__(self, dtype)
546
547 def __array__(self, dtype=None):
--> 548 self._ensure_cached()
549 return np.asarray(self.array, dtype=dtype)
550
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/indexing.py in _ensure_cached(self)
543 def _ensure_cached(self):
544 if not isinstance(self.array, NumpyIndexingAdapter):
--> 545 self.array = NumpyIndexingAdapter(np.asarray(self.array))
546
547 def __array__(self, dtype=None):
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/indexing.py in __array__(self, dtype)
516
517 def __array__(self, dtype=None):
--> 518 return np.asarray(self.array, dtype=dtype)
519
520 def __getitem__(self, key):
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/indexing.py in __array__(self, dtype)
417 def __array__(self, dtype=None):
418 array = as_indexable(self.array)
--> 419 return np.asarray(array[self.key], dtype=None)
420
421 def transpose(self, order):
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/netCDF4_.py in __getitem__(self, key)
89
90 def __getitem__(self, key):
---> 91 return indexing.explicit_indexing_adapter(
92 key, self.shape, indexing.IndexingSupport.OUTER, self._getitem
93 )
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/core/indexing.py in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
708 """
709 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
--> 710 result = raw_indexing_method(raw_key.tuple)
711 if numpy_indices.tuple:
712 # index the loaded np.ndarray
~/Documents/projects/tess/tess_venv/lib/python3.9/site-packages/xarray/backends/netCDF4_.py in _getitem(self, key)
102 with self.datastore.lock:
103 original_array = self.get_array(needs_lock=False)
--> 104 array = getitem(original_array, key)
105 except IndexError:
106 # Catch IndexError in netCDF4 and return a more informative
src/netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__()
src/netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable._get()
src/netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()
RuntimeError: NetCDF: HDF error
write gp model using celerite2
testing issuue
Using a simple GP example from celerite2, I was able to save the inference trace without an issue.
I tried running again with our transit model, and got the the xarray cannot serialize arbitrary Python objects
error...
I guess one of the variables that in my pymc3 reference object is an object -- need to figure out where this is happening.
The inference_data.obsereved_data.obs
is what is causing the error!!
Its storing an object rather than an array...
>>> inference_data.observed_data.obs
<xarray.DataArray 'obs' (obs_dim_0: 1)>
array([Elemwise{sub,no_inplace}.0], dtype=object)
Coordinates:
* obs_dim_0 (obs_dim_0) int64 0
Previously:
>>> inference_data.observed_data.obs
<xarray.DataArray 'obs' (obs_dim_0: 37177)>
array([-4.861564, -1.387775, 1.397878, ..., 5.90694 , 12.050734, 9.48945 ])
Coordinates:
* obs_dim_0 (obs_dim_0) int64 0 1 2 3 4 5 ... 37172 37173 37174 37175 37176
Which commit changed this?
Goes to show that we really need better unit tests...
Aha the 'fix gp' merge is what caused this...
https://github.com/dfm/tess-atlas/commit/bfcf880c24bd0c8b1dfc71d1aca46b1f2508d235
However, we still need to use the changes made here...
I'm a little surprised about this - are you sure? I would have expected the previous version to fail with this error, not the version after that commit?!?
Hmm yeah I've probably done something silly, but I think the issue lies with changing the following line from
gp.marginal(name="obs", observed=y) ## calling this model a
to
gp.marginal(name="obs", observed=residual) ## calling this model b
I tried sampling with the two different models and model A's obs
array contains floats, while model B'sobs
array contains an object
Attaching the notebook with the two models for future reference. model_comparisons.ipynb.zip
Im adding some prints in the model to figure out why obs
is storing an object rather than float values.
Maybe this could be because
type(y)
--> numpy.ndarray
type(residual)
--> theano.tensor.var.TensorVariable
Ok, so residual
being a TensorVar is the issue. Was able to reproduce this error with a simple line model:
import pymc3 as pm
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
true_m = 0.5
true_b = -1.3
true_logs = np.log(0.3)
x = np.sort(np.random.uniform(0, 5, 50))
y = true_b + true_m * x + np.exp(true_logs) * np.random.randn(len(x))
N = 1000
sampler_kwargs = dict(
draws=N, tune=N, chains=1, cores=1, return_inferencedata=True, progressbar=False
)
def sample_line_model_a():
with pm.Model() as model:
m = pm.Uniform("m", lower=-5, upper=5)
b = pm.Uniform("b", lower=-5, upper=5)
logs = pm.Uniform("logs", lower=-5, upper=5)
line = m * x + b
pm.Normal("obs", mu=line, sd=pm.math.exp(logs), observed=y)
return pm.sample(**sampler_kwargs)
def sample_line_model_b():
with pm.Model() as model:
m = pm.Uniform("m", lower=-5, upper=5)
b = pm.Uniform("b", lower=-5, upper=5)
logs = pm.Uniform("logs", lower=-5, upper=5)
line = m * x + b
residual = y-line
pm.Normal("obs", mu=0, sd=pm.math.exp(logs), observed=residual)
return pm.sample(**sampler_kwargs)
Again, here
trace_a.observed_data.obs
--> array of floats (can save inference_object)trace_b.observed_data.obs
--> array with one object (can not save inference_object)Maybe the way to proceed is to just not save the obs
array? We don't really use it/need it
Very interesting - good sleuthing!!