Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constant_data_vars = {
name: var for name, var in self.model.named_vars.items() if name not in model_vars
}
if not constant_data_vars:
return None
if self.dims is None:
dims = {}
else:
dims = self.dims
constant_data = {}
for name, vals in constant_data_vars.items():
if hasattr(vals, "get_value"):
vals = vals.get_value()
vals = np.atleast_1d(vals)
val_dims = dims.get(name)
val_dims, coords = generate_dims_coords(
vals.shape, name, dims=val_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
constant_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=constant_data, attrs=make_attrs(library=self.pymc3))
def observed_data_to_xarray(self):
"""Convert observed data to xarray."""
if self.dims is None:
dims = {}
else:
dims = self.dims
observed_data = {}
for name, vals in self.observations.items():
vals = utils.one_de(vals)
val_dims = dims.get(name)
val_dims, coords = generate_dims_coords(
vals.shape, name, dims=val_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
observed_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.pyro))
def observed_data_to_xarray(self):
"""Convert observed data to xarray."""
observed_data = {}
for key, vals in self.observed_data.items():
vals = utils.one_de(vals)
val_dims = self.dims.get(key) if self.dims is not None else None
val_dims, coords = generate_dims_coords(
vals.shape, key, dims=val_dims, coords=self.coords
)
observed_data[key] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.cmdstanpy))
]
if bad_groups:
raise SyntaxError(
"all arg_groups values should be either 'observed_data' or 'constant_data' "
", not {}".format(bad_groups)
)
obs_const_dict = {group: {} for group in arg_groups_set}
for idx, (arg_name, group) in enumerate(zip(self.arg_names, self.arg_groups)):
# Use emcee3 syntax, else use emcee2
arg_array = np.atleast_1d(
self.sampler.log_prob_fn.args[idx]
if hasattr(self.sampler, "log_prob_fn")
else self.sampler.args[idx]
)
arg_dims = dims.get(arg_name)
arg_dims, coords = generate_dims_coords(
arg_array.shape, arg_name, dims=arg_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in arg_dims}
obs_const_dict[group][arg_name] = xr.DataArray(arg_array, dims=arg_dims, coords=coords)
for key, values in obs_const_dict.items():
obs_const_dict[key] = xr.Dataset(data_vars=values, attrs=make_attrs(library=self.emcee))
return obs_const_dict
def observed_data_to_xarray(self):
"""Convert observed data to xarray."""
if self.dims is None:
dims = {}
else:
dims = self.dims
observed_data = {}
for name, vals in self.observations.items():
vals = utils.one_de(vals)
val_dims = dims.get(name)
val_dims, coords = generate_dims_coords(
vals.shape, name, dims=val_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
observed_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.numpyro))
posterior_model = self.posterior_model
if self.dims is None:
dims = {}
else:
dims = self.dims
obs_const_dict = {}
for group_name in ("observed_data", "constant_data"):
names = getattr(self, group_name)
if names is None:
continue
names = [names] if isinstance(names, str) else names
data = OrderedDict()
for key in names:
vals = np.atleast_1d(posterior_model.data[key])
val_dims = dims.get(key)
val_dims, coords = generate_dims_coords(
vals.shape, key, dims=val_dims, coords=self.coords
)
data[key] = xr.DataArray(vals, dims=val_dims, coords=coords)
obs_const_dict[group_name] = xr.Dataset(
data_vars=data, attrs=make_attrs(library=self.stan)
)
return obs_const_dict
observed_data = {}
if isinstance(self.observed, self.tf.Tensor):
with self.tf.Session() as sess:
vals = sess.run(self.observed, feed_dict=self.feed_dict)
else:
vals = self.observed
if self.dims is None:
dims = {}
else:
dims = self.dims
name = "obs"
val_dims = dims.get(name)
vals = utils.one_de(vals)
val_dims, coords = generate_dims_coords(vals.shape, name, dims=val_dims, coords=self.coords)
# coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
observed_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.tfp))
def observed_data_to_xarray(self):
"""Convert observed data to xarray."""
if self.dims is None:
dims = {}
else:
dims = self.dims
observed_data = {}
for name, vals in self.observations.items():
if hasattr(vals, "get_value"):
vals = vals.get_value()
vals = utils.one_de(vals)
val_dims = dims.get(name)
val_dims, coords = generate_dims_coords(
vals.shape, name, dims=val_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
observed_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.pymc3))