Npndarray dict
Module for working with dictionary of NumPy ndarrays.
Primaril used by State.
NpNdarrayDict
A class that encapsulates a dictionary of NumPy ndarrays, each associated with a specific data type and a defined min-max range. It provides a structured and efficient way to manage and manipulate multidimensional arrays with constraints on their values.
Attributes:
Name | Type | Description |
---|---|---|
data |
Dict[str, Dict[str, Union[ndarray, Any]]]
|
A dictionary where each key represents an attribute, |
shape |
Tuple[int, int]
|
The shape of the ndarrays, which is consistent across all attributes. |
Example
state = NpNdarrayDict({ 'i': (np.int32, 2, 10), 'f': (np.float32, 0., 1.), 'v2': (np_vec2, 0., 1.), 'v3': (np_vec3, 0., 1.), 'v4': (np_vec4, 0., 1.), }, (2,2)) state.set_value('i', (0, 0), 5) print(state.get_value('i', (0, 0))) 5
Source code in src/tolvera/npndarray_dict.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|
__init__(data_dict, shape)
Initialize the State class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_dict |
dict[str, tuple[Any, Any, Any]]
|
A dictionary where keys are attribute names and values are tuples of (dtype, min_value, max_value). |
required |
shape |
tuple[int]
|
The shape of the numpy arrays for each attribute. |
required |
Source code in src/tolvera/npndarray_dict.py
attr_apply(key, func)
Apply a user-defined function to the array of a specified key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The attribute key. |
required |
func |
Callable[[ndarray], ndarray]
|
A function that takes a numpy array and returns a numpy array. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If the key is not found. |
Source code in src/tolvera/npndarray_dict.py
attr_broadcast(key, other, op)
Perform a broadcasting operation between the array of the specified key and another array or NpNdarrayDict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key of the array in the dictionary to operate on. |
required |
other |
Union[ndarray, NpNdarrayDict]
|
The other array or NpNdarrayDict to use in the operation. |
required |
op |
Callable[[ndarray, ndarray], ndarray]
|
A function to perform the operation. This should be a NumPy ufunc (like np.add, np.multiply). |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If the key is not found in the dictionary. |
ValueError
|
If the operation cannot be broadcasted or if it violates the min-max constraints. |
Source code in src/tolvera/npndarray_dict.py
get_data()
Get the entire current data as a dictionary.
Returns:
Type | Description |
---|---|
dict[str, ndarray]
|
A dictionary where each key is an attribute and the value is a numpy array. |
randomise()
Randomize the entire state dictionary based on the datatype, minimum, and maximum values for each attribute.
Source code in src/tolvera/npndarray_dict.py
randomise_attr(key)
Randomize a specific attribute in the state dictionary based on its datatype, minimum, and maximum values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The attribute key to randomize. |
required |
Source code in src/tolvera/npndarray_dict.py
set_data(new_data)
Set the data with a new data dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_data |
dict[str, ndarray]
|
A dictionary representing the new data, where each key is an attribute and the value is a numpy array. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the new data is invalid (e.g., wrong shape, type, or value range). |
Source code in src/tolvera/npndarray_dict.py
dict_from_vector_args(a, scalars=None)
Convert a list of arguments to a dictionary.
Args: - a: A list of arguments. - scalars: A list of keys that should be unwrapped from lists.
Returns: - A dictionary of keyword arguments.
Source code in src/tolvera/npndarray_dict.py
dict_to_vector_args(kw)
Convert a dictionary to a list of arguments.
This function takes a dictionary and returns a list of arguments.
Args: - kw: A dictionary of keyword arguments.
Returns: - A list of arguments.
Source code in src/tolvera/npndarray_dict.py
ndarraydict_from_vector_args(lst, shapes)
Convert a list to a dictionary where each list is turned into a numpy array.
This function takes a list in the format output by dict_from_vector_args
and converts it
into a dictionary. Each key's list of values is converted into a numpy array with a
specified shape.
Args: - lst: The list to be converted. - shapes: A dictionary where keys correspond to the keys in the original list and values are tuples representing the desired shape of the numpy array.
Returns: - A dictionary with keys mapped to numpy arrays.
Source code in src/tolvera/npndarray_dict.py
shapes_from_ndarray_dict(ndarray_dict)
Return a dictionary of shapes given a dictionary of numpy ndarrays.
This function takes a dictionary where values are numpy ndarrays and returns a new dictionary with the same keys, where each value is the shape of the ndarray.
Args: - ndarray_dict: A dictionary where values are numpy ndarrays.
Returns: - A dictionary where each key maps to the shape of the corresponding ndarray.