question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using var_names argument in Arviz plots raise 'RVIdentifier' object has no attribute 'startswith'

See original GitHub issue

Issue Description

var_names is a mechanism in arviz for selectively plotting a subset of posterior samples. It is important for avoiding excessive plots when a high dimensional posterior is targeted. However, combining this argument with an InferenceData obtained from beanmachine results in an exception.

Steps to Reproduce

import beanmachine.ppl as bm
import torch.distributions as dist
import arviz as az
foo = bm.random_variable(lambda: dist.MultivariateNormal(torch.zeros(4), torch.eye(4)))
xx = bm.SingleSiteRandomWalk().infer(queries=[foo()], observations={}, num_samples=100, num_chains=2)
az.plot_trace(xx.to_inference_data(), var_names=['xx()'])

will raise

'RVIdentifier' object has no attribute 'startswith'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-37-cb5a8769793b> in <module>
      3 idata = samples.to_inference_data()
      4 
----> 5 az.plot_trace(
      6     idata, var_names=['prevalence()']
      7 )
/mnt/xarfuse/uid-25957/a9b3979e-seed-nspid4026533620_cgpid78576781-ns-4026533617/arviz/plots/traceplot.py in plot_trace(data, var_names, filter_vars, transform, coords, divergences, kind, figsize, rug, lines, circ_var_names, circ_var_units, compact, compact_prop, combined, chain_prop, legend, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, rank_kwargs, axes, backend, backend_config, backend_kwargs, show)
    182         coords_data = transform(coords_data)
    183 
--> 184     var_names = _var_names(var_names, coords_data, filter_vars)
    185 
    186     if compact:
/mnt/xarfuse/uid-25957/a9b3979e-seed-nspid4026533620_cgpid78576781-ns-4026533617/arviz/utils.py in _var_names(var_names, data, filter_vars)
     46             all_vars = list(data.data_vars)
     47 
---> 48         all_vars_tilde = [var for var in all_vars if var.startswith("~")]
     49         if all_vars_tilde:
     50             warnings.warn(
/mnt/xarfuse/uid-25957/a9b3979e-seed-nspid4026533620_cgpid78576781-ns-4026533617/arviz/utils.py in <listcomp>(.0)
     46             all_vars = list(data.data_vars)
     47 
---> 48         all_vars_tilde = [var for var in all_vars if var.startswith("~")]
     49         if all_vars_tilde:
     50             warnings.warn(
AttributeError: 'RVIdentifier' object has no attribute 'startswith'

Expected Behavior

A trace plot containing only the xx() RV

Additional Context

stringifying RVIdentifiers sidesteps this issue, eg

idata = az.convert_to_inference_data({
    str(k):samples[k] for k in samples
})

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
feynmanliangcommented, Aug 26, 2022

Apologies, I didn’t mean for the lambda name inference / rename() to be a part of this issue.

With arviz 0.12.1, I just tried

import beanmachine.ppl as bm
import torch
import torch.distributions as dist
import arviz as az
@bm.random_variable
def xx():
  return dist.MultivariateNormal(torch.zeros(4), torch.eye(4))
mcs = bm.SingleSiteRandomWalk().infer(queries=[xx()], observations={}, num_samples=100, num_chains=2)
az.plot_trace(mcs.to_inference_data(), var_names=[xx()])

and it succeeds, but fails on 0.11.2. So yes, thank you for helping me figure out this is an arviz version issue!

0reactions
feynmanliangcommented, Oct 11, 2022

Yeah, I agree with you. Dunno why I couldn’t think of calling it in a loop / kwargs splatting…

On Tue, Oct 11, 2022, 02:09 Rob Zinkov @.***> wrote:

I think programmatic renaming is still ok even with a large number of variables. If that seems not too elegant, I think there is a design question over making the default str nicer.

On Tue, 11 Oct 2022, 10:09 Feynman Liang, @.***> wrote:

Will this be sufficiently ergonomic in models with large numbers of random variables eg queries=[x(i) for i in range(1000)]?

On Thu, Aug 11, 2022, 10:07 Rob Zinkov @.***> wrote:

I don’t get this error in the latest versions of arviz, but I’m getting another error:

KeyError: ‘var names: “['xx()'] are not present” in dataset’

I’m not sure the semantics of var_names support this. The best way to implement this is using xarray’s rename

import beanmachine.ppl as bmimport torch.distributions as distimport arviz as azfoo = bm.random_variable(lambda: dist.MultivariateNormal(torch.zeros(4), torch.eye(4)))xx = bm.SingleSiteRandomWalk().infer(queries=[foo()], observations={}, num_samples=100, num_chains=2)xx = xx.to_inference_data().rename({foo(): “xx”})az.plot_trace(xx, var_names=“xx”)

— Reply to this email directly, view it on GitHub <

https://github.com/facebookresearch/beanmachine/issues/1564#issuecomment-1212258940

, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AAHRW5OQ74524K3A4YASFVDVYUXNNANCNFSM54OEQU7Q

. You are receiving this because you were mentioned.Message ID: @.***>

— Reply to this email directly, view it on GitHub < https://github.com/facebookresearch/beanmachine/issues/1564#issuecomment-1274273657 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AAACCUMOPLCWYHE62N5BJFDWCUOD7ANCNFSM54OEQU7Q

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/facebookresearch/beanmachine/issues/1564#issuecomment-1274365930, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHRW5LQKTYZFNMJKVAWVD3WCUVFFANCNFSM54OEQU7Q . You are receiving this because you were mentioned.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arviz combined plot - Questions - PyMC Discourse
Is there a way to plot the same variables on a single violin plot? ... AttributeError: 'DataArray' object has no attribute 'startswith'.
Read more >
'DataFrame' object has no attribute 'startswith' error
But every single time I run it using a different module to open the file, it always has the error that : '...
Read more >
arviz.plot_posterior — ArviZ 0.14.0 documentation
var_names: list of variable names. Variables to be plotted, two variables are required. Prefix the variables with ~ when you want to exclude...
Read more >
Plotting with Bokeh — ArviZ dev documentation
In this guide, advanced plotting with Bokeh will be covered. ... As the options available depends on the backend, this parameter is not...
Read more >
`File` object has no attribute 'startswith' · Issue #424 - GitHub
Hi guys. Here's a little issue with meson → meson.build: # vim: set ft=python project('Ricin', 'vala', 'c', # compiling vala requires a C...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found