Record state variables of all layers and connections.
| 131 | |
| 132 | |
| 133 | class NetworkMonitor(AbstractMonitor): |
| 134 | # language=rst |
| 135 | """ |
| 136 | Record state variables of all layers and connections. |
| 137 | """ |
| 138 | |
| 139 | def __init__( |
| 140 | self, |
| 141 | network: "Network", |
| 142 | layers: Optional[Iterable[str]] = None, |
| 143 | connections: Optional[Iterable[str]] = None, |
| 144 | state_vars: Optional[Iterable[str]] = None, |
| 145 | time: Optional[int] = None, |
| 146 | ): |
| 147 | # language=rst |
| 148 | """ |
| 149 | Constructs a ``NetworkMonitor`` object. |
| 150 | |
| 151 | :param network: Network to record state variables from. |
| 152 | :param layers: Layers to record state variables from. |
| 153 | :param connections: Connections to record state variables from. |
| 154 | :param state_vars: List of strings indicating names of state variables to |
| 155 | record. |
| 156 | :param time: If not ``None``, pre-allocate memory for state variable recording. |
| 157 | """ |
| 158 | super().__init__() |
| 159 | |
| 160 | self.network = network |
| 161 | self.layers = layers if layers is not None else list(self.network.layers.keys()) |
| 162 | self.connections = ( |
| 163 | connections |
| 164 | if connections is not None |
| 165 | else list(self.network.connections.keys()) |
| 166 | ) |
| 167 | self.state_vars = state_vars if state_vars is not None else ("v", "s", "w") |
| 168 | self.time = time |
| 169 | |
| 170 | if self.time is not None: |
| 171 | self.i = 0 |
| 172 | |
| 173 | # Initialize empty recording. |
| 174 | self.recording = {k: {} for k in self.layers + self.connections} |
| 175 | |
| 176 | # If no simulation time is specified, specify 0-dimensional recordings. |
| 177 | if self.time is None: |
| 178 | for v in self.state_vars: |
| 179 | for l in self.layers: |
| 180 | if hasattr(self.network.layers[l], v): |
| 181 | self.recording[l][v] = torch.Tensor() |
| 182 | |
| 183 | for c in self.connections: |
| 184 | if hasattr(self.network.connections[c], v): |
| 185 | self.recording[c][v] = torch.Tensor() |
| 186 | |
| 187 | # If simulation time is specified, pre-allocate recordings in memory for speed. |
| 188 | else: |
| 189 | for v in self.state_vars: |
| 190 | for l in self.layers: |