Miscellaneous utilities¶
utils
¶
check_possible_values(param_name, params, valid_values)
¶
Check if the provided parameters are all valid values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param_name
|
str
|
The name of the parameter (for error message). |
required |
params
|
Iterable
|
The parameters to check. |
required |
valid_values
|
Iterable
|
The valid values to check against. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the parameters are not one of the valid values. |
Source code in src/bioplnn/utils/common.py
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 |
|
count_parameters(model)
¶
Count the number of trainable parameters in a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
PyTorch model. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
Number of trainable parameters. |
Source code in src/bioplnn/utils/torch.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
|
create_neuron_typed_connectome(num_neurons, neuron_type_probs, neuron_type_connectivity, deterministic_type_assignment=False)
¶
Initialize a synthetic connectome as a sparse adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_neurons
|
int
|
Total number of neurons. |
required |
neuron_type_probs
|
ndarray
|
Proportion of each neuron type, summing to 1. |
required |
neuron_type_connectivity
|
ndarray
|
neuron-type to neuron-type connectivity probabilities. |
required |
deterministic_type_assignment
|
bool
|
If True, assigns neurons deterministically based on neuron_type_probs interpreted as exact counts rather than probabilities. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
tuple[Tensor, ndarray]
|
tuple[torch.sparse.FloatTensor, np.ndarray]: Sparse adjacency matrix of neuron-neuron connections and the assigned neuron types. |
Source code in src/bioplnn/utils/torch.py
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 |
|
create_sparse_projection(size, num_neurons, indices=None, mode='ih')
¶
Create identity connectivity for input-to-hidden or hidden-to-output connections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
Size of the input or output. |
required |
num_neurons
|
int
|
Number of neurons in the hidden layer. |
required |
indices
|
Union[Tensor, PathLike]
|
Indices of neurons that receive input. If None, all neurons receive input from corresponding input indices. Defaults to None. |
None
|
mode
|
Literal['ih', 'ho']
|
Whether to create input-to-hidden or hidden-to-output connectivity (only changes the orientation of the connectivity matrix). Defaults to "ih". |
'ih'
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Sparse connectivity matrix in COO format. |
Raises:
Type | Description |
---|---|
ValueError
|
If input_indices are invalid. |
Source code in src/bioplnn/utils/torch.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
create_sparse_topographic_connectome(sheet_size, synapse_std, synapses_per_neuron, self_recurrence)
¶
Create random topographic hidden-to-hidden connectivity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet_size
|
tuple[int, int]
|
Size of the sheet-like neural layer (rows, columns). |
required |
synapse_std
|
float
|
Standard deviation of the Gaussian distribution for sampling synapse connections. |
required |
synapses_per_neuron
|
int
|
Number of incoming synapses per neuron. |
required |
self_recurrence
|
bool
|
Whether neurons can connect to themselves. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Sparse connectivity matrix in COO format. |
Source code in src/bioplnn/utils/torch.py
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 |
|
dict_flatten(d, delimiter='.', key=None)
¶
Flattens a nested dictionary into a single-level dictionary.
Keys of the flattened dictionary will be the path to the value, with path components joined by delimiter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
Dictionary to flatten. |
required |
delimiter
|
str
|
String to join key path components. Defaults to ".". |
'.'
|
key
|
str
|
Current key prefix. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Flattened dictionary. |
Raises:
Type | Description |
---|---|
ValueError
|
If flattening would result in duplicate keys. |
Source code in src/bioplnn/utils/common.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
expand_array_2d(x, m, n, depth=0)
¶
Expands a value to a 2D numpy array of shape (m, n).
Use depth > 0 if the intended type T can be indexed recursively, where depth is the maximum number of times x can be recursively indexed if of type T. For example, if x is a shallow list, then depth = 1. If x is a list of lists or an array or tensor, then depth = 2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
The variable to expand. |
required |
m
|
int
|
The number of rows in the expanded array. |
required |
n
|
int
|
The number of columns in the expanded array. |
required |
depth
|
int
|
The depth x can be recursively indexed. A depth of -1 will assume x is of type list[T] and check if x is already of the correct shape. |
0
|
Returns:
Type | Description |
---|---|
NDArray[Any]
|
np.ndarray: Expanded 2D numpy array. |
Source code in src/bioplnn/utils/common.py
142 143 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 |
|
expand_list(x, n, depth=0)
¶
Expands a value to a list of length n.
If x is already a list, then the list is returned unchanged.
If x is not a list, then x is expanded to a list of length n.
Use depth > 0 if the intended type T can be indexed recursively, where depth is the maximum number of times x can be recursively indexed if of type T. For example, if x is a shallow list, then depth = 1. If T is a list of lists or an array or tensor, then depth = 2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
The variable to expand. |
required |
n
|
int
|
The number of lists or tuples to expand to. |
required |
depth
|
int
|
The depth x can be recursively indexed. A depth of -1 will assume x is of type list[T] and check if x is already of the correct length. Defaults to 0. |
0
|
Returns: list[Any]: Expanded list.
Source code in src/bioplnn/utils/common.py
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
get_activation(activation)
¶
Get an initialized activation function module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
activation
|
Union[str, Module]
|
The name(s) of the activation function(s) or an already initialized nn.Module. If the latter, the moduleis returned as is. If None, returns nn.Identity(). |
required |
Returns:
Type | Description |
---|---|
Module
|
nn.Module: The initialized activation function. |
Source code in src/bioplnn/utils/torch.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
get_activation_class(activation)
¶
Get one or more activation function classes.
If activation is a string with commas, split and get each activation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
activation
|
str
|
The name(s) of the activation function(s). If None, returns nn.Identity. Defaults to None. |
required |
Returns:
Type | Description |
---|---|
Union[Type[Module], list[Type[Module]]]
|
Union[Type[nn.Module], List[Type[nn.Module]]]: A single activation class or a list of activation classes if comma-separated. |
Source code in src/bioplnn/utils/torch.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
idx_1D_to_2D_tensor(x, m, n)
¶
Convert 1D indices to 2D coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
1D indices tensor. |
required |
m
|
int
|
Number of rows in the 2D grid. |
required |
n
|
int
|
Number of columns in the 2D grid. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: 2D coordinates tensor of shape (len(x), 2). |
Source code in src/bioplnn/utils/torch.py
131 132 133 134 135 136 137 138 139 140 141 142 |
|
idx_2D_to_1D_tensor(x, m, n)
¶
Convert 2D coordinates to 1D indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
2D coordinates tensor of shape (N, 2). |
required |
m
|
int
|
Number of rows in the 2D grid. |
required |
n
|
int
|
Number of columns in the 2D grid. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: 1D indices tensor. |
Source code in src/bioplnn/utils/torch.py
145 146 147 148 149 150 151 152 153 154 155 156 |
|
init_tensor(init_fn, *args, **kwargs)
¶
Initialize a tensor with a specified initialization function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_fn
|
Union[str, TensorInitFnType]
|
The initialization function name or callable. |
required |
*args
|
Arguments to pass to the initialization function (usually shape). |
()
|
|
**kwargs
|
Keyword arguments to pass to the initialization function. |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The initialized tensor. |
Raises:
Type | Description |
---|---|
ValueError
|
If the initialization function is not supported. |
Source code in src/bioplnn/utils/torch.py
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 |
|
initialize_criterion(*, class_name, **kwargs)
¶
Initialize a loss criterion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the criterion class to use. |
required |
**kwargs
|
Additional keyword arguments to pass to the criterion. |
{}
|
Returns:
Type | Description |
---|---|
Module
|
torch.nn.Module: The initialized criterion. |
Source code in src/bioplnn/utils/initializers.py
77 78 79 80 81 82 83 84 85 86 87 |
|
initialize_dataloader(*, dataset, seed=None, **kwargs)
¶
Initialize a dataloader for a given dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
str
|
The dataset to use. |
required |
seed
|
int
|
The seed to use for the dataloader. Defaults to None. |
None
|
**kwargs
|
Additional keyword arguments to pass to the dataloader. |
{}
|
Returns:
Type | Description |
---|---|
tuple[DataLoader, DataLoader]
|
tuple[torch.utils.data.DataLoader, torch.utils.data.DataLoader]: The train and validation dataloaders. |
Source code in src/bioplnn/utils/initializers.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
initialize_model(*, class_name, **kwargs)
¶
Initialize a model based on the class name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the model class to use. |
required |
**kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
Type | Description |
---|---|
Module
|
nn.Module: The initialized model. |
Source code in src/bioplnn/utils/initializers.py
30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
initialize_optimizer(*, class_name, model_parameters, **kwargs)
¶
Initialize an optimizer for model training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the optimizer class to use. |
required |
model_parameters
|
ParameterList
|
The model parameters to optimize. |
required |
**kwargs
|
Additional keyword arguments to pass to the optimizer. |
{}
|
Returns:
Type | Description |
---|---|
Optimizer
|
torch.optim.Optimizer: The initialized optimizer. |
Source code in src/bioplnn/utils/initializers.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
initialize_scheduler(*, class_name, optimizer, **kwargs)
¶
Initialize a learning rate scheduler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the scheduler class to use. |
required |
optimizer
|
Optimizer
|
The optimizer to schedule. |
required |
**kwargs
|
Additional keyword arguments to pass to the scheduler. |
{}
|
Returns:
Type | Description |
---|---|
LRScheduler
|
torch.optim.lr_scheduler.LRScheduler: The initialized scheduler. |
Source code in src/bioplnn/utils/initializers.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
is_list_like(x)
¶
Determines if an object is list-like (iterable but not a string or mapping).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
Object to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the object is list-like, False otherwise. |
Source code in src/bioplnn/utils/common.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
load_array(array)
¶
Load a numpy array from an array, iterable, or file.
Supported file formats: - npz - npy - csv - pt
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array
|
Union[ndarray, Tensor, DataFrame, Iterable[Any], PathLikeType]
|
numpy array, iterable, or path to file containing numpy array. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The original or loaded numpy array. |
Raises:
Type | Description |
---|---|
ValueError
|
If the array cannot be loaded from the given file or iterable. |
Source code in src/bioplnn/utils/torch.py
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 |
|
load_sparse_tensor(x)
¶
Load a torch tensor from an array, iterable, or file.
Source code in src/bioplnn/utils/torch.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
load_tensor(tensor)
¶
Load a torch tensor from an array, iterable, or file.
Source code in src/bioplnn/utils/torch.py
339 340 341 342 343 344 345 346 347 348 |
|
manual_seed(seed)
¶
Set random seeds for reproducibility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The random seed to use. |
required |
Source code in src/bioplnn/utils/torch.py
19 20 21 22 23 24 25 26 27 28 29 |
|
manual_seed_deterministic(seed)
¶
Set random seeds and configure PyTorch for deterministic execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The random seed to use. |
required |
Source code in src/bioplnn/utils/torch.py
32 33 34 35 36 37 38 39 40 41 42 |
|
pass_fn(*args, **kwargs)
¶
A no-op function that accepts any arguments and does nothing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any positional arguments. |
()
|
|
**kwargs
|
Any keyword arguments. |
{}
|
Source code in src/bioplnn/utils/common.py
11 12 13 14 15 16 17 18 |
|
print_cuda_mem_stats(device=None)
¶
Print CUDA memory statistics for debugging.
Source code in src/bioplnn/utils/torch.py
567 568 569 570 |
|
profile_fn(fn, sort_by='cuda_time_total', row_limit=50, profile_kwargs={}, fn_kwargs={})
¶
Profile a function with PyTorch's profiler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn
|
Function to profile. |
required | |
sort_by
|
str
|
Column to sort results by. Defaults to "cuda_time_total". |
'cuda_time_total'
|
row_limit
|
int
|
Maximum number of rows to display. Defaults to 50. |
50
|
**fn_kwargs
|
Keyword arguments to pass to the function. |
{}
|
Source code in src/bioplnn/utils/torch.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
|
without_keys(d, keys)
¶
Creates a new dictionary without specified keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
Mapping
|
Input dictionary. |
required |
keys
|
list[str]
|
List of keys to exclude. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A new dictionary without the specified keys. |
Source code in src/bioplnn/utils/common.py
21 22 23 24 25 26 27 28 29 30 31 |
|