flycns 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
flycns/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """flycns: fly connectome releases compiled for simulation, two compound eyes, and the whole CNS in motion.
2
+
3
+ The display version (``X.XX.XXX``) lives in the ``VERSION`` file at the repository root; ``__version__`` is its
4
+ semantic form, the one the package index and ``pyproject.toml`` carry.
5
+ """
6
+
7
+ __version__ = "0.6.0"
flycns/compiled.py ADDED
@@ -0,0 +1,150 @@
1
+ """The compiled directory: the one thing both simulators read.
2
+
3
+ A compiled connectome is a directory holding ``manifest.json`` and one little-endian binary file per array. The
4
+ manifest lists every array with its dtype, shape and SHA-256, plus the release it came from, the hashes of the source
5
+ tables, the string tables the index arrays point into, and the counts the compiler measured. The reader refuses a
6
+ directory whose arrays do not match their recorded hashes: a simulation must never run on a graph that is not the one
7
+ that was compiled.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import hashlib
13
+ import json
14
+ from dataclasses import dataclass, field
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ import numpy as np
19
+
20
+ SCHEMA = "flycns.compiled/1"
21
+
22
+ #: dtypes the format allows, by name; each is written little-endian.
23
+ DTYPES = {
24
+ "int8": np.int8,
25
+ "uint8": np.uint8,
26
+ "int16": np.int16,
27
+ "uint16": np.uint16,
28
+ "int32": np.int32,
29
+ "uint32": np.uint32,
30
+ "int64": np.int64,
31
+ "float32": np.float32,
32
+ }
33
+
34
+
35
+ class CompiledError(ValueError):
36
+ """A compiled directory that cannot be trusted: a missing file, a wrong shape, or a hash that differs."""
37
+
38
+
39
+ def sha256_bytes(data: bytes) -> str:
40
+ return hashlib.sha256(data).hexdigest()
41
+
42
+
43
+ def sha256_file(path: Path, chunk: int = 1 << 24) -> str:
44
+ digest = hashlib.sha256()
45
+ with open(path, "rb") as handle:
46
+ while block := handle.read(chunk):
47
+ digest.update(block)
48
+ return digest.hexdigest()
49
+
50
+
51
+ def _dtype_name(array: np.ndarray) -> str:
52
+ for name, dtype in DTYPES.items():
53
+ if array.dtype == np.dtype(dtype):
54
+ return name
55
+ raise CompiledError(f"dtype {array.dtype} is not allowed in a compiled directory")
56
+
57
+
58
+ def write_compiled(directory: Path, arrays: dict[str, np.ndarray], meta: dict[str, Any],
59
+ schema: str = SCHEMA) -> dict[str, Any]:
60
+ """Write ``arrays`` and a manifest into ``directory``; return the manifest.
61
+
62
+ ``meta`` carries the release, sources, counts and string tables; it is stored as given under the manifest's
63
+ ``release``, ``sources``, ``counts`` and ``strings`` keys. Array files are written in name order so two
64
+ compilations of the same inputs produce the same bytes.
65
+ """
66
+ directory = Path(directory)
67
+ directory.mkdir(parents=True, exist_ok=True)
68
+ entries = []
69
+ for name in sorted(arrays):
70
+ array = np.ascontiguousarray(arrays[name])
71
+ dtype = _dtype_name(array)
72
+ data = array.astype(array.dtype.newbyteorder("<"), copy=False).tobytes(order="C")
73
+ file_name = f"{name}.bin"
74
+ (directory / file_name).write_bytes(data)
75
+ entries.append(
76
+ {"name": name, "dtype": dtype, "shape": list(array.shape), "file": file_name, "bytes": len(data),
77
+ "sha256": sha256_bytes(data)}
78
+ )
79
+ manifest = {
80
+ "schema": schema,
81
+ "release": meta.get("release", {}),
82
+ "sources": meta.get("sources", []),
83
+ "counts": meta.get("counts", {}),
84
+ "strings": meta.get("strings", {}),
85
+ "arrays": entries,
86
+ }
87
+ # LF on every system, so the same inputs give the same bytes on Windows and Linux
88
+ (directory / "manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8",
89
+ newline="\n")
90
+ return manifest
91
+
92
+
93
+ @dataclass
94
+ class Compiled:
95
+ """A verified compiled connectome: its manifest, arrays and string tables."""
96
+
97
+ directory: Path
98
+ manifest: dict[str, Any]
99
+ arrays: dict[str, np.ndarray] = field(repr=False)
100
+
101
+ @property
102
+ def strings(self) -> dict[str, list[str]]:
103
+ return self.manifest["strings"]
104
+
105
+ @property
106
+ def counts(self) -> dict[str, Any]:
107
+ return self.manifest["counts"]
108
+
109
+ @property
110
+ def n_neurons(self) -> int:
111
+ return int(self.arrays["neuron_body_id"].shape[0])
112
+
113
+ @property
114
+ def n_edges(self) -> int:
115
+ return int(self.arrays["csr_indices"].shape[0])
116
+
117
+ def __getitem__(self, name: str) -> np.ndarray:
118
+ return self.arrays[name]
119
+
120
+
121
+ def read_compiled(directory: Path, verify: bool = True, schema: str = SCHEMA) -> Compiled:
122
+ """Read a compiled directory, checking every array against the manifest.
123
+
124
+ With ``verify`` (the default) each file's SHA-256 is recomputed; any difference, a missing file or a size that
125
+ does not match the declared shape raises :class:`CompiledError`.
126
+ """
127
+ directory = Path(directory)
128
+ manifest_path = directory / "manifest.json"
129
+ if not manifest_path.is_file():
130
+ raise CompiledError(f"{directory} holds no manifest.json")
131
+ manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
132
+ if manifest.get("schema") != schema:
133
+ raise CompiledError(f"schema {manifest.get('schema')!r} is not {schema!r}")
134
+ arrays: dict[str, np.ndarray] = {}
135
+ for entry in manifest["arrays"]:
136
+ path = directory / entry["file"]
137
+ if not path.is_file():
138
+ raise CompiledError(f"array {entry['name']} is missing its file {entry['file']}")
139
+ data = path.read_bytes()
140
+ if verify and sha256_bytes(data) != entry["sha256"]:
141
+ raise CompiledError(
142
+ f"array {entry['name']}: SHA-256 {sha256_bytes(data)} differs from the manifest's {entry['sha256']}"
143
+ )
144
+ dtype = np.dtype(DTYPES[entry["dtype"]]).newbyteorder("<")
145
+ shape = tuple(entry["shape"])
146
+ expected = int(np.prod(shape, dtype=np.int64)) * dtype.itemsize
147
+ if len(data) != expected:
148
+ raise CompiledError(f"array {entry['name']}: {len(data)} bytes where shape {shape} needs {expected}")
149
+ arrays[entry["name"]] = np.frombuffer(data, dtype=dtype).reshape(shape).astype(dtype.newbyteorder("="))
150
+ return Compiled(directory=directory, manifest=manifest, arrays=arrays)
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Janne K. Lappalainen, Fabian D. Tschopp, Mason McGill, Jakob H. Macke, Srinivas C. Turaga
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,248 @@
1
+ {
2
+ "arrays": [
3
+ {
4
+ "bytes": 13000,
5
+ "dtype": "float32",
6
+ "file": "bias.bin",
7
+ "name": "bias",
8
+ "sha256": "aab655c3bb8801e3b9583b67a8299877a8278d0c23a03611f25ba46a927242ae",
9
+ "shape": [
10
+ 50,
11
+ 65
12
+ ]
13
+ },
14
+ {
15
+ "bytes": 4710,
16
+ "dtype": "int16",
17
+ "file": "group_du.bin",
18
+ "name": "group_du",
19
+ "sha256": "d317a8269e34bb7c24e5fe8b674203e5ef8821409f63842288527035d73c8083",
20
+ "shape": [
21
+ 2355
22
+ ]
23
+ },
24
+ {
25
+ "bytes": 4710,
26
+ "dtype": "int16",
27
+ "file": "group_dv.bin",
28
+ "name": "group_dv",
29
+ "sha256": "fbb9e3bd3d5d77f0cc3942dac8cfdfb93eae90a9215a991046d0d38d4246accf",
30
+ "shape": [
31
+ 2355
32
+ ]
33
+ },
34
+ {
35
+ "bytes": 9420,
36
+ "dtype": "float32",
37
+ "file": "group_n_syn.bin",
38
+ "name": "group_n_syn",
39
+ "sha256": "19ae8debe3345a1c545cabdbe8ea98e6687fc7723349d2a9f7beeb007f7ea746",
40
+ "shape": [
41
+ 2355
42
+ ]
43
+ },
44
+ {
45
+ "bytes": 4710,
46
+ "dtype": "uint16",
47
+ "file": "group_pair.bin",
48
+ "name": "group_pair",
49
+ "sha256": "ca442065d4dd5bf0b15bd4e75576616e2de00dca0e702c1ca170263cdc77bc7f",
50
+ "shape": [
51
+ 2355
52
+ ]
53
+ },
54
+ {
55
+ "bytes": 604,
56
+ "dtype": "int8",
57
+ "file": "pair_sign.bin",
58
+ "name": "pair_sign",
59
+ "sha256": "5725dc8fd49f11dfd62e51420feffb5eef4276c5bab8becc1d3fb476fa36c023",
60
+ "shape": [
61
+ 604
62
+ ]
63
+ },
64
+ {
65
+ "bytes": 1208,
66
+ "dtype": "uint16",
67
+ "file": "pair_source.bin",
68
+ "name": "pair_source",
69
+ "sha256": "e4457b0d98807d82c371726f4d46aca1e0dab4d2d39c22f45563c04859dc0e59",
70
+ "shape": [
71
+ 604
72
+ ]
73
+ },
74
+ {
75
+ "bytes": 1208,
76
+ "dtype": "uint16",
77
+ "file": "pair_target.bin",
78
+ "name": "pair_target",
79
+ "sha256": "205cf8d6bc7c7202e4bc173aeeeedb1a0d840eb751392d0a207df3a460bd64dd",
80
+ "shape": [
81
+ 604
82
+ ]
83
+ },
84
+ {
85
+ "bytes": 120800,
86
+ "dtype": "float32",
87
+ "file": "strength.bin",
88
+ "name": "strength",
89
+ "sha256": "cd36b8a4982233534bae69084faf6719e1cd32fa5b3fa7dfdbfc31ee0d6972b8",
90
+ "shape": [
91
+ 50,
92
+ 604
93
+ ]
94
+ },
95
+ {
96
+ "bytes": 13000,
97
+ "dtype": "float32",
98
+ "file": "time_const_s.bin",
99
+ "name": "time_const_s",
100
+ "sha256": "9f35013972404b1a453a7621cb6609627c04f1a48cdc5b16621ac0c5a33486a1",
101
+ "shape": [
102
+ 50,
103
+ 65
104
+ ]
105
+ }
106
+ ],
107
+ "counts": {
108
+ "groups": 2355,
109
+ "models": 50,
110
+ "pairs": 604,
111
+ "types": 65
112
+ },
113
+ "release": {
114
+ "ensemble": "flow/0000",
115
+ "model": "flyvis",
116
+ "paper": "doi:10.1038/s41586-024-07939-3",
117
+ "version": "1.2.0"
118
+ },
119
+ "schema": "flycns.compiled/1",
120
+ "sources": [
121
+ {
122
+ "checkpoints_sha256": [
123
+ "d8a57a022aa18ca338599be713af9db29b89cde4b00098ffe42b78f0d583e46a",
124
+ "f351b025e25fc02e6ab9e2a80f6fd641fc62f5b3f33210e0a2318af5cc0614ae",
125
+ "34c70cdc875f9602053f31acf03443cc071982923a9407a367666086637ae866",
126
+ "b6764982671b5f0051e682360adc52f8ed6493e478fb9c0edbf66f1d256e1501",
127
+ "d3e5ffd015020dec45ccb87c92ceb8091a8f8e65a83a553a9dc3887dd1b6072d",
128
+ "237db8bd6b015aacd54a0719014ef5a146236a6b4e2ab769b81d314907753ffa",
129
+ "6fb45c35839d04f78073b5a8e67fabcf59131fbf340925459952a5ff1de3b4ea",
130
+ "cd1b53eef47db91d3529613a98d0879a739390dcfc53d6148a6953ff9fb13469",
131
+ "e54b1f0790f97ef791b7aeee056fb88a2bfc7ab4329dd9f7dca10300eb945897",
132
+ "37c2676a83587dd7642cbbd3a0d0be219cb999d587ac1830285bdc838f4891f6",
133
+ "ed8be3c396c7a549b2d8b85cd5b14527c43eec58b5ddaae054d18ab9a2b6a300",
134
+ "bad9dabe0e9432a1f305a4544a847a1fdf7da871021f6d69b1e52a3fa7fcda67",
135
+ "d0c816909660cae35811b1631b3ff8cbd40a3acc076b6b083f4c88abbfc19f60",
136
+ "bde19f62d1a9ce2fce20fce68b72cced1a082a5192c210f4a69c89708e1476d8",
137
+ "0bcac347539e36750a00adf71e7c1236a341c4b363cf6b84c0fb6fed349cf967",
138
+ "02d2cd384f7f8040582ab33bd52fa2c44edce50ca5be27897d0f105475d819cb",
139
+ "8fbfc7d39d48eca441e0e23a735dcbedb0a3e364b3a9de48cfa651d0e7c8e7bc",
140
+ "299ac9ab1a50e5dfc5f1716627a9b09f7c5abd8820436eda63f4a178c555e1e1",
141
+ "8c738269fdbecce5da3e4cc5d44718910be1adc61fc693959792eb1817960277",
142
+ "6fb18d56f4c014ccf7a53b6914df6fe93f8f594d28b056ebe056a919d70aa596",
143
+ "03061634c6c83a2b4560005d0e97f40103ef5767d0a831b5205256bc47919ef9",
144
+ "9285061e7e0af9b9e23e16a976f3ad158808a0a708c24590de1b85c23aea0727",
145
+ "dd864e23444915ad4bc4fab9d1c401567923c31388c174d3b3caf2b7874d4383",
146
+ "41a283e29e36ad7dbf2be0604138abd7887b87a1e7d168cf17d3f21103d5b88d",
147
+ "8400223db1a0702c2ec4eb863e47918fdcc5b998f6718c4f8b0f1a41264f386a",
148
+ "3c780db8bc7c1d5fa614e2475e4dc42b2ad15375b522f6e7ed8e153fe62847e4",
149
+ "6981fb1933705bfcb13b072f4c26520e3c3af5cb7e87652efbb6877ddf68b3e9",
150
+ "5f3723c0011f6d561ecb3861b2a2b8e13ea923011fd23a10f7c33ff0cbb6b6e7",
151
+ "b19c5c1e94760ec1479bea2adba122441c2c4c95e2ca174526a922d964395b64",
152
+ "2454e817485656d040fcee69eddc30e6ff7cd587ecd6a908ef3564e03ec95835",
153
+ "2b7a878d5f4328e2a26291522d2807e5b5a47410da16c15fa0fd55c195d464f7",
154
+ "1b6a145b53375ba5eea53c30aacd1ad916f756458acdce1c11dfbf733bb6c49a",
155
+ "e8b23729a4951ab3584d8d6fb7bf9b54e4a675ab05fa4afb3d49d9dde07ea858",
156
+ "4dead734972b429850aca796eb23874e16d449036c892a99cbae0b02856b1ef6",
157
+ "7145f3e4504557d0439ab95c79a1b300fcdef9b8889c65022bd5318db7097980",
158
+ "2c59918d9bd267330d9a8c2a9f42da1dc5335901a6c1c62fdcfdc10faa3ff737",
159
+ "052b7e31cebc6b60f73e37e166911dc6889795abbaa5f38717d5374ab1d0cc6d",
160
+ "93f25c9d4dc6a6f93d7a8d19fc372a9eeedc5455b388779de8152763337baeee",
161
+ "a336ca91868486678c4dfbf871ef8f1c009c12974df20289b8697a79f1db86e0",
162
+ "a280006d540ceb2d1e6e0cd25e912500235ea79e3c258cf3f93ea518459083d7",
163
+ "67a85cdcfb55d9e59fbe750a365c044d333d782ab89c0d44d75ffe9a736fac84",
164
+ "76759a8d7d714bf7886000e41eca56b8c02aecd2df7feb630f4aa98f645f6f32",
165
+ "d57aa0a374be4f63a2e8d955a73b15fbcc72b799fcb7906ff5cd6366b42a0f6d",
166
+ "9536964c530de66dd3356e04c47d4057a2764e25a40faf04a45c8e8083612d76",
167
+ "efc1850265ed305bdc2b6c645fe8a74faa2b2ea2a238d097148ca490b83fcd80",
168
+ "19323db328787e33b91ac0473046f0e361ef4dadf0394e3f389b5359b9fdedce",
169
+ "604b2cf4dddbd7fbaefad7b350903ace541acb1c412242705dccf246cd904750",
170
+ "f5f98dbc53a94606a07e21079268b57a26950be66fe2f879fb06647a35d870fb",
171
+ "781eae13aaef429af6b98f0e4a436974a41f399ed532aabfc2d7973d28545bfe",
172
+ "b0dd6e351eed6b3ac18941fb02760c97426fe71aba1c194b0bd24a09d8d5960f"
173
+ ],
174
+ "key": "flyvis-1.2.0-pretrained-flow-0000",
175
+ "license": "MIT (Copyright (c) 2023 Janne K. Lappalainen, Fabian D. Tschopp, Mason McGill, Jakob H. Macke, Srinivas C. Turaga)",
176
+ "url": "https://github.com/TuragaLab/flyvis (results_pretrained_models.zip, via flyvis download_pretrained)"
177
+ }
178
+ ],
179
+ "strings": {
180
+ "type": [
181
+ "R1",
182
+ "R2",
183
+ "R3",
184
+ "R4",
185
+ "R5",
186
+ "R6",
187
+ "R7",
188
+ "R8",
189
+ "L1",
190
+ "L2",
191
+ "L3",
192
+ "L4",
193
+ "L5",
194
+ "Lawf1",
195
+ "Lawf2",
196
+ "Am",
197
+ "C2",
198
+ "C3",
199
+ "CT1(Lo1)",
200
+ "CT1(M10)",
201
+ "Mi1",
202
+ "Mi2",
203
+ "Mi3",
204
+ "Mi4",
205
+ "Mi9",
206
+ "Mi10",
207
+ "Mi11",
208
+ "Mi12",
209
+ "Mi13",
210
+ "Mi14",
211
+ "Mi15",
212
+ "T1",
213
+ "T2",
214
+ "T2a",
215
+ "T3",
216
+ "T4a",
217
+ "T4b",
218
+ "T4c",
219
+ "T4d",
220
+ "T5a",
221
+ "T5b",
222
+ "T5c",
223
+ "T5d",
224
+ "Tm1",
225
+ "Tm2",
226
+ "Tm3",
227
+ "Tm4",
228
+ "Tm5Y",
229
+ "Tm5a",
230
+ "Tm5b",
231
+ "Tm5c",
232
+ "Tm9",
233
+ "Tm16",
234
+ "Tm20",
235
+ "Tm28",
236
+ "Tm30",
237
+ "TmY3",
238
+ "TmY4",
239
+ "TmY5a",
240
+ "TmY9",
241
+ "TmY10",
242
+ "TmY13",
243
+ "TmY14",
244
+ "TmY15",
245
+ "TmY18"
246
+ ]
247
+ }
248
+ }
@@ -0,0 +1 @@
1
+ ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
@@ -0,0 +1,18 @@
1
+ """Neuron dynamics: the published spiking model, the graded optic lobe, and their coupling."""
2
+
3
+ from .graded import GradedNetwork, GradedReference, GradedTorch
4
+ from .hybrid import HybridNetwork, HybridReference, HybridTorch, build_hybrid
5
+ from .lif import (
6
+ Drive,
7
+ LIFParams,
8
+ LIFReference,
9
+ LIFTorch,
10
+ Run,
11
+ photoreceptor_drive,
12
+ stabilised_weights,
13
+ synaptic_weights,
14
+ )
15
+
16
+ __all__ = ["Drive", "GradedNetwork", "GradedReference", "GradedTorch", "HybridNetwork", "HybridReference",
17
+ "HybridTorch", "LIFParams", "LIFReference", "LIFTorch", "Run", "build_hybrid", "photoreceptor_drive",
18
+ "stabilised_weights", "synaptic_weights"]
@@ -0,0 +1,184 @@
1
+ """Graded (non-spiking) point neurons, as flyvis computes them (Lappalainen et al., Nature 2024).
2
+
3
+ ``PPNeuronIGRSynapses`` in flyvis 1.2.0: passive point neurons with instantaneous graded release,
4
+
5
+ tau_eff dV/dt = -V + b + sum_j w_ij max(V_j, 0) + x, tau_eff = max(tau, dt),
6
+
7
+ integrated by forward Euler. ``GradedReference`` (NumPy, float64) and ``GradedTorch`` (PyTorch, float32) take the
8
+ same network and give the same run; both are held to flyvis's own recording of its network 000 (see
9
+ ``docs/design/features/graded/design.md``).
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from dataclasses import dataclass
15
+
16
+ import numpy as np
17
+
18
+
19
+ @dataclass
20
+ class GradedNetwork:
21
+ """A network of graded neurons.
22
+
23
+ Light enters through ``input_neuron``: each of those neurons receives the intensity of the column
24
+ ``input_column`` names (flyvis adds the same intensity to R1 to R8 of a column; on MaleCNS a column holds as many
25
+ photoreceptors as the release reconstructed there, plus stand-ins).
26
+ """
27
+
28
+ bias: np.ndarray # (n,) resting potential of each neuron
29
+ time_const_s: np.ndarray # (n,) time constant of each neuron, seconds
30
+ source: np.ndarray # (e,) presynaptic neuron of each connection
31
+ target: np.ndarray # (e,) postsynaptic neuron of each connection
32
+ weight: np.ndarray # (e,) sign x synapse count x unitary strength
33
+ input_neuron: np.ndarray # (k,) the neurons that receive light
34
+ input_column: np.ndarray # (k,) the column each of them sees
35
+ n_columns: int
36
+
37
+ @classmethod
38
+ def from_input_index(cls, bias, time_const_s, source, target, weight, input_index) -> GradedNetwork:
39
+ """flyvis's layout: ``input_index`` has one row per input type and one column per eye column."""
40
+ input_index = np.asarray(input_index)
41
+ types, columns = input_index.shape
42
+ return cls(bias=bias, time_const_s=time_const_s, source=source, target=target, weight=weight,
43
+ input_neuron=input_index.reshape(-1), input_column=np.tile(np.arange(columns), types),
44
+ n_columns=columns)
45
+
46
+ @property
47
+ def n(self) -> int:
48
+ return len(self.bias)
49
+
50
+ def column_current(self, intensity: np.ndarray) -> np.ndarray:
51
+ """Per-neuron input for one frame of per-column intensities (zero off the input neurons)."""
52
+ current = np.zeros(self.n)
53
+ np.add.at(current, np.asarray(self.input_neuron, dtype=np.int64),
54
+ np.asarray(intensity, dtype=np.float64)[np.asarray(self.input_column, dtype=np.int64)])
55
+ return current
56
+
57
+
58
+ class GradedReference:
59
+ """The NumPy engine: float64 state; each neuron's input summed with ``bincount`` in connection order."""
60
+
61
+ def __init__(self, network: GradedNetwork, dt_s: float):
62
+ self.net = network
63
+ self.dt = float(dt_s)
64
+ self.bias = np.asarray(network.bias, dtype=np.float64)
65
+ self.rate = 1.0 / np.maximum(np.asarray(network.time_const_s, dtype=np.float64), self.dt)
66
+ self.source = np.asarray(network.source, dtype=np.int64)
67
+ self.target = np.asarray(network.target, dtype=np.int64)
68
+ self.weight = np.asarray(network.weight, dtype=np.float64)
69
+
70
+ def column_current(self, intensity: np.ndarray) -> np.ndarray:
71
+ return self.net.column_current(intensity)
72
+
73
+ def synaptic_input(self, v: np.ndarray) -> np.ndarray:
74
+ release = np.maximum(v[self.source], 0.0)
75
+ return np.bincount(self.target, weights=self.weight * release, minlength=self.net.n)
76
+
77
+ def step(self, v: np.ndarray, current: np.ndarray) -> np.ndarray:
78
+ """One Euler step with a per-neuron input ``current``."""
79
+ velocity = self.rate * (-v + self.bias + self.synaptic_input(v) + current)
80
+ return v + velocity * self.dt
81
+
82
+ def steady_state(self, t_pre_s: float, grey: float = 0.5, initial: np.ndarray | None = None) -> np.ndarray:
83
+ """The state after ``t_pre_s`` of uniform intensity ``grey``, from the resting potentials unless given."""
84
+ v = self.bias.copy() if initial is None else np.asarray(initial, dtype=np.float64).copy()
85
+ current = self.net.column_current(np.full(self.net.n_columns, grey))
86
+ for _ in range(int(t_pre_s / self.dt)):
87
+ v = self.step(v, current)
88
+ return v
89
+
90
+ def run(self, intensity: np.ndarray, initial: np.ndarray | None = None,
91
+ record: np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
92
+ """Step through frames of per-column intensity (frames x columns); returns (final state, activity), the
93
+ activity after each step for the neurons in ``record`` (all when omitted), as float32."""
94
+ v = self.bias.copy() if initial is None else np.asarray(initial, dtype=np.float64).copy()
95
+ record = np.arange(self.net.n) if record is None else np.asarray(record)
96
+ out = np.zeros((len(intensity), len(record)), dtype=np.float32)
97
+ for k, frame in enumerate(np.asarray(intensity)):
98
+ v = self.step(v, self.net.column_current(frame))
99
+ out[k] = v[record]
100
+ return v, out
101
+
102
+
103
+ class GradedTorch:
104
+ """The same dynamics in PyTorch (float32), for the GPU."""
105
+
106
+ def __init__(self, network: GradedNetwork, dt_s: float, device: str | None = None):
107
+ import torch
108
+
109
+ self.torch = torch
110
+ self.net = network
111
+ self.dt = float(dt_s)
112
+ self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
113
+ as_f32 = lambda a: torch.as_tensor(np.asarray(a, dtype=np.float32), device=self.device) # noqa: E731
114
+ as_i64 = lambda a: torch.as_tensor(np.asarray(a, dtype=np.int64), device=self.device) # noqa: E731
115
+ self.bias = as_f32(network.bias)
116
+ self.rate = 1.0 / torch.clamp(as_f32(network.time_const_s), min=self.dt)
117
+ self.source = as_i64(network.source)
118
+ self.target = as_i64(network.target)
119
+ self.weight = as_f32(network.weight)
120
+ self.input_neuron = as_i64(network.input_neuron)
121
+ self.input_column = as_i64(network.input_column)
122
+
123
+ def column_current(self, intensity):
124
+ torch = self.torch
125
+ current = torch.zeros(self.net.n, device=self.device)
126
+ frame = torch.as_tensor(np.asarray(intensity, dtype=np.float32), device=self.device)
127
+ return current.index_add_(0, self.input_neuron, frame[self.input_column])
128
+
129
+ def step(self, v, current):
130
+ synaptic = self.torch.zeros_like(v).index_add_(0, self.target, self.weight * self.torch.relu(v[self.source]))
131
+ return v + self.rate * (-v + self.bias + synaptic + current) * self.dt
132
+
133
+ def steady_state(self, t_pre_s: float, grey: float = 0.5, initial=None):
134
+ torch = self.torch
135
+ v = self.bias.clone() if initial is None else torch.as_tensor(np.asarray(initial, dtype=np.float32),
136
+ device=self.device).clone()
137
+ current = self.column_current(np.full(self.net.n_columns, grey))
138
+ for _ in range(int(t_pre_s / self.dt)):
139
+ v = self.step(v, current)
140
+ return v
141
+
142
+ def run(self, intensity: np.ndarray, initial=None, record: np.ndarray | None = None):
143
+ torch = self.torch
144
+ v = self.bias.clone() if initial is None else torch.as_tensor(np.asarray(initial, dtype=np.float32),
145
+ device=self.device).clone()
146
+ record_t = torch.as_tensor(np.arange(self.net.n) if record is None else np.asarray(record),
147
+ device=self.device)
148
+ out = torch.zeros((len(intensity), len(record_t)), device=self.device)
149
+ for k, frame in enumerate(np.asarray(intensity)):
150
+ v = self.step(v, self.column_current(frame))
151
+ out[k] = v[record_t]
152
+ return v.cpu().numpy(), out.cpu().numpy()
153
+
154
+ def run_batch(self, intensity: np.ndarray, initial, record: np.ndarray) -> np.ndarray:
155
+ """Several stimuli at once: ``intensity`` is (stimuli, frames, columns), every stimulus starts from
156
+ ``initial``; returns the recorded neurons after each step, (stimuli, frames, recorded), as float32. Each
157
+ stimulus is the same computation as ``run``; batching only shares the kernel launches."""
158
+ torch = self.torch
159
+ stimuli = torch.as_tensor(np.asarray(intensity, dtype=np.float32), device=self.device)
160
+ batch, frames, _ = stimuli.shape
161
+ matrix = self._matrix()
162
+ v = torch.as_tensor(np.asarray(initial, dtype=np.float32), device=self.device)[:, None].repeat(1, batch)
163
+ record_t = torch.as_tensor(np.asarray(record, dtype=np.int64), device=self.device)
164
+ out = torch.zeros((batch, frames, len(record_t)), device=self.device)
165
+ rate, bias = self.rate[:, None], self.bias[:, None]
166
+ for k in range(frames): # state is (neurons, stimuli)
167
+ current = torch.zeros_like(v).index_add_(0, self.input_neuron, stimuli[:, k, self.input_column].T)
168
+ synaptic = matrix @ torch.relu(v)
169
+ v = v + rate * (-v + bias + synaptic + current) * self.dt
170
+ out[:, k] = v[record_t].T
171
+ return out.cpu().numpy()
172
+
173
+ def _matrix(self):
174
+ """The weights as a sparse (target x source) CSR matrix, built once: batches multiply by it instead of
175
+ materialising one release per connection and stimulus."""
176
+ if getattr(self, "_csr", None) is None:
177
+ torch = self.torch
178
+ order = torch.argsort(self.target * self.net.n + self.source)
179
+ rows = torch.bincount(self.target[order], minlength=self.net.n)
180
+ crow = torch.zeros(self.net.n + 1, dtype=torch.int64, device=self.device)
181
+ crow[1:] = torch.cumsum(rows, 0)
182
+ self._csr = torch.sparse_csr_tensor(crow, self.source[order], self.weight[order],
183
+ size=(self.net.n, self.net.n))
184
+ return self._csr