Utilities & Typing
These are internal Utilities and variable types for type hinting. The end user is unlikely to need to worry about these, but these may of interest to developers
litmus._utils
These are internal utilities and convenience functions for use in LITMUS
.
utils.py Handy internal utilities for brevity and convenience. Nothing in here is accesible in the public _init file
suppress_stdout()
Source code in litmus/_utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
isiter(x: any) -> bool
Checks to see if an object is itterable
Source code in litmus/_utils.py
50 51 52 53 54 55 56 57 58 59 60 61 |
|
isiter_dict(DICT: dict) -> bool
like isiter but for a dictionary. Checks only the first element in DICT.keys
Source code in litmus/_utils.py
64 65 66 67 68 69 70 71 72 73 |
|
dict_dim(DICT: dict) -> (int, int)
Checks the first element of a dictionary and returns its length
Source code in litmus/_utils.py
76 77 78 79 80 81 82 83 84 85 |
|
dict_pack(DICT: dict, keys=None, recursive=True, H=None, d0={}) -> np.array
Packs a dictionary into an array format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
DICT
|
dict
|
the dict to unpack |
required |
keys
|
the order in which to index the keyed elements. If none, will use DICT.keys(). Can be partial |
None
|
|
recursive
|
whether to recurse into arrays |
True
|
|
H
|
Matrix to scale parameters by |
None
|
|
d0
|
Value to offset by before packing |
{}
|
Returns:
Type | Description |
---|---|
array
|
(nkeys x len_array) np.arrayobject X = H (d-d0) |
Source code in litmus/_utils.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
dict_unpack(X: np.array, keys: [str], recursive=True, Hinv=None, x0=None) -> np.array
Unpacks an array into a dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array
|
Array to unpack |
required |
keys
|
[str]
|
keys to unpack with |
required |
Returns:
Type | Description |
---|---|
array
|
Hinv(X) + x0 |
Source code in litmus/_utils.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
dict_sortby(A: dict, B: dict, match_only=True) -> dict
Sorts dict A to match keys of dict B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
dict
|
Dict to be sorted |
required |
B
|
dict
|
Dict whose keys are will provide the ordering |
required |
match_only
|
If true, returns only for keys common to both A and B. Else, append un-sorted entries to end |
True
|
Returns:
Type | Description |
---|---|
dict
|
{key: A[key] for key in B if key in A} |
Source code in litmus/_utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
dict_extend(A: dict, B: dict = None) -> dict
Extends all single-length entries of a dict to match the length of a non-singular element
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
dict
|
Dictionary whose elements are to be extended |
required |
B
|
dict
|
(optional) the array to extend by, equivalent to dict_extend(A|B) |
None
|
Returns:
Type | Description |
---|---|
dict
|
Dict A with any singleton elements extended to the longest entry in A or B |
Source code in litmus/_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
dict_combine(X: [dict]) -> {str: [float]}
Combines an array, list etc. of dictionaries into a dictionary of arrays
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
[dict]
|
1D Iterable of dicts |
required |
Returns:
Type | Description |
---|---|
{str: [float]}
|
Dict of 1D iterables |
Source code in litmus/_utils.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
dict_divide(X: dict) -> [dict]
Splits dict of arrays into array of dicts. Opposite of dict_combine
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
dict
|
Dict of 1D iterables |
required |
Returns:
Type | Description |
---|---|
[dict]
|
1D Iterable of dicts |
Source code in litmus/_utils.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
dict_split(X: dict, keys: [str]) -> (dict, dict)
Splits a dict in two based on keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
dict
|
Dict to be split into A,B |
required |
keys
|
[str]
|
Keys to be present in A, but not in B |
required |
Returns:
Type | Description |
---|---|
(dict, dict)
|
tuple of dicts (A,B) |
Source code in litmus/_utils.py
223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
pack_function(func, packed_keys: [str], fixed_values: dict = {}, invert: bool = False, jit: bool = False, H: np.array = None, d0: dict = {}) -> _types.FunctionType
Re-arranges a function that takes dict arguments to tak array-like arguments instead, so as to be autograd friendly Takes a function f(D:dict, arg, kwargs) and returns f(X, D2, args, **kwargs), D2 is all elements of D not listed in 'packed_keys' or fixed_values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Function to be unpacked |
required | |
packed_keys
|
[str]
|
Keys in 'D' to be packed in an array |
required |
fixed_values
|
dict
|
Elements of 'D' to be fixed |
{}
|
invert
|
bool
|
If true, will 'flip' the function upside down |
False
|
jit
|
bool
|
If true, will 'jit' the function |
False
|
H
|
array
|
(optional) scaling matrix to reparameterize H with |
None
|
d0
|
dict
|
(optional) If given, will center the reparameterized function at x0 |
{}
|
Source code in litmus/_utils.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
randint()
Quick utility to generate a random integer
Source code in litmus/_utils.py
287 288 289 290 291 |
|
litmus._types
Data types for type hinting. By convention in LITMUS
, N
indicates an over sample sites or observations while M
indicates an index over number of parameters.
A single ref package to create and pull in types for type hinting
DType = TypeVar('DType', bound=generic)
module-attribute
Any Dtype
ArrayN = Annotated[NDArray[DType], Literal['N']]
module-attribute
1D array corresponding to N sample sites, e.g. an array of log densities
ArrayNxN = Annotated[NDArray[DType], Literal['N', 'N']]
module-attribute
2D array corresponding to NxN sample sites, e.g. a GP covariance matrix
ArrayM = Annotated[NDArray[DType], Literal['M']]
module-attribute
1D array corresponding to M parameters, e.g. a grad
ArrayMxM = Annotated[NDArray[DType], Literal['M', 'M']]
module-attribute
2D array corresponding to MxM parameters, e.g. a hessian
ArrayNxMxM = Annotated[NDArray[DType], Literal['M', 'N', 'N']]
module-attribute
3D array corresponding to N sheets of MxM arrays for N data points and M parameters, e.g. a plate of hessians