bebi103.utils.dict_to_array
- bebi103.utils.dict_to_array(input_dict: dict, base_name: str, one_indexed: bool = True, dtype=None, squeeze: bool = False, squeeze_to_scalar: bool = True) ndarray[tuple[Any, ...], dtype[_ScalarT]]
Given a dictionary input_dict with keys ‘base_name[1]’, ‘base_name[2]’, etc., for a one-dimensional array, or ‘base_name[1,1]’, ‘base_name[1,2]’, etc., for a multidimensional array, where each value is a scalar, a 1D array, or a 2D array, as would be returned in a dictionary from MCMC sampling, convert to a NumPy array. If there are n indices in the keys, these become the last n indices of the outputted array.
- Parameters:
input_dict (dict) –
Dictionary with entries like ‘base_name[1]’, ‘base_name[2]’, base_name[1,1], etc. Whitespace around the indices is ignored, so base_name[1, 1] is also accepted. Each entry is a 1D array or 2D array, and all entries must have the same shape. As an example, an input dictionary could be:
{'a[1]': np.array([[1, 2, 3, 4]]), 'a[2]': np.array([[3, 5, 6, 7]]), 'a[3]': np.array([[9, 10, 11, 12]])}
base_name (str) – Name of array represented in the dictionary. In the above example, base_name is ‘a’.
one_indexed (bool, default True) – True if indexing of the array in the dictionary starts at 1, as is the case with Stan, and False if indexing starts at 0.
dtype (data type or None) – If not None, outputted array is returned as data type dtype.
squeeze (bool, default False) – If True, dimensions of length 1 are removed from the arrays in the output dictionary using np.squeeze().
squeeze_to_scalar (bool, default True) – If all dimensions are of length 1, np.squeeze() gives a 0D array, not a scalar. If squeeze_to_scalar is True, then 0D arrays are converted to scalars.
- Returns:
output – Numpy array whose leading indices are those of the samples (the shape of the entries of input_dict) and whose last n indices are the n indices of the array in the keys of input_dict.
- Return type:
Numpy array
- Raises:
ValueError – If input_dict has no array in it with name base_name; if the keys for base_name do not all have the same number of indices; if the entries of input_dict for base_name do not all have the same shape; or if any element of the array is missing from input_dict.
Notes
Every element of the array must be present in input_dict; the indices in the keys must cover a complete array. An exception is raised otherwise.
In typical applications in MCMC, the first index of the outputted Numpy array is the chain ID, the second is the draw, and the remaining indices are the indices of the array.
This is the inverse of array_to_dict(). For an array a,
dict_to_array( array_to_dict(a, 'a', n_index_axes=k), 'a', squeeze=False )
gives back a. With squeeze=True, axes of length one are discarded, so the conversion is not in general invertible.
Examples
>>> dict_to_array( {'a[1]': np.array([[1, 2, 3, 4]]), 'a[2]': np.array([[5, 6, 7, 8]]), 'a[3]': np.array([[9, 10, 11, 12]])}, 'a', squeeze=True, ) array([[ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11], [ 4, 8, 12]])
>>> dict_to_array( {'a[1]': np.array([[1, 2, 3, 4]]), 'a[2]': np.array([[5, 6, 7, 8]]), 'a[3]': np.array([[9, 10, 11, 12]])}, 'a', ) array([[[ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11], [ 4, 8, 12]]])
>>> dict_to_array( {'a[1]': np.array([[1, 2, 3, 4], [1.1, 2.2, 3.3, 4.4]]), 'a[2]': np.array([[5, 6, 7, 8], [5.5, 6.6, 7.7, 8.8]]), 'a[3]': np.array([[9, 10, 11, 12], [9.9, 10.0, 11.1, 12.2]])}, 'a', ) array([[[ 1. , 5. , 9. ], [ 2. , 6. , 10. ], [ 3. , 7. , 11. ], [ 4. , 8. , 12. ]],
- [[ 1.1, 5.5, 9.9],
[ 2.2, 6.6, 10. ], [ 3.3, 7.7, 11.1], [ 4.4, 8.8, 12.2]]])
>>> dict_to_array( {'a[1,1]': np.array([[1, 2]]), 'a[1,2]': np.array([[3, 4]]), 'a[2,1]': np.array([[5, 6]]), 'a[2,2]': np.array([[7, 8]]), 'a[3,1]': np.array([[9, 10]]), 'a[3,2]': np.array([[11, 12]])}, 'a', ) array([[[ 1, 3], [ 5, 7], [ 9, 11]],
- [[ 2, 4],
[ 6, 8], [10, 12]]])