From 0d08aa4c6e0d810c7baac2d6cbda304abf5758f8 Mon Sep 17 00:00:00 2001 From: percygautam Date: Thu, 30 Jan 2020 01:05:26 +0530 Subject: [PATCH 1/8] Added example for concat method --- arviz/data/inference_data.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 6a5985ed2d..3b0be0bf14 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -252,6 +252,46 @@ 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: + + .. ipython:: + + In [1]: import arviz as az + ...: import numpy as np + ...: import xarray as xr + ...: dataset = xr.Dataset( + ...: { + ...: "a": (["chain", "draw", "a_dim"], np.random.normal(size=(4, 100, 3))), + ...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))), + ...: }, + ...: coords={ + ...: "chain": (["chain"], np.arange(4)), + ...: "draw": (["draw"], np.arange(100)), + ...: "a_dim": (["a_dim"], ["x", "y", "z"]), + ...: } + ...: ) + ...: dataA = az.convert_to_inference_data(dataset) + ...: dataA + + We have created an ``InferenceData`` object with default group 'posterior'. Now we will + create another InferenceData object: + + .. ipython:: + + In [1]: dataB = az.convert_to_inference_data(dataset, group = "prior") + ...: 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) + """ # pylint: disable=undefined-loop-variable, too-many-nested-blocks if len(args) == 0: From 1584b7d936ff29414f4de294264f534b088af707 Mon Sep 17 00:00:00 2001 From: percygautam Date: Thu, 30 Jan 2020 01:13:09 +0530 Subject: [PATCH 2/8] Changes to changelog --- CHANGELOG.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa50c73bdd..95d5805fff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From 66597294745182972002e8bd0b98a17a429adafb Mon Sep 17 00:00:00 2001 From: percygautam Date: Sat, 1 Feb 2020 11:25:17 +0530 Subject: [PATCH 3/8] Minor changes --- arviz/data/inference_data.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 3b0be0bf14..e44923e867 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -262,19 +262,7 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): In [1]: import arviz as az ...: import numpy as np - ...: import xarray as xr - ...: dataset = xr.Dataset( - ...: { - ...: "a": (["chain", "draw", "a_dim"], np.random.normal(size=(4, 100, 3))), - ...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))), - ...: }, - ...: coords={ - ...: "chain": (["chain"], np.arange(4)), - ...: "draw": (["draw"], np.arange(100)), - ...: "a_dim": (["a_dim"], ["x", "y", "z"]), - ...: } - ...: ) - ...: dataA = az.convert_to_inference_data(dataset) + ...: dataA = az.from_dict(posterior={"mu": np.random.randn()}) ...: dataA We have created an ``InferenceData`` object with default group 'posterior'. Now we will @@ -282,7 +270,7 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): .. ipython:: - In [1]: dataB = az.convert_to_inference_data(dataset, group = "prior") + In [1]: dataB = az.from_dict(prior={"mu": np.random.randn()}) ...: dataB We have created another ``InferenceData`` object with group 'prior'. Now we will concatenate From 412fbc812a0ce4b4aa0d3604d6893c7cd0a9369e Mon Sep 17 00:00:00 2001 From: percygautam Date: Tue, 4 Feb 2020 01:40:50 +0530 Subject: [PATCH 4/8] final changes --- arviz/data/inference_data.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index e44923e867..2521027ae4 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -262,18 +262,27 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): In [1]: import arviz as az ...: import numpy as np - ...: dataA = az.from_dict(posterior={"mu": np.random.randn()}) + ...: data = { + ...: "a": (["chain", "draw", "a_dim"], np.random.normal(size=(4, 100, 3))), + ...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))), + ...: } + ...: 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) ...: dataA - We have created an ``InferenceData`` object with default group 'posterior'. Now we will + We have created an ``InferenceData`` object with default group 'posterior'. Now, we will create another InferenceData object: .. ipython:: - In [1]: dataB = az.from_dict(prior={"mu": np.random.randn()}) + In [1]: dataB = az.from_dict(prior=data, coords=coords) ...: dataB - We have created another ``InferenceData`` object with group 'prior'. Now we will concatenate + We have created another ``InferenceData`` object with group 'prior'. Now, we will concatenate these two ``InferenceData`` objects: .. ipython:: From be8b5f77fba6789cb8c001037799dcef4aea950b Mon Sep 17 00:00:00 2001 From: percygautam Date: Tue, 4 Feb 2020 03:07:20 +0530 Subject: [PATCH 5/8] corrections --- arviz/data/inference_data.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 2521027ae4..0fcb468ccd 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -267,11 +267,9 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): ...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))), ...: } ...: coords = { - ...: "chain": (["chain"], np.arange(4)), - ...: "draw": (["draw"], np.arange(100)), - ...: "a_dim": (["a_dim"], ["x", "y", "z"]), + ...: "a_dim": ["x", "y", "z"] ...: } - ...: dataA = az.from_dict(posterior=data, coords=coords) + ...: dataA = az.from_dict(data, coords=coords, dims={"a": ["a_dim"]}) ...: dataA We have created an ``InferenceData`` object with default group 'posterior'. Now, we will @@ -279,7 +277,7 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): .. ipython:: - In [1]: dataB = az.from_dict(prior=data, coords=coords) + In [1]: dataB = az.from_dict(prior=data, coords=coords, dims={"a": ["a_dim"]}) ...: dataB We have created another ``InferenceData`` object with group 'prior'. Now, we will concatenate From 616ecb45c1bb0af530f88219d72c49023a189982 Mon Sep 17 00:00:00 2001 From: percygautam Date: Tue, 4 Feb 2020 03:45:20 +0530 Subject: [PATCH 6/8] corrections --- arviz/data/inference_data.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 0fcb468ccd..3223fa34f3 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -263,12 +263,10 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): 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))), - ...: } - ...: coords = { - ...: "a_dim": ["x", "y", "z"] - ...: } + ...: "a": (["chain", "draw", "a_dim"], np.random.normal(size=(4, 100, 3))), + ...: "b": (["chain", "draw"], np.random.normal(size=(4, 100))), + ...: } + ...: coords = {"a_dim": ["x", "y", "z"]} ...: dataA = az.from_dict(data, coords=coords, dims={"a": ["a_dim"]}) ...: dataA From a255c68585f93f74708d67b992e999abe4097775 Mon Sep 17 00:00:00 2001 From: percygautam Date: Mon, 10 Feb 2020 23:53:41 +0530 Subject: [PATCH 7/8] minor --- arviz/data/inference_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 526cbc72c9..46e46c1065 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -280,7 +280,7 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): Examples -------- Use ``concat`` method to concatenate InferenceData objects. This will concatenates over - unique groups by default. We first create an InferenceData object: + unique groups by default. We first create an ``InferenceData`` object: .. ipython:: @@ -295,7 +295,7 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): ...: dataA We have created an ``InferenceData`` object with default group 'posterior'. Now, we will - create another InferenceData object: + create another ``InferenceData`` object: .. ipython:: From e0a9648628580a3aa02a8456bb67b1c567dddbdc Mon Sep 17 00:00:00 2001 From: percygautam Date: Tue, 11 Feb 2020 01:05:53 +0530 Subject: [PATCH 8/8] added example of concat over chain/draw --- arviz/data/inference_data.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 46e46c1065..68284d8953 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -309,6 +309,16 @@ def concat(*args, dim=None, copy=True, inplace=False, reset_dim=True): In [1]: az.concat(dataA, dataB) + Now, we will concatenate over chain (or draw). It requires identical groups and variables. + Here we are concatenating two identical ``InferenceData`` objects over dimension chain: + + .. ipython:: + + In [1]: az.concat(dataA, dataA, dim="chain") + + It will create an ``InferenceData`` with the original group 'posterior'. In similar way, + we can also concatenate over draws. + """ # pylint: disable=undefined-loop-variable, too-many-nested-blocks if len(args) == 0: