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

Added example for concat method #1037

Merged
merged 9 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
### Documentation
* Updated `InferenceData` schema specification (`log_likelihood`,
`predictions` and `predictions_constant_data` groups)
* Clarify the usage of "plot_joint" (#1001)
* Added the API link of function to examples (#1013)
* Clarify the usage of "plot_joint" (#1001)
* Clarify the usage of `plot_joint` (#1001)
* Added the API link of function to examples (#1013)
* Updated PyStan_schema_example to include example of out-of-sample prediction (#1032)
* Added example for `concat` method (#1037)


## v0.6.1 (2019 Dec 28)
Expand Down
37 changes: 37 additions & 0 deletions arviz/data/inference_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,43 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True):
InferenceData
A new InferenceData object by default.
When `inplace==True` merge args to first arg and return `None`

Examples
--------
Use ``concat`` method to concatenate InferenceData objects. This will concatenates over
unique groups by default. We first create an InferenceData object:
percygautam marked this conversation as resolved.
Show resolved Hide resolved

.. ipython::

In [1]: import arviz as az
...: import numpy as np
...: data = {
...: "a": (["chain", "draw", "a_dim"], np.random.normal(size=(4, 100, 3))),
...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))),
...: }
Copy link
Member

Choose a reason for hiding this comment

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

We should follow black formatting, the } should be on the same level as the d in data and the two lines in between indented 4 spaces.

...: coords = {
...: "chain": (["chain"], np.arange(4)),
...: "draw": (["draw"], np.arange(100)),
...: "a_dim": (["a_dim"], ["x", "y", "z"]),
...: }
...: dataA = az.from_dict(posterior=data, coords=coords)
Copy link
Member

Choose a reason for hiding this comment

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

I tried this code locally and it does not create an InferenceData object properly. ArviZ idiom should be followed:

data = { 
   "a": np.random.normal(size=(4, 100, 3)),
   "b": np.random.normal(size=(4, 100))
}
coords = {"a_dim": ["x", "y", "z"]}
dataA = az.from_dict(data, coords=coords, dims={"a": ["a_dim"]})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, for the correction. It seems the xarray dataset (used in the previous case) map dims automatically, but we need to specify the mapping in case of from_dict. Is this the case?

Copy link
Member

Choose a reason for hiding this comment

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

xarray's Dataset allows several notations, in the one used, the data and coordinates are specified as dicts of tuples, where the tuple contains both the data and the dimensions. ArviZ uses only one syntax, dicts of arrays for all 3 of them, the values of the dicts are directly understood as data, coordinate values or dimension list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, thanks for the clarification :)

...: dataA

We have created an ``InferenceData`` object with default group 'posterior'. Now, we will
create another InferenceData object:
percygautam marked this conversation as resolved.
Show resolved Hide resolved

.. ipython::

In [1]: dataB = az.from_dict(prior=data, coords=coords)
...: dataB

We have created another ``InferenceData`` object with group 'prior'. Now, we will concatenate
these two ``InferenceData`` objects:

.. ipython::

In [1]: az.concat(dataA, dataB)
OriolAbril marked this conversation as resolved.
Show resolved Hide resolved

"""
# pylint: disable=undefined-loop-variable, too-many-nested-blocks
if len(args) == 0:
Expand Down