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

Add nicer exception message for mistake in coords argument #236

Merged
merged 5 commits into from
Sep 16, 2018

Conversation

canyon289
Copy link
Member

Fix for #213

If you guys like how this looks I'll change all the other plots to this pattern. Did a couple as an example

# Sample code
import arviz as az
data = az.load_arviz_data('non_centered_eight')
az.traceplot(data, var_names=('theta_t'), coords={'theta_t_dim_0': [0, 1, 100]})
az.traceplot(data, var_names=('theta_t'), coords={'theta_t_dim_0_t': [0, 1]})

Copy link
Member

@ColCarroll ColCarroll left a comment

Choose a reason for hiding this comment

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

This looks good to me.

As an aside, there's lots of reorganizing to do - most functions in utils, plot_utils and xarray_utils should probably be in a more general spot (data_utils?), but that shouldn't happen here.

return data.sel(**coords)

except ValueError as err:
raise ValueError("Verify coords keys. {}".format(err))
Copy link
Member

Choose a reason for hiding this comment

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

can you verify that the stack trace is still informative? I think the pattern is

except ValueError as err:
    raise ValueError("Verify coords keys") from err

I might also print out more information - like "make sure {list(coords.keys()} match with the data data_vars ({list(data.data_vars)})"

Copy link
Member Author

Choose a reason for hiding this comment

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

If you do from err the text from the original exception is lost. I was keeping it in because it was informative as to which coordinates were missing.

But I agree with your second statement which means the above point will be irrelevant! I'll make the changes and post another example.

except ValueError as err:
raise ValueError("Verify coords keys. {}".format(err))

except KeyError as err:
Copy link
Member

Choose a reason for hiding this comment

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

...are these right? A KeyError gets thrown for bad .values and a ValueError for bad keys?

Copy link
Member Author

@canyon289 canyon289 Sep 13, 2018

Choose a reason for hiding this comment

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

Maybe not. I'll verify. Thanks for double checking

Copy link
Member Author

Choose a reason for hiding this comment

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

So yes,
Having a wrong key in coords dictionary raises a ValueError in xarray, and having a value in the coords dictionary raises a KeyError.

I wrote tests but just double checked by hand as well

Ill make the exceptions more clear

@canyon289
Copy link
Member Author

There's a gist below of both xarray defined ValueError and KeyError, and also two Options at the bottom to consider for arviz

Let me know which one you guys like, (or any other suggestions to try) and I'll implement throughout the library.

https://gist.github.com/canyon289/cbd3744a36d8198c6d4ac796eae60e4b

@ahartikainen
Copy link
Contributor

Could we have suggestion where to go to see correct coord/dim format?

For new users these things can be complicated.

Our docs + xarray docs?

@canyon289
Copy link
Member Author

I agree it is complicated! There's two separate issues, one is the format, but the other is just trying to select something that doesn't exist.

Maybe we should have three exceptions?

  1. Format is invalid
  2. Format is valid but coord name doesn't exist
  3. Format is valid but dim doesn't exist

@ahartikainen
Copy link
Contributor

ahartikainen commented Sep 14, 2018

That does sound reasonable.

I think if we add any links, they should be on their own lines (or something users can click):

See.
https ://linktoxarrayprops.xyz/...

@canyon289
Copy link
Member Author

canyon289 commented Sep 15, 2018

Turns out there's four paths to three types of exceptions all shown in the gist.

I took the strategy of letting xarray raise the exception before handling it for two reasons

  1. Xarray's exceptions have useful information as to what might be wrong so its worth capturing
  2. I don't want arviz to raise exceptions in cases when xarray would not. If arviz handles the exception first we can't be certain of this.

https://gist.github.com/canyon289/51281affde8e8a8891d503e45dea0920

In regards to @ahartikainen's comment, I do agree we need an "xarray 101" type documentation. For this PR though would you guys be ok with merging this (once I update all plots and it looks ok) and I'll follow up with the documentation, and link in exception, in another PR?

@ColCarroll
Copy link
Member

ColCarroll commented Sep 15, 2018

Looks good to me - is this all the places dataset.sel(**coords) happens? Even if it isn't, feel free to merge and we can followup with the rest.

@ahartikainen
Copy link
Contributor

LGTM

@canyon289
Copy link
Member Author

Updated to all the places with **sel. I'll merge if tests pasts. Thank you

@canyon289 canyon289 changed the title [WIP] Add nicer exception message for mistake in coords argument Add nicer exception message for mistake in coords argument Sep 16, 2018
@canyon289 canyon289 merged commit f663e62 into arviz-devs:master Sep 16, 2018
canyon289 added a commit that referenced this pull request Sep 16, 2018
@canyon289 canyon289 deleted the nicer_exception branch September 16, 2018 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants