Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Is it possible to have a different rope for every variable dimension #1382

Closed
sperezconesa opened this issue Sep 15, 2020 · 6 comments · Fixed by #1390
Closed

Is it possible to have a different rope for every variable dimension #1382

sperezconesa opened this issue Sep 15, 2020 · 6 comments · Fixed by #1390
Assignees

Comments

@sperezconesa
Copy link

Hello,

I have been looking at the forest_plot documentation and examples and it seems pretty clear to me that you can have a different rope for each variable. But say I have a different rope for each dimension of a variable say mu 0, mu 1... which all correspond to the same:

mu_o = pm.Normal('mu_o', mu=data_fo.mean(), sd=sd_prior, dims='resid')

Thank you all very much and keep up the good work!
Sergio

@OriolAbril
Copy link
Member

There may be a bug in the rope code. There are examples of the usage you ask for in the documentation of plot_posterior, and I have been able to use it with:

rope={"mu": [{"mu_dim": 2, "rope": (-1, 1)}, {"mu_dim": 4, "rope": (0, 3)}]}

image

but it does not work for plot_forest cc @aloctavodia

@sperezconesa
Copy link
Author

Following on the other issue #1381 . I have created this forestplot:

az.plot_forest(
    (posterior.sel(state="o"), posterior.sel(state="fo")),
    var_names="mu",
    combined=True, 
    colors=["C0", "C1"], 
    model_names=["o", "fo"],
    textsize=9,
    #rope=rope
)

If I add a rope like the one you suggested:

rope={"mu": [{"mu_dim": 'A42', "rope": (-1, 1)}, {"mu_dim": 'T33', "rope": (0, 3)}]}

I get this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-124-7b15dd7be9eb> in <module>
----> 1 az.plot_forest(
      2     (posterior.sel(state="o"), posterior.sel(state="fo")),
      3     var_names="mu",
      4     combined=True,
      5     colors=["C0", "C1"],

~/data_partition/bin2/anaconda3/envs/nmr_assign_state/lib/python3.8/site-packages/arviz/plots/forestplot.py in plot_forest(data, kind, model_names, var_names, filter_vars, transform, coords, combined, hdi_prob, rope, quartiles, ess, r_hat, colors, textsize, linewidth, markersize, ridgeplot_alpha, ridgeplot_overlap, ridgeplot_kind, ridgeplot_quantiles, figsize, ax, backend, backend_config, backend_kwargs, show, credible_interval)
    226     # TODO: Add backend kwargs
    227     plot = get_plotting_function("plot_forest", "forestplot", backend)
--> 228     axes = plot(**plot_forest_kwargs)
    229     return axes

~/data_partition/bin2/anaconda3/envs/nmr_assign_state/lib/python3.8/site-packages/arviz/plots/backends/matplotlib/forestplot.py in plot_forest(ax, datasets, var_names, model_names, combined, colors, figsize, width_ratios, linewidth, markersize, kind, ncols, hdi_prob, quartiles, rope, ridgeplot_overlap, ridgeplot_alpha, ridgeplot_kind, ridgeplot_quantiles, textsize, ess, r_hat, backend_kwargs, show)
     91     axes = np.atleast_1d(axes)
     92     if kind == "forestplot":
---> 93         plot_handler.forestplot(
     94             hdi_prob, quartiles, xt_labelsize, titlesize, linewidth, markersize, axes[0], rope,
     95         )

~/data_partition/bin2/anaconda3/envs/nmr_assign_state/lib/python3.8/site-packages/arviz/plots/backends/matplotlib/forestplot.py in forestplot(self, hdi_prob, quartiles, xt_labelsize, titlesize, linewidth, markersize, ax, rope)
    325             for y, rope_var, values, color in plotter.treeplot(qlist, hdi_prob):
    326                 if isinstance(rope, dict):
--> 327                     self.display_multiple_ropes(rope, ax, y, linewidth, rope_var)
    328 
    329                 mid = len(values) // 2

~/data_partition/bin2/anaconda3/envs/nmr_assign_state/lib/python3.8/site-packages/arviz/plots/backends/matplotlib/forestplot.py in display_multiple_ropes(self, rope, ax, y, linewidth, rope_var)
    221     def display_multiple_ropes(self, rope, ax, y, linewidth, rope_var):
    222         """Display ROPE when more than one interval is provided."""
--> 223         vals = dict(rope[rope_var][0])["rope"]
    224         ax.plot(
    225             vals,

KeyError: 'mu T112'

Really appreciate your help

@OriolAbril
Copy link
Member

I am quite sure this is a bug. Given that plot_posterior accepts the rope dict, we should see where they differ and fix the forest plot version so it works and behaves like the one in plot_posterior.

It may also be a good idea once this is done to add a link in the description of rope argument in forest plot to plot_posterior to help understand the rope scalar/list/dict possibilities

@sperezconesa
Copy link
Author

Yes, the documentation is a bit confusing.

What would you like me to do? Can you reproduce this unexpected behaviour in a simple test-case?

@OriolAbril
Copy link
Member

I think the first step would be creating a minimal example to reproduce the bug. If my intuition is correct, using the same data and rope argument as in plot_posterior docs should serve this goal. This example in particular looks like the one that will show this issue:

rope = {'mu': [{'rope': (-2, 2)}], 'theta': [{'school': 'Choate', 'rope': (2, 4)}]}
az.plot_posterior(data, var_names=['mu', 'theta'], rope=rope)

The next step should probably be comparing the source codes of plot_posterior and plot_forest, I don't know why but instead of having the same behaviour when interpreting the rope argument, one works correctly and the other crashes. Hopefully, fixing the differences between the two will solve the issue as plot_posterior seems to work correctly.

@sperezconesa
Copy link
Author

I am affraid I don't have much knowledge to work on this... If there is anything I can help you with let me know.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants