Network¶
flyvision.network.network.Network ¶
Bases: Module
A connectome-constrained network with nodes, edges, and dynamics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
connectome |
Dict[str, Any]
|
Connectome configuration. |
Namespace(type='ConnectomeFromAvgFilters', file='fib25-fib19_v2.2.json', extent=15, n_syn_fill=1)
|
dynamics |
Dict[str, Any]
|
Network dynamics configuration. |
Namespace(type='PPNeuronIGRSynapses', activation=Namespace(type='relu'))
|
node_config |
Dict[str, Any]
|
Node parameter configuration. |
Namespace(bias=Namespace(type='RestingPotential', groupby=['type'], initial_dist='Normal', mode='sample', requires_grad=True, mean=0.5, std=0.05, penalize=Namespace(activity=True), seed=0), time_const=Namespace(type='TimeConstant', groupby=['type'], initial_dist='Value', value=0.05, requires_grad=True))
|
edge_config |
Dict[str, Any]
|
Edge parameter configuration. |
Namespace(sign=Namespace(type='SynapseSign', initial_dist='Value', requires_grad=False, groupby=['source_type', 'target_type']), syn_count=Namespace(type='SynapseCount', initial_dist='Lognormal', mode='mean', requires_grad=False, std=1.0, groupby=['source_type', 'target_type', 'dv', 'du']), syn_strength=Namespace(type='SynapseCountScaling', initial_dist='Value', requires_grad=True, scale=0.01, clamp='non_negative', groupby=['source_type', 'target_type']))
|
Attributes:
Name | Type | Description |
---|---|---|
connectome |
Connectome
|
Connectome directory. |
dynamics |
NetworkDynamics
|
Network dynamics. |
node_params |
Namespace
|
Node parameters. |
edge_params |
Namespace
|
Edge parameters. |
n_nodes |
int
|
Number of nodes. |
n_edges |
int
|
Number of edges. |
num_parameters |
int
|
Number of parameters. |
config |
Namespace
|
Config namespace. |
_source_indices |
Tensor
|
Source indices. |
_target_indices |
Tensor
|
Target indices. |
symmetry_config |
Namespace
|
Symmetry config. |
clamp_config |
Namespace
|
Clamp config. |
stimulus |
Stimulus
|
Stimulus object. |
_state_hooks |
tuple
|
State hooks. |
Source code in flyvision/network/network.py
41 42 43 44 45 46 47 48 49 50 51 52 53 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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 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 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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
prepare_configs ¶
prepare_configs(
connectome,
dynamics,
node_config,
edge_config,
stimulus_config,
)
Prepare configs for network initialization.
Source code in flyvision/network/network.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
param_api ¶
param_api()
Param api for inspection.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Tensor]]
|
Parameter namespace for inspection. |
Note
This is not the same as the parameter api passed to the dynamics. This is a convenience function to inspect the parameters, but does not write derived parameters or sources and targets states.
Source code in flyvision/network/network.py
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 |
|
target_sum ¶
target_sum(x)
Scatter sum operation creating target node states from inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Edge inputs to targets, e.g., currents. Shape is (batch_size, n_edges). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Node-level input. Shape is (batch_size, n_nodes). |
Source code in flyvision/network/network.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
register_state_hook ¶
register_state_hook(state_hook, **kwargs)
Register a state hook to retrieve or modify the state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_hook |
Callable
|
Callable to be used as a hook. |
required |
**kwargs |
Keyword arguments to pass to the callable. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
If state_hook is not callable. |
Note
The hook is called in _state_api. Useful for targeted perturbations.
Source code in flyvision/network/network.py
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 |
|
clear_state_hooks ¶
clear_state_hooks(clear=True)
Clear all state hooks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clear |
bool
|
If True, clear all state hooks. |
True
|
Source code in flyvision/network/network.py
471 472 473 474 475 476 477 478 |
|
clamp ¶
clamp()
Clamp free parameters to their range specified in their config.
Valid configs are non_negative
to clamp at zero and tuple of the form
(min, max) to clamp to an arbitrary range.
Note
This function also enforces symmetry constraints.
Source code in flyvision/network/network.py
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 |
|
forward ¶
forward(x, dt, state=None, as_states=False)
Forward pass of the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Whole-network stimulus of shape (batch_size, n_frames, n_cells). |
required |
dt |
float
|
Integration time constant. |
required |
state |
AutoDeref
|
Initial state of the network. If not given, computed from NetworksDynamics.write_initial_state. initial_state and fade_in_state are convenience functions to compute initial steady states. |
None
|
as_states |
bool
|
If True, returns the states as List[AutoDeref], else concatenates the activity of the nodes and returns a tensor. |
False
|
Returns:
Type | Description |
---|---|
Union[Tensor, AutoDeref]
|
Network activity or states. |
Source code in flyvision/network/network.py
509 510 511 512 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 |
|
steady_state ¶
steady_state(
t_pre,
dt,
batch_size,
value=0.5,
state=None,
grad=False,
return_last=True,
)
Compute state after grey-scale stimulus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t_pre |
float
|
Time of the grey-scale stimulus. |
required |
dt |
float
|
Integration time constant. |
required |
batch_size |
int
|
Batch size. |
required |
value |
float
|
Value of the grey-scale stimulus. |
0.5
|
state |
Optional[AutoDeref]
|
Initial state of the network. If not given, computed from NetworksDynamics.write_initial_state. initial_state and fade_in_state are convenience functions to compute initial steady states. |
None
|
grad |
bool
|
If True, the state is computed with gradient. |
False
|
return_last |
bool
|
If True, return only the last state. |
True
|
Returns:
Type | Description |
---|---|
AutoDeref
|
Steady state of the network after a grey-scale stimulus. |
Source code in flyvision/network/network.py
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
|
fade_in_state ¶
fade_in_state(
t_fade_in, dt, initial_frames, state=None, grad=False
)
Compute state after fade-in stimulus of initial_frames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t_fade_in |
float
|
Time of the fade-in stimulus. |
required |
dt |
float
|
Integration time constant. |
required |
initial_frames |
Tensor
|
Tensor of shape (batch_size, 1, n_input_elements). |
required |
state |
Optional[AutoDeref]
|
Initial state of the network. If not given, computed from NetworksDynamics.write_initial_state. initial_state and fade_in_state are convenience functions to compute initial steady states. |
None
|
grad |
bool
|
If True, the state is computed with gradient. |
False
|
Returns:
Type | Description |
---|---|
AutoDeref
|
State after fade-in stimulus. |
Source code in flyvision/network/network.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
simulate ¶
simulate(
movie_input,
dt,
initial_state="auto",
as_states=False,
as_layer_activity=False,
)
Simulate the network activity from movie input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
movie_input |
Tensor
|
Tensor of shape (batch_size, n_frames, 1, hexals). |
required |
dt |
float
|
Integration time constant. Warns if dt > 1/50. |
required |
initial_state |
Union[AutoDeref, None, Literal['auto']]
|
Network activity at the beginning of the simulation. Use fade_in_state or steady_state to compute the initial state from grey input or from ramping up the contrast of the first movie frame. Defaults to “auto”, which uses the steady_state after 1s of grey input. |
'auto'
|
as_states |
bool
|
If True, return the states as AutoDeref dictionary instead of a tensor. Defaults to False. |
False
|
as_layer_activity |
bool
|
If True, return a LayerActivity object. Defaults to False. Currently only supported for ConnectomeFromAvgFilters. |
False
|
Returns:
Type | Description |
---|---|
Union[Tensor, AutoDeref, LayerActivity]
|
Activity tensor of shape (batch_size, n_frames, #neurons), |
Union[Tensor, AutoDeref, LayerActivity]
|
or AutoDeref dictionary if |
Union[Tensor, AutoDeref, LayerActivity]
|
or LayerActivity object if |
Raises:
Type | Description |
---|---|
ValueError
|
If the movie_input is not four-dimensional. |
ValueError
|
If the integration time step is bigger than 1/50. |
ValueError
|
If the network is not in evaluation mode or any parameters require grad. |
Source code in flyvision/network/network.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
enable_grad ¶
enable_grad(grad=True)
Context manager to enable or disable gradient computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grad |
bool
|
If True, enable gradient computation. |
True
|
Source code in flyvision/network/network.py
698 699 700 701 702 703 704 705 706 707 708 709 710 |
|
stimulus_response ¶
stimulus_response(
stim_dataset,
dt,
indices=None,
t_pre=1.0,
t_fade_in=0.0,
grad=False,
default_stim_key="lum",
batch_size=1,
)
Compute stimulus responses for a given stimulus dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stim_dataset |
SequenceDataset
|
Stimulus dataset. |
required |
dt |
float
|
Integration time constant. |
required |
indices |
Optional[Iterable[int]]
|
Indices of the stimuli to compute the response for. If not given, all stimuli responses are computed. |
None
|
t_pre |
float
|
Time of the grey-scale stimulus. |
1.0
|
t_fade_in |
float
|
Time of the fade-in stimulus (slow). |
0.0
|
grad |
bool
|
If True, the state is computed with gradient. |
False
|
default_stim_key |
Any
|
Key of the stimulus in the dataset if it returns a dictionary. |
'lum'
|
batch_size |
int
|
Batch size for processing. |
1
|
Note
Per default, applies a grey-scale stimulus for 1 second, no fade-in stimulus.
Yields:
Type | Description |
---|---|
Tuple of (stimulus, response) as numpy arrays. |
Source code in flyvision/network/network.py
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
|
current_response ¶
current_response(
stim_dataset,
dt,
indices=None,
t_pre=1.0,
t_fade_in=0,
default_stim_key="lum",
)
Compute stimulus currents and responses for a given stimulus dataset.
Note
Requires Dynamics to implement currents
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stim_dataset |
SequenceDataset
|
Stimulus dataset. |
required |
dt |
float
|
Integration time constant. |
required |
indices |
Optional[Iterable[int]]
|
Indices of the stimuli to compute the response for. If not given, all stimuli responses are computed. |
None
|
t_pre |
float
|
Time of the grey-scale stimulus. |
1.0
|
t_fade_in |
float
|
Time of the fade-in stimulus (slow). |
0
|
default_stim_key |
Any
|
Key of the stimulus in the dataset if it returns a dictionary. |
'lum'
|
Yields:
Type | Description |
---|---|
Tuple of (stimulus, activity, currents) as numpy arrays. |
Source code in flyvision/network/network.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
Stimulus¶
Stimuli must implement the StimulusProtocol
to be compatible with
flyvision.network.network.Network
.
flyvision.network.stimulus.StimulusProtocol ¶
Bases: Protocol
Protocol for the Stimulus class.
Source code in flyvision/network/stimulus.py
15 16 17 18 19 20 21 22 23 |
|
flyvision.network.stimulus.Stimulus ¶
Interface to control the cell-specific stimulus buffer for the network.
Creates a buffer and maps standard video input to the photoreceptors but can map input to any other cell as well, e.g. to do perturbation experiments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
connectome |
ConnectomeFromAvgFilters
|
Connectome directory to retrieve indexes for the stimulus buffer at the respective cell positions. |
required |
n_samples |
int
|
Number of samples to initialize the buffer with. |
1
|
n_frames |
int
|
Number of frames to initialize the buffer with. |
1
|
init_buffer |
bool
|
If False, do not initialize the stimulus buffer. |
True
|
Attributes:
Name | Type | Description |
---|---|---|
layer_index |
Dict[str, NDArray]
|
Dictionary of cell type to index array. |
central_cells_index |
Dict[str, int]
|
Dictionary of cell type to central cell index. |
input_index |
NDArray
|
Index array of photoreceptors. |
n_frames |
int
|
Number of frames in the stimulus buffer. |
n_samples |
int
|
Number of samples in the stimulus buffer. |
n_nodes |
int
|
Number of nodes in the stimulus buffer. |
n_input_elements |
int
|
Number of input elements. |
buffer |
Tensor
|
Stimulus buffer of shape (n_samples, n_frames, n_cells). |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Stimulus of shape (n_samples, n_frames, n_cells) |
Example
stim = Stimulus(network.connectome, *x.shape[:2])
stim.add_input(x)
response = network(stim(), dt)
Source code in flyvision/network/stimulus.py
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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 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 |
|
nonzero
property
¶
nonzero
Check if elements have been added to the stimulus buffer.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if elements have been added, even if those elements were all zero. |
zero ¶
zero(n_samples=None, n_frames=None)
Reset the stimulus buffer to zero.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_samples |
Optional[int]
|
Number of samples. If provided, the buffer will be resized. |
None
|
n_frames |
Optional[int]
|
Number of frames. If provided, the buffer will be resized. |
None
|
Source code in flyvision/network/stimulus.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
add_input ¶
add_input(
x,
start=None,
stop=None,
n_frames_buffer=None,
cumulate=False,
)
Add input to the input/photoreceptor cells.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Input video of shape (n_samples, n_frames, 1, n_input_elements). |
required |
start |
Optional[int]
|
Temporal start index of the stimulus. |
None
|
stop |
Optional[int]
|
Temporal stop index of the stimulus. |
None
|
n_frames_buffer |
Optional[int]
|
Number of frames to resize the buffer to. |
None
|
cumulate |
bool
|
If True, add input to the existing buffer. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If input shape is incorrect. |
RuntimeError
|
If input shape doesn’t match buffer shape. |
Source code in flyvision/network/stimulus.py
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 |
|
add_pre_stim ¶
add_pre_stim(
x, start=None, stop=None, n_frames_buffer=None
)
Add a constant or sequence of constants to the input/photoreceptor cells.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Grey value(s). If Tensor, must have length |
required |
start |
Optional[int]
|
Start index in time. |
None
|
stop |
Optional[int]
|
Stop index in time. |
None
|
n_frames_buffer |
Optional[int]
|
Number of frames to resize the buffer to. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If input shape doesn’t match buffer shape. |
Source code in flyvision/network/stimulus.py
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 |
|
__call__ ¶
__call__()
Return the stimulus tensor.
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The stimulus buffer. |
Source code in flyvision/network/stimulus.py
249 250 251 252 253 254 255 |
|
flyvision.network.stimulus.register_stimulus ¶
register_stimulus(cls=None)
Register a stimulus class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Optional[Type[StimulusProtocol]]
|
The stimulus class to register (optional when used as a decorator). |
None
|
Returns:
Type | Description |
---|---|
Union[Callable[[Type[StimulusProtocol]], Type[StimulusProtocol]], Type[StimulusProtocol]]
|
Registered class or decorator function. |
Example
As a standalone function:
register_stimulus(CustomStimulus)
As a decorator:
@register_stimulus
class CustomStimulus(StimulusProtocol): ...
Source code in flyvision/network/stimulus.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
flyvision.network.stimulus.init_stimulus ¶
init_stimulus(connectome, **kwargs)
Source code in flyvision/network/stimulus.py
262 263 264 265 266 |
|
Dynamics¶
flyvision.network.dynamics.NetworkDynamics ¶
Defines the initialization and behavior of a Network during simulation.
This class serves as an extension point for implementing custom network dynamics models. Subclasses must implement the following methods:
- write_derived_params
- write_initial_state
- write_state_velocity
Attributes:
Name | Type | Description |
---|---|---|
activation |
Module
|
The activation function for the network. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
activation |
dict
|
A dictionary specifying the activation function type and its parameters. |
{'type': 'relu'}
|
Source code in flyvision/network/dynamics.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
write_derived_params ¶
write_derived_params(params, **kwargs)
Augment params
, called once at every forward pass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing two subdirectories: |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Note
This is called once per forward pass at the beginning. It’s required after parameters have been updated by an optimizer but not at every timestep. Called by Network._param_api.
Source code in flyvision/network/dynamics.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
write_initial_state ¶
write_initial_state(state, params, **kwargs)
Initialize a network’s state variables from its network parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing two subdirectories: |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing four subdirectories: |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Note
Called by Network._initial_state.
Source code in flyvision/network/dynamics.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
write_state_velocity ¶
write_state_velocity(
vel, state, params, target_sum, **kwargs
)
Compute dx/dt for each state variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vel |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing two subdirectories: |
required |
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing two subdirectories: |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing four subdirectories: |
required |
target_sum |
Callable
|
Sums the entries in a |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Note
Called by Network._next_state.
Source code in flyvision/network/dynamics.py
89 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 |
|
currents ¶
currents(state, params)
Compute the current flowing through each edge.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing two subdirectories: |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing four subdirectories: |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of currents flowing through each edge. |
Note
Called by Network.current_response.
Source code in flyvision/network/dynamics.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
flyvision.network.dynamics.PPNeuronIGRSynapses ¶
Bases: NetworkDynamics
Passive point neurons with instantaneous graded release synapses.
Source code in flyvision/network/dynamics.py
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 |
|
write_derived_params ¶
write_derived_params(params, **kwargs)
Calculate weights as the product of sign, synapse count, and strength.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing edge parameters. |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Source code in flyvision/network/dynamics.py
155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
write_initial_state ¶
write_initial_state(state, params, **kwargs)
Set the initial state to the bias.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory to write the initial state. |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing node parameters. |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Source code in flyvision/network/dynamics.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
write_state_velocity ¶
write_state_velocity(
vel, state, params, target_sum, x_t, dt, **kwargs
)
Calculate velocity as bias plus sum of weighted rectified inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vel |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory to write the calculated velocity. |
required |
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing current state values. |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing node and edge parameters. |
required |
target_sum |
Callable
|
Function to sum edge values for each target node. |
required |
x_t |
Tensor
|
External input at time t. |
required |
dt |
float
|
Time step. |
required |
**kwargs |
Additional keyword arguments. |
{}
|
Source code in flyvision/network/dynamics.py
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 |
|
currents ¶
currents(state, params)
Calculate the internal chemical current.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing current state values. |
required |
params |
AutoDeref[str, AutoDeref[str, RefTensor]]
|
A directory containing edge parameters. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The calculated internal chemical current. |
Source code in flyvision/network/dynamics.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
Initialization¶
flyvision.network.initialization ¶
The parameters that the networks can be initialized with. Each parameter is a type on its own, because different parameters are shared differently. These types handle the initialization of indices to perform gather and scatter opera- tions. Parameter types can be initialized from a range of initial distribution types.
InitialDistribution ¶
Initial distribution base class.
Attributes:
Name | Type | Description |
---|---|---|
raw_values |
Tensor
|
Initial parameters must store raw_values as attribute in their init. |
readers |
Dict[str, Tensor]
|
Readers will be written by the network during initialization. |
Note
To add a new initial distribution type, subclass this class and implement the init method. The init method should take the param_config as its first argument, and should store the attribute raw_values as a torch.nn.Parameter.
Example
An example of a viable param_config is:
param_config = Namespace(
requires_grad=True,
initial_dist="Normal",
mean=0,
std=1,
mode="sample",
)
Source code in flyvision/network/initialization.py
45 46 47 48 49 50 51 52 53 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 94 95 96 97 98 99 100 101 |
|
semantic_values
property
¶
semantic_values
Optional reparametrization of raw values invoked for computation.
clamp ¶
clamp(values, mode)
To clamp the raw_values of the parameters at initialization.
Note, mild clash with raw_values/semantic_values reparametrization. Parameters that use reparametrization in terms of semantic_values should not use clamp.
Source code in flyvision/network/initialization.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
Value ¶
Bases: InitialDistribution
Initializes parameters with a single value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
The value to initialize the parameter with. |
required | |
requires_grad |
bool
|
Whether the parameter requires gradients. |
required |
clamp |
bool
|
Whether to clamp the values. Defaults to False. |
False
|
**kwargs |
Additional keyword arguments. |
{}
|
Example
param_config = Namespace(
requires_grad=True,
initial_dist="Value",
value=0,
)
Source code in flyvision/network/initialization.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
Normal ¶
Bases: InitialDistribution
Initializes parameters independently from normal distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mean |
The mean of the normal distribution. |
required | |
std |
The standard deviation of the normal distribution. |
required | |
requires_grad |
bool
|
Whether the parameter requires gradients. |
required |
mode |
str
|
The initialization mode. Defaults to “sample”. |
'sample'
|
clamp |
bool
|
Whether to clamp the values. Defaults to False. |
False
|
seed |
int
|
Random seed for reproducibility. Defaults to None. |
None
|
**kwargs |
Additional keyword arguments. |
{}
|
Example
param_config = Namespace(
requires_grad=True,
initial_dist="Normal",
mean=0,
std=1,
mode="sample",
)
Source code in flyvision/network/initialization.py
129 130 131 132 133 134 135 136 137 138 139 140 141 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 |
|
Lognormal ¶
Bases: Normal
Initializes parameters independently from lognormal distributions.
Note
The lognormal distribution reparametrizes a normal through semantic values.
Example
param_config = Namespace(
requires_grad=True,
initial_dist="Lognormal",
mean=0,
std=1,
mode="sample",
)
Source code in flyvision/network/initialization.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
Parameter ¶
Base class for all parameters to share across nodes or edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param_config |
Namespace
|
Namespace containing parameter configuration. |
required |
connectome |
Connectome
|
Connectome object. |
required |
Attributes:
Name | Type | Description |
---|---|---|
parameter |
InitialDistribution
|
InitialDistribution object. |
indices |
Tensor
|
Indices for parameter sharing. |
keys |
List[Any]
|
Keys to access individual parameter values associated with certain identifiers. |
symmetry_masks |
List[Tensor]
|
Symmetry masks that can be configured optionally to apply further symmetry constraints to the parameter values. |
Note
Subclasses must implement __init__(self, param_config, connectome_dir)
with the
following requirements:
- Configure all attributes defined in the base class.
- Decorate
__init__
with@deepcopy_config
if it updatesparam_config
to prevent mutations in the outer scope. - Update
param_config
with key-value pairs informed byconnectome
and matching the desiredInitialDistribution
. - Store
parameter
fromInitialDistribution(param_config)
, which constructs and holds thenn.Parameter
. - Store
indices
for parameter sharing usingget_scatter_indices(dataframe, grouped_dataframe, groupby)
. - Store
keys
to access individual parameter values associated with certain identifiers. - Store
symmetry_masks
(optional) to apply further symmetry constraints to the parameter values.
Example implementation structure:
@deepcopy_config
def __init__(self, param_config: Namespace, connectome: Connectome):
# Update param_config based on connectome data
# ...
# Initialize parameter
self.parameter = InitialDistribution(param_config)
# Set up indices, keys, and symmetry masks
self.indices = get_scatter_indices(...)
self.keys = ...
self.symmetry_masks = symmetry_masks(...)
Source code in flyvision/network/initialization.py
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 |
|
__repr__ ¶
__repr__()
Return a string representation of the Parameter object.
Source code in flyvision/network/initialization.py
287 288 289 290 291 |
|
__getitem__ ¶
__getitem__(key)
Get parameter value for a given key.
Source code in flyvision/network/initialization.py
293 294 295 296 297 298 299 300 |
|
__len__ ¶
__len__()
Return the length of raw_values.
Source code in flyvision/network/initialization.py
302 303 304 |
|
RestingPotential ¶
Bases: Parameter
Initialize resting potentials a.k.a. biases for cell types.
Source code in flyvision/network/initialization.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
TimeConstant ¶
Bases: Parameter
Initialize time constants for cell types.
Source code in flyvision/network/initialization.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
SynapseSign ¶
Bases: Parameter
Initialize synapse signs for edge types.
Source code in flyvision/network/initialization.py
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 |
|
SynapseCount ¶
Bases: Parameter
Initialize synapse counts for edge types.
Source code in flyvision/network/initialization.py
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 |
|
SynapseCountScaling ¶
Bases: Parameter
Initialize synapse count scaling for edge types.
This class initializes synapse strengths based on the average synapse count for each edge type, scaling them differently for chemical and electrical synapses.
The initialization follows this equation:
where:
- \(\alpha_{t_it_j}\) is the synapse strength between neurons \(i\) and \(j\).
- \(\langle N \rangle_{t_it_j}\) is the average synapse count for the edge type across columnar offsets \(u_i-u_j\) and \(v_i-v_j\)
- \(\rho\) is a scaling factor (default: 0.01)
Source code in flyvision/network/initialization.py
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 |
|
deepcopy_config ¶
deepcopy_config(f)
Decorator to deepcopy the parameter configuration.
Note
This decorator is necessary because the __init__
method of parameter classes
often modifies the param_config
object. By creating a deep copy, we ensure
that these modifications don’t affect the original param_config
object in the
outer scope. This prevents unintended side effects and maintains the integrity
of the original configuration.
Source code in flyvision/network/initialization.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
get_scatter_indices ¶
get_scatter_indices(dataframe, grouped_dataframe, groupby)
Get indices for scattering operations to share parameters.
Maps each node/edge from the complete computational graph to a parameter index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe |
DataFrame
|
Dataframe of nodes or edges of the graph. |
required |
grouped_dataframe |
DataFrame
|
Aggregated version of the same dataframe. |
required |
groupby |
List[str]
|
The same columns from which the grouped_dataframe was constructed. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor of indices for scattering operations. |
Note
For N elements that are grouped into M groups, this function returns N indices from 0 to M-1 that can be used to scatter the parameters of the M groups to the N elements.
Example
elements = ["A", "A", "A", "B", "B", "C", "D", "D", "E"]
groups = ["A", "B", "C", "D", "E"]
parameter = [1, 2, 3, 4, 5]
# get_scatter_indices would return
scatter_indices = [0, 0, 0, 1, 1, 2, 3, 3, 4]
scattered_parameters = [parameter[idx] for idx in scatter_indices]
scattered_parameters == [1, 1, 1, 2, 2, 3, 4, 4, 5]
Source code in flyvision/network/initialization.py
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 |
|
symmetry_masks ¶
symmetry_masks(symmetric, keys, as_mask=False)
Create masks for subsets of parameters for joint constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symmetric |
List[Any]
|
Contains subsets of keys that point to the subsets of parameters to be indexed. |
required |
keys |
List[Any]
|
List of keys that point to individual parameter values. |
required |
as_mask |
bool
|
If True, returns a boolean mask, otherwise integer indices. |
False
|
Returns:
Type | Description |
---|---|
List[Tensor]
|
List of masks (List[torch.BoolTensor]). |
Note
This is experimental for configuration-based fine-grained shared parameter optimization, e.g. for models including multi-compartment cells or gap junctions.
Example
# For node type parameters with individual node types as keys:
symmetric = [["T4a", "T4b", "T4c", "T4d"], ["T5a", "T5b", "T5c", "T5d"]]
# This would constrain the parameter values of all T4 subtypes to their joint
# mean and the parameter values of all T5 subtypes to their joint mean.
# For edge type parameters with individual edge types as keys:
symmetric = [[("CT1(M10)", "CT1(Lo1)"), ("CT1(Lo1)", "CT1(M10)")]]
# This would constrain the edge parameter of the directed edge from CT1(M10) to
# CT1(Lo1) and the directed edge from CT1(Lo1) to CT1(M10) to their joint mean.
Source code in flyvision/network/initialization.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
|