-
-
Notifications
You must be signed in to change notification settings - Fork 427
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
Allow xarray.Dataarray input to plots. #1120
Conversation
arviz/data/converters.py
Outdated
@@ -97,6 +99,10 @@ def convert_to_inference_data(obj, *, group="posterior", coords=None, dims=None, | |||
# Cases that convert to xarray | |||
if isinstance(obj, xr.Dataset): | |||
dataset = obj | |||
elif isinstance(obj, xr.DataArray): | |||
if obj.name is None: | |||
obj.name = 'plot' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the name is None, we should set it to x
to follow current convention with numpy arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, true. I will change it.
Do I need to write testcases for this? If yes, please let me know how can I add testcases and what should that test case verify? |
As this is a new feature it would be great to add some tests for it. Maybe something like |
Codecov Report
@@ Coverage Diff @@
## master #1120 +/- ##
=======================================
Coverage 92.67% 92.68%
=======================================
Files 93 93
Lines 9069 9073 +4
=======================================
+ Hits 8405 8409 +4
Misses 664 664
Continue to review full report at Codecov.
|
arviz/tests/base_tests/test_data.py
Outdated
class TestDataArrayToDataset: | ||
def test_1d_dataset(self): | ||
size = 100 | ||
dataset = convert_to_dataset(xr.DataArray(np.random.randn(1, size), dims=('chain', 'draw'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could set the name of the input dataarray in this test to make sure the name is not overwritten during conversion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I forgot to test the name. I'll make suitable changes
arviz/tests/base_tests/test_data.py
Outdated
assert inference_data.prior.chain.shape == shape[:1] | ||
assert inference_data.prior.draw.shape == shape[1:2] | ||
assert inference_data.prior[var_name].shape == shape | ||
assert repr(inference_data).startswith("Inference data with groups") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this last assert should be removed, it will only fail if the inference data object has not been created, and in this case all asserts before this one would have already failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well observed. I'll change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please merge latest changes in master into the PR? There were some issues checking code style with black.
I think after these last changes it will be ready to merge.
arviz/tests/base_tests/test_data.py
Outdated
dataset = convert_to_dataset(xr.DataArray(np.random.randn(1, size), | ||
name="plot", dims=('chain', 'draw'))) | ||
assert len(dataset.data_vars) == 1 | ||
assert dataset.variables.get("plot") is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about assert "plot" in dataset.data_vars
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will make changes.
But, where can we see code style issues by black? Because I did not see any suggestion in automation check that runs after each commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can run black --check
I believe and it will run black checks. The CI will run black on every commit that is pushed so you'll see a failure on azure pipelines, but its easier just to check locally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nevermind, what I said is how things should ideally run. I saw oriols comment below that CI is not failing on black errors right now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it can easily run locally. I will run all checks before pushing any changes.
dims=('chain', 'draw', 'dim_0', 'dim_1', 'dim_2')), group="prior") | ||
assert hasattr(inference_data, "prior") | ||
assert len(inference_data.prior.data_vars) == 1 | ||
var_name = list(inference_data.prior.data_vars)[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick, move this up above the asserts so all the asserts are together. That way when reading the tests we can see all the logic in one place and all the asserts in another. Same for test above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah sure.
You also have to make sure the code is formatted according to black, see contributing guide to check. Code is not formatted with black currently, however, due to #1124, Azure build is not failing even though it should: see https://dev.azure.com/ArviZ/ArviZ/_build/results?buildId=2185&view=logs&j=e6a7683b-6131-58a8-ef68-5f3a9120796c&t=49079c21-ba2f-59ec-b1ba-71c17ead5efa&l=14 |
Ok, now I got it. I will make sure code is formatted according to black |
Thanks, I think it is ready to merge now |
Description
This PR is not related to any issue but, as Discussed in #1104 I have made changes to converter.py to allow dataarray input
Consider following code snippet-
import arviz as az
import numpy as np
import xarray as xr
xarr=xr.DataArray(np.random.randn(6,2),name="nm",dims=('chains', 'draw'),coords={'draw':[1,2]})
az.plot_posterior(xarr,var_names="nm")
`
Output without changes-
Traceback (most recent call last):
After changes-

Checklist