Skip to content

supported variable mapping in stan convertors#163

Open
Chirag3841 wants to merge 16 commits into
arviz-devs:mainfrom
Chirag3841:pairs
Open

supported variable mapping in stan convertors#163
Chirag3841 wants to merge 16 commits into
arviz-devs:mainfrom
Chirag3841:pairs

Conversation

@Chirag3841

@Chirag3841 Chirag3841 commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

#59
This PR updates the Stan converters to support mapping predictive and log-likelihood variables to the corresponding observed variable name.

Changes

  • Updated "posterior_predictive_to_xarray" in both "cmdstanpy" and "pystan" converters and "log_likelihood_to_xarray" in"pystan" converters to support variable name mapping.
  • Allows mappings such as "{"y": "y_hat"}" and "{"y": "log_lik"}" so that predictive and log-likelihood variables align with the observed variable name.
  • Added tests covering mapping behavior for stan converters.

All tests and pre-commit checks pass locally.

@read-the-docs-community

read-the-docs-community Bot commented Mar 5, 2026

Copy link
Copy Markdown

Documentation build overview

📚 arviz-base | 🛠️ Build #32124050 | 📁 Comparing 6fd5548 against latest (0dc5042)

  🔍 Preview build  

Show files changed (24 files in total): 📝 21 modified | ➕ 0 added | ➖ 3 deleted
File Status
genindex.html 📝 modified
api/index.html 📝 modified
how_to/ConversionGuideNumPyro.html 📝 modified
_modules/arviz_base/base.html 📝 modified
_modules/arviz_base/citations.html 📝 modified
_modules/arviz_base/converters.html 📝 modified
_modules/arviz_base/io_cmdstanpy.html 📝 modified
_modules/arviz_base/io_numpyro.html 📝 modified
_modules/arviz_base/rcparams.html 📝 modified
_modules/arviz_base/reorg.html 📝 modified
api/generated/arviz_base.MCMCAdapter.html ➖ deleted
api/generated/arviz_base.NumPyroInferenceAdapter.html ➖ deleted
api/generated/arviz_base.SVIAdapter.html ➖ deleted
api/generated/arviz_base.dataset_to_dataarray.html 📝 modified
api/generated/arviz_base.dataset_to_dataframe.html 📝 modified
api/generated/arviz_base.dict_to_dataset.html 📝 modified
api/generated/arviz_base.explode_dataset_dims.html 📝 modified
api/generated/arviz_base.extract.html 📝 modified
api/generated/arviz_base.from_numpyro.html 📝 modified
api/generated/arviz_base.labels.BaseLabeller.html 📝 modified
api/generated/arviz_base.make_attrs.html 📝 modified
api/generated/arviz_base.ndarray_to_dataarray.html 📝 modified
api/generated/arviz_base.references_to_dataset.html 📝 modified
api/generated/arviz_base.xarray_sel_iter.html 📝 modified

Comment thread src/arviz_base/io_cmdstanpy.py Outdated
def posterior_predictive_to_xarray(self):
"""Convert posterior_predictive samples to xarray."""
data, data_warmup = self.predictive_to_xarray(self.posterior_predictive, self.posterior)
posterior_predictive = self.posterior_predictive

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I mentioned observed_data, log_likelihood and posterior_predictive in my comment, but then also mentioned to look at behaviour differences between log_likelihood and posterior_predictive or predictions. Renames might be needed for log_likelihood, posterior_predictive, predictions and/or prior_predictive.

Tangentially, the converter has this predictive_to_xarray function that is called in both posterior_predictive and prior_predictive because the logic for both is extremely similar. We are far from perfect (in fact, predictive_to_xarray could also be used for predictions and it is not, but we try to keep things simple and maintainable. If after some changes you end up with a function that is only ever called once by another one it is a signal for asking about it and maybe re-evaluating the need for that function (it might still abstract some logic away to keep the second function easier to follow).

Comment thread src/arviz_base/io_cmdstanpy.py Outdated
Comment on lines +179 to +203
if isinstance(posterior_predictive, str):
posterior_predictive = [posterior_predictive]

if isinstance(posterior_predictive, list | tuple):
posterior_predictive = {name: name for name in posterior_predictive}

data, data_warmup = _unpack_fit(
self.posterior,
list(posterior_predictive.values()),
self.save_warmup,
self.dtypes,
)

data = {
obs_var_name: data[var_name]
for obs_var_name, var_name in posterior_predictive.items()
if var_name in data
}

if data_warmup:
data_warmup = {
obs_var_name: data_warmup[var_name]
for obs_var_name, var_name in posterior_predictive.items()
if var_name in data_warmup
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In a similar vein to the previous comment. If log_likelihood_to_xarray is already doing the renames we want why develop a whole new logic to the renaming? It means the next person to work on this file will need to understand two different sets of code that are actually doing the same, making their work harder (also that next person could be future you or future me).

Overall, in this particular case, my recommendation is to use the same logic as in log_likelihood but adding it within predictive_to_xarray which will then be called from posterior_predictive, prior_predictive (both already do) and predictions (doesn't yet).

Comment thread src/arviz_base/io_pystan.py
Comment thread src/arviz_base/io_pystan.py Outdated
for obs_var_name, log_like_name in log_likelihood.items()
if log_like_name in log_likelihood_draws_warmup
}
if (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the idea of renaming automatically whenever there is only one observation and log_likelihood is a string. That being said, I think this is something that needs a bit more thinking. If we do it for the log likelihood why not do it also for the posterior predictive? And if we do it in multiple places, then it is probably better to define the obs_name in the __init__ as a special attribute then in the first check for log likelihood being a string, updating log likelihood to either a list or a dict depending on this obs_name attribute being present (and similar idea for posterior_predictive...).

Comment thread src/arviz_base/io_pystan.py Outdated
posterior,
model=posterior_model,
variables=posterior_predictive,
variables=list(posterior_predictive.values()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

given we'll want these renames to happen in several groups and we are basically using posterior_predictive, data and data_warmup I think it will be helpful to allow variables to be a dictionary and if that is the case handle the renaming directlly in get_draws

Comment thread tests/test_stan_convertors.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

test should be added yes but they should be added along the existing ones https://github.com/arviz-devs/arviz-base/tree/main/external_tests.

Also, in none of these 4 tests the name or docstring has anything to do with the actual body. The name makes it sound it is testing these new features but that is not the case at all. Please review your own changes and ask questions, you can review your own PR too to ask questions about specific parts of your proposed changes.

@OriolAbril OriolAbril left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in the previous round of review I mentioned the tests that were being added were not useful but I also said tests should be added and pointed out where they should go into. I am struggling to follow why there are now no tests at all nor any question related to that

Comment thread src/arviz_base/io_cmdstanpy.py Outdated
def posterior_predictive_to_xarray(self):
"""Convert posterior_predictive samples to xarray."""
data, data_warmup = self.predictive_to_xarray(self.posterior_predictive, self.posterior)
data, data_warmup = self.predictive_to_xarray(self.prior_predictive, self.prior)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it seems very dumb but if you review the diff of your own PR you'll easily things like this yourself

Comment thread src/arviz_base/io_cmdstanpy.py
Comment thread src/arviz_base/io_pystan.py
@Chirag3841

Chirag3841 commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

in the previous round of review I mentioned the tests that were being added were not useful but I also said tests should be added and pointed out where they should go into. I am struggling to follow why there are now no tests at all nor any question related to that

Thanks for the clarification .
Sorry but I was not able to do any changes in this PR from past week that's why there is no changes in it .
I assured you I will address all the required issues in this week.

@codecov-commenter

codecov-commenter commented Mar 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.51%. Comparing base (4477130) to head (6fd5548).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/arviz_base/io_pystan.py 0.00% 30 Missing ⚠️
src/arviz_base/io_cmdstanpy.py 0.00% 7 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (4477130) and HEAD (6fd5548). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (4477130) HEAD (6fd5548)
5 3
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #163       +/-   ##
===========================================
- Coverage   72.30%   50.51%   -21.80%     
===========================================
  Files          19       20        +1     
  Lines        1914     2057      +143     
===========================================
- Hits         1384     1039      -345     
- Misses        530     1018      +488     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Chirag3841

Copy link
Copy Markdown
Contributor Author

@OriolAbril I have added a test file for both converters with required changes(not fully furnished yet).

Comment thread external_tests/test_cmdstanpy.py Outdated
Comment thread external_tests/test_cmdstanpy.py Outdated
Comment thread external_tests/test_cmdstanpy.py Outdated
@Chirag3841

Copy link
Copy Markdown
Contributor Author

@OriolAbril Any update ?

Comment thread src/arviz_base/io_cmdstanpy.py Outdated
Comment thread src/arviz_base/io_pystan.py
Comment thread external_tests/test_cmdstanpy.py Outdated
Comment thread external_tests/test_pystan.py Outdated
@Chirag3841

Chirag3841 commented Apr 5, 2026

Copy link
Copy Markdown
Contributor Author

@OriolAbril Changes addressed as per the requirement .
Let me know any changes are required or not .Waiting for your response.

@Chirag3841

Copy link
Copy Markdown
Contributor Author

@OriolAbril Sorry for tagging you again but is there any update regarding this PR ?

@Chirag3841

Copy link
Copy Markdown
Contributor Author

@OriolAbril Any update regarding this PR ?
Waiting for your response .

@Chirag3841

Copy link
Copy Markdown
Contributor Author

Any update regarding this PR?

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.

3 participants