-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
1507 lines (1379 loc) · 62.8 KB
/
Copy pathapp.py
File metadata and controls
1507 lines (1379 loc) · 62.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
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
957
958
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
# -*- coding: utf-8 -*-
"""
FlyBrain Lab Pro
================
Interactive explorer + structural simulator for large FlyWire/FAFB datasets.
Design goals
------------
* Reads your dataset folders automatically.
* Never loads the whole ~13GB dataset into browser RAM.
* Materializes only compact connection/label/coordinate indexes in DuckDB.
* Reads SWC morphology on demand.
* Reads synapse coordinates on demand from raw CSV partitions.
* Persian UI + Vazirmatn font in browser.
* Structural graph is data-driven; simulation is explicitly approximate.
Run:
pip install -r requirements.txt
python app.py run --data "D:\\path\\to\\dataset"
First run prepares a local cache DB under:
<dataset>\\.flybrain_lab\\flybrain.duckdb
"""
from __future__ import annotations
import argparse
import csv
import json
import math
import os
import re
import sys
import time
import hashlib
import threading
import webbrowser
from collections import deque
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Iterable
import duckdb
from flask import Flask, jsonify, request, render_template, Response, send_file
from swc_atlas import SWCAtlas
from knowledge_fa import explain_neuron_fa
APP_VERSION = "3.1.1"
# ---------------------------------------------------------------------
# Dataset discovery
# ---------------------------------------------------------------------
ROLE_NAMES = {
"connections_buhmann_no_threshold": "connections_buhmann_no_threshold.csv",
"connections_princeton": "connections_princeton.csv",
"connections_princeton_no_threshold": "connections_princeton_no_threshold.csv",
"connectivity_tags": "connectivity_tags.csv",
"coordinates": "coordinates.csv",
"synapses_princeton": "fafb_v783_princeton_synapse_table.csv",
"labels": "labels.csv",
"neuropil_synapses": "neuropil_synapse_table.csv",
"processed_labels": "processed_labels.csv",
"skeletons": "sk_lod1_783_healed",
}
PRE_CANDS = [
"pre_root_id","pre_pt_root_id","pre","source","source_id","upstream_root_id",
"presynaptic_root_id","pre_root","pre_id"
]
POST_CANDS = [
"post_root_id","post_pt_root_id","post","target","target_id","downstream_root_id",
"postsynaptic_root_id","post_root","post_id"
]
WEIGHT_CANDS = [
"syn_count","synapse_count","weight","n_synapses","count","synapses","n"
]
NT_CANDS = ["nt_type","neurotransmitter","predicted_nt","nt","transmitter"]
ID_CANDS = ["root_id","pt_root_id","neuron_id","id","root","cell_id"]
LABEL_CANDS = ["label","name","cell_type","type","hemibrain_type","community_label","processed_label"]
TAG_CANDS = ["tag","connectivity_tag","tags","label","value"]
X_CANDS = ["x","pt_x","x_nm","x_coord","x_position","pre_x","post_x"]
Y_CANDS = ["y","pt_y","y_nm","y_coord","y_position","pre_y","post_y"]
Z_CANDS = ["z","pt_z","z_nm","z_coord","z_position","pre_z","post_z"]
def norm(s: str) -> str:
return re.sub(r"[^a-z0-9]+", "_", str(s).strip().lower()).strip("_")
def qident(name: str) -> str:
return '"' + name.replace('"', '""') + '"'
def sql_str(s: str) -> str:
return "'" + str(s).replace("'", "''") + "'"
def choose_col(cols: List[str], candidates: List[str], allow_prefix: bool = True) -> Optional[str]:
"""Conservative matcher: exact first; safe long-prefix second."""
cmap = {norm(c): c for c in cols}
for cand in candidates:
nc = norm(cand)
if nc in cmap:
return cmap[nc]
if not allow_prefix:
return None
for cand in candidates:
nc = norm(cand)
if len(nc) < 4:
continue
matches = [(k, original) for k, original in cmap.items()
if k.startswith(nc + "_")]
if matches:
matches.sort(key=lambda x: len(x[0]))
return matches[0][1]
return None
def list_csv_files(p: Path) -> List[Path]:
if p.is_file() and p.suffix.lower() == ".csv":
return [p]
if p.is_dir():
return sorted(x for x in p.rglob("*.csv") if x.is_file())
return []
def discover_dataset(data_root: Path) -> Dict[str, dict]:
data_root = data_root.resolve()
out: Dict[str, dict] = {}
# exact names first
all_nodes = [data_root] + list(data_root.rglob("*"))
by_name = {}
for p in all_nodes:
by_name.setdefault(p.name.lower(), []).append(p)
for role, expected in ROLE_NAMES.items():
matches = by_name.get(expected.lower(), [])
if matches:
p = matches[0]
if role == "skeletons":
out[role] = {"path": str(p), "kind": "swc_dir"}
else:
files = list_csv_files(p)
if files:
out[role] = {"path": str(p), "kind": "csv", "files": [str(x) for x in files]}
# fuzzy fallback for missing roles
if "skeletons" not in out:
swc_dirs = {}
for p in data_root.rglob("*.swc"):
swc_dirs[p.parent] = swc_dirs.get(p.parent, 0) + 1
if swc_dirs:
best = max(swc_dirs.items(), key=lambda kv: kv[1])[0]
out["skeletons"] = {"path": str(best), "kind": "swc_dir"}
return out
def duckdb_csv_expr(files: List[str]) -> str:
"""Return a safe DuckDB read_csv_auto expression for file(s)."""
if not files:
raise ValueError("No CSV files")
quoted = "[" + ",".join(sql_str(str(Path(f).resolve()).replace("\\","/")) for f in files) + "]"
return (
f"read_csv_auto({quoted}, union_by_name=true, header=true, "
f"sample_size=50000, ignore_errors=true, null_padding=true, "
f"parallel=false)"
)
def probe_columns(con, files: List[str]) -> List[str]:
expr = duckdb_csv_expr(files[:min(len(files), 25)])
rows = con.execute(f"DESCRIBE SELECT * FROM {expr}").fetchall()
return [r[0] for r in rows]
# ---------------------------------------------------------------------
# Cache preparation
# ---------------------------------------------------------------------
class FlyDataset:
def __init__(self, data_root: Path):
self.data_root = data_root.resolve()
self.cache_dir = self.data_root / ".flybrain_lab"
self.cache_dir.mkdir(exist_ok=True)
self.db_path = self.cache_dir / "flybrain.duckdb"
self.catalog_path = self.cache_dir / "catalog.json"
self.catalog: Dict[str, dict] = {}
self._swc_index: Dict[str, str] = {}
# Full-brain SWC LOD atlas cache. This must be initialized before
# CLI/API code can call ds.atlas.build/status/batch/selection.
self.atlas = SWCAtlas(self.cache_dir)
self._processed_label_cache: Dict[str, List[str]] = {}
self._neuropil_cache: Dict[str, dict] = {}
self._neuron_full_cache: Dict[tuple, dict] = {}
self._load_or_discover()
def con(self):
c = duckdb.connect(str(self.db_path))
c.execute("PRAGMA threads=4")
c.execute("PRAGMA enable_progress_bar=false")
return c
def _load_or_discover(self):
if self.catalog_path.exists():
try:
self.catalog = json.loads(self.catalog_path.read_text(encoding="utf-8"))
except Exception:
self.catalog = {}
fresh = discover_dataset(self.data_root)
# Keep schema metadata if same role still exists.
for role, info in fresh.items():
old = self.catalog.get(role, {})
old.update(info)
fresh[role] = old
self.catalog = fresh
self.catalog_path.write_text(json.dumps(self.catalog, ensure_ascii=False, indent=2), encoding="utf-8")
def status(self):
prepared = self.db_path.exists()
return {
"version": APP_VERSION,
"data_root": str(self.data_root),
"prepared": prepared,
"roles": {
k: {
"path": v.get("path"),
"files": len(v.get("files", [])) if v.get("kind") == "csv" else None,
"schema": v.get("schema")
} for k,v in self.catalog.items()
}
}
def prepare(self, force=False, verbose=True):
con = self.con()
try:
con.execute("""
CREATE TABLE IF NOT EXISTS app_meta(
key VARCHAR PRIMARY KEY,
value VARCHAR
)
""")
# Probe schemas
for role, info in self.catalog.items():
if info.get("kind") != "csv":
continue
try:
cols = probe_columns(con, info["files"])
info["columns"] = cols
schema = {}
if role.startswith("connections_"):
schema["pre"] = choose_col(cols, PRE_CANDS)
schema["post"] = choose_col(cols, POST_CANDS)
schema["weight"] = choose_col(cols, WEIGHT_CANDS, allow_prefix=False)
schema["nt"] = choose_col(cols, NT_CANDS, allow_prefix=False)
elif role == "synapses_princeton":
schema["pre"] = choose_col(cols, PRE_CANDS)
schema["post"] = choose_col(cols, POST_CANDS)
schema["weight"] = choose_col(cols, WEIGHT_CANDS, allow_prefix=False)
schema["nt"] = choose_col(cols, NT_CANDS, allow_prefix=False)
schema["x"] = choose_col(cols, X_CANDS, allow_prefix=False)
schema["y"] = choose_col(cols, Y_CANDS, allow_prefix=False)
schema["z"] = choose_col(cols, Z_CANDS, allow_prefix=False)
schema["neuropil"] = choose_col(cols, ["neuropil","region","brain_region"], allow_prefix=False)
elif role == "neuropil_synapses":
schema["id"] = choose_col(cols, ID_CANDS)
schema["neuropil"] = choose_col(cols, ["neuropil","region","brain_region"], allow_prefix=False)
schema["input_synapses"] = choose_col(cols, ["input_synapses","input synapses","inputs"], allow_prefix=False)
schema["output_synapses"] = choose_col(cols, ["output_synapses","output synapses","outputs"], allow_prefix=False)
if role in ("labels","processed_labels","connectivity_tags","coordinates"):
schema["id"] = choose_col(cols, ID_CANDS)
if role in ("labels","processed_labels"):
schema["label"] = choose_col(cols, LABEL_CANDS, allow_prefix=False)
if role == "processed_labels" and not schema["label"]:
schema["label"] = choose_col(cols, ["processed_labels"], allow_prefix=False)
text_cols = []
for c in cols:
nc = norm(c)
if any(k in nc for k in ("label","name","type","class","side","flow","lineage")):
text_cols.append(c)
schema["display_cols"] = text_cols[:12]
if role == "connectivity_tags":
schema["tag"] = choose_col(cols, TAG_CANDS, allow_prefix=False)
if role == "coordinates":
schema["x"] = choose_col(cols, X_CANDS, allow_prefix=False)
schema["y"] = choose_col(cols, Y_CANDS, allow_prefix=False)
schema["z"] = choose_col(cols, Z_CANDS, allow_prefix=False)
schema["position"] = choose_col(cols, ["position","coordinate","coordinates","xyz"], allow_prefix=False)
info["schema"] = schema
if verbose:
print(f"[schema] {role}: {schema}")
except Exception as e:
info["schema_error"] = str(e)
if verbose:
print(f"[warn] schema {role}: {e}")
self.catalog_path.write_text(json.dumps(self.catalog, ensure_ascii=False, indent=2), encoding="utf-8")
# Build compact standardized connection tables.
conn_roles = [r for r in self.catalog if r.startswith("connections_")]
for role in conn_roles:
info = self.catalog[role]
s = info.get("schema") or {}
pre, post = s.get("pre"), s.get("post")
if not pre or not post:
print(f"[skip] {role}: pre/post columns not detected")
continue
table = "c_" + role
exists = con.execute(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_name=?",
[table]
).fetchone()[0] > 0
if exists and not force:
if verbose: print(f"[ok] {table} already prepared")
continue
if verbose:
print(f"\n[build] {table} ... this may take time on first run.")
con.execute(f"DROP TABLE IF EXISTS {qident(table)}")
expr = duckdb_csv_expr(info["files"])
weight_expr = f"TRY_CAST({qident(s['weight'])} AS DOUBLE)" if s.get("weight") else "1.0"
nt_expr = f"CAST({qident(s['nt'])} AS VARCHAR)" if s.get("nt") else "NULL"
query = f"""
CREATE TABLE {qident(table)} AS
SELECT
CAST({qident(pre)} AS VARCHAR) AS pre_id,
CAST({qident(post)} AS VARCHAR) AS post_id,
SUM(COALESCE({weight_expr},1.0))::DOUBLE AS syn_count,
any_value({nt_expr}) AS nt_type
FROM {expr}
WHERE {qident(pre)} IS NOT NULL AND {qident(post)} IS NOT NULL
GROUP BY 1,2
"""
t0 = time.time()
con.execute(query)
con.execute(f"CREATE INDEX IF NOT EXISTS {qident('idx_'+table+'_pre')} ON {qident(table)}(pre_id)")
con.execute(f"CREATE INDEX IF NOT EXISTS {qident('idx_'+table+'_post')} ON {qident(table)}(post_id)")
n = con.execute(f"SELECT COUNT(*) FROM {qident(table)}").fetchone()[0]
if verbose: print(f"[done] {table}: {n:,} edges in {time.time()-t0:.1f}s")
self._prepare_labels(con, force, verbose)
self._prepare_coordinates(con, force, verbose)
self._prepare_tags(con, force, verbose)
self._prepare_swc_index(force, verbose)
con.execute("INSERT OR REPLACE INTO app_meta VALUES ('prepared_at', ?)", [str(time.time())])
con.execute("INSERT OR REPLACE INTO app_meta VALUES ('version', ?)", [APP_VERSION])
finally:
con.close()
def _meta_ready(self, con, key: str) -> bool:
try:
row = con.execute("SELECT value FROM app_meta WHERE key=?", [key]).fetchone()
return bool(row and row[0] == "1")
except Exception:
return False
def _set_meta_ready(self, con, key: str):
con.execute("INSERT OR REPLACE INTO app_meta VALUES (?, '1')", [key])
def _prepare_labels(self, con, force, verbose):
sources = [r for r in ("processed_labels","labels") if r in self.catalog]
if not sources:
return
exists = con.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name='neuron_labels'").fetchone()[0] > 0
if exists and not force and self._meta_ready(con, "neuron_labels_ready"):
return
con.execute("DROP TABLE IF EXISTS neuron_labels")
con.execute("CREATE TABLE neuron_labels(root_id VARCHAR, label VARCHAR, source VARCHAR, details VARCHAR)")
for role in sources:
info = self.catalog[role]; s = info.get("schema") or {}
rid = s.get("id")
if not rid: continue
expr = duckdb_csv_expr(info["files"])
display_cols = s.get("display_cols") or ([s.get("label")] if s.get("label") else [])
display_cols = [c for c in display_cols if c]
if not display_cols:
continue
label_col = s.get("label") or display_cols[0]
# DuckDB JSON object would be overkill; concatenate useful fields.
pieces = []
for c in display_cols[:8]:
pieces.append(f"{sql_str(c+'=')} || COALESCE(CAST({qident(c)} AS VARCHAR),'')")
details = " || ' | ' || ".join(pieces) if pieces else "''"
con.execute(f"""
INSERT INTO neuron_labels
SELECT CAST({qident(rid)} AS VARCHAR),
COALESCE(CAST({qident(label_col)} AS VARCHAR),''),
{sql_str(role)},
{details}
FROM {expr}
WHERE {qident(rid)} IS NOT NULL
""")
con.execute("CREATE INDEX IF NOT EXISTS idx_labels_root ON neuron_labels(root_id)")
self._set_meta_ready(con, "neuron_labels_ready")
if verbose:
n = con.execute("SELECT COUNT(*) FROM neuron_labels").fetchone()[0]
print(f"[done] neuron_labels: {n:,}")
def _position_sql_expr(self, colname: str, axis: int) -> str:
"""
Extract x/y/z from a generic position string such as:
[123,456,789], (123, 456, 789), POINT(123 456 789),
or other text containing at least three numbers.
DuckDB regexp_extract_all returns a list of numeric-looking strings.
"""
q = qident(colname)
idx = axis + 1 # DuckDB list indexes are 1-based
# numeric regex including optional sign/decimal/exponent
rx = r"[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][-+]?\\d+)?"
return f"TRY_CAST(regexp_extract_all(CAST({q} AS VARCHAR), {sql_str(rx)})[{idx}] AS DOUBLE)"
def _prepare_coordinates(self, con, force, verbose):
if "coordinates" not in self.catalog:
return
info = self.catalog["coordinates"]; s = info.get("schema") or {}
rid = s.get("id")
x, y, z = s.get("x"), s.get("y"), s.get("z")
pos = s.get("position")
if not rid:
if verbose:
print("[skip] neuron_coords: root id column not detected")
return
# If explicit x/y/z are absent, parse the generic `position` column.
if not all((x, y, z)) and not pos:
if verbose:
print("[skip] neuron_coords: neither explicit x/y/z nor a position column was detected; SWC geometry will be used.")
return
exists = con.execute(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_name='neuron_coords'"
).fetchone()[0] > 0
if exists and not force and self._meta_ready(con, "neuron_coords_ready"):
return
con.execute("DROP TABLE IF EXISTS neuron_coords")
expr = duckdb_csv_expr(info["files"])
if all((x, y, z)):
xexpr = f"TRY_CAST({qident(x)} AS DOUBLE)"
yexpr = f"TRY_CAST({qident(y)} AS DOUBLE)"
zexpr = f"TRY_CAST({qident(z)} AS DOUBLE)"
mode = "explicit x/y/z"
else:
xexpr = self._position_sql_expr(pos, 0)
yexpr = self._position_sql_expr(pos, 1)
zexpr = self._position_sql_expr(pos, 2)
mode = f"parsed from {pos}"
con.execute(f"""
CREATE TABLE neuron_coords AS
SELECT
CAST({qident(rid)} AS VARCHAR) root_id,
AVG({xexpr}) x,
AVG({yexpr}) y,
AVG({zexpr}) z
FROM {expr}
WHERE {qident(rid)} IS NOT NULL
GROUP BY 1
""")
con.execute("CREATE INDEX IF NOT EXISTS idx_coords_root ON neuron_coords(root_id)")
self._set_meta_ready(con, "neuron_coords_ready")
if verbose:
n = con.execute("SELECT COUNT(*) FROM neuron_coords").fetchone()[0]
valid = con.execute("""
SELECT COUNT(*) FROM neuron_coords
WHERE x IS NOT NULL AND y IS NOT NULL AND z IS NOT NULL
""").fetchone()[0]
print(f"[done] neuron_coords: {n:,} rows, {valid:,} valid XYZ ({mode})")
def _prepare_tags(self, con, force, verbose):
if "connectivity_tags" not in self.catalog:
return
info = self.catalog["connectivity_tags"]; s = info.get("schema") or {}
rid, tag = s.get("id"), s.get("tag")
if not rid or not tag:
return
exists = con.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name='neuron_tags'").fetchone()[0] > 0
if exists and not force and self._meta_ready(con, "neuron_tags_ready"):
return
con.execute("DROP TABLE IF EXISTS neuron_tags")
expr = duckdb_csv_expr(info["files"])
con.execute(f"""
CREATE TABLE neuron_tags AS
SELECT CAST({qident(rid)} AS VARCHAR) root_id,
CAST({qident(tag)} AS VARCHAR) tag
FROM {expr}
WHERE {qident(rid)} IS NOT NULL
""")
con.execute("CREATE INDEX IF NOT EXISTS idx_tags_root ON neuron_tags(root_id)")
self._set_meta_ready(con, "neuron_tags_ready")
if verbose:
n = con.execute("SELECT COUNT(*) FROM neuron_tags").fetchone()[0]
print(f"[done] neuron_tags: {n:,}")
def _prepare_swc_index(self, force=False, verbose=True):
info = self.catalog.get("skeletons")
if not info:
return
index_path = self.cache_dir / "swc_index.json"
if index_path.exists() and not force:
try:
self._swc_index = json.loads(index_path.read_text(encoding="utf-8"))
return
except Exception:
pass
base = Path(info["path"])
idx = {}
count = 0
for p in base.rglob("*.swc"):
stem = p.stem
# Exact stem + longest long integer token as aliases.
idx.setdefault(stem, str(p))
nums = re.findall(r"\d{8,}", stem)
if nums:
idx.setdefault(max(nums, key=len), str(p))
count += 1
if verbose and count % 20000 == 0:
print(f"\r[swc] indexed {count:,}", end="", flush=True)
if verbose and count:
print(f"\r[swc] indexed {count:,} files")
self._swc_index = idx
index_path.write_text(json.dumps(idx), encoding="utf-8")
def _ensure_swc_index(self):
if self._swc_index:
return
p = self.cache_dir / "swc_index.json"
if p.exists():
self._swc_index = json.loads(p.read_text(encoding="utf-8"))
else:
self._prepare_swc_index(False, False)
def available_sources(self):
con = self.con()
try:
tabs = {r[0] for r in con.execute("SELECT table_name FROM information_schema.tables").fetchall()}
finally:
con.close()
out = []
for role in self.catalog:
if role.startswith("connections_"):
table = "c_" + role
if table in tabs:
out.append(role)
return out
# -----------------------------------------------------------------
# Queries
# -----------------------------------------------------------------
def search(self, q: str, limit=50):
con = self.con()
try:
result = []
q = (q or "").strip()
if q:
# exact/partial IDs + labels
rows = con.execute("""
SELECT root_id, label, source, details
FROM neuron_labels
WHERE root_id LIKE ? OR lower(label) LIKE lower(?) OR lower(details) LIKE lower(?)
LIMIT ?
""", [f"%{q}%", f"%{q}%", f"%{q}%", limit]).fetchall() if self._table_exists(con,"neuron_labels") else []
seen=set()
for r in rows:
if r[0] in seen: continue
seen.add(r[0])
result.append({"root_id":r[0],"label":r[1],"source":r[2],"details":r[3]})
# If a numeric ID wasn't in labels, still return it.
if re.fullmatch(r"\d{6,}", q) and q not in seen:
result.insert(0, {"root_id":q,"label":"","source":"id","details":""})
else:
if self._table_exists(con,"neuron_labels"):
rows=con.execute("SELECT root_id,label,source,details FROM neuron_labels WHERE root_id IS NOT NULL LIMIT ?",[limit]).fetchall()
result=[{"root_id":r[0],"label":r[1] or "","source":r[2],"details":r[3]} for r in rows]
if not result:
sources = self.available_sources()
if sources:
table = "c_" + sources[0]
rows = con.execute(f"SELECT DISTINCT pre_id FROM {qident(table)} LIMIT ?", [limit]).fetchall()
result=[{"root_id":r[0],"label":"","source":sources[0],"details":""} for r in rows]
return result[:limit]
finally:
con.close()
def _table_exists(self, con, name):
return con.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name=?", [name]).fetchone()[0] > 0
def neuron_info(self, root_id: str):
con = self.con()
try:
labels=[]
tags=[]
coord=None
if self._table_exists(con,"neuron_labels"):
rows=con.execute("SELECT label,source,details FROM neuron_labels WHERE root_id=? LIMIT 20",[root_id]).fetchall()
labels=[{"label":r[0],"source":r[1],"details":r[2]} for r in rows]
if self._table_exists(con,"neuron_tags"):
tags=[r[0] for r in con.execute("SELECT DISTINCT tag FROM neuron_tags WHERE root_id=? LIMIT 50",[root_id]).fetchall()]
if self._table_exists(con,"neuron_coords"):
r=con.execute("SELECT x,y,z FROM neuron_coords WHERE root_id=? LIMIT 1",[root_id]).fetchone()
if r: coord={"x":r[0],"y":r[1],"z":r[2]}
return {"root_id":root_id,"labels":labels,"tags":tags,"coord":coord,"has_swc":self.has_swc(root_id)}
finally:
con.close()
def has_swc(self, root_id: str):
self._ensure_swc_index()
if root_id in self._swc_index:
return True
return any(root_id in k for k in list(self._swc_index.keys())[:2000]) if self._swc_index else False
def _swc_path(self, root_id: str) -> Optional[Path]:
self._ensure_swc_index()
p = self._swc_index.get(root_id)
if p:
return Path(p)
# fallback exact containment scan through keys
for k,v in self._swc_index.items():
if root_id == k or root_id in k:
return Path(v)
return None
def skeleton(self, root_id: str, max_segments=80000):
p = self._swc_path(root_id)
if not p or not p.exists():
return {"root_id":root_id,"found":False,"segments":[]}
nodes={}
segments=[]
with open(p,"r",encoding="utf-8",errors="ignore") as f:
for line in f:
line=line.strip()
if not line or line.startswith("#"):
continue
parts=line.split()
if len(parts)<7:
continue
try:
nid=int(float(parts[0])); typ=int(float(parts[1]))
x=float(parts[2]); y=float(parts[3]); z=float(parts[4]); radius=float(parts[5]); parent=int(float(parts[6]))
except Exception:
continue
nodes[nid]=(x,y,z,radius,typ,parent)
for nid,(x,y,z,radius,typ,parent) in nodes.items():
if parent in nodes:
px,py,pz,pr,pt,pp=nodes[parent]
segments.append([px,py,pz,x,y,z,radius,typ])
original=len(segments)
if original>max_segments:
step=original/max_segments
segments=[segments[int(i*step)] for i in range(max_segments)]
return {
"root_id":root_id,"found":True,"file":p.name,
"node_count":len(nodes),"segment_count":original,
"rendered_segments":len(segments),"segments":segments
}
def neighbors(self, root_id: str, source: str, topk=50, min_syn=1):
table="c_"+source
con=self.con()
try:
if not self._table_exists(con,table):
raise ValueError(f"Prepared source not found: {source}")
ins=con.execute(f"""
SELECT pre_id partner_id, syn_count, nt_type
FROM {qident(table)}
WHERE post_id=? AND syn_count>=?
ORDER BY syn_count DESC LIMIT ?
""",[root_id,min_syn,topk]).fetchall()
outs=con.execute(f"""
SELECT post_id partner_id, syn_count, nt_type
FROM {qident(table)}
WHERE pre_id=? AND syn_count>=?
ORDER BY syn_count DESC LIMIT ?
""",[root_id,min_syn,topk]).fetchall()
return (
[{"partner_id":r[0],"syn_count":float(r[1]),"nt_type":r[2],"direction":"in"} for r in ins],
[{"partner_id":r[0],"syn_count":float(r[1]),"nt_type":r[2],"direction":"out"} for r in outs]
)
finally:
con.close()
def graph(self, center_id: str, source: str, topk=50, depth=1,
min_syn=1, max_nodes=1500):
"""
Scalable subgraph extraction.
Unlike the original per-node neighbor loop, expansion is batched by BFS
depth and uses SQL window functions. This allows thousands of neurons to
be explored without opening a query for every node.
"""
topk=max(1,min(int(topk),2000))
depth=max(1,min(int(depth),3))
min_syn=max(0,float(min_syn))
max_nodes=max(10,min(int(max_nodes),10000))
table="c_"+source
con=self.con()
try:
if not self._table_exists(con,table):
raise ValueError(f"Prepared source not found: {source}")
nodes={str(center_id):{"id":str(center_id)}}
edges=[]
edge_seen=set()
visited=set()
frontier=[str(center_id)]
def expand_chunk(ids, d):
if not ids:
return []
ph=",".join(["?"]*len(ids))
# First hop can be broad; deeper hops are capped to control explosion.
per_node=topk if d==0 else min(topk,40 if d==1 else 18)
incoming=con.execute(f"""
SELECT partner_id, center_id, syn_count, nt_type FROM (
SELECT pre_id partner_id, post_id center_id, syn_count, nt_type,
row_number() OVER(PARTITION BY post_id ORDER BY syn_count DESC) rn
FROM {qident(table)}
WHERE post_id IN ({ph}) AND syn_count>=?
) WHERE rn<=?
""", ids+[min_syn,per_node]).fetchall()
outgoing=con.execute(f"""
SELECT center_id, partner_id, syn_count, nt_type FROM (
SELECT pre_id center_id, post_id partner_id, syn_count, nt_type,
row_number() OVER(PARTITION BY pre_id ORDER BY syn_count DESC) rn
FROM {qident(table)}
WHERE pre_id IN ({ph}) AND syn_count>=?
) WHERE rn<=?
""", ids+[min_syn,per_node]).fetchall()
return [(str(a),str(b),float(w),nt) for a,b,w,nt in incoming+outgoing]
for d in range(depth):
current=[x for x in frontier if x not in visited]
if not current or len(nodes)>=max_nodes:
break
visited.update(current)
next_frontier=[]
for c0 in range(0,len(current),250):
rows=expand_chunk(current[c0:c0+250],d)
for a,b,w,nt in rows:
key=(a,b)
if key not in edge_seen:
edge_seen.add(key)
edges.append({"source":a,"target":b,"weight":w,"nt_type":nt})
for nid in (a,b):
if nid not in nodes and len(nodes)<max_nodes:
nodes[nid]={"id":nid}
next_frontier.append(nid)
if len(nodes)>=max_nodes:
break
if len(nodes)>=max_nodes:
break
frontier=next_frontier
ids=list(nodes)
# Enrich in chunks.
if ids and self._table_exists(con,"neuron_coords"):
for c0 in range(0,len(ids),500):
chunk=ids[c0:c0+500]; ph=",".join(["?"]*len(chunk))
for rid,x,y,z in con.execute(
f"SELECT root_id,x,y,z FROM neuron_coords WHERE root_id IN ({ph})",chunk
).fetchall():
nodes[str(rid)]["coord"]={"x":x,"y":y,"z":z}
if ids and self._table_exists(con,"neuron_labels"):
for c0 in range(0,len(ids),500):
chunk=ids[c0:c0+500]; ph=",".join(["?"]*len(chunk))
for rid,label in con.execute(f"""
SELECT root_id, any_value(label) FROM neuron_labels
WHERE root_id IN ({ph}) AND label<>'' GROUP BY root_id
""",chunk).fetchall():
nodes[str(rid)]["label"]=label
# Deterministic fallback position.
for nid in ids:
c=nodes[nid].get("coord")
if not c or c.get("x") is None:
h=int(hashlib.md5(nid.encode()).hexdigest()[:12],16)
phi=(h%100000)/100000*math.pi*2
cost=((h//100000)%10000)/5000-1
cost=max(-1,min(1,cost)); sint=math.sqrt(max(0,1-cost*cost))
r=1.0+0.25*((h//1000000000)%1000)/1000
nodes[nid]["coord"]={
"x":r*sint*math.cos(phi),"y":r*sint*math.sin(phi),
"z":r*cost,"fallback":True
}
return {
"center_id":str(center_id),"source":source,
"nodes":list(nodes.values()),"edges":edges,
"limits":{"topk":topk,"depth":depth,"max_nodes":max_nodes,"min_syn":min_syn}
}
finally:
con.close()
def synapse_points(self, pre_id: str, post_id: str, limit=5000):
"""
Read raw synapse tables on demand. We avoid materializing 13GB synapse data.
Returns sampled/limited coordinates if detectable.
"""
limit=max(10,min(int(limit),20000))
role = "synapses_princeton" if "synapses_princeton" in self.catalog else ("neuropil_synapses" if "neuropil_synapses" in self.catalog else None)
if not role:
return {"found":False,"points":[],"reason":"synapse table not discovered"}
info=self.catalog[role]; s=info.get("schema") or {}
if not s.get("pre") or not s.get("post"):
return {"found":False,"points":[],"reason":"pre/post columns not detected"}
x,y,z=s.get("x"),s.get("y"),s.get("z")
if not all((x,y,z)):
return {"found":False,"points":[],"reason":"x/y/z columns not detected"}
expr=duckdb_csv_expr(info["files"])
con=self.con()
try:
rows=con.execute(f"""
SELECT TRY_CAST({qident(x)} AS DOUBLE),
TRY_CAST({qident(y)} AS DOUBLE),
TRY_CAST({qident(z)} AS DOUBLE),
{qident(s['neuropil']) if s.get('neuropil') else 'NULL'}
FROM {expr}
WHERE CAST({qident(s['pre'])} AS VARCHAR)=?
AND CAST({qident(s['post'])} AS VARCHAR)=?
LIMIT ?
""",[pre_id,post_id,limit]).fetchall()
return {
"found":True,"role":role,
"points":[{"x":r[0],"y":r[1],"z":r[2],"neuropil":r[3]} for r in rows if None not in r[:3]]
}
finally:
con.close()
# -----------------------------------------------------------------
# Pro explorer / simulator support
# -----------------------------------------------------------------
def _label_map(self, ids, con=None):
ids = [str(x) for x in ids if x is not None]
if not ids:
return {}
own = con is None
if own:
con = self.con()
try:
if not self._table_exists(con, "neuron_labels"):
return {}
out = {}
# Chunk so large path/neighbor requests do not create huge SQL.
for start in range(0, len(ids), 500):
chunk = ids[start:start+500]
ph = ",".join(["?"] * len(chunk))
rows = con.execute(f"""
SELECT root_id,
any_value(CASE WHEN label IS NULL THEN '' ELSE label END)
FROM neuron_labels
WHERE root_id IN ({ph})
GROUP BY root_id
""", chunk).fetchall()
for rid, label in rows:
out[str(rid)] = label or ""
return out
finally:
if own:
con.close()
def _processed_labels_raw(self, root_id: str, limit=30):
if root_id in self._processed_label_cache:
return self._processed_label_cache[root_id][:limit]
info = self.catalog.get("processed_labels")
if not info:
return []
cols = info.get("columns") or []
s = info.get("schema") or {}
rid = s.get("id") or ("root_id" if "root_id" in cols else None)
label_col = s.get("label")
if not label_col and "processed_labels" in cols:
label_col = "processed_labels"
if not rid or not label_col:
return []
expr = duckdb_csv_expr(info["files"])
con = self.con()
try:
rows = con.execute(f"""
SELECT DISTINCT CAST({qident(label_col)} AS VARCHAR)
FROM {expr}
WHERE CAST({qident(rid)} AS VARCHAR)=?
AND {qident(label_col)} IS NOT NULL
LIMIT ?
""", [root_id, limit]).fetchall()
result = [r[0] for r in rows if r and r[0]]
self._processed_label_cache[root_id] = result
return result[:limit]
except Exception:
return []
finally:
con.close()
def neuropil_profile(self, root_id: str, top=20):
cached = self._neuropil_cache.get(root_id)
if cached:
return {**cached, "inputs": cached.get("inputs", [])[:top], "outputs": cached.get("outputs", [])[:top]}
info = self.catalog.get("neuropil_synapses")
if not info:
return {"found": False, "inputs": [], "outputs": []}
s = info.get("schema") or {}
rid = s.get("id")
if not rid:
return {"found": False, "inputs": [], "outputs": []}
expr = duckdb_csv_expr(info["files"])
con = self.con()
try:
cur = con.execute(f"""
SELECT * FROM {expr}
WHERE CAST({qident(rid)} AS VARCHAR)=?
LIMIT 1
""", [root_id])
row = cur.fetchone()
if not row:
return {"found": False, "inputs": [], "outputs": []}
cols = [d[0] for d in cur.description]
inputs, outputs = [], []
totals = {}
for col, value in zip(cols, row):
if value is None:
continue
nc = str(col).strip()
l = nc.lower()
try:
val = float(value)
except Exception:
continue
if l == "input synapses":
totals["input_synapses"] = val
elif l == "output synapses":
totals["output_synapses"] = val
elif l == "input partners":
totals["input_partners"] = val
elif l == "output partners":
totals["output_partners"] = val
elif l.startswith("input synapses in ") and val > 0:
inputs.append({"neuropil": nc[len("input synapses in "):], "value": val})
elif l.startswith("output synapses in ") and val > 0:
outputs.append({"neuropil": nc[len("output synapses in "):], "value": val})
inputs.sort(key=lambda x: x["value"], reverse=True)
outputs.sort(key=lambda x: x["value"], reverse=True)
result = {
"found": True,
"totals": totals,
"inputs": inputs,
"outputs": outputs
}
self._neuropil_cache[root_id] = result
return {**result, "inputs": inputs[:max(1, int(top))], "outputs": outputs[:max(1, int(top))]}
except Exception as e:
return {"found": False, "inputs": [], "outputs": [], "error": str(e)}
finally:
con.close()
def neuron_connection_stats(self, root_id: str, source: str):
table = "c_" + source
con = self.con()
try:
if not self._table_exists(con, table):
return {}
r = con.execute(f"""
SELECT
(SELECT COUNT(*) FROM {qident(table)} WHERE post_id=?) AS in_partners,
(SELECT COALESCE(SUM(syn_count),0) FROM {qident(table)} WHERE post_id=?) AS in_synapses,
(SELECT COUNT(*) FROM {qident(table)} WHERE pre_id=?) AS out_partners,
(SELECT COALESCE(SUM(syn_count),0) FROM {qident(table)} WHERE pre_id=?) AS out_synapses
""", [root_id, root_id, root_id, root_id]).fetchone()
return {
"input_partners": int(r[0] or 0),
"input_synapses": float(r[1] or 0),
"output_partners": int(r[2] or 0),
"output_synapses": float(r[3] or 0)
}
finally:
con.close()
def neurotransmitter_profile(self, root_id: str, source: str):
table="c_"+source
con=self.con()
try:
if not self._table_exists(con,table):
return {}
rows=con.execute(f"""
SELECT COALESCE(nt_type,'unknown'), SUM(syn_count)
FROM {qident(table)}
WHERE pre_id=?
GROUP BY 1 ORDER BY 2 DESC
""",[root_id]).fetchall()
return {str(k):float(v or 0) for k,v in rows}
finally:
con.close()
def neuron_full(self, root_id: str, source: Optional[str] = None):
if not source:
sources=self.available_sources()
source=sources[0] if sources else ""
basic=self.neuron_info(root_id)
basic["source"]=source