Skip to content

.from_dict() messes up chain/draw dimensions when chains have uneven lengths #2094

Description

@michaelosthege

Describe the bug
When the chains fed to .from_dict() have different lengths, the chain and draw dimension collapse to chain: 1, draw: nchains.

To Reproduce

import arviz
import numpy as np
array = np.array

kwargs = {
    'warmup_posterior': {
        'A': [
            array([0, 1, 2, 3, 4], dtype=np.uint16),
            array([0, 1, 2, 3, 4], dtype=np.uint16)
        ]
    },
    'warmup_sample_stats': {
        'tune': [
            array([ True,  True,  True,  True,  True]),
            array([ True,  True,  True,  True,  True])
        ]
    },
    'posterior': {
        'A': [
            array([ 5,  6,  7,  8,  9, 10, 11], dtype=np.uint16),
            array([ 5,  6,  7,  8,  9, 10], dtype=np.uint16)
        ]
    },
    'sample_stats': {
        'tune': [
            array([False, False, False, False, False, False, False]),
            array([False, False, False, False, False, False])
        ]
    },
    'coords': {},
    'dims': {
        'A': []
    },
    'attrs': {},
    'constant_data': {},
    'observed_data': {},
    'save_warmup': True
}
idata = arviz.from_dict(**kwargs)
print(idata.warmup_posterior.dims) # {'chain': 2, 'draw': 5}
print(idata.posterior.dims)        # {'chain': 1, 'draw': 2} but shoud be {'chain': 2, 'draw': 7}

Expected behavior
I expected the "missing" entries of the shorter chain to be filled-up by np.nan such that both chains have the same length (equal to the length of the longer chain).

Additional context
Looking at the idata.posterior, the "A" variable is stored as

array(
    [
        [array([ 5,  6,  7,  8,  9, 10, 11], dtype=uint16),
         array([ 5,  6,  7,  8,  9, 10], dtype=uint16)]
    ],
    dtype=object
)

which is an object-array containing one "chain" that has two "draws" each of which is the vector of draws of the actual chains.

This bug was the underlying cause of pymc-devs/mcbackend#44

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions