Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pymc/backends/arviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,13 @@ def dataset_to_point_list(
for vn in var_names:
if not isinstance(vn, str):
raise ValueError(f"Variable names must be str, but dataset key {vn} is a {type(vn)}.")

num_sample_dims = len(sample_dims)
stacked_dims = {dim_name: ds[var_names[0]][dim_name] for dim_name in sample_dims}
transposed_dict = {vn: da.transpose(*sample_dims, ...) for vn, da in ds.items()}
stacked_size = np.prod(transposed_dict[var_names[0]].shape[:num_sample_dims], dtype=int)
Copy link
Member

Choose a reason for hiding this comment

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

should be the same as

stacked_size = np.prod([ds.sizes[dim] for dim in sample_dims], dtype=int)

feel free to choose whichever you prefer

Copy link
Member Author

Choose a reason for hiding this comment

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

ds may be a dict

stacked_dict = {
vn: da.values.reshape((-1, *da.shape[num_sample_dims:]))
vn: da.values.reshape((stacked_size, *da.shape[num_sample_dims:]))
for vn, da in transposed_dict.items()
}
points = [
Expand Down
11 changes: 11 additions & 0 deletions tests/backends/test_arviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,14 @@ def test_dataset_to_point_list_str_key(self):
ds[3] = xarray.DataArray([1, 2, 3])
with pytest.raises(ValueError, match="must be str"):
dataset_to_point_list(ds, sample_dims=["chain", "draw"])

def test_zero_size(self):
ds = xarray.Dataset()
ds["x"] = xarray.DataArray(
np.zeros((4, 10, 0, 5)), dims=("chain", "draw", "dim_0", "dim_5")
)
pl, _ = dataset_to_point_list(ds, sample_dims=("chain", "draw"))
assert len(pl) == 40
assert tuple(pl[0]) == ("x",)
assert pl[0]["x"].shape == (0, 5)
assert pl[0]["x"].dtype == np.float64