Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Convention for output of tuples

Open
#708 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
numpy, python
Domain
api, data

Research direction

Start with the tuple_out.stan example and the test_tuple_out test shown in the issue, then inspect fit.stan_variable("c") and fit.c behavior. Compare array-of-tuples and tuple-of-arrays layouts, and define the resulting sample indexing convention before updating the relevant tests and documentation.

Written by the indexing model from the issue text.

Description

In the develop branch, tuples generated by Stan programs are one-dimensional arrays of tuples. This can make it difficult to access elements of tuples. For example, consider the following test.

def test_tuple_out() -> None:
    stan = os.path.join(DATAFILES_PATH, 'tuple_out.stan')
    model = CmdStanModel(stan_file=stan)
    a = np.random.normal(0, 1, (5, 5))
    b = np.random.normal(0, 1, 3)
    fit = model.sample({"a": a, "b": b}, fixed_param=True, chains=1,
                       iter_sampling=20, iter_warmup=1, sig_figs=18)
    np.testing.assert_allclose(a, fit.stan_variable("c")[0][0])
    np.testing.assert_allclose(b, fit.stan_variable("c")[0][1])
// tuple_out.stan
data {
    matrix [5, 5] a;
    vector [3] b;
}

generated quantities {
    tuple(matrix[5, 5], vector[3]) c;
    c.1 = a;
    c.2 = b;
}

Then accessing c.1 samples is only possible through list comprehension (there may be some fancy indexing I'm not familiar with).

>>> fit.c.shape
(20,)
>>> fit.c[:, 0]
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Would it make sense to return a tuple of arrays rather than an array of tuples? This would allow accessing samples more easily, e.g., in the above example we'd get

>>> fit.c[0].shape
(20, 5, 5)

This would however go against the convention that the first index refers to samples.

Dominant language
Python
Stars
198
Forks
81
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stan-dev/cmdstanpy

All issues in stan-dev/cmdstanpy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.