Skip to content

Spatially embedded cortical networks

spatially_embedded

Conv2dRectify

Bases: Conv2d

Applies a 2d convolution with nonnegative weights and biases.

Source code in src/bioplnn/models/spatially_embedded.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Conv2dRectify(nn.Conv2d):
    """Applies a 2d convolution with nonnegative weights and biases."""

    def forward(self, *args, **kwargs):
        """Forward pass of the layer.

        Args:
            *args: Positional arguments passed to `nn.Conv2d`.
            **kwargs: Keyword arguments passed to `nn.Conv2d`.

        Returns:
            The convolution output.
        """
        self.weight.data.clamp_(min=0.0)
        if self.bias is not None:
            self.bias.data.clamp_(min=0.0)
        return super().forward(*args, **kwargs)

forward(*args, **kwargs)

Forward pass of the layer.

Parameters:

Name Type Description Default
*args

Positional arguments passed to nn.Conv2d.

()
**kwargs

Keyword arguments passed to nn.Conv2d.

{}

Returns:

Type Description

The convolution output.

Source code in src/bioplnn/models/spatially_embedded.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def forward(self, *args, **kwargs):
    """Forward pass of the layer.

    Args:
        *args: Positional arguments passed to `nn.Conv2d`.
        **kwargs: Keyword arguments passed to `nn.Conv2d`.

    Returns:
        The convolution output.
    """
    self.weight.data.clamp_(min=0.0)
    if self.bias is not None:
        self.bias.data.clamp_(min=0.0)
    return super().forward(*args, **kwargs)

SpatiallyEmbeddedArea

Bases: Module

A biologically-plausible, spatially embedded neural area.

This module imposes a series of biologically-inspired constraints on artificial neural networks. At its core, it is a collection of 2D (hence spatially embedded) convolutional layers organized into a 'circuit motif'. Here, 'circuit motif' refers to the connectivity pattern between the input, feedback, neuron types, and output within a distinct neural area. For example, if we have two neuron types, an excitatory and an inhibitory, then the circuit motif determines which neuron types receive input, which neuron types receive feedback, which neuron types are connected to which other neuron types, and which neuron types project to the output of the area.

Key features:
  • Configurable neuron types (excitatory/inhibitory/hybrid)
  • Configurable spatial extents of lateral connections (same/half)
  • Convolutional connectivity between neuron populations
  • Recurrent dynamics with learnable time constants
  • Optional feedback connections
  • Customizable activation functions

Attributes:

Name Type Description
in_size tuple[int, int]

Spatial size of the input data (height, width).

in_channels int

Number of input channels.

out_channels int

Number of output channels.

feedback_channels int

Number of feedback channels (0 if none).

use_feedback bool

Whether this area receives feedback from another area.

in_class str

Class of input signal ("excitatory", "inhibitory", or "hybrid").

feedback_class str

Class of feedback signal ("excitatory", "inhibitory", or "hybrid").

out_nonlinearity Module

Nonlinearity applied to the output.

num_neuron_types int

Number of neuron types in the area.

num_neuron_subtypes list[int]

Number of subtypes for each neuron type.

neuron_type_class list[str]

Class of each neuron type ("excitatory", "inhibitory", or "hybrid").

neuron_type_density list[str]

Spatial density of each neuron type ("same" or "half").

neuron_type_nonlinearity ModuleList

Nonlinearity for each neuron type's activity.

neuron_type_size list[tuple[int, int]]

Spatial size of each neuron type.

num_rows_connectivity int

Number of rows in the connectivity matrix.

num_cols_connectivity int

Number of columns in the connectivity matrix.

inter_neuron_type_connectivity ndarray

Connectivity matrix for the circuit motif.

inter_neuron_type_spatial_extents ndarray

Spatial extent for each circuit motif connection.

inter_neuron_type_num_subtype_groups ndarray

Number of subtype groups for each circuit motif connection.

inter_neuron_type_nonlinearity ndarray

Nonlinearity for each circuit motif connection.

inter_neuron_type_bias ndarray

Whether to add a bias term for each circuit motif connection.

tau_mode list[str]

Mode determining which parts of neuron activity share a time constant.

tau_init_fn list[Union[str, TensorInitFnType]]

Initialization for the membrane time constants.

tau ParameterList

Learnable time constants for each neuron type.

default_neuron_state_init_fn Union[str, TensorInitFnType]

Default initialization for neuron states.

default_feedback_state_init_fn Union[str, TensorInitFnType]

Default initialization for feedback states.

default_output_state_init_fn Union[str, TensorInitFnType]

Default initialization for output states.

convs ModuleDict

Convolutional layers representing connections between neuron types.

out_convs ModuleDict

Convolutional layers connecting to the output.

Examples:

>>> config = SpatiallyEmbeddedAreaConfig(
...     in_size=(32, 32),
...     in_channels=3,
...     out_channels=16,
...     num_neuron_types=2,
...     num_neuron_subtypes=16,
...     neuron_type_class=["excitatory", "inhibitory"],
...     neuron_type_density=["same", "half"],
...     neuron_type_nonlinearity=,
...     inter_neuron_type_connectivity=[[1, 0, 0], [1, 1, 1], [1, 0, 0]],
...     inter_neuron_type_spatial_extents=(3, 3),
... )
>>> area = SpatiallyEmbeddedArea(config)
>>> print(area.summary())
Source code in src/bioplnn/models/spatially_embedded.py
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
class SpatiallyEmbeddedArea(nn.Module):
    """A biologically-plausible, spatially embedded neural area.

    This module imposes a series of biologically-inspired constraints on
    artificial neural networks. At its core, it is a collection of 2D (hence
    spatially embedded) convolutional layers organized into a 'circuit
    motif'. Here, 'circuit motif' refers to the connectivity pattern between
    the input, feedback, neuron types, and output within a distinct neural
    area. For example, if we have two neuron types, an excitatory and an
    inhibitory, then the circuit motif determines which neuron types receive
    input, which neuron types receive feedback, which neuron types are connected
    to which other neuron types, and which neuron types project to the output of
    the area.

    ## Key features:

    - Configurable neuron types (excitatory/inhibitory/hybrid)
    - Configurable spatial extents of lateral connections (same/half)
    - Convolutional connectivity between neuron populations
    - Recurrent dynamics with learnable time constants
    - Optional feedback connections
    - Customizable activation functions

    Attributes:
        in_size (tuple[int, int]): Spatial size of the input data
            (height, width).
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        feedback_channels (int): Number of feedback channels (0 if none).
        use_feedback (bool): Whether this area receives feedback from another
            area.
        in_class (str): Class of input signal ("excitatory", "inhibitory", or
            "hybrid").
        feedback_class (str): Class of feedback signal ("excitatory", "inhibitory",
            or "hybrid").
        out_nonlinearity (nn.Module): Nonlinearity applied to the output.

        num_neuron_types (int): Number of neuron types in the area.
        num_neuron_subtypes (list[int]): Number of subtypes for each neuron type.
        neuron_type_class (list[str]): Class of each neuron type ("excitatory",
            "inhibitory", or "hybrid").
        neuron_type_density (list[str]): Spatial density of each neuron type
            ("same" or "half").
        neuron_type_nonlinearity (nn.ModuleList): Nonlinearity for each neuron
            type's activity.
        neuron_type_size (list[tuple[int, int]]): Spatial size of each neuron type.

        num_rows_connectivity (int): Number of rows in the connectivity matrix.
        num_cols_connectivity (int): Number of columns in the connectivity matrix.
        inter_neuron_type_connectivity (np.ndarray): Connectivity matrix for the
            circuit motif.
        inter_neuron_type_spatial_extents (np.ndarray): Spatial extent for each
            circuit motif connection.
        inter_neuron_type_num_subtype_groups (np.ndarray): Number of subtype groups
            for each circuit motif connection.
        inter_neuron_type_nonlinearity (np.ndarray): Nonlinearity for each circuit
            motif connection.
        inter_neuron_type_bias (np.ndarray): Whether to add a bias term for each
            circuit motif connection.

        tau_mode (list[str]): Mode determining which parts of neuron activity share
            a time constant.
        tau_init_fn (list[Union[str, TensorInitFnType]]): Initialization for the
            membrane time constants.
        tau (nn.ParameterList): Learnable time constants for each neuron type.

        default_neuron_state_init_fn (Union[str, TensorInitFnType]): Default
            initialization for neuron states.
        default_feedback_state_init_fn (Union[str, TensorInitFnType]): Default
            initialization for feedback states.
        default_output_state_init_fn (Union[str, TensorInitFnType]): Default
            initialization for output states.

        convs (nn.ModuleDict): Convolutional layers representing connections between
            neuron types.
        out_convs (nn.ModuleDict): Convolutional layers connecting to the output.

    Examples:
        >>> config = SpatiallyEmbeddedAreaConfig(
        ...     in_size=(32, 32),
        ...     in_channels=3,
        ...     out_channels=16,
        ...     num_neuron_types=2,
        ...     num_neuron_subtypes=16,
        ...     neuron_type_class=["excitatory", "inhibitory"],
        ...     neuron_type_density=["same", "half"],
        ...     neuron_type_nonlinearity=,
        ...     inter_neuron_type_connectivity=[[1, 0, 0], [1, 1, 1], [1, 0, 0]],
        ...     inter_neuron_type_spatial_extents=(3, 3),
        ... )
        >>> area = SpatiallyEmbeddedArea(config)
        >>> print(area.summary())
    """

    def __init__(
        self, config: Optional[SpatiallyEmbeddedAreaConfig] = None, **kwargs
    ):
        """Initialize the SpatiallyEmbeddedArea.

        Args:
            config: Configuration object that specifies the area architecture and parameters.
                See SpatiallyEmbeddedAreaConfig for details. If None, parameters must be
                provided as keyword arguments.
            **kwargs: Keyword arguments to instantiate the configuration if
                `config` is not provided. Cannot provide both `config` and
                keyword arguments.

        Raises:
            ValueError: If an invalid configuration is provided.
        """

        super().__init__()

        if config is None:
            config = SpatiallyEmbeddedAreaConfig(**kwargs)
        elif kwargs:
            raise ValueError(
                "Cannot provide both config and keyword arguments. Please provide "
                "only one of the two."
            )

        #####################################################################
        # Input, output, and feedback parameters
        #####################################################################

        self.in_size = config.in_size
        self.in_channels = config.in_channels
        self.out_channels = config.out_channels
        self.feedback_channels = (
            config.feedback_channels
            if config.feedback_channels is not None
            else 0
        )
        self.use_feedback = self.feedback_channels > 0

        self.in_class = config.in_class
        check_possible_values(
            "in_class",
            (self.in_class,),
            ("excitatory", "inhibitory", "hybrid"),
        )
        self.feedback_class = config.feedback_class
        check_possible_values(
            "feedback_class",
            (self.feedback_class,),
            ("excitatory", "inhibitory", "hybrid"),
        )

        self.out_nonlinearity = get_activation(config.out_nonlinearity)

        #####################################################################
        # Neuron type parameters
        #####################################################################

        self.num_neuron_types = config.num_neuron_types

        # Format neuron type
        self.num_neuron_subtypes = expand_list(
            config.num_neuron_subtypes, self.num_neuron_types
        )
        self.neuron_type_class = expand_list(
            config.neuron_type_class, self.num_neuron_types
        )
        check_possible_values(
            "neuron_type_class",
            self.neuron_type_class,
            ("excitatory", "inhibitory", "hybrid"),
        )
        self.neuron_type_density = expand_list(
            config.neuron_type_density, self.num_neuron_types
        )
        # TODO: Add support for quarter
        check_possible_values(
            "neuron_type_density",
            self.neuron_type_density,
            ("same", "half"),
        )
        neuron_type_nonlinearity = expand_list(
            config.neuron_type_nonlinearity, self.num_neuron_types
        )
        self.neuron_type_nonlinearity = nn.ModuleList(
            [
                get_activation(nonlinearity)
                for nonlinearity in neuron_type_nonlinearity
            ]
        )

        # Save number of "types" for the input to and output from the area
        self.num_rows_connectivity = (
            1 + int(self.use_feedback) + self.num_neuron_types
        )  # input + feedback + neurons
        self.num_cols_connectivity = (
            self.num_neuron_types + 1
        )  # neurons + output

        # Calculate half spatial size
        self.half_size = (
            ceil(self.in_size[0] / 2),
            ceil(self.in_size[1] / 2),
        )

        self.neuron_type_size = [
            self.in_size
            if self.neuron_type_density[i] == "same"
            else self.half_size
            for i in range(self.num_neuron_types)
        ]

        #####################################################################
        # Circuit motif connectivity
        #####################################################################

        # Format circuit connectivity
        self.inter_neuron_type_connectivity = np.array(
            config.inter_neuron_type_connectivity
        )
        if self.inter_neuron_type_connectivity.shape != (
            self.num_rows_connectivity,
            self.num_cols_connectivity,
        ):
            raise ValueError(
                "The shape of inter_neuron_type_connectivity must match the number of "
                "rows and columns in the connectivity matrix."
            )

        # Format connectivity variables to match circuit connectivity
        self.inter_neuron_type_spatial_extents = expand_array_2d(
            config.inter_neuron_type_spatial_extents,
            self.inter_neuron_type_connectivity.shape[0],
            self.inter_neuron_type_connectivity.shape[1],
            depth=1,
        )
        self.inter_neuron_type_num_subtype_groups = expand_array_2d(
            config.inter_neuron_type_num_subtype_groups,
            self.inter_neuron_type_connectivity.shape[0],
            self.inter_neuron_type_connectivity.shape[1],
        )
        self.inter_neuron_type_nonlinearity = expand_array_2d(
            config.inter_neuron_type_nonlinearity,
            self.inter_neuron_type_connectivity.shape[0],
            self.inter_neuron_type_connectivity.shape[1],
        )
        self.inter_neuron_type_bias = expand_array_2d(
            config.inter_neuron_type_bias,
            self.inter_neuron_type_connectivity.shape[0],
            self.inter_neuron_type_connectivity.shape[1],
        )

        #####################################################################
        # Circuit motif convolutions
        # Here, we represent the circuit connectivity between neuron classes
        # as an array of convolutions (implemented as a dictionary for
        # efficiency). The convolution self.convs[f"{i}->{j}"] corresponds
        # to the connection from neuron class i to neuron class j.
        #####################################################################

        self.convs = nn.ModuleDict()
        self.out_convs = nn.ModuleDict()
        for i, row in enumerate(self.inter_neuron_type_connectivity):
            # Handle input neuron channel and spatial mode based on neuron type
            conv_in_type = self._source_from_row_idx(i)
            if conv_in_type == "input":
                conv_in_channels = self.in_channels
                conv_in_density = "same"
            elif conv_in_type == "feedback":
                conv_in_channels = self.feedback_channels
                conv_in_density = "same"
            else:
                assert conv_in_type == "cell"
                conv_in_channels = self.num_neuron_subtypes[
                    i - 1 - int(self.use_feedback)
                ]
                conv_in_density = self.neuron_type_density[
                    i - 1 - int(self.use_feedback)
                ]

            conv_in_class = self._class_from_row_idx(i)
            if conv_in_class in ("excitatory", "inhibitory"):
                Conv2d = Conv2dRectify
            else:
                assert conv_in_class == "hybrid"
                Conv2d = nn.Conv2d

            # Handle output neurons
            # TODO: Optimize using smart grouping and convolution sharing
            to_indices = np.nonzero(row)[0]
            for j in to_indices:
                if self.inter_neuron_type_connectivity[i, j]:
                    # Handle output neuron channel and spatial mode based on neuron type
                    conv_out_type = self._destination_from_col_idx(j)
                    if conv_out_type == "cell":
                        conv_out_channels: int = self.num_neuron_subtypes[j]  # type: ignore
                        conv_out_density: str = self.neuron_type_density[j]  # type: ignore
                    else:
                        assert conv_out_type == "output"
                        if conv_in_type in ("input", "feedback"):
                            warnings.warn(
                                "Input or feedback is connected to output. "
                                "This is typically undesired as the signal "
                                "will bypass the neuron types and go directly "
                                "to the output. Consider removing this "
                                "connection in the inter_neuron_type_connectivity "
                                "matrix."
                            )
                        conv_out_channels = self.out_channels
                        conv_out_density = "same"

                    # Handle stride upsampling if necessary
                    conv = nn.Sequential()
                    conv_stride = 1
                    if (
                        conv_in_density == "half"
                        and conv_out_density == "same"
                    ):
                        conv_stride = 2
                    elif (
                        conv_in_density == "same"
                        and conv_out_density == "half"
                    ):
                        conv.append(
                            nn.Upsample(size=self.in_size, mode="bilinear")
                        )

                    # Handle upsampling if necessary
                    conv.append(
                        Conv2d(
                            in_channels=conv_in_channels,
                            out_channels=conv_out_channels,
                            kernel_size=self.inter_neuron_type_spatial_extents[
                                i, j
                            ],
                            stride=conv_stride,
                            padding=(
                                self.inter_neuron_type_spatial_extents[i, j][0]
                                // 2,
                                self.inter_neuron_type_spatial_extents[i, j][1]
                                // 2,
                            ),
                            groups=self.inter_neuron_type_num_subtype_groups[
                                i, j
                            ],
                            bias=self.inter_neuron_type_bias[i, j],
                        )
                    )
                    conv.append(
                        get_activation(
                            self.inter_neuron_type_nonlinearity[i, j]
                        )
                    )
                    if conv_out_type == "output":
                        self.out_convs[f"{i}->out"] = conv
                    else:
                        self.convs[f"{i}->{j}"] = conv

        #####################################################################
        # Post convolution operations
        #####################################################################

        # Initialize membrane time constants
        self.tau_mode = expand_list(config.tau_mode, self.num_neuron_types)
        check_possible_values(
            "tau_mode",
            self.tau_mode,
            ("subtype", "spatial", "subtype_spatial", "type"),
        )
        self.tau_init_fn = expand_list(
            config.tau_init_fn, self.num_neuron_types
        )

        self.tau = nn.ParameterList()
        for i in range(self.num_neuron_types):
            if self.tau_mode[i] == "spatial":
                tau_channels = 1
                tau_size = self.neuron_type_size[i]
            elif self.tau_mode[i] == "subtype":
                tau_channels = self.num_neuron_subtypes[i]
                tau_size = (1, 1)
            elif self.tau_mode[i] == "subtype_spatial":
                tau_channels = self.num_neuron_subtypes[i]
                tau_size = self.neuron_type_size[i]
            else:
                assert self.tau_mode[i] == "type"
                tau_channels = 1
                tau_size = (1, 1)

            tau = init_tensor(
                self.tau_init_fn[i],
                1,
                tau_channels,
                *tau_size,
            )
            noise = torch.rand_like(tau) * 1e-6

            self.tau.append(
                nn.Parameter(
                    tau + noise,
                    requires_grad=True,
                )
            )

        #####################################################################
        # Tensor initialization
        #####################################################################

        self.default_neuron_state_init_fn = config.default_neuron_state_init_fn
        self.default_feedback_state_init_fn = (
            config.default_feedback_state_init_fn
        )
        self.default_output_state_init_fn = config.default_output_state_init_fn

    def _source_from_row_idx(self, idx: int) -> Optional[str]:
        """Converts a row index to the corresponding source.

        Args:
            idx: Row index in the circuit connectivity matrix.

        Returns:
            The source associated with the index. Can be "input",
            "feedback", or "cell".
        """
        if idx == 0:
            return "input"
        elif self.use_feedback and idx == 1:
            return "feedback"
        else:
            return "cell"

    def _class_from_row_idx(self, idx: int) -> Optional[str]:
        """Converts a row index to the corresponding class.

        Args:
            idx: Row index in the circuit connectivity matrix.

        Returns:
            The class associated with the index. Can be "excitatory",
            "inhibitory", or "hybrid".
        """
        source = self._source_from_row_idx(idx)
        if source == "input":
            return self.in_class
        elif source == "feedback":
            return self.feedback_class
        else:
            return self.neuron_type_class[idx - 1 - int(self.use_feedback)]  # type: ignore

    def _destination_from_col_idx(self, idx: int) -> Optional[str]:
        """Converts a column index to the corresponding destination.

        Args:
            idx: Column index in the circuit connectivity matrix.

        Returns:
            The destination associated with the index. Can be "cell" or
            "output".
        """
        if idx < self.num_cols_connectivity - 1:
            return "cell"
        else:
            return "output"

    def _clamp_tau(self) -> None:
        for tau in self.tau:
            tau.data = torch.clamp(tau, min=1.0)

    def init_neuron_state(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> list[torch.Tensor]:
        """initializers the neuron hidden states.

        Args:
            batch_size: Batch size.
            init_fn: Initialization mode. Must be 'zeros', 'ones', 'randn', 'rand',
                a function, or None. If None, the default initialization mode will be used.
                If a function, it must take a variable number of positional arguments
                corresponding to the shape of the tensor to initialize, as well
                as a `device` keyword argument that sends the device to allocate
                the tensor on.
            device: Device to allocate the hidden states on.

        Returns:
            A list containing the initialized neuron hidden states.
        """

        init_fn_corrected = (
            init_fn
            if init_fn is not None
            else self.default_neuron_state_init_fn
        )

        return [
            init_tensor(
                init_fn_corrected,
                batch_size,
                self.num_neuron_subtypes[i],
                *self.in_size,
                device=device,
            )
            for i in range(self.num_neuron_types)
        ]

    def init_output_state(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> torch.Tensor:
        """Initializes the output.

        Args:
            batch_size: Batch size.
            init_fn: Initialization function. Must be 'zeros', 'ones', 'randn', 'rand',
                a function, or None. If None, the default initialization mode will be used.
            device: Device to allocate the hidden states on.

        Returns:
            The initialized output.
        """

        init_fn_corrected = (
            init_fn
            if init_fn is not None
            else self.default_output_state_init_fn
        )

        return init_tensor(
            init_fn_corrected,
            batch_size,
            self.out_channels,
            *self.in_size,
            device=device,
        )

    def init_feedback_state(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> Union[torch.Tensor, None]:
        """Initializes the feedback input.

        Args:
            batch_size: Batch size.
            init_fn: Initialization function. Must be 'zeros', 'ones', 'randn', 'rand',
                a function, or None. If None, the default initialization mode will be used.
            device: Device to allocate the hidden states on.

        Returns:
            The initialized feedback input if `use_feedback` is True, otherwise None.
        """

        if not self.use_feedback:
            return None

        init_fn_corrected = (
            init_fn
            if init_fn is not None
            else self.default_feedback_state_init_fn
        )

        return init_tensor(
            init_fn_corrected,
            batch_size,
            self.feedback_channels,
            *self.in_size,
            device=device,
        )

    def neuron_description_df(self) -> pd.DataFrame:
        """Creates a DataFrame representing the neuron types.

        Returns:
            DataFrame with columns for neuron type, spatial mode, and number of channels.
        """

        df_columns = defaultdict(list)
        for i in range(self.num_neuron_types):
            df_columns["type"].append(self.neuron_type_class[i])
            df_columns["spatial_mode"].append(self.neuron_type_density[i])
            df_columns["channels"].append(self.num_neuron_subtypes[i])

        df = pd.DataFrame(df_columns)

        return df

    def conv_connectivity_df(self) -> pd.DataFrame:
        """Creates a DataFrame representing connectivity between neural populations.

        Returns:
            DataFrame with rows representing source populations ("from") and
            columns representing target populations ("to").
        """
        row_labels = (
            ["input"]
            + (["feedback"] if self.use_feedback else [])
            + [f"neuron_{i}" for i in range(self.num_neuron_types)]
        )
        column_labels = [
            f"neuron_{i}" for i in range(self.num_neuron_types)
        ] + ["output"]

        assert len(row_labels) == self.num_rows_connectivity
        assert len(column_labels) == self.num_cols_connectivity

        array = np.empty((len(row_labels), len(column_labels)), dtype=object)
        for i in range(self.num_rows_connectivity):
            for j in range(self.num_cols_connectivity):
                if self.inter_neuron_type_connectivity[i, j]:
                    content = []
                    content.append(
                        "b" if self.inter_neuron_type_bias[i, j] else "_"
                    )
                    content.append(
                        self.inter_neuron_type_nonlinearity[i, j][:2]
                        if self.inter_neuron_type_nonlinearity[i, j]
                        else "_"
                    )
                    array[i, j] = ",".join(content)
                else:
                    array[i, j] = ""

        df = pd.DataFrame(
            array.tolist(), index=row_labels, columns=column_labels
        )
        df.index.name = "from"
        df.columns.name = "to"

        return df

    def summary(self) -> str:
        """Returns a string representation of the SpatiallyEmbeddedArea.

        Returns:
            String representation of the SpatiallyEmbeddedArea.
        """
        repr_str = "SpatiallyEmbeddedArea:\n"
        repr_str += "=" * 80 + "\n"
        repr_str += "Connectivity:\n"
        repr_str += self.conv_connectivity_df().to_string()
        repr_str += "-" * 80 + "\n"
        repr_str += "Neuron Description:\n"
        repr_str += self.neuron_description_df().to_string()
        repr_str += "=" * 80 + "\n"
        return repr_str

    def forward(
        self,
        input: torch.Tensor,
        neuron_state: Union[torch.Tensor, list[torch.Tensor]],
        feedback_state: Optional[torch.Tensor] = None,
    ) -> tuple[torch.Tensor, Union[torch.Tensor, list[torch.Tensor]]]:
        """Forward pass of the SpatiallyEmbeddedArea.

        Args:
            input: Input tensor of shape (batch_size, in_channels, in_size[0], in_size[1]).
            h_neuron: List of neuron hidden states of shape (batch_size, neuron_channels[i],
                in_size[0], in_size[1]) for each neuron type i.
            feedback_state: Feedback input of shape (batch_size, feedback_channels,
                in_size[0], in_size[1]).

        Returns:
            A tuple containing the output and new neuron hidden state.
        """

        # Expand h_neuron to match the number of neuron channels
        if isinstance(neuron_state, torch.Tensor):
            if self.num_neuron_types != 1:
                raise ValueError(
                    "neuron_state must be a list of tensors if num_neuron_types is not 1."
                )
            neuron_state = [neuron_state]

        # Check if feedback is provided if necessary
        if self.use_feedback == (feedback_state is None):
            raise ValueError(
                "use_feedback must be True if and only if feedback_state is provided."
            )

        # Compute convolutions for each connection in the circuit
        circuit_ins = (
            [input]
            + ([feedback_state] if self.use_feedback else [])
            + neuron_state
        )
        circuit_outs = [[] for _ in range(self.num_neuron_types)]
        for key, conv in self.convs.items():
            i, j = key.split("->")
            i, j = int(i), int(j)
            if self._class_from_row_idx(i) == "inhibitory":
                sign = -1
            else:
                sign = 1

            circuit_outs[j].append(sign * conv(circuit_ins[i]))

        # Update neuron states
        self._clamp_tau()
        neuron_state_new = []
        for i in range(self.num_neuron_types):
            # Aggregate all circuit outputs to this neuron type
            state_new = torch.stack(circuit_outs[i], dim=0).sum(dim=0)
            state_new = self.neuron_type_nonlinearity[i](state_new)

            # Euler update
            state_new = (
                state_new / self.tau[i]
                + (1 - 1 / self.tau[i]) * neuron_state[i]
            )
            neuron_state_new.append(state_new)

        # Compute output
        out = []
        for key, conv in self.out_convs.items():
            i, j = key.split("->")
            i = int(i)

            if self._class_from_row_idx(i) == "inhibitory":
                sign = -1
            else:
                sign = 1

            source = self._source_from_row_idx(i)
            if source == "cell":
                i = i - 1 - int(self.use_feedback)
                out.append(sign * conv(neuron_state_new[i]))
            else:
                warnings.warn(
                    f"Connection from {source} to output is not "
                    "recommended. Consider changing inter_neuron_type_connectivity "
                    "to remove this connection."
                )
                out.append(sign * conv(circuit_ins[i]))

        out = torch.stack(out, dim=0).sum(dim=0)

        return out, neuron_state_new

__init__(config=None, **kwargs)

Initialize the SpatiallyEmbeddedArea.

Parameters:

Name Type Description Default
config Optional[SpatiallyEmbeddedAreaConfig]

Configuration object that specifies the area architecture and parameters. See SpatiallyEmbeddedAreaConfig for details. If None, parameters must be provided as keyword arguments.

None
**kwargs

Keyword arguments to instantiate the configuration if config is not provided. Cannot provide both config and keyword arguments.

{}

Raises:

Type Description
ValueError

If an invalid configuration is provided.

Source code in src/bioplnn/models/spatially_embedded.py
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
def __init__(
    self, config: Optional[SpatiallyEmbeddedAreaConfig] = None, **kwargs
):
    """Initialize the SpatiallyEmbeddedArea.

    Args:
        config: Configuration object that specifies the area architecture and parameters.
            See SpatiallyEmbeddedAreaConfig for details. If None, parameters must be
            provided as keyword arguments.
        **kwargs: Keyword arguments to instantiate the configuration if
            `config` is not provided. Cannot provide both `config` and
            keyword arguments.

    Raises:
        ValueError: If an invalid configuration is provided.
    """

    super().__init__()

    if config is None:
        config = SpatiallyEmbeddedAreaConfig(**kwargs)
    elif kwargs:
        raise ValueError(
            "Cannot provide both config and keyword arguments. Please provide "
            "only one of the two."
        )

    #####################################################################
    # Input, output, and feedback parameters
    #####################################################################

    self.in_size = config.in_size
    self.in_channels = config.in_channels
    self.out_channels = config.out_channels
    self.feedback_channels = (
        config.feedback_channels
        if config.feedback_channels is not None
        else 0
    )
    self.use_feedback = self.feedback_channels > 0

    self.in_class = config.in_class
    check_possible_values(
        "in_class",
        (self.in_class,),
        ("excitatory", "inhibitory", "hybrid"),
    )
    self.feedback_class = config.feedback_class
    check_possible_values(
        "feedback_class",
        (self.feedback_class,),
        ("excitatory", "inhibitory", "hybrid"),
    )

    self.out_nonlinearity = get_activation(config.out_nonlinearity)

    #####################################################################
    # Neuron type parameters
    #####################################################################

    self.num_neuron_types = config.num_neuron_types

    # Format neuron type
    self.num_neuron_subtypes = expand_list(
        config.num_neuron_subtypes, self.num_neuron_types
    )
    self.neuron_type_class = expand_list(
        config.neuron_type_class, self.num_neuron_types
    )
    check_possible_values(
        "neuron_type_class",
        self.neuron_type_class,
        ("excitatory", "inhibitory", "hybrid"),
    )
    self.neuron_type_density = expand_list(
        config.neuron_type_density, self.num_neuron_types
    )
    # TODO: Add support for quarter
    check_possible_values(
        "neuron_type_density",
        self.neuron_type_density,
        ("same", "half"),
    )
    neuron_type_nonlinearity = expand_list(
        config.neuron_type_nonlinearity, self.num_neuron_types
    )
    self.neuron_type_nonlinearity = nn.ModuleList(
        [
            get_activation(nonlinearity)
            for nonlinearity in neuron_type_nonlinearity
        ]
    )

    # Save number of "types" for the input to and output from the area
    self.num_rows_connectivity = (
        1 + int(self.use_feedback) + self.num_neuron_types
    )  # input + feedback + neurons
    self.num_cols_connectivity = (
        self.num_neuron_types + 1
    )  # neurons + output

    # Calculate half spatial size
    self.half_size = (
        ceil(self.in_size[0] / 2),
        ceil(self.in_size[1] / 2),
    )

    self.neuron_type_size = [
        self.in_size
        if self.neuron_type_density[i] == "same"
        else self.half_size
        for i in range(self.num_neuron_types)
    ]

    #####################################################################
    # Circuit motif connectivity
    #####################################################################

    # Format circuit connectivity
    self.inter_neuron_type_connectivity = np.array(
        config.inter_neuron_type_connectivity
    )
    if self.inter_neuron_type_connectivity.shape != (
        self.num_rows_connectivity,
        self.num_cols_connectivity,
    ):
        raise ValueError(
            "The shape of inter_neuron_type_connectivity must match the number of "
            "rows and columns in the connectivity matrix."
        )

    # Format connectivity variables to match circuit connectivity
    self.inter_neuron_type_spatial_extents = expand_array_2d(
        config.inter_neuron_type_spatial_extents,
        self.inter_neuron_type_connectivity.shape[0],
        self.inter_neuron_type_connectivity.shape[1],
        depth=1,
    )
    self.inter_neuron_type_num_subtype_groups = expand_array_2d(
        config.inter_neuron_type_num_subtype_groups,
        self.inter_neuron_type_connectivity.shape[0],
        self.inter_neuron_type_connectivity.shape[1],
    )
    self.inter_neuron_type_nonlinearity = expand_array_2d(
        config.inter_neuron_type_nonlinearity,
        self.inter_neuron_type_connectivity.shape[0],
        self.inter_neuron_type_connectivity.shape[1],
    )
    self.inter_neuron_type_bias = expand_array_2d(
        config.inter_neuron_type_bias,
        self.inter_neuron_type_connectivity.shape[0],
        self.inter_neuron_type_connectivity.shape[1],
    )

    #####################################################################
    # Circuit motif convolutions
    # Here, we represent the circuit connectivity between neuron classes
    # as an array of convolutions (implemented as a dictionary for
    # efficiency). The convolution self.convs[f"{i}->{j}"] corresponds
    # to the connection from neuron class i to neuron class j.
    #####################################################################

    self.convs = nn.ModuleDict()
    self.out_convs = nn.ModuleDict()
    for i, row in enumerate(self.inter_neuron_type_connectivity):
        # Handle input neuron channel and spatial mode based on neuron type
        conv_in_type = self._source_from_row_idx(i)
        if conv_in_type == "input":
            conv_in_channels = self.in_channels
            conv_in_density = "same"
        elif conv_in_type == "feedback":
            conv_in_channels = self.feedback_channels
            conv_in_density = "same"
        else:
            assert conv_in_type == "cell"
            conv_in_channels = self.num_neuron_subtypes[
                i - 1 - int(self.use_feedback)
            ]
            conv_in_density = self.neuron_type_density[
                i - 1 - int(self.use_feedback)
            ]

        conv_in_class = self._class_from_row_idx(i)
        if conv_in_class in ("excitatory", "inhibitory"):
            Conv2d = Conv2dRectify
        else:
            assert conv_in_class == "hybrid"
            Conv2d = nn.Conv2d

        # Handle output neurons
        # TODO: Optimize using smart grouping and convolution sharing
        to_indices = np.nonzero(row)[0]
        for j in to_indices:
            if self.inter_neuron_type_connectivity[i, j]:
                # Handle output neuron channel and spatial mode based on neuron type
                conv_out_type = self._destination_from_col_idx(j)
                if conv_out_type == "cell":
                    conv_out_channels: int = self.num_neuron_subtypes[j]  # type: ignore
                    conv_out_density: str = self.neuron_type_density[j]  # type: ignore
                else:
                    assert conv_out_type == "output"
                    if conv_in_type in ("input", "feedback"):
                        warnings.warn(
                            "Input or feedback is connected to output. "
                            "This is typically undesired as the signal "
                            "will bypass the neuron types and go directly "
                            "to the output. Consider removing this "
                            "connection in the inter_neuron_type_connectivity "
                            "matrix."
                        )
                    conv_out_channels = self.out_channels
                    conv_out_density = "same"

                # Handle stride upsampling if necessary
                conv = nn.Sequential()
                conv_stride = 1
                if (
                    conv_in_density == "half"
                    and conv_out_density == "same"
                ):
                    conv_stride = 2
                elif (
                    conv_in_density == "same"
                    and conv_out_density == "half"
                ):
                    conv.append(
                        nn.Upsample(size=self.in_size, mode="bilinear")
                    )

                # Handle upsampling if necessary
                conv.append(
                    Conv2d(
                        in_channels=conv_in_channels,
                        out_channels=conv_out_channels,
                        kernel_size=self.inter_neuron_type_spatial_extents[
                            i, j
                        ],
                        stride=conv_stride,
                        padding=(
                            self.inter_neuron_type_spatial_extents[i, j][0]
                            // 2,
                            self.inter_neuron_type_spatial_extents[i, j][1]
                            // 2,
                        ),
                        groups=self.inter_neuron_type_num_subtype_groups[
                            i, j
                        ],
                        bias=self.inter_neuron_type_bias[i, j],
                    )
                )
                conv.append(
                    get_activation(
                        self.inter_neuron_type_nonlinearity[i, j]
                    )
                )
                if conv_out_type == "output":
                    self.out_convs[f"{i}->out"] = conv
                else:
                    self.convs[f"{i}->{j}"] = conv

    #####################################################################
    # Post convolution operations
    #####################################################################

    # Initialize membrane time constants
    self.tau_mode = expand_list(config.tau_mode, self.num_neuron_types)
    check_possible_values(
        "tau_mode",
        self.tau_mode,
        ("subtype", "spatial", "subtype_spatial", "type"),
    )
    self.tau_init_fn = expand_list(
        config.tau_init_fn, self.num_neuron_types
    )

    self.tau = nn.ParameterList()
    for i in range(self.num_neuron_types):
        if self.tau_mode[i] == "spatial":
            tau_channels = 1
            tau_size = self.neuron_type_size[i]
        elif self.tau_mode[i] == "subtype":
            tau_channels = self.num_neuron_subtypes[i]
            tau_size = (1, 1)
        elif self.tau_mode[i] == "subtype_spatial":
            tau_channels = self.num_neuron_subtypes[i]
            tau_size = self.neuron_type_size[i]
        else:
            assert self.tau_mode[i] == "type"
            tau_channels = 1
            tau_size = (1, 1)

        tau = init_tensor(
            self.tau_init_fn[i],
            1,
            tau_channels,
            *tau_size,
        )
        noise = torch.rand_like(tau) * 1e-6

        self.tau.append(
            nn.Parameter(
                tau + noise,
                requires_grad=True,
            )
        )

    #####################################################################
    # Tensor initialization
    #####################################################################

    self.default_neuron_state_init_fn = config.default_neuron_state_init_fn
    self.default_feedback_state_init_fn = (
        config.default_feedback_state_init_fn
    )
    self.default_output_state_init_fn = config.default_output_state_init_fn

conv_connectivity_df()

Creates a DataFrame representing connectivity between neural populations.

Returns:

Type Description
DataFrame

DataFrame with rows representing source populations ("from") and

DataFrame

columns representing target populations ("to").

Source code in src/bioplnn/models/spatially_embedded.py
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
def conv_connectivity_df(self) -> pd.DataFrame:
    """Creates a DataFrame representing connectivity between neural populations.

    Returns:
        DataFrame with rows representing source populations ("from") and
        columns representing target populations ("to").
    """
    row_labels = (
        ["input"]
        + (["feedback"] if self.use_feedback else [])
        + [f"neuron_{i}" for i in range(self.num_neuron_types)]
    )
    column_labels = [
        f"neuron_{i}" for i in range(self.num_neuron_types)
    ] + ["output"]

    assert len(row_labels) == self.num_rows_connectivity
    assert len(column_labels) == self.num_cols_connectivity

    array = np.empty((len(row_labels), len(column_labels)), dtype=object)
    for i in range(self.num_rows_connectivity):
        for j in range(self.num_cols_connectivity):
            if self.inter_neuron_type_connectivity[i, j]:
                content = []
                content.append(
                    "b" if self.inter_neuron_type_bias[i, j] else "_"
                )
                content.append(
                    self.inter_neuron_type_nonlinearity[i, j][:2]
                    if self.inter_neuron_type_nonlinearity[i, j]
                    else "_"
                )
                array[i, j] = ",".join(content)
            else:
                array[i, j] = ""

    df = pd.DataFrame(
        array.tolist(), index=row_labels, columns=column_labels
    )
    df.index.name = "from"
    df.columns.name = "to"

    return df

forward(input, neuron_state, feedback_state=None)

Forward pass of the SpatiallyEmbeddedArea.

Parameters:

Name Type Description Default
input Tensor

Input tensor of shape (batch_size, in_channels, in_size[0], in_size[1]).

required
h_neuron

List of neuron hidden states of shape (batch_size, neuron_channels[i], in_size[0], in_size[1]) for each neuron type i.

required
feedback_state Optional[Tensor]

Feedback input of shape (batch_size, feedback_channels, in_size[0], in_size[1]).

None

Returns:

Type Description
tuple[Tensor, Union[Tensor, list[Tensor]]]

A tuple containing the output and new neuron hidden state.

Source code in src/bioplnn/models/spatially_embedded.py
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
def forward(
    self,
    input: torch.Tensor,
    neuron_state: Union[torch.Tensor, list[torch.Tensor]],
    feedback_state: Optional[torch.Tensor] = None,
) -> tuple[torch.Tensor, Union[torch.Tensor, list[torch.Tensor]]]:
    """Forward pass of the SpatiallyEmbeddedArea.

    Args:
        input: Input tensor of shape (batch_size, in_channels, in_size[0], in_size[1]).
        h_neuron: List of neuron hidden states of shape (batch_size, neuron_channels[i],
            in_size[0], in_size[1]) for each neuron type i.
        feedback_state: Feedback input of shape (batch_size, feedback_channels,
            in_size[0], in_size[1]).

    Returns:
        A tuple containing the output and new neuron hidden state.
    """

    # Expand h_neuron to match the number of neuron channels
    if isinstance(neuron_state, torch.Tensor):
        if self.num_neuron_types != 1:
            raise ValueError(
                "neuron_state must be a list of tensors if num_neuron_types is not 1."
            )
        neuron_state = [neuron_state]

    # Check if feedback is provided if necessary
    if self.use_feedback == (feedback_state is None):
        raise ValueError(
            "use_feedback must be True if and only if feedback_state is provided."
        )

    # Compute convolutions for each connection in the circuit
    circuit_ins = (
        [input]
        + ([feedback_state] if self.use_feedback else [])
        + neuron_state
    )
    circuit_outs = [[] for _ in range(self.num_neuron_types)]
    for key, conv in self.convs.items():
        i, j = key.split("->")
        i, j = int(i), int(j)
        if self._class_from_row_idx(i) == "inhibitory":
            sign = -1
        else:
            sign = 1

        circuit_outs[j].append(sign * conv(circuit_ins[i]))

    # Update neuron states
    self._clamp_tau()
    neuron_state_new = []
    for i in range(self.num_neuron_types):
        # Aggregate all circuit outputs to this neuron type
        state_new = torch.stack(circuit_outs[i], dim=0).sum(dim=0)
        state_new = self.neuron_type_nonlinearity[i](state_new)

        # Euler update
        state_new = (
            state_new / self.tau[i]
            + (1 - 1 / self.tau[i]) * neuron_state[i]
        )
        neuron_state_new.append(state_new)

    # Compute output
    out = []
    for key, conv in self.out_convs.items():
        i, j = key.split("->")
        i = int(i)

        if self._class_from_row_idx(i) == "inhibitory":
            sign = -1
        else:
            sign = 1

        source = self._source_from_row_idx(i)
        if source == "cell":
            i = i - 1 - int(self.use_feedback)
            out.append(sign * conv(neuron_state_new[i]))
        else:
            warnings.warn(
                f"Connection from {source} to output is not "
                "recommended. Consider changing inter_neuron_type_connectivity "
                "to remove this connection."
            )
            out.append(sign * conv(circuit_ins[i]))

    out = torch.stack(out, dim=0).sum(dim=0)

    return out, neuron_state_new

init_feedback_state(batch_size, init_fn=None, device=None)

Initializes the feedback input.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization function. Must be 'zeros', 'ones', 'randn', 'rand', a function, or None. If None, the default initialization mode will be used.

None
device Optional[Union[device, str]]

Device to allocate the hidden states on.

None

Returns:

Type Description
Union[Tensor, None]

The initialized feedback input if use_feedback is True, otherwise None.

Source code in src/bioplnn/models/spatially_embedded.py
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
def init_feedback_state(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> Union[torch.Tensor, None]:
    """Initializes the feedback input.

    Args:
        batch_size: Batch size.
        init_fn: Initialization function. Must be 'zeros', 'ones', 'randn', 'rand',
            a function, or None. If None, the default initialization mode will be used.
        device: Device to allocate the hidden states on.

    Returns:
        The initialized feedback input if `use_feedback` is True, otherwise None.
    """

    if not self.use_feedback:
        return None

    init_fn_corrected = (
        init_fn
        if init_fn is not None
        else self.default_feedback_state_init_fn
    )

    return init_tensor(
        init_fn_corrected,
        batch_size,
        self.feedback_channels,
        *self.in_size,
        device=device,
    )

init_neuron_state(batch_size, init_fn=None, device=None)

initializers the neuron hidden states.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization mode. Must be 'zeros', 'ones', 'randn', 'rand', a function, or None. If None, the default initialization mode will be used. If a function, it must take a variable number of positional arguments corresponding to the shape of the tensor to initialize, as well as a device keyword argument that sends the device to allocate the tensor on.

None
device Optional[Union[device, str]]

Device to allocate the hidden states on.

None

Returns:

Type Description
list[Tensor]

A list containing the initialized neuron hidden states.

Source code in src/bioplnn/models/spatially_embedded.py
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
def init_neuron_state(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> list[torch.Tensor]:
    """initializers the neuron hidden states.

    Args:
        batch_size: Batch size.
        init_fn: Initialization mode. Must be 'zeros', 'ones', 'randn', 'rand',
            a function, or None. If None, the default initialization mode will be used.
            If a function, it must take a variable number of positional arguments
            corresponding to the shape of the tensor to initialize, as well
            as a `device` keyword argument that sends the device to allocate
            the tensor on.
        device: Device to allocate the hidden states on.

    Returns:
        A list containing the initialized neuron hidden states.
    """

    init_fn_corrected = (
        init_fn
        if init_fn is not None
        else self.default_neuron_state_init_fn
    )

    return [
        init_tensor(
            init_fn_corrected,
            batch_size,
            self.num_neuron_subtypes[i],
            *self.in_size,
            device=device,
        )
        for i in range(self.num_neuron_types)
    ]

init_output_state(batch_size, init_fn=None, device=None)

Initializes the output.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization function. Must be 'zeros', 'ones', 'randn', 'rand', a function, or None. If None, the default initialization mode will be used.

None
device Optional[Union[device, str]]

Device to allocate the hidden states on.

None

Returns:

Type Description
Tensor

The initialized output.

Source code in src/bioplnn/models/spatially_embedded.py
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
def init_output_state(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> torch.Tensor:
    """Initializes the output.

    Args:
        batch_size: Batch size.
        init_fn: Initialization function. Must be 'zeros', 'ones', 'randn', 'rand',
            a function, or None. If None, the default initialization mode will be used.
        device: Device to allocate the hidden states on.

    Returns:
        The initialized output.
    """

    init_fn_corrected = (
        init_fn
        if init_fn is not None
        else self.default_output_state_init_fn
    )

    return init_tensor(
        init_fn_corrected,
        batch_size,
        self.out_channels,
        *self.in_size,
        device=device,
    )

neuron_description_df()

Creates a DataFrame representing the neuron types.

Returns:

Type Description
DataFrame

DataFrame with columns for neuron type, spatial mode, and number of channels.

Source code in src/bioplnn/models/spatially_embedded.py
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
def neuron_description_df(self) -> pd.DataFrame:
    """Creates a DataFrame representing the neuron types.

    Returns:
        DataFrame with columns for neuron type, spatial mode, and number of channels.
    """

    df_columns = defaultdict(list)
    for i in range(self.num_neuron_types):
        df_columns["type"].append(self.neuron_type_class[i])
        df_columns["spatial_mode"].append(self.neuron_type_density[i])
        df_columns["channels"].append(self.num_neuron_subtypes[i])

    df = pd.DataFrame(df_columns)

    return df

summary()

Returns a string representation of the SpatiallyEmbeddedArea.

Returns:

Type Description
str

String representation of the SpatiallyEmbeddedArea.

Source code in src/bioplnn/models/spatially_embedded.py
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
def summary(self) -> str:
    """Returns a string representation of the SpatiallyEmbeddedArea.

    Returns:
        String representation of the SpatiallyEmbeddedArea.
    """
    repr_str = "SpatiallyEmbeddedArea:\n"
    repr_str += "=" * 80 + "\n"
    repr_str += "Connectivity:\n"
    repr_str += self.conv_connectivity_df().to_string()
    repr_str += "-" * 80 + "\n"
    repr_str += "Neuron Description:\n"
    repr_str += self.neuron_description_df().to_string()
    repr_str += "=" * 80 + "\n"
    return repr_str

SpatiallyEmbeddedAreaConfig dataclass

Configuration for SpatiallyEmbeddedArea.

This class defines the configuration for a spatially embedded area. It specifies the size of the input data, the number of input and output channels, the connectivity matrix for the circuit motif, and the parameters for the neuron types.

The default configuration corresponds to a spatially embedded area with one excitatory neuron type which is stimulated by the input and by itself (lateral connections).

Any of the parameters annotated with ScalarOrListLike can be either a single value that applies to all neuron types, or a list of values that apply to each neuron type.

Any of the parameters annotated with ScalarOrArray2dType can be either a single value that applies to all connections in the circuit motif, or a 2D array that applies to each connection in the circuit motif.

Attributes:

Name Type Description
in_size tuple[int, int]

Spatial size of the input data (height, width). This size determines the spatial sizes of the feedback signal, the neuronal states, and the output.

in_channels int

Number of input channels.

out_channels int

Number of output channels.

feedback_channels Optional[int]

Number of feedback channels. If provided, this area must receive feedback from another area.

in_class str

Class of input signal. Can be "excitatory", "inhibitory", or "hybrid".

feedback_class str

Class of feedback signal. Can be "excitatory", "inhibitory", or "hybrid".

num_neuron_types int

Number of neuron types.

num_neuron_subtypes ScalarOrListLike[int]

Number of subtypes for each neuron type.

neuron_type_class ScalarOrListLike[str]

Class of neuron type. Can be "excitatory", "inhibitory", or "hybrid".

neuron_type_density ScalarOrListLike[str]

Spatial density of each neuron type. Can be "same" or "half".

neuron_type_nonlinearity ScalarOrListLike[Optional[Union[str, Module]]]

Nonlinearity to apply to each neuron type's activity after adding the impact of all connected inputs/neuron types in the circuit motif.

inter_neuron_type_connectivity Array2dType[Union[int, bool]]

Connectivity matrix for the circuit motif. The shape should be (1 + int(use_feedback) + num_neuron_types, num_neuron_types + 1). Here, rows represent source types and columns represent destination types. A True entry in the matrix indicates a connection from the source to the destination. The first row corresponds to the input, the second row corresponds to the feedback (if feedback_channels > 0), and the remaining rows correspond to the neuron types. The first num_neuron_types columns correspond to the neuron types and the last column corresponds to the output. To get a template of the connectivity matrix for your configuration with appropriate row and column labels, use the inter_neuron_type_connectivity_template_df method of this class.

inter_neuron_type_spatial_extents ScalarOrArray2dType[tuple[int, int]]

Spatial extent for each circuit motif connection. Same shape as inter_neuron_type_connectivity.

inter_neuron_type_num_subtype_groups ScalarOrArray2dType[int]

Number of subtype groups for each circuit motif connection. Same shape as inter_neuron_type_connectivity.

inter_neuron_type_nonlinearity ScalarOrArray2dType[Optional[Union[str, Module]]]

Nonlinearity for each circuit motif connection. Same shape as inter_neuron_type_connectivity.

inter_neuron_type_bias ScalarOrArray2dType[bool]

Whether to add a bias term for each circuit motif connection. Same shape as inter_neuron_type_connectivity.

tau_mode ScalarOrListLike[str]

Mode determining which parts of neuron activity share a time constant. Can be "type" (one tau for each neuron type), "subtype" (one tau per neuron subtype), "spatial" (one tau per spatial location), or "subtype_spatial" (one tau per neuron subtype and spatial location)

tau_init_fn ScalarOrListLike[Union[str, TensorInitFnType]]

Initialization mode for the membrane time constants.

out_nonlinearity Optional[Union[str, Module]]

Nonlinearity to apply to the output.

default_neuron_state_init_fn Union[str, TensorInitFnType]

Initialization mode for the hidden state.

default_feedback_state_init_fn Union[str, TensorInitFnType]

Initialization mode for the feedback state.

default_output_state_init_fn Union[str, TensorInitFnType]

Initialization mode for the output state.

Examples:

>>> connectivity_df = SpatiallyEmbeddedAreaConfig.inter_neuron_type_connectivity_template_df(
...     use_feedback=False,
...     num_neuron_types=2,
... )
>>> print(connectivity_df)
                destination
source          neuron_0  neuron_1  output
input           False     False     False
neuron_0        False     False     False
neuron_1        False     False     False
>>> connectivity_df.loc["input", "neuron_0"] = True
>>> connectivity_df.loc["neuron_0", "neuron_0"] = True
>>> connectivity_df.loc["neuron_0", "neuron_1"] = True
>>> connectivity_df.loc["neuron_1", "neuron_0"] = True
>>> connectivity_df.loc["neuron_0", "output"] = True
>>> config = SpatiallyEmbeddedAreaConfig(
...     in_size=(32, 32),
...     in_channels=3,
...     out_channels=16,
...     inter_neuron_type_connectivity=connectivity_df.to_numpy(),
... )
Source code in src/bioplnn/models/spatially_embedded.py
 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
@dataclass
class SpatiallyEmbeddedAreaConfig:
    """Configuration for `SpatiallyEmbeddedArea`.

    This class defines the configuration for a spatially embedded area. It
    specifies the size of the input data, the number of input and output
    channels, the connectivity matrix for the circuit motif, and the parameters
    for the neuron types.

    The default configuration corresponds to a spatially embedded area with
    one excitatory neuron type which is stimulated by the input and by itself
    (lateral connections).

    Any of the parameters annotated with `ScalarOrListLike` can be either a
    single value that applies to all neuron types, or a list of values that
    apply to each neuron type.

    Any of the parameters annotated with `ScalarOrArray2dType` can be either a
    single value that applies to all connections in the circuit motif, or a
    2D array that applies to each connection in the circuit motif.

    Attributes:
        in_size: Spatial size of the input data (height, width).
            This size determines the spatial sizes of the feedback signal, the
            neuronal states, and the output.
        in_channels: Number of input channels.
        out_channels: Number of output channels.
        feedback_channels: Number of feedback channels. If provided, this area
            must receive feedback from another area.
        in_class: Class of input signal. Can be
            "excitatory", "inhibitory", or "hybrid".
        feedback_class: Class of feedback signal. Can be
            "excitatory", "inhibitory", or "hybrid".
        num_neuron_types: Number of neuron types.
        num_neuron_subtypes: Number of subtypes for each neuron type.
        neuron_type_class: Class of neuron type. Can be
            "excitatory", "inhibitory", or "hybrid".
        neuron_type_density: Spatial density of each neuron type. Can be "same"
            or "half".
        neuron_type_nonlinearity: Nonlinearity to apply to each neuron type's
            activity after adding the impact of all connected inputs/neuron
            types in the circuit motif.
        inter_neuron_type_connectivity: Connectivity matrix for the circuit motif.
            The shape should be
            (1 + int(use_feedback) + num_neuron_types, num_neuron_types + 1). Here,
            rows represent source types and columns represent destination types.
            A True entry in the matrix indicates a connection from the source to
            the destination.
            The first row corresponds to the input, the second row corresponds to
            the feedback (if feedback_channels > 0), and the remaining rows
            correspond to the neuron types.
            The first num_neuron_types columns correspond to the neuron types
            and the last column corresponds to the output. To get a template of
            the connectivity matrix for your configuration with appropriate row
            and column labels, use the `inter_neuron_type_connectivity_template_df`
            method of this class.
        inter_neuron_type_spatial_extents: Spatial extent for each circuit
            motif connection. Same shape as `inter_neuron_type_connectivity`.
        inter_neuron_type_num_subtype_groups: Number of subtype groups for each
            circuit motif connection. Same shape as `inter_neuron_type_connectivity`.
        inter_neuron_type_nonlinearity: Nonlinearity for each circuit motif
            connection. Same shape as `inter_neuron_type_connectivity`.
        inter_neuron_type_bias: Whether to add a bias term for each circuit
            motif connection. Same shape as `inter_neuron_type_connectivity`.
        tau_mode: Mode determining which parts of
            neuron activity share a time constant. Can be "type" (one tau for each neuron type),
            "subtype" (one tau per neuron subtype), "spatial" (one tau per spatial location), or
            "subtype_spatial" (one tau per neuron subtype and spatial location)
        tau_init_fn: Initialization mode for the membrane time constants.
        out_nonlinearity: Nonlinearity to apply to the output.
        default_neuron_state_init_fn: Initialization mode for the hidden state.
        default_feedback_state_init_fn: Initialization mode for the feedback state.
        default_output_state_init_fn: Initialization mode for the output state.

    Examples:
        >>> connectivity_df = SpatiallyEmbeddedAreaConfig.inter_neuron_type_connectivity_template_df(
        ...     use_feedback=False,
        ...     num_neuron_types=2,
        ... )
        >>> print(connectivity_df)
                        destination
        source          neuron_0  neuron_1  output
        input           False     False     False
        neuron_0        False     False     False
        neuron_1        False     False     False
        >>> connectivity_df.loc["input", "neuron_0"] = True
        >>> connectivity_df.loc["neuron_0", "neuron_0"] = True
        >>> connectivity_df.loc["neuron_0", "neuron_1"] = True
        >>> connectivity_df.loc["neuron_1", "neuron_0"] = True
        >>> connectivity_df.loc["neuron_0", "output"] = True
        >>> config = SpatiallyEmbeddedAreaConfig(
        ...     in_size=(32, 32),
        ...     in_channels=3,
        ...     out_channels=16,
        ...     inter_neuron_type_connectivity=connectivity_df.to_numpy(),
        ... )
    """

    # Input, output, and feedback parameters
    in_size: tuple[int, int]
    in_channels: int
    out_channels: int
    feedback_channels: Optional[int] = None
    in_class: str = "hybrid"
    feedback_class: str = "hybrid"

    # Neuron type parameters
    num_neuron_types: int = 1
    num_neuron_subtypes: ScalarOrListLike[int] = 16
    neuron_type_class: ScalarOrListLike[str] = "hybrid"
    neuron_type_density: ScalarOrListLike[str] = "same"
    neuron_type_nonlinearity: ScalarOrListLike[
        Optional[Union[str, nn.Module]]
    ] = "Sigmoid"
    tau_mode: ScalarOrListLike[str] = "subtype"
    tau_init_fn: ScalarOrListLike[Union[str, TensorInitFnType]] = "ones"

    # Circuit motif connectivity parameters
    inter_neuron_type_connectivity: Array2dType[Union[int, bool]] = field(
        default_factory=lambda: [[1, 0], [1, 1]]
    )
    inter_neuron_type_spatial_extents: ScalarOrArray2dType[tuple[int, int]] = (
        3,
        3,
    )
    inter_neuron_type_num_subtype_groups: ScalarOrArray2dType[int] = 1
    inter_neuron_type_nonlinearity: ScalarOrArray2dType[
        Optional[Union[str, nn.Module]]
    ] = None
    inter_neuron_type_bias: ScalarOrArray2dType[bool] = True
    out_nonlinearity: Optional[Union[str, nn.Module]] = None
    default_neuron_state_init_fn: Union[str, TensorInitFnType] = "zeros"
    default_feedback_state_init_fn: Union[str, TensorInitFnType] = "zeros"
    default_output_state_init_fn: Union[str, TensorInitFnType] = "zeros"

    def asdict(self) -> dict[str, Any]:
        """Converts the configuration object to a dictionary.

        Returns:
            dict[str, Any]: Dictionary representation of the configuration.
        """
        return asdict(self)

    @staticmethod
    def inter_neuron_type_connectivity_template_df(
        use_feedback: bool, num_neuron_types: int
    ) -> pd.DataFrame:
        """Samples the inter-neuron type connectivity matrix.

        Returns:
            pd.DataFrame: DataFrame representation of the connectivity matrix.
        """
        row_labels = (
            ["input"]
            + (["feedback"] if use_feedback else [])
            + [f"neuron_{i}" for i in range(num_neuron_types)]
        )
        column_labels = [f"neuron_{i}" for i in range(num_neuron_types)] + [
            "output"
        ]

        return pd.DataFrame(
            np.zeros((len(row_labels), len(column_labels)), dtype=np.bool),
            index=row_labels,
            columns=column_labels,
        )

asdict()

Converts the configuration object to a dictionary.

Returns:

Type Description
dict[str, Any]

dict[str, Any]: Dictionary representation of the configuration.

Source code in src/bioplnn/models/spatially_embedded.py
186
187
188
189
190
191
192
def asdict(self) -> dict[str, Any]:
    """Converts the configuration object to a dictionary.

    Returns:
        dict[str, Any]: Dictionary representation of the configuration.
    """
    return asdict(self)

inter_neuron_type_connectivity_template_df(use_feedback, num_neuron_types) staticmethod

Samples the inter-neuron type connectivity matrix.

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame representation of the connectivity matrix.

Source code in src/bioplnn/models/spatially_embedded.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
@staticmethod
def inter_neuron_type_connectivity_template_df(
    use_feedback: bool, num_neuron_types: int
) -> pd.DataFrame:
    """Samples the inter-neuron type connectivity matrix.

    Returns:
        pd.DataFrame: DataFrame representation of the connectivity matrix.
    """
    row_labels = (
        ["input"]
        + (["feedback"] if use_feedback else [])
        + [f"neuron_{i}" for i in range(num_neuron_types)]
    )
    column_labels = [f"neuron_{i}" for i in range(num_neuron_types)] + [
        "output"
    ]

    return pd.DataFrame(
        np.zeros((len(row_labels), len(column_labels)), dtype=np.bool),
        index=row_labels,
        columns=column_labels,
    )

SpatiallyEmbeddedRNN

Bases: Module

Spatially embedded RNN.

This module stacks multiple SpatiallyEmbeddedArea instances with optional feedback connections between them. It handles spatial dimension matching between areas and provides a flexible interface for configuring the network.

Attributes:

Name Type Description
num_areas

Number of SpatiallyEmbeddedArea instances in the network.

areas

ModuleList containing the SpatiallyEmbeddedArea instances.

feedback_convs

ModuleDict of feedback convolution layers between areas.

area_time_delay

Whether to introduce a time delay between areas.

pool_mode

Pooling mode for area outputs ('max' or 'avg').

batch_first

Whether input has batch dimension as the first dimension.

inter_area_feedback_connectivity

Connectivity matrix for feedback connections between areas.

inter_area_feedback_nonlinearity

Nonlinearities for feedback connections between areas.

inter_area_feedback_spatial_extents

Kernel sizes for feedback convolutions between areas.

Examples:

>>> # Create a SpatiallyEmbeddedRNN with 2 areas
>>> area_configs = [
...     SpatiallyEmbeddedAreaConfig(
...         in_channels=1,
...         out_channels=16,
...         in_size=(64, 64),
...         feedback_channels=16,
...     ),
...     SpatiallyEmbeddedAreaConfig(
...         in_channels=16,
...         out_channels=32,
...         in_size=(32, 32),
...         feedback_channels=32,
...     ),
... ]
>>> connectivity = [
...     [0, 0],
...     [1, 0]
... ]
>>> rnn = SpatiallyEmbeddedRNN(
...     num_areas=2,
...     area_configs=area_configs,
...     inter_area_feedback_connectivity=connectivity,
... )
>>> # Create a SpatiallyEmbeddedRNN with 2 areas
>>> area_configs = [
>>>     SpatiallyEmbeddedAreaConfig(in_channels=1, out_channels=16, in_size=(64, 64)),
>>>     SpatiallyEmbeddedAreaConfig(in_channels=16, out_channels=32, in_size=(32, 32)),
>>> ]
>>> rnn = SpatiallyEmbeddedRNN(num_areas=2, area_configs=area_configs)
Source code in src/bioplnn/models/spatially_embedded.py
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
class SpatiallyEmbeddedRNN(nn.Module):
    """Spatially embedded RNN.

    This module stacks multiple SpatiallyEmbeddedArea instances with optional
    feedback connections between them.
    It handles spatial dimension matching between areas and provides a
    flexible interface for configuring the network.

    Attributes:
        num_areas: Number of SpatiallyEmbeddedArea instances in the network.
        areas: ModuleList containing the SpatiallyEmbeddedArea instances.
        feedback_convs: ModuleDict of feedback convolution layers between areas.
        area_time_delay: Whether to introduce a time delay between areas.
        pool_mode: Pooling mode for area outputs ('max' or 'avg').
        batch_first: Whether input has batch dimension as the first dimension.
        inter_area_feedback_connectivity: Connectivity matrix for feedback
            connections between areas.
        inter_area_feedback_nonlinearity: Nonlinearities for feedback
            connections between areas.
        inter_area_feedback_spatial_extents: Kernel sizes for feedback
            convolutions between areas.

    Examples:
        >>> # Create a SpatiallyEmbeddedRNN with 2 areas
        >>> area_configs = [
        ...     SpatiallyEmbeddedAreaConfig(
        ...         in_channels=1,
        ...         out_channels=16,
        ...         in_size=(64, 64),
        ...         feedback_channels=16,
        ...     ),
        ...     SpatiallyEmbeddedAreaConfig(
        ...         in_channels=16,
        ...         out_channels=32,
        ...         in_size=(32, 32),
        ...         feedback_channels=32,
        ...     ),
        ... ]
        >>> connectivity = [
        ...     [0, 0],
        ...     [1, 0]
        ... ]
        >>> rnn = SpatiallyEmbeddedRNN(
        ...     num_areas=2,
        ...     area_configs=area_configs,
        ...     inter_area_feedback_connectivity=connectivity,
        ... )
        >>> # Create a SpatiallyEmbeddedRNN with 2 areas
        >>> area_configs = [
        >>>     SpatiallyEmbeddedAreaConfig(in_channels=1, out_channels=16, in_size=(64, 64)),
        >>>     SpatiallyEmbeddedAreaConfig(in_channels=16, out_channels=32, in_size=(32, 32)),
        >>> ]
        >>> rnn = SpatiallyEmbeddedRNN(num_areas=2, area_configs=area_configs)
    """

    def __init__(
        self,
        *,
        num_areas: int = 1,
        area_configs: Optional[Sequence[SpatiallyEmbeddedAreaConfig]] = None,
        area_kwargs: Optional[Sequence[Mapping[str, Any]]] = None,
        common_area_kwargs: Optional[Mapping[str, Any]] = None,
        inter_area_feedback_connectivity: Optional[
            InterAreaParam[Union[int, bool]]
        ] = None,
        inter_area_feedback_nonlinearity: Optional[
            InterAreaParam[Union[str, nn.Module, None]]
        ] = None,
        inter_area_feedback_spatial_extents: InterAreaParam[
            tuple[int, int]
        ] = (3, 3),
        area_time_delay: bool = False,
        pool_mode: Optional[str] = "max",
        batch_first: bool = True,
    ):
        """Initialize the SpatiallyEmbeddedRNN.

        Args:
            num_areas: Number of `SpatiallyEmbeddedArea` instances in the network.
            area_configs: Configuration object(s) for the `SpatiallyEmbeddedArea` instances.
                If provided as a list, must match the number of areas. If a single config
                is provided, it will be used for all areas with appropriate adjustments.
            area_kwargs: Additional keyword arguments for each area. If provided as a list,
                must match the number of areas.
            common_area_kwargs: Keyword arguments to apply to all areas.
            inter_area_feedback_connectivity: Connectivity matrix for feedback connections
                between areas of shape (num_areas, num_areas). Must be lower triangular and
                zero/False on the diagonal.
            inter_area_feedback_nonlinearity: Nonlinearities for feedback connections of
                shape (num_areas, num_areas).
            inter_area_feedback_spatial_extents: Kernel sizes for feedback convolutions
                of shape (num_areas, num_areas).
            pool_mode: Pooling mode for area outputs.
            area_time_delay: Whether to introduce a time delay between areas.
            batch_first: Whether the input tensor has batch dimension as the first dimension.

        Raises:
            ValueError: If any of the provided parameters are invalid.
        """
        super().__init__()

        ############################################################
        # Area configs
        ############################################################

        self.num_areas = num_areas

        if area_configs is not None:
            if area_kwargs is not None or common_area_kwargs is not None:
                raise ValueError(
                    "area_configs cannot be provided if area_configs_kwargs "
                    "or common_area_config_kwargs is provided."
                )
            if len(area_configs) != self.num_areas:
                raise ValueError("area_configs must be of length num_areas.")
        else:
            if area_kwargs is None:
                if common_area_kwargs is None:
                    raise ValueError(
                        "area_kwargs or common_area_kwargs must be provided if "
                        "area_configs is not provided."
                    )
                area_kwargs = [{}] * self.num_areas  # type: ignore
            elif len(area_kwargs) != self.num_areas:
                raise ValueError("area_kwargs must be of length num_areas.")

            if common_area_kwargs is None:
                common_area_kwargs = {}

            area_configs = [
                SpatiallyEmbeddedAreaConfig(
                    **common_area_kwargs,
                    **area_kwargs[i],  # type: ignore
                )
                for i in range(self.num_areas)
            ]

        # Validate area configurations
        for i in range(self.num_areas - 1):
            if area_configs[i].out_channels != area_configs[i + 1].in_channels:
                raise ValueError(
                    f"The output channels of area {i} must match the input "
                    f"channels of area {i + 1}."
                )

        ############################################################
        # RNN parameters
        ############################################################

        self.area_time_delay = area_time_delay
        self.pool_mode = pool_mode
        self.batch_first = batch_first

        ############################################################
        # Initialize areas
        ############################################################

        # Create areas
        self.areas = nn.ModuleList(
            [
                SpatiallyEmbeddedArea(area_config)
                for area_config in area_configs
            ]
        )

        ############################################################
        # Initialize feedback connections
        ############################################################

        self.feedback_convs = nn.ModuleDict()
        if inter_area_feedback_connectivity is None:
            if any(
                area_config.feedback_channels for area_config in area_configs
            ):
                raise ValueError(
                    "inter_area_feedback_connectivity must be provided if and only if "
                    "feedback_channels is provided for at least one area."
                )
        else:
            self.inter_area_feedback_connectivity = np.array(
                inter_area_feedback_connectivity, dtype=bool
            )
            if self.inter_area_feedback_connectivity.shape != (
                self.num_areas,
                self.num_areas,
            ):
                raise ValueError(
                    "The shape of inter_area_feedback_connectivity must be (num_areas, num_areas)."
                )
            self.inter_area_feedback_nonlinearity = expand_array_2d(
                inter_area_feedback_nonlinearity,
                self.num_areas,
                self.num_areas,
            )
            self.inter_area_feedback_spatial_extents = expand_array_2d(
                inter_area_feedback_spatial_extents,
                self.num_areas,
                self.num_areas,
                depth=1,
            )

            # Validate inter_area_feedback_connectivity tensor
            if (
                self.inter_area_feedback_connectivity.ndim != 2
                or self.inter_area_feedback_connectivity.shape[0]
                != self.num_areas
                or self.inter_area_feedback_connectivity.shape[1]
                != self.num_areas
            ):
                raise ValueError(
                    "The dimensions of inter_area_feedback_connectivity must match the number of areas."
                )
            if self.inter_area_feedback_connectivity.sum() == 0:
                raise ValueError(
                    "inter_area_feedback_connectivity must be a non-zero tensor if provided."
                )

            # Create feedback convolutions
            for i, row in enumerate(self.inter_area_feedback_connectivity):
                nonzero_indices = np.nonzero(row)[0]
                for j in nonzero_indices:
                    if i <= j:
                        raise ValueError(
                            f"the feedback connection from area {i} to area {j} "
                            f"is not valid because feedback connections must "
                            f"pass information from later areas to earlier "
                            f"areas (i.e. inter_area_feedback_connectivity must "
                            f"be lower triangular and zero on the diagonal)."
                        )
                    if not self.areas[j].use_feedback:
                        raise ValueError(
                            f"the connection from area {i} to area {j} is "
                            f"not valid because area {j} does not receive "
                            f"feedback (hint: feedback_channels may not be provided)"
                        )
                    self.feedback_convs[f"{i}->{j}"] = nn.Sequential(
                        nn.Upsample(
                            size=area_configs[j].in_size,
                            mode="bilinear",
                        ),
                        nn.Conv2d(
                            in_channels=area_configs[i].out_channels,
                            out_channels=area_configs[j].feedback_channels,
                            kernel_size=self.inter_area_feedback_spatial_extents[
                                i, j
                            ],
                            padding=(
                                self.inter_area_feedback_spatial_extents[i, j][
                                    0
                                ]
                                // 2,
                                self.inter_area_feedback_spatial_extents[i, j][
                                    1
                                ]
                                // 2,
                            ),
                            bias=area_configs[
                                j
                            ].default_feedback_state_init_fn,
                        ),
                        get_activation(
                            self.inter_area_feedback_nonlinearity[i, j]
                        ),
                    )

    def init_neuron_states(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> list[list[torch.Tensor]]:
        """Initializes the hidden states for all areas.

        Args:
            batch_size: Batch size.
            init_fn: Initialization function.
            device: Device to allocate tensors.

        Returns:
            A list containing the initialized neuron hidden states for each area.
        """

        return [
            area.init_neuron_state(batch_size, init_fn, device)  # type: ignore
            for area in self.areas
        ]

    def init_feedback_states(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> list[Optional[torch.Tensor]]:
        """Initializes the feedback inputs for all areas.

        Args:
            batch_size: Batch size.
            init_fn: Initialization function.
            device: Device to allocate tensors.

        Returns:
            A list of initialized feedback inputs for each area.
        """
        return [
            area.init_feedback_state(batch_size, init_fn, device)  # type: ignore
            for area in self.areas
        ]

    def init_output_states(
        self,
        batch_size: int,
        init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> list[torch.Tensor]:
        """Initializes the outputs for all areas.

        Args:
            batch_size: Batch size.
            init_fn: Initialization function.
            device: Device to allocate tensors.

        Returns:
            A list of initialized outputs for each area.
        """

        return [
            area.init_output_state(batch_size, init_fn, device)  # type: ignore
            for area in self.areas
        ]

    def init_states(
        self,
        out0: Optional[Sequence[Union[torch.Tensor, None]]],
        h_neuron0: Optional[Sequence[Sequence[Union[torch.Tensor, None]]]],
        fb0: Optional[Sequence[Union[torch.Tensor, None]]],
        num_steps: int,
        batch_size: int,
        out_init_fn: Optional[Union[str, TensorInitFnType]] = None,
        hidden_init_fn: Optional[Union[str, TensorInitFnType]] = None,
        fb_init_fn: Optional[Union[str, TensorInitFnType]] = None,
        device: Optional[Union[torch.device, str]] = None,
    ) -> tuple[
        list[list[Union[torch.Tensor, None]]],
        list[list[list[Union[torch.Tensor, None]]]],
        list[list[Union[torch.Tensor, int, None]]],
    ]:
        """Initializes the state of the network.

        Args:
            out0: Initial outputs for each area. If None, default initialization is used.
            h_neuron0: Initial neuron hidden states for each area. If None, default
                initialization is used.
            fb0: Initial feedback inputs for each area. If None, default initialization is used.
            num_steps: Number of time steps.
            batch_size: Batch size.
            out_init_fn: Initialization function for outputs if out0 is None. Defaults to None.
            hidden_init_fn: Initialization function for hidden states if h_neuron0 is None.
                Defaults to None.
            fb_init_fn: Initialization function for feedback inputs if fb0 is None.
                Defaults to None.
            device: Device to allocate tensors on. Defaults to None.

        Returns:
            A tuple containing:
            - Initialized outputs for each area and time step
            - Initialized neuron hidden states for each area, time step, and neuron type
            - Initialized feedback inputs for each area and time step

        Raises:
            ValueError: If the length of out0, h_neuron0, or fb0 doesn't match
                the number of areas.
        """

        # Validate input shapes of out0, h_neuron0, fb0
        if out0 is None:
            out0 = [None] * self.num_areas
        elif len(out0) != self.num_areas:
            raise ValueError(
                "The length of out0 must be equal to the number of areas."
            )
        if h_neuron0 is None:
            h_neuron0 = [
                [None] * self.areas[i].num_neuron_types  # type: ignore
                for i in range(self.num_areas)
            ]
        elif len(h_neuron0) != self.num_areas:
            raise ValueError(
                "The length of h_neuron0 must be equal to the number of areas."
            )
        if fb0 is None:
            fb0 = [None] * self.num_areas
        elif len(fb0) != self.num_areas:
            raise ValueError(
                "The length of fb0 must be equal to the number of areas."
            )

        # Initialize default values
        h_neuron0_default = self.init_neuron_states(
            batch_size, hidden_init_fn, device
        )
        fb0_default = self.init_feedback_states(batch_size, fb_init_fn, device)
        out0_default = self.init_output_states(batch_size, out_init_fn, device)

        # Initialize output, hidden state, and feedback lists
        outs: list[list[Union[torch.Tensor, None]]] = [
            [None] * num_steps for _ in range(self.num_areas)
        ]
        h_neurons: list[list[list[Union[torch.Tensor, None]]]] = [
            [[None] * self.areas[i].num_neuron_types for _ in range(num_steps)]  # type: ignore
            for i in range(self.num_areas)
        ]
        fbs: list[list[Union[torch.Tensor, int, None]]] = [
            [0 if self.areas[i].use_feedback else None] * num_steps
            for i in range(self.num_areas)
        ]

        # Fill time step -1 with the initial values
        for i in range(self.num_areas):
            outs[i][-1] = out0[i] if out0[i] is not None else out0_default[i]
            fbs[i][-1] = fb0[i] if fb0[i] is not None else fb0_default[i]
            for k in range(self.areas[i].num_neuron_types):  # type: ignore
                h_neurons[i][-1][k] = (
                    h_neuron0[i][k]
                    if h_neuron0[i][k] is not None
                    else h_neuron0_default[i][k]
                )

        return outs, h_neurons, fbs

    def _format_x(
        self,
        x: torch.Tensor,
        num_steps: Optional[int] = None,
    ) -> tuple[torch.Tensor, int]:
        """Formats the input tensor to match the expected shape.

        This method handles both single-step (4D) and multi-step (5D) input tensors,
        converting them to a consistent format with seq_len as the first dimension.
        For 4D inputs, it replicates the input across all time steps.

        Args:
            x: Input tensor, can be:
                - 4D tensor of shape (batch_size, channels, height, width) for a
                  single time step. Will be expanded to all time steps.
                - 5D tensor of shape (seq_len, batch_size, channels, height, width) or
                  (batch_size, seq_len, channels, height, width) if batch_first=True.
            num_steps: Number of time steps. Required if x is 4D.
                If x is 5D, it will be inferred from the sequence dimension unless
                explicitly provided.

        Returns:
            A tuple containing:
            - The formatted input tensor with shape (seq_len, batch_size, channels, height, width)
            - The number of time steps

        Raises:
            ValueError: If x has invalid dimensions (not 4D or 5D), if num_steps is
                not provided for 4D inputs, or if num_steps is inconsistent with
                the sequence length of 5D inputs.
        """
        if x.dim() == 4:
            if num_steps is None or num_steps < 1:
                raise ValueError(
                    "If x is 4D, num_steps must be provided and greater than 0"
                )
            x = x.unsqueeze(0).expand((num_steps, -1, -1, -1, -1))
        elif x.dim() == 5:
            if self.batch_first:
                x = x.transpose(0, 1)
            if num_steps is not None and num_steps != x.shape[0]:
                raise ValueError(
                    "If x is 5D and num_steps is provided, it must match the sequence length."
                )
            num_steps = x.shape[0]
        else:
            raise ValueError(
                "The input must be a 4D tensor or a 5D tensor with sequence length."
            )

        return x, num_steps

    def _format_result(
        self,
        outs: list[list[torch.Tensor]],
        h_neurons: list[list[list[torch.Tensor]]],
        fbs: list[list[Optional[torch.Tensor]]],
    ) -> tuple[
        list[torch.Tensor],
        list[list[torch.Tensor]],
        list[Optional[torch.Tensor]],
    ]:
        """Formats the outputs, hidden states, and feedback inputs for return.

        This method stacks tensors across time steps and applies batch_first
        transposition if needed. Each tensor has shape (batch_size, channels,
        height, width).

        Args:
            outs: Outputs for each area and time step. Shape: [num_areas][num_steps],
            h_neurons: Neuron hidden states for each area, time step, and neuron type.
                Shape: [num_areas][num_steps][num_neuron_types].
            fbs: Feedback inputs for each area and time step. Shape: [num_areas][num_steps].

        Returns:
            A tuple containing:
            - List of stacked outputs per area. Each tensor has shape:
              (seq_len, batch_size, channels, height, width) or
              (batch_size, seq_len, channels, height, width) if batch_first=True.
            - List of lists of stacked hidden states per area and neuron type.
              Same shape pattern as outputs.
            - List of stacked feedback inputs per area (or None if not used).
              Same shape pattern as outputs.
        """
        outs_stack: list[torch.Tensor] = []
        h_neurons_stack: list[list[torch.Tensor]] = []
        fbs_stack: Optional[list[Optional[torch.Tensor]]] = []

        for i in range(self.num_areas):
            outs_stack.append(torch.stack(outs[i]))
            h_neurons_stack.append(
                [
                    torch.stack(
                        [h_neurons[i][t][j] for t in range(len(h_neurons[i]))]
                    )
                    for j in range(self.areas[i].num_neuron_types)  # type: ignore
                ]
            )
            if self.areas[i].use_feedback:
                fbs_stack.append(torch.stack(fbs[i]))  # type: ignore
            else:
                assert all(feedback_state is None for feedback_state in fbs[i])
                fbs_stack.append(None)
            if self.batch_first:
                outs_stack[i] = outs_stack[i].transpose(0, 1)
                for j in range(self.areas[i].num_neuron_types):  # type: ignore
                    h_neurons_stack[i][j] = h_neurons_stack[i][j].transpose(
                        0, 1
                    )
                if self.areas[i].use_feedback:
                    fbs_stack[i] = fbs_stack[i].transpose(0, 1)  # type: ignore

        return outs_stack, h_neurons_stack, fbs_stack

    def _match_spatial_size(
        self,
        x: torch.Tensor,
        size: tuple[int, int],
    ) -> torch.Tensor:
        """Adjusts the spatial dimensions of the input tensor.

        This method ensures that tensors have compatible spatial dimensions when
        passing between areas. It uses either pooling (when downsampling) or
        interpolation (when upsampling).

        Args:
            x: Input tensor of shape (batch_size, channels, height, width).
            size: Target spatial size (height, width).

        Returns:
            Resized tensor matching the target spatial size.

        Raises:
            ValueError: If self.pool_mode is not 'avg' or 'max'.
        """
        if x.shape[-2] > size[0] and x.shape[-1] > size[1]:
            if self.pool_mode == "avg":
                return F.adaptive_avg_pool2d(x, size)
            elif self.pool_mode == "max":
                return F.adaptive_max_pool2d(x, size)
            else:
                raise ValueError(f"Invalid pool_mode: {self.pool_mode}")
        elif x.shape[-2] < size[0] and x.shape[-1] < size[1]:
            return F.interpolate(x, size, mode="bilinear", align_corners=False)
        else:
            assert x.shape[-2] == size[0] and x.shape[-1] == size[1]
            return x

    def query_neuron_states(
        self,
        neuron_states: list[list[torch.Tensor]],
        area: int,
        neuron_type: int,
        time_step: Optional[Union[int, slice]] = None,
        batch: Optional[Union[int, slice]] = None,
        neuron_subtype: Optional[Union[int, slice]] = None,
        spatial_location_i: Optional[Union[int, slice]] = None,
        spatial_location_j: Optional[Union[int, slice]] = None,
    ) -> torch.Tensor:
        """Query the model states for a given area, time step, neuron type, neuron subtype, and spatial location.

        Args:
            neuron_states: List of lists of tensors containing the model states.
            area: The area index.
            neuron_type: The neuron type index.
            time_step: The time step index. If not provided, all time steps are returned.
            batch: The batch index. If not provided, all batches are returned.
            neuron_subtype: The neuron subtype index. If not provided, all neuron subtypes are returned.
            spatial_location_i: The spatial location (height). If not provided, all spatial locations are returned.
            spatial_location_j: The spatial location (width). If not provided, all spatial locations are returned.

        Returns:
            The queried state of shape (batch_size_slice, channels, height, width)
        """
        if time_step is None:
            time_idx = slice(None)
        else:
            time_idx = time_step
        if batch is None:
            batch_idx = slice(None)
        else:
            batch_idx = batch
        if neuron_subtype is None:
            neuron_subtype_idx = slice(None)
        else:
            neuron_subtype_idx = neuron_subtype
        if spatial_location_i is None:
            spatial_location_idx_i = slice(None)
        else:
            spatial_location_idx_i = spatial_location_i
        if spatial_location_j is None:
            spatial_location_idx_j = slice(None)
        else:
            spatial_location_idx_j = spatial_location_j

        if self.batch_first:
            return neuron_states[area][neuron_type][
                batch_idx,
                time_idx,
                neuron_subtype_idx,
                spatial_location_idx_i,
                spatial_location_idx_j,
            ]
        else:
            return neuron_states[area][neuron_type][
                time_idx,
                batch_idx,
                neuron_subtype_idx,
                spatial_location_idx_i,
                spatial_location_idx_j,
            ]

    def forward(
        self,
        x: torch.Tensor,
        num_steps: Optional[int] = None,
        output_state0: Optional[Sequence[Optional[torch.Tensor]]] = None,
        neuron_state0: Optional[
            Sequence[Sequence[Optional[torch.Tensor]]]
        ] = None,
        feedback_state0: Optional[Sequence[Optional[torch.Tensor]]] = None,
    ) -> tuple[
        list[torch.Tensor],
        list[list[torch.Tensor]],
        list[Union[torch.Tensor, None]],
    ]:
        """Performs forward pass of the SpatiallyEmbeddedRNN.

        Args:
            x: Input tensor. Can be either:
                - 4D tensor of shape (batch_size, in_channels, height, width)
                  representing a single time step. In this case, num_steps must be
                  provided.
                - 5D tensor of shape (seq_len, batch_size, in_channels, height, width)
                  or (batch_size, seq_len, in_channels, height, width) if
                  batch_first=True.
            num_steps: Number of time steps. Required if x is 4D.
                If x is 5D, this must match the sequence length dimension in x.
            output_state0: Initial outputs for each area. Length should match the
                number of areas. Each element can be None to use default initialization.
            neuron_state0: Initial neuron hidden states for each area and neuron type.
                Length should match the number of areas, and each inner sequence length
                should match the number of neuron types in that area.
            feedback_state0: Initial feedback inputs for each area. Length should match
                the number of areas.

        Returns:
            A tuple containing:
            - Outputs for each area. Each tensor has shape:
              (seq_len, batch_size, out_channels, height, width) or
              (batch_size, seq_len, out_channels, height, width) if batch_first=True.
            - Hidden states for each area and neuron type.
              Same shape pattern as outputs but with neuron_channels.
            - Feedback inputs for each area (None if the area doesn't use feedback).

        Raises:
            ValueError: If input shape is invalid or num_steps is inconsistent with
                the input shape.
        """

        device = x.device

        x, num_steps = self._format_x(x, num_steps)

        batch_size = x.shape[1]

        output_states, neuron_states, feedback_states = self.init_states(
            output_state0,
            neuron_state0,
            feedback_state0,
            num_steps,
            batch_size,
            device=device,
        )

        for t in range(num_steps):
            for i, area in enumerate(self.areas):
                # Compute area update and output
                if i == 0:
                    area_in = x[t]
                else:
                    if self.area_time_delay:
                        area_in = output_states[i - 1][t - 1]
                    else:
                        area_in = output_states[i - 1][t]
                    assert isinstance(area_in, torch.Tensor)
                    area_in = self._match_spatial_size(
                        area_in,
                        self.areas[i].in_size,  # type: ignore
                    )

                output_states[i][t], neuron_states[i][t] = area(
                    input=area_in,
                    neuron_state=neuron_states[i][t - 1],
                    feedback_state=feedback_states[i][t - 1],
                )

            # Apply feedback
            for key, conv in self.feedback_convs.items():
                area_i, area_j = key.split("->")
                area_i, area_j = int(area_i), int(area_j)
                feedback_states[area_j][t] = feedback_states[area_j][t] + conv(
                    output_states[area_i][t]
                )

        output_states, neuron_states, feedback_states = self._format_result(
            output_states,  # type: ignore
            neuron_states,  # type: ignore
            feedback_states,  # type: ignore
        )

        return output_states, neuron_states, feedback_states

__init__(*, num_areas=1, area_configs=None, area_kwargs=None, common_area_kwargs=None, inter_area_feedback_connectivity=None, inter_area_feedback_nonlinearity=None, inter_area_feedback_spatial_extents=(3, 3), area_time_delay=False, pool_mode='max', batch_first=True)

Initialize the SpatiallyEmbeddedRNN.

Parameters:

Name Type Description Default
num_areas int

Number of SpatiallyEmbeddedArea instances in the network.

1
area_configs Optional[Sequence[SpatiallyEmbeddedAreaConfig]]

Configuration object(s) for the SpatiallyEmbeddedArea instances. If provided as a list, must match the number of areas. If a single config is provided, it will be used for all areas with appropriate adjustments.

None
area_kwargs Optional[Sequence[Mapping[str, Any]]]

Additional keyword arguments for each area. If provided as a list, must match the number of areas.

None
common_area_kwargs Optional[Mapping[str, Any]]

Keyword arguments to apply to all areas.

None
inter_area_feedback_connectivity Optional[InterAreaParam[Union[int, bool]]]

Connectivity matrix for feedback connections between areas of shape (num_areas, num_areas). Must be lower triangular and zero/False on the diagonal.

None
inter_area_feedback_nonlinearity Optional[InterAreaParam[Union[str, Module, None]]]

Nonlinearities for feedback connections of shape (num_areas, num_areas).

None
inter_area_feedback_spatial_extents InterAreaParam[tuple[int, int]]

Kernel sizes for feedback convolutions of shape (num_areas, num_areas).

(3, 3)
pool_mode Optional[str]

Pooling mode for area outputs.

'max'
area_time_delay bool

Whether to introduce a time delay between areas.

False
batch_first bool

Whether the input tensor has batch dimension as the first dimension.

True

Raises:

Type Description
ValueError

If any of the provided parameters are invalid.

Source code in src/bioplnn/models/spatially_embedded.py
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
def __init__(
    self,
    *,
    num_areas: int = 1,
    area_configs: Optional[Sequence[SpatiallyEmbeddedAreaConfig]] = None,
    area_kwargs: Optional[Sequence[Mapping[str, Any]]] = None,
    common_area_kwargs: Optional[Mapping[str, Any]] = None,
    inter_area_feedback_connectivity: Optional[
        InterAreaParam[Union[int, bool]]
    ] = None,
    inter_area_feedback_nonlinearity: Optional[
        InterAreaParam[Union[str, nn.Module, None]]
    ] = None,
    inter_area_feedback_spatial_extents: InterAreaParam[
        tuple[int, int]
    ] = (3, 3),
    area_time_delay: bool = False,
    pool_mode: Optional[str] = "max",
    batch_first: bool = True,
):
    """Initialize the SpatiallyEmbeddedRNN.

    Args:
        num_areas: Number of `SpatiallyEmbeddedArea` instances in the network.
        area_configs: Configuration object(s) for the `SpatiallyEmbeddedArea` instances.
            If provided as a list, must match the number of areas. If a single config
            is provided, it will be used for all areas with appropriate adjustments.
        area_kwargs: Additional keyword arguments for each area. If provided as a list,
            must match the number of areas.
        common_area_kwargs: Keyword arguments to apply to all areas.
        inter_area_feedback_connectivity: Connectivity matrix for feedback connections
            between areas of shape (num_areas, num_areas). Must be lower triangular and
            zero/False on the diagonal.
        inter_area_feedback_nonlinearity: Nonlinearities for feedback connections of
            shape (num_areas, num_areas).
        inter_area_feedback_spatial_extents: Kernel sizes for feedback convolutions
            of shape (num_areas, num_areas).
        pool_mode: Pooling mode for area outputs.
        area_time_delay: Whether to introduce a time delay between areas.
        batch_first: Whether the input tensor has batch dimension as the first dimension.

    Raises:
        ValueError: If any of the provided parameters are invalid.
    """
    super().__init__()

    ############################################################
    # Area configs
    ############################################################

    self.num_areas = num_areas

    if area_configs is not None:
        if area_kwargs is not None or common_area_kwargs is not None:
            raise ValueError(
                "area_configs cannot be provided if area_configs_kwargs "
                "or common_area_config_kwargs is provided."
            )
        if len(area_configs) != self.num_areas:
            raise ValueError("area_configs must be of length num_areas.")
    else:
        if area_kwargs is None:
            if common_area_kwargs is None:
                raise ValueError(
                    "area_kwargs or common_area_kwargs must be provided if "
                    "area_configs is not provided."
                )
            area_kwargs = [{}] * self.num_areas  # type: ignore
        elif len(area_kwargs) != self.num_areas:
            raise ValueError("area_kwargs must be of length num_areas.")

        if common_area_kwargs is None:
            common_area_kwargs = {}

        area_configs = [
            SpatiallyEmbeddedAreaConfig(
                **common_area_kwargs,
                **area_kwargs[i],  # type: ignore
            )
            for i in range(self.num_areas)
        ]

    # Validate area configurations
    for i in range(self.num_areas - 1):
        if area_configs[i].out_channels != area_configs[i + 1].in_channels:
            raise ValueError(
                f"The output channels of area {i} must match the input "
                f"channels of area {i + 1}."
            )

    ############################################################
    # RNN parameters
    ############################################################

    self.area_time_delay = area_time_delay
    self.pool_mode = pool_mode
    self.batch_first = batch_first

    ############################################################
    # Initialize areas
    ############################################################

    # Create areas
    self.areas = nn.ModuleList(
        [
            SpatiallyEmbeddedArea(area_config)
            for area_config in area_configs
        ]
    )

    ############################################################
    # Initialize feedback connections
    ############################################################

    self.feedback_convs = nn.ModuleDict()
    if inter_area_feedback_connectivity is None:
        if any(
            area_config.feedback_channels for area_config in area_configs
        ):
            raise ValueError(
                "inter_area_feedback_connectivity must be provided if and only if "
                "feedback_channels is provided for at least one area."
            )
    else:
        self.inter_area_feedback_connectivity = np.array(
            inter_area_feedback_connectivity, dtype=bool
        )
        if self.inter_area_feedback_connectivity.shape != (
            self.num_areas,
            self.num_areas,
        ):
            raise ValueError(
                "The shape of inter_area_feedback_connectivity must be (num_areas, num_areas)."
            )
        self.inter_area_feedback_nonlinearity = expand_array_2d(
            inter_area_feedback_nonlinearity,
            self.num_areas,
            self.num_areas,
        )
        self.inter_area_feedback_spatial_extents = expand_array_2d(
            inter_area_feedback_spatial_extents,
            self.num_areas,
            self.num_areas,
            depth=1,
        )

        # Validate inter_area_feedback_connectivity tensor
        if (
            self.inter_area_feedback_connectivity.ndim != 2
            or self.inter_area_feedback_connectivity.shape[0]
            != self.num_areas
            or self.inter_area_feedback_connectivity.shape[1]
            != self.num_areas
        ):
            raise ValueError(
                "The dimensions of inter_area_feedback_connectivity must match the number of areas."
            )
        if self.inter_area_feedback_connectivity.sum() == 0:
            raise ValueError(
                "inter_area_feedback_connectivity must be a non-zero tensor if provided."
            )

        # Create feedback convolutions
        for i, row in enumerate(self.inter_area_feedback_connectivity):
            nonzero_indices = np.nonzero(row)[0]
            for j in nonzero_indices:
                if i <= j:
                    raise ValueError(
                        f"the feedback connection from area {i} to area {j} "
                        f"is not valid because feedback connections must "
                        f"pass information from later areas to earlier "
                        f"areas (i.e. inter_area_feedback_connectivity must "
                        f"be lower triangular and zero on the diagonal)."
                    )
                if not self.areas[j].use_feedback:
                    raise ValueError(
                        f"the connection from area {i} to area {j} is "
                        f"not valid because area {j} does not receive "
                        f"feedback (hint: feedback_channels may not be provided)"
                    )
                self.feedback_convs[f"{i}->{j}"] = nn.Sequential(
                    nn.Upsample(
                        size=area_configs[j].in_size,
                        mode="bilinear",
                    ),
                    nn.Conv2d(
                        in_channels=area_configs[i].out_channels,
                        out_channels=area_configs[j].feedback_channels,
                        kernel_size=self.inter_area_feedback_spatial_extents[
                            i, j
                        ],
                        padding=(
                            self.inter_area_feedback_spatial_extents[i, j][
                                0
                            ]
                            // 2,
                            self.inter_area_feedback_spatial_extents[i, j][
                                1
                            ]
                            // 2,
                        ),
                        bias=area_configs[
                            j
                        ].default_feedback_state_init_fn,
                    ),
                    get_activation(
                        self.inter_area_feedback_nonlinearity[i, j]
                    ),
                )

forward(x, num_steps=None, output_state0=None, neuron_state0=None, feedback_state0=None)

Performs forward pass of the SpatiallyEmbeddedRNN.

Parameters:

Name Type Description Default
x Tensor

Input tensor. Can be either: - 4D tensor of shape (batch_size, in_channels, height, width) representing a single time step. In this case, num_steps must be provided. - 5D tensor of shape (seq_len, batch_size, in_channels, height, width) or (batch_size, seq_len, in_channels, height, width) if batch_first=True.

required
num_steps Optional[int]

Number of time steps. Required if x is 4D. If x is 5D, this must match the sequence length dimension in x.

None
output_state0 Optional[Sequence[Optional[Tensor]]]

Initial outputs for each area. Length should match the number of areas. Each element can be None to use default initialization.

None
neuron_state0 Optional[Sequence[Sequence[Optional[Tensor]]]]

Initial neuron hidden states for each area and neuron type. Length should match the number of areas, and each inner sequence length should match the number of neuron types in that area.

None
feedback_state0 Optional[Sequence[Optional[Tensor]]]

Initial feedback inputs for each area. Length should match the number of areas.

None

Returns:

Type Description
list[Tensor]

A tuple containing:

list[list[Tensor]]
  • Outputs for each area. Each tensor has shape: (seq_len, batch_size, out_channels, height, width) or (batch_size, seq_len, out_channels, height, width) if batch_first=True.
list[Union[Tensor, None]]
  • Hidden states for each area and neuron type. Same shape pattern as outputs but with neuron_channels.
tuple[list[Tensor], list[list[Tensor]], list[Union[Tensor, None]]]
  • Feedback inputs for each area (None if the area doesn't use feedback).

Raises:

Type Description
ValueError

If input shape is invalid or num_steps is inconsistent with the input shape.

Source code in src/bioplnn/models/spatially_embedded.py
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
def forward(
    self,
    x: torch.Tensor,
    num_steps: Optional[int] = None,
    output_state0: Optional[Sequence[Optional[torch.Tensor]]] = None,
    neuron_state0: Optional[
        Sequence[Sequence[Optional[torch.Tensor]]]
    ] = None,
    feedback_state0: Optional[Sequence[Optional[torch.Tensor]]] = None,
) -> tuple[
    list[torch.Tensor],
    list[list[torch.Tensor]],
    list[Union[torch.Tensor, None]],
]:
    """Performs forward pass of the SpatiallyEmbeddedRNN.

    Args:
        x: Input tensor. Can be either:
            - 4D tensor of shape (batch_size, in_channels, height, width)
              representing a single time step. In this case, num_steps must be
              provided.
            - 5D tensor of shape (seq_len, batch_size, in_channels, height, width)
              or (batch_size, seq_len, in_channels, height, width) if
              batch_first=True.
        num_steps: Number of time steps. Required if x is 4D.
            If x is 5D, this must match the sequence length dimension in x.
        output_state0: Initial outputs for each area. Length should match the
            number of areas. Each element can be None to use default initialization.
        neuron_state0: Initial neuron hidden states for each area and neuron type.
            Length should match the number of areas, and each inner sequence length
            should match the number of neuron types in that area.
        feedback_state0: Initial feedback inputs for each area. Length should match
            the number of areas.

    Returns:
        A tuple containing:
        - Outputs for each area. Each tensor has shape:
          (seq_len, batch_size, out_channels, height, width) or
          (batch_size, seq_len, out_channels, height, width) if batch_first=True.
        - Hidden states for each area and neuron type.
          Same shape pattern as outputs but with neuron_channels.
        - Feedback inputs for each area (None if the area doesn't use feedback).

    Raises:
        ValueError: If input shape is invalid or num_steps is inconsistent with
            the input shape.
    """

    device = x.device

    x, num_steps = self._format_x(x, num_steps)

    batch_size = x.shape[1]

    output_states, neuron_states, feedback_states = self.init_states(
        output_state0,
        neuron_state0,
        feedback_state0,
        num_steps,
        batch_size,
        device=device,
    )

    for t in range(num_steps):
        for i, area in enumerate(self.areas):
            # Compute area update and output
            if i == 0:
                area_in = x[t]
            else:
                if self.area_time_delay:
                    area_in = output_states[i - 1][t - 1]
                else:
                    area_in = output_states[i - 1][t]
                assert isinstance(area_in, torch.Tensor)
                area_in = self._match_spatial_size(
                    area_in,
                    self.areas[i].in_size,  # type: ignore
                )

            output_states[i][t], neuron_states[i][t] = area(
                input=area_in,
                neuron_state=neuron_states[i][t - 1],
                feedback_state=feedback_states[i][t - 1],
            )

        # Apply feedback
        for key, conv in self.feedback_convs.items():
            area_i, area_j = key.split("->")
            area_i, area_j = int(area_i), int(area_j)
            feedback_states[area_j][t] = feedback_states[area_j][t] + conv(
                output_states[area_i][t]
            )

    output_states, neuron_states, feedback_states = self._format_result(
        output_states,  # type: ignore
        neuron_states,  # type: ignore
        feedback_states,  # type: ignore
    )

    return output_states, neuron_states, feedback_states

init_feedback_states(batch_size, init_fn=None, device=None)

Initializes the feedback inputs for all areas.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization function.

None
device Optional[Union[device, str]]

Device to allocate tensors.

None

Returns:

Type Description
list[Optional[Tensor]]

A list of initialized feedback inputs for each area.

Source code in src/bioplnn/models/spatially_embedded.py
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
def init_feedback_states(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> list[Optional[torch.Tensor]]:
    """Initializes the feedback inputs for all areas.

    Args:
        batch_size: Batch size.
        init_fn: Initialization function.
        device: Device to allocate tensors.

    Returns:
        A list of initialized feedback inputs for each area.
    """
    return [
        area.init_feedback_state(batch_size, init_fn, device)  # type: ignore
        for area in self.areas
    ]

init_neuron_states(batch_size, init_fn=None, device=None)

Initializes the hidden states for all areas.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization function.

None
device Optional[Union[device, str]]

Device to allocate tensors.

None

Returns:

Type Description
list[list[Tensor]]

A list containing the initialized neuron hidden states for each area.

Source code in src/bioplnn/models/spatially_embedded.py
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
def init_neuron_states(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> list[list[torch.Tensor]]:
    """Initializes the hidden states for all areas.

    Args:
        batch_size: Batch size.
        init_fn: Initialization function.
        device: Device to allocate tensors.

    Returns:
        A list containing the initialized neuron hidden states for each area.
    """

    return [
        area.init_neuron_state(batch_size, init_fn, device)  # type: ignore
        for area in self.areas
    ]

init_output_states(batch_size, init_fn=None, device=None)

Initializes the outputs for all areas.

Parameters:

Name Type Description Default
batch_size int

Batch size.

required
init_fn Optional[Union[str, TensorInitFnType]]

Initialization function.

None
device Optional[Union[device, str]]

Device to allocate tensors.

None

Returns:

Type Description
list[Tensor]

A list of initialized outputs for each area.

Source code in src/bioplnn/models/spatially_embedded.py
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
def init_output_states(
    self,
    batch_size: int,
    init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> list[torch.Tensor]:
    """Initializes the outputs for all areas.

    Args:
        batch_size: Batch size.
        init_fn: Initialization function.
        device: Device to allocate tensors.

    Returns:
        A list of initialized outputs for each area.
    """

    return [
        area.init_output_state(batch_size, init_fn, device)  # type: ignore
        for area in self.areas
    ]

init_states(out0, h_neuron0, fb0, num_steps, batch_size, out_init_fn=None, hidden_init_fn=None, fb_init_fn=None, device=None)

Initializes the state of the network.

Parameters:

Name Type Description Default
out0 Optional[Sequence[Union[Tensor, None]]]

Initial outputs for each area. If None, default initialization is used.

required
h_neuron0 Optional[Sequence[Sequence[Union[Tensor, None]]]]

Initial neuron hidden states for each area. If None, default initialization is used.

required
fb0 Optional[Sequence[Union[Tensor, None]]]

Initial feedback inputs for each area. If None, default initialization is used.

required
num_steps int

Number of time steps.

required
batch_size int

Batch size.

required
out_init_fn Optional[Union[str, TensorInitFnType]]

Initialization function for outputs if out0 is None. Defaults to None.

None
hidden_init_fn Optional[Union[str, TensorInitFnType]]

Initialization function for hidden states if h_neuron0 is None. Defaults to None.

None
fb_init_fn Optional[Union[str, TensorInitFnType]]

Initialization function for feedback inputs if fb0 is None. Defaults to None.

None
device Optional[Union[device, str]]

Device to allocate tensors on. Defaults to None.

None

Returns:

Type Description
list[list[Union[Tensor, None]]]

A tuple containing:

list[list[list[Union[Tensor, None]]]]
  • Initialized outputs for each area and time step
list[list[Union[Tensor, int, None]]]
  • Initialized neuron hidden states for each area, time step, and neuron type
tuple[list[list[Union[Tensor, None]]], list[list[list[Union[Tensor, None]]]], list[list[Union[Tensor, int, None]]]]
  • Initialized feedback inputs for each area and time step

Raises:

Type Description
ValueError

If the length of out0, h_neuron0, or fb0 doesn't match the number of areas.

Source code in src/bioplnn/models/spatially_embedded.py
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
def init_states(
    self,
    out0: Optional[Sequence[Union[torch.Tensor, None]]],
    h_neuron0: Optional[Sequence[Sequence[Union[torch.Tensor, None]]]],
    fb0: Optional[Sequence[Union[torch.Tensor, None]]],
    num_steps: int,
    batch_size: int,
    out_init_fn: Optional[Union[str, TensorInitFnType]] = None,
    hidden_init_fn: Optional[Union[str, TensorInitFnType]] = None,
    fb_init_fn: Optional[Union[str, TensorInitFnType]] = None,
    device: Optional[Union[torch.device, str]] = None,
) -> tuple[
    list[list[Union[torch.Tensor, None]]],
    list[list[list[Union[torch.Tensor, None]]]],
    list[list[Union[torch.Tensor, int, None]]],
]:
    """Initializes the state of the network.

    Args:
        out0: Initial outputs for each area. If None, default initialization is used.
        h_neuron0: Initial neuron hidden states for each area. If None, default
            initialization is used.
        fb0: Initial feedback inputs for each area. If None, default initialization is used.
        num_steps: Number of time steps.
        batch_size: Batch size.
        out_init_fn: Initialization function for outputs if out0 is None. Defaults to None.
        hidden_init_fn: Initialization function for hidden states if h_neuron0 is None.
            Defaults to None.
        fb_init_fn: Initialization function for feedback inputs if fb0 is None.
            Defaults to None.
        device: Device to allocate tensors on. Defaults to None.

    Returns:
        A tuple containing:
        - Initialized outputs for each area and time step
        - Initialized neuron hidden states for each area, time step, and neuron type
        - Initialized feedback inputs for each area and time step

    Raises:
        ValueError: If the length of out0, h_neuron0, or fb0 doesn't match
            the number of areas.
    """

    # Validate input shapes of out0, h_neuron0, fb0
    if out0 is None:
        out0 = [None] * self.num_areas
    elif len(out0) != self.num_areas:
        raise ValueError(
            "The length of out0 must be equal to the number of areas."
        )
    if h_neuron0 is None:
        h_neuron0 = [
            [None] * self.areas[i].num_neuron_types  # type: ignore
            for i in range(self.num_areas)
        ]
    elif len(h_neuron0) != self.num_areas:
        raise ValueError(
            "The length of h_neuron0 must be equal to the number of areas."
        )
    if fb0 is None:
        fb0 = [None] * self.num_areas
    elif len(fb0) != self.num_areas:
        raise ValueError(
            "The length of fb0 must be equal to the number of areas."
        )

    # Initialize default values
    h_neuron0_default = self.init_neuron_states(
        batch_size, hidden_init_fn, device
    )
    fb0_default = self.init_feedback_states(batch_size, fb_init_fn, device)
    out0_default = self.init_output_states(batch_size, out_init_fn, device)

    # Initialize output, hidden state, and feedback lists
    outs: list[list[Union[torch.Tensor, None]]] = [
        [None] * num_steps for _ in range(self.num_areas)
    ]
    h_neurons: list[list[list[Union[torch.Tensor, None]]]] = [
        [[None] * self.areas[i].num_neuron_types for _ in range(num_steps)]  # type: ignore
        for i in range(self.num_areas)
    ]
    fbs: list[list[Union[torch.Tensor, int, None]]] = [
        [0 if self.areas[i].use_feedback else None] * num_steps
        for i in range(self.num_areas)
    ]

    # Fill time step -1 with the initial values
    for i in range(self.num_areas):
        outs[i][-1] = out0[i] if out0[i] is not None else out0_default[i]
        fbs[i][-1] = fb0[i] if fb0[i] is not None else fb0_default[i]
        for k in range(self.areas[i].num_neuron_types):  # type: ignore
            h_neurons[i][-1][k] = (
                h_neuron0[i][k]
                if h_neuron0[i][k] is not None
                else h_neuron0_default[i][k]
            )

    return outs, h_neurons, fbs

query_neuron_states(neuron_states, area, neuron_type, time_step=None, batch=None, neuron_subtype=None, spatial_location_i=None, spatial_location_j=None)

Query the model states for a given area, time step, neuron type, neuron subtype, and spatial location.

Parameters:

Name Type Description Default
neuron_states list[list[Tensor]]

List of lists of tensors containing the model states.

required
area int

The area index.

required
neuron_type int

The neuron type index.

required
time_step Optional[Union[int, slice]]

The time step index. If not provided, all time steps are returned.

None
batch Optional[Union[int, slice]]

The batch index. If not provided, all batches are returned.

None
neuron_subtype Optional[Union[int, slice]]

The neuron subtype index. If not provided, all neuron subtypes are returned.

None
spatial_location_i Optional[Union[int, slice]]

The spatial location (height). If not provided, all spatial locations are returned.

None
spatial_location_j Optional[Union[int, slice]]

The spatial location (width). If not provided, all spatial locations are returned.

None

Returns:

Type Description
Tensor

The queried state of shape (batch_size_slice, channels, height, width)

Source code in src/bioplnn/models/spatially_embedded.py
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
def query_neuron_states(
    self,
    neuron_states: list[list[torch.Tensor]],
    area: int,
    neuron_type: int,
    time_step: Optional[Union[int, slice]] = None,
    batch: Optional[Union[int, slice]] = None,
    neuron_subtype: Optional[Union[int, slice]] = None,
    spatial_location_i: Optional[Union[int, slice]] = None,
    spatial_location_j: Optional[Union[int, slice]] = None,
) -> torch.Tensor:
    """Query the model states for a given area, time step, neuron type, neuron subtype, and spatial location.

    Args:
        neuron_states: List of lists of tensors containing the model states.
        area: The area index.
        neuron_type: The neuron type index.
        time_step: The time step index. If not provided, all time steps are returned.
        batch: The batch index. If not provided, all batches are returned.
        neuron_subtype: The neuron subtype index. If not provided, all neuron subtypes are returned.
        spatial_location_i: The spatial location (height). If not provided, all spatial locations are returned.
        spatial_location_j: The spatial location (width). If not provided, all spatial locations are returned.

    Returns:
        The queried state of shape (batch_size_slice, channels, height, width)
    """
    if time_step is None:
        time_idx = slice(None)
    else:
        time_idx = time_step
    if batch is None:
        batch_idx = slice(None)
    else:
        batch_idx = batch
    if neuron_subtype is None:
        neuron_subtype_idx = slice(None)
    else:
        neuron_subtype_idx = neuron_subtype
    if spatial_location_i is None:
        spatial_location_idx_i = slice(None)
    else:
        spatial_location_idx_i = spatial_location_i
    if spatial_location_j is None:
        spatial_location_idx_j = slice(None)
    else:
        spatial_location_idx_j = spatial_location_j

    if self.batch_first:
        return neuron_states[area][neuron_type][
            batch_idx,
            time_idx,
            neuron_subtype_idx,
            spatial_location_idx_i,
            spatial_location_idx_j,
        ]
    else:
        return neuron_states[area][neuron_type][
            time_idx,
            batch_idx,
            neuron_subtype_idx,
            spatial_location_idx_i,
            spatial_location_idx_j,
        ]