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

support PyStan version < 2.18 #189

Merged
merged 2 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions arviz/utils/xarray_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ def posterior_to_xarray(self):
dtypes = self.infer_dtypes()
data = {}
var_dict = self.fit.extract(self._var_names, dtypes=dtypes, permuted=False)
if not isinstance(var_dict, dict):
# PyStan version < 2.18
var_dict = self.fit.extract(self._var_names, dtypes=dtypes, permuted=True)
permutation_order = self.fit.sim["permutation"]
original_order = []
for i_permutation_order in permutation_order:
reorder = np.argsort(i_permutation_order) + len(original_order)
original_order.extend(list(reorder))
nchain = self.fit.sim["chains"]
for key, values in var_dict.items():
var_dict[key] = self.unpermute(values, original_order, nchain)
for var_name, values in var_dict.items():
data[var_name] = np.swapaxes(values, 0, 1)
return dict_to_dataset(data, coords=self.coords, dims=self.dims)
Expand All @@ -287,13 +298,35 @@ def sample_stats_to_xarray(self):
}

sampler_params = self.fit.get_sampler_params(inc_warmup=False)
stat_lp = self.fit.extract('lp__', permuted=False)
if not isinstance(stat_lp, dict):
# PyStan version < 2.18
permutation_order = self.fit.sim["permutation"]
original_order = []
for i_permutation_order in permutation_order:
reorder = np.argsort(i_permutation_order) + len(original_order)
original_order.extend(list(reorder))
nchain = self.fit.sim["chains"]
stat_lp = self.fit.extract('lp__', permuted=True)['lp__']
stat_lp = self.unpermute(stat_lp, original_order, nchain)
else:
# PyStan version 2.18+
stat_lp = stat_lp['lp__']
# Add lp to sampler_params
for i, _ in enumerate(sampler_params):
sampler_params[i]['lp__'] = stat_lp[:, i]
data = {}
for key in sampler_params[0]:
name = rename_key.get(key, re.sub('__$', "", key))
data[name] = np.vstack([j[key].astype(dtypes.get(key)) for j in sampler_params])
return dict_to_dataset(data, coords=self.coords, dims=self.dims)

@requires('fit')
def infer_dtypes(self):
"""Infer dtypes from Stan model code. Function strips out generated quantities block
and searchs for `int` dtypes after stripping out comments inside the block.

"""
pattern_remove_comments = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL|re.MULTILINE
Expand All @@ -317,6 +350,33 @@ def infer_dtypes(self):
dtypes = {item.strip() : 'int' for item in dtypes if item.strip() in self._var_names}
return dtypes

def unpermute(self, ary, idx, nchain):
"""Unpermute permuted sample

Returns output compatible with PyStan 2.18+
fit.extract(par, permuted=False)[par]

Parameters
----------
ary : list
Permuted sample
idx : list
list containing reorder indexes.
`idx = np.argsort(permutation_order)`
nchain : int
number of chains used
`fit.sim['chains']´

Returns
-------
numpy.ndarray
Unpermuted sample
"""
ary = np.asarray(ary)[idx]
ary_shape = ary.shape[1:]
ary = ary.reshape((-1, nchain, *ary_shape), order='F')
return ary

def to_inference_data(self):
return InferenceData(**{
'posterior': self.posterior_to_xarray(),
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
git+https://github.com/pymc-devs/pymc3
git+https://github.com/stan-dev/pystan
pystan
ipython
nbsphinx
numpydoc
Expand Down