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

xarray usage #1

Merged
merged 1 commit into from
Mar 24, 2021
Merged
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
18 changes: 11 additions & 7 deletions src/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ def plot_marginals(
true_values: pd.Series = None,
):
"""Plot marginal ability parameters against true values."""
qs = infd.posterior[var_name].to_series().unstack().quantile([0.1, 0.9]).T
qs["truth"] = true_values
qs["truth_in_interval"] = (qs["truth"] < qs[0.9]) & (qs["truth"] > qs[0.1])
qs = qs.sort_values(0.9)
y = np.linspace(*ax.get_ylim(), len(qs))
qs = infd.posterior[[var_name]].quantile([0.1, 0.9], dim=("chain", "draw"))
qs["truth"] = (("skater_name",), true_values)
qs["truth_in_interval"] = (
(qs["truth"] < qs[var_name].sel(quantile=0.9)) &
(qs["truth"] > qs[var_name].sel(quantile=0.1))
)
qs = qs.sortby(qs[var_name].sel(quantile=0.9))
y = np.linspace(*ax.get_ylim(), qs.dims["skater_name"])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably the piece that is more xarray specific and can be confusing. qs.dims["skater_name"] returns an the length of the dimension as integer. The key is in the difference between dimension and coordinate. Here both things have the same name, like in all ArviZ generated datasets but this is not a requirement in xarray. qs.skater_name or qs.coords.skater_name will return a DataArray with all the skater name values.

ax.set_yticks(y)
ax.set_yticklabels(qs.index)
ax.set_yticklabels(qs.skater_name)
ax.hlines(
y, qs[0.1], qs[0.9], color="tab:blue", label="90% marginal interval"
y, qs[var_name].sel(quantile=0.1), qs.sel(quantile=0.9),
color="tab:blue", label="90% marginal interval"
)
if true_values is not None:
ax.scatter(
Expand Down