Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix to flatten_list_parameters #327

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions src/dynode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,14 +1121,14 @@ def drop_sample_chains(samples: dict, dropped_chain_vals: list):


def flatten_list_parameters(
samples: dict[str, np.ndarray],
samples: dict[str, np.ndarray | Array],
) -> dict[str, np.ndarray]:
"""
Flatten plated parameters into separate keys in the samples dictionary.

Parameters
----------
samples : dict[str, np.ndarray]
samples : dict[str, np.ndarray | Array]
Dictionary with parameter names as keys and sample
arrays as values. Arrays may have shape MxNxP for P independent draws.

Expand All @@ -1144,7 +1144,7 @@ def flatten_list_parameters(
"""
return_dict = {}
for key, value in samples.items():
if isinstance(value, np.ndarray) and value.ndim > 2:
if isinstance(value, (np.ndarray, Array)) and value.ndim > 2:
num_dims = value.ndim - 2
indices = (
np.indices(value.shape[-num_dims:]).reshape(num_dims, -1).T
Expand Down
55 changes: 55 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import IntEnum

import jax.numpy as jnp
import numpy as np
import numpyro.distributions as dist

from dynode import utils
Expand Down Expand Up @@ -426,3 +427,57 @@ def test_get_timeline_from_solution_with_command_compartment_slice():
assert jnp.all(
timeline == 16
) # Each element in sol is 1, summed over 4*1*1*4 = 16


def test_flatten_list_params_numpy():
# simulate 4 chains and 20 samples each with 4 plated parameters
testing = {"test": np.ones((4, 20, 5))}
flattened = utils.flatten_list_parameters(testing)
assert "test" not in flattened.keys()
for suffix in range(5):
key = "test_%s" % str(suffix)
assert (
key in flattened.keys()
), "flatten_list_parameters not naming split params correctly."
assert flattened[key].shape == (
4,
20,
), "flatten_list_parameters breaking up wrong axis"


def test_flatten_list_params_jax_numpy():
# simulate 4 chains and 20 samples each with 4 plated parameters
# this time with jax numpy instead of numpy
testing = {"test": jnp.ones((4, 20, 5))}
flattened = utils.flatten_list_parameters(testing)
assert "test" not in flattened.keys()
for suffix in range(5):
key = "test_%s" % str(suffix)
assert (
key in flattened.keys()
), "flatten_list_parameters not naming split params correctly."
assert flattened[key].shape == (
4,
20,
), "flatten_list_parameters breaking up wrong axis"


def test_flatten_list_params_multi_dim():
# simulate 4 chains and 20 samples each with 10 plated parameters
# this time with jax numpy instead of numpy
testing = {"test": jnp.ones((4, 20, 5, 2))}
flattened = utils.flatten_list_parameters(testing)
assert "test" not in flattened.keys()
for suffix_first_dim in range(5):
for suffix_second_dim in range(2):
key = "test_%s_%s" % (
str(suffix_first_dim),
str(suffix_second_dim),
)
assert (
key in flattened.keys()
), "flatten_list_parameters not naming split params correctly."
assert flattened[key].shape == (
4,
20,
), "flatten_list_parameters breaking up wrong axis when passed >3"
Loading