-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrain_monitor.py
More file actions
1253 lines (1056 loc) · 45.5 KB
/
brain_monitor.py
File metadata and controls
1253 lines (1056 loc) · 45.5 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
#!/usr/bin/env python3
"""
Brain Monitor v2 — Futuristic real-time dorsal brain visualization.
Sci-fi / Black Mirror aesthetic: gaussian glow, animated particles,
dashed connections, hex grid, scanlines, pulsing regions.
Runs in a separate process (pygame) to avoid GL context conflicts with MuJoCo.
Receives neural activity data via multiprocessing.Queue and renders a glowing
dorsal brain map with HUD overlay.
Usage:
Launched automatically by fly_embodied.py --monitor
"""
import multiprocessing as mp
import math
import time
import random
# ---------------------------------------------------------------------------
# Neon color palette
# ---------------------------------------------------------------------------
COL_BG = (5, 5, 20)
COL_VISUAL = (0, 255, 200)
COL_LOOMING = (255, 120, 0)
COL_ESCAPE = (255, 20, 60)
COL_MOTOR = (0, 255, 100)
COL_BACKWARD = (180, 60, 255)
COL_GROOM = (255, 200, 0)
COL_FEED = (255, 80, 180)
COL_JO_TOUCH = (0, 200, 255) # cyan-blue for mechanosensory
COL_JO_SOUND = (120, 80, 255) # indigo for auditory
COL_SUGAR = (80, 255, 80) # bright green for sugar GRN
COL_BITTER = (255, 80, 80) # bright red for bitter GRN
COL_OR_ATT = (120, 255, 60) # lime green for attractive ORN
COL_OR_REP = (255, 60, 200) # hot pink for repulsive ORN
COL_WING = (255, 220, 50) # gold for wing song
COL_FLIGHT = (50, 200, 255) # cyan for flight system
COL_HUD = (0, 200, 220)
COL_TITLE = (180, 230, 255)
COL_HEX = (15, 20, 50)
COL_BRAIN_CENTER = (18, 22, 50)
# Consciousness index gradient: black → blue → green → white
CI_GRADIENT = [
(0.0, (5, 5, 20)), # black/dark
(0.25, (20, 60, 200)), # blue
(0.5, (0, 200, 100)), # green
(0.75, (100, 255, 200)), # bright green-white
(1.0, (240, 255, 255)), # white
]
COL_CI_PEAK = (255, 215, 0) # gold for peak markers
# ---------------------------------------------------------------------------
# Brain regions: (name, x, y, radius, color_rgb, group)
# ---------------------------------------------------------------------------
REGIONS = [
# Visual pathway
('Retina_L', 120, 100, 35, COL_VISUAL, 'visual'),
('Retina_R', 680, 100, 35, COL_VISUAL, 'visual'),
('T2_L', 185, 155, 22, COL_VISUAL, 'visual'),
('T2_R', 615, 155, 22, COL_VISUAL, 'visual'),
# Looming detectors
('LC4_L', 215, 200, 20, COL_LOOMING, 'looming'),
('LC4_R', 585, 200, 20, COL_LOOMING, 'looming'),
('LPLC2_L', 245, 245, 20, COL_LOOMING, 'looming'),
('LPLC2_R', 555, 245, 20, COL_LOOMING, 'looming'),
# Johnston's Organ — touch (antenna area, dorsal)
('JO_tch_L', 155, 60, 16, COL_JO_TOUCH, 'jo_touch'),
('JO_tch_R', 645, 60, 16, COL_JO_TOUCH, 'jo_touch'),
# Johnston's Organ — sound (antenna area, slightly lower)
('JO_snd_L', 170, 90, 14, COL_JO_SOUND, 'jo_sound'),
('JO_snd_R', 630, 90, 14, COL_JO_SOUND, 'jo_sound'),
# Olfactory — attractive ORN (DM1/Or42b, antennal lobe)
('OR_att_L', 255, 55, 14, COL_OR_ATT, 'olfactory'),
('OR_att_R', 545, 55, 14, COL_OR_ATT, 'olfactory'),
# Olfactory — repulsive ORN (DA2/Or56a, antennal lobe)
('OR_rep_L', 290, 75, 12, COL_OR_REP, 'olfactory'),
('OR_rep_R', 510, 75, 12, COL_OR_REP, 'olfactory'),
# Gustatory — sugar / bitter GRNs (SEZ input)
('Sugar_GRN', 360, 385, 14, COL_SUGAR, 'gustatory'),
('Bitter_GRN',440, 385, 14, COL_BITTER, 'gustatory'),
# Giant Fiber — escape command
('GF', 400, 290, 28, COL_ESCAPE, 'escape'),
# Motor — turning
('DNa_L', 320, 350, 16, COL_MOTOR, 'motor'),
('DNa_R', 480, 350, 16, COL_MOTOR, 'motor'),
# Motor — forward
('P9_L', 340, 420, 16, COL_MOTOR, 'motor'),
('P9_R', 460, 420, 16, COL_MOTOR, 'motor'),
# Backward
('MDN', 400, 460, 16, COL_BACKWARD, 'backward'),
# Grooming
('aDN1', 300, 460, 14, COL_GROOM, 'groom'),
# Feeding
('MN9', 500, 460, 14, COL_FEED, 'feed'),
# Wing song
('Wing_Song', 400, 510, 16, COL_WING, 'wing'),
# Flight
('Flight', 400, 555, 18, COL_FLIGHT, 'flight'),
]
_REGION_IDX = {r[0]: i for i, r in enumerate(REGIONS)}
CONNECTIONS = [
('Retina_L', 'T2_L'),
('Retina_R', 'T2_R'),
('T2_L', 'LC4_L'),
('T2_R', 'LC4_R'),
('LC4_L', 'LPLC2_L'),
('LC4_R', 'LPLC2_R'),
('LPLC2_L', 'GF'),
('LPLC2_R', 'GF'),
('GF', 'DNa_L'),
('GF', 'DNa_R'),
('DNa_L', 'P9_L'),
('DNa_R', 'P9_R'),
('GF', 'MDN'),
# JO touch → grooming
('JO_tch_L', 'aDN1'),
('JO_tch_R', 'aDN1'),
# JO touch → escape (strong tactile)
('JO_tch_L', 'GF'),
('JO_tch_R', 'GF'),
# JO sound → turning (orientation)
('JO_snd_L', 'DNa_L'),
('JO_snd_R', 'DNa_R'),
# Sugar GRN → feeding (MN9) + approach (P9)
('Sugar_GRN', 'MN9'),
('Sugar_GRN', 'P9_L'),
# Bitter GRN → escape (GF) + backward (MDN)
('Bitter_GRN', 'GF'),
('Bitter_GRN', 'MDN'),
# Attractive ORN → approach (P9) + turning (DNa)
('OR_att_L', 'P9_L'),
('OR_att_R', 'P9_R'),
('OR_att_L', 'DNa_L'),
('OR_att_R', 'DNa_R'),
# Repulsive ORN → escape (GF)
('OR_rep_L', 'GF'),
('OR_rep_R', 'GF'),
# Wing song ← motor triggers
('MN9', 'Wing_Song'),
('GF', 'Wing_Song'),
# Wing song → JO self-hearing
('Wing_Song', 'JO_snd_L'),
('Wing_Song', 'JO_snd_R'),
# Flight: GF triggers takeoff, DNa controls direction, P9 thrust
('GF', 'Flight'),
('DNa_L', 'Flight'),
('DNa_R', 'Flight'),
('P9_L', 'Flight'),
]
_DATA_KEY_MAP = {
'bright_left': 'Retina_L',
'bright_right': 'Retina_R',
't2_left': 'T2_L',
't2_right': 'T2_R',
'lc4_left': 'LC4_L',
'lc4_right': 'LC4_R',
'lplc2_left': 'LPLC2_L',
'lplc2_right': 'LPLC2_R',
'dn_escape': 'GF',
'dn_turn_L': 'DNa_L',
'dn_turn_R': 'DNa_R',
'dn_forward': 'P9_L',
'dn_forward_R': 'P9_R',
'dn_backward': 'MDN',
'dn_groom': 'aDN1',
'dn_feed': 'MN9',
'jo_touch_L': 'JO_tch_L',
'jo_touch_R': 'JO_tch_R',
'jo_sound_L': 'JO_snd_L',
'jo_sound_R': 'JO_snd_R',
'sugar_level': 'Sugar_GRN',
'bitter_level': 'Bitter_GRN',
'or_att_L': 'OR_att_L',
'or_att_R': 'OR_att_R',
'or_rep_L': 'OR_rep_L',
'or_rep_R': 'OR_rep_R',
'wing_level': 'Wing_Song',
'flight_level': 'Flight',
}
SIDEBAR_BARS = [
('FWD', 'dn_forward', COL_MOTOR),
('ESC', 'dn_escape', COL_ESCAPE),
('TRN', 'dn_turn_L', COL_MOTOR),
('GRM', 'dn_groom', COL_GROOM),
('BKW', 'dn_backward', COL_BACKWARD),
('FED', 'dn_feed', COL_FEED),
('TCH', 'jo_contact', COL_JO_TOUCH),
('SND', 'jo_sound', COL_JO_SOUND),
('SGR', 'sugar_level', COL_SUGAR),
('BTR', 'bitter_level',COL_BITTER),
('ATT', 'or_attractive',COL_OR_ATT),
('REP', 'or_repulsive',COL_OR_REP),
('WNG', 'wing_level', COL_WING),
('FLT', 'flight_level', COL_FLIGHT),
]
MODE_COLORS = {
'walking': COL_MOTOR,
'escape': COL_ESCAPE,
'grooming': COL_GROOM,
'feeding': COL_FEED,
'flight': COL_FLIGHT,
}
GLOW_LEVELS = 16
def _clamp(v, lo=0.0, hi=1.0):
return max(lo, min(hi, v))
# ============================================================================
# Glow Cache — gaussian numpy pre-render
# ============================================================================
class GlowCache:
"""Pre-renders gaussian glow textures at 16 intensity levels per region."""
def __init__(self, pygame_module):
self.pg = pygame_module
self.cache = {} # (region_idx, level) -> Surface
def _render_glow(self, radius, color, intensity):
"""Render a gaussian glow surface with 3 layers."""
import numpy as np
pg = self.pg
# Surface size: enough for the outermost halo
half = int(radius * 4) + 4
size = half * 2
surf = pg.Surface((size, size), pg.SRCALPHA)
if intensity < 0.005:
return surf
# Build coordinate grid centered on (half, half)
y_coords, x_coords = np.mgrid[0:size, 0:size]
dist_sq = (x_coords - half).astype(np.float32) ** 2 + \
(y_coords - half).astype(np.float32) ** 2
r, g, b = color
# Accumulate RGB + alpha in float
out_r = np.zeros((size, size), dtype=np.float32)
out_g = np.zeros((size, size), dtype=np.float32)
out_b = np.zeros((size, size), dtype=np.float32)
out_a = np.zeros((size, size), dtype=np.float32)
# Layer 1: Outer halo (σ = r×1.8)
sigma1 = radius * 1.8
gauss1 = np.exp(-dist_sq / (2.0 * sigma1 * sigma1))
a1 = gauss1 * 0.25 * intensity
out_r += r * a1
out_g += g * a1
out_b += b * a1
out_a += a1 * 255
# Layer 2: Inner glow (σ = r×0.8)
sigma2 = radius * 0.8
gauss2 = np.exp(-dist_sq / (2.0 * sigma2 * sigma2))
a2 = gauss2 * 0.55 * intensity
out_r += r * a2
out_g += g * a2
out_b += b * a2
out_a += a2 * 255
# Layer 3: Hot core (σ = r×0.3), shifts toward white at high intensity
sigma3 = radius * 0.3
gauss3 = np.exp(-dist_sq / (2.0 * sigma3 * sigma3))
a3 = gauss3 * 0.9 * intensity
white_mix = intensity * 0.6 # how much to blend toward white
cr = r + (255 - r) * white_mix
cg = g + (255 - g) * white_mix
cb = b + (255 - b) * white_mix
out_r += cr * a3
out_g += cg * a3
out_b += cb * a3
out_a += a3 * 255
# Clamp and assemble RGBA
out_r = np.clip(out_r, 0, 255).astype(np.uint8)
out_g = np.clip(out_g, 0, 255).astype(np.uint8)
out_b = np.clip(out_b, 0, 255).astype(np.uint8)
out_a = np.clip(out_a, 0, 255).astype(np.uint8)
# Stack into (size, size, 4) RGBA array
rgba = np.stack([out_r, out_g, out_b, out_a], axis=-1)
# Blit numpy array to surface
# pygame surfarray wants (width, height, 4) = transposed
pg.surfarray.blit_array(surf, rgba[:, :, :3].transpose(1, 0, 2))
# Set alpha channel via separate pixel_alpha array
alpha_surf = pg.surfarray.pixels_alpha(surf)
alpha_surf[:] = out_a.T
del alpha_surf # unlock surface
return surf
def build(self):
"""Pre-render all region x intensity combinations."""
for idx, (name, x, y, radius, color, group) in enumerate(REGIONS):
for level in range(GLOW_LEVELS + 1):
intensity = level / GLOW_LEVELS
surf = self._render_glow(radius, color, intensity)
self.cache[(idx, level)] = surf
def get(self, region_idx, intensity):
"""Get pre-rendered surface for region at given intensity [0-1]."""
level = int(round(_clamp(intensity) * GLOW_LEVELS))
return self.cache.get((region_idx, level))
# ============================================================================
# Particle System
# ============================================================================
class ConnectionParticle:
"""A single luminous particle traveling along a connection."""
__slots__ = ('conn_idx', 't', 'speed', 'life', 'max_life')
def __init__(self, conn_idx, speed):
self.conn_idx = conn_idx
self.t = 0.0 # 0..1 progress along connection
self.speed = speed # units per second (t goes 0->1)
self.life = 0.0
self.max_life = 1.0 / max(speed, 0.01)
class ParticleSystem:
"""Manages particles flowing along neural connections."""
MAX_PER_CONN = 6
MAX_RATE = 4.0 # max spawns/sec per connection
def __init__(self):
self.particles = [] # list of ConnectionParticle
self._spawn_accum = {} # conn_idx -> accumulated spawn fraction
def update(self, dt, conn_intensities):
"""Spawn new particles and advance existing ones."""
# Spawn
for ci, intensity in enumerate(conn_intensities):
if intensity < 0.05:
self._spawn_accum[ci] = 0.0
continue
rate = intensity * self.MAX_RATE
acc = self._spawn_accum.get(ci, 0.0) + rate * dt
count_on_conn = sum(1 for p in self.particles if p.conn_idx == ci)
while acc >= 1.0 and count_on_conn < self.MAX_PER_CONN:
speed = 0.6 + 0.8 * intensity + random.random() * 0.3
p = ConnectionParticle(ci, speed)
self.particles.append(p)
acc -= 1.0
count_on_conn += 1
self._spawn_accum[ci] = acc
# Update positions, remove dead
alive = []
for p in self.particles:
p.t += p.speed * dt
p.life += dt
if p.t < 1.0:
alive.append(p)
self.particles = alive
def draw(self, screen, pg, connections):
"""Draw all particles as small glowing dots."""
for p in self.particles:
src_name, dst_name = connections[p.conn_idx]
src = REGIONS[_REGION_IDX[src_name]]
dst = REGIONS[_REGION_IDX[dst_name]]
# Interpolate position
x = src[1] + (dst[1] - src[1]) * p.t
y = src[2] + (dst[2] - src[2]) * p.t
# Fade in/out at endpoints
fade = 1.0
if p.t < 0.15:
fade = p.t / 0.15
elif p.t > 0.85:
fade = (1.0 - p.t) / 0.15
fade = _clamp(fade)
# Color from source region
color = src[4]
alpha = int(220 * fade)
r2 = 3 if fade > 0.5 else 2
# Draw glow dot (outer + core)
glow_surf = pg.Surface((12, 12), pg.SRCALPHA)
pg.draw.circle(glow_surf,
(color[0], color[1], color[2], alpha // 3),
(6, 6), 5)
pg.draw.circle(glow_surf,
(min(color[0] + 80, 255),
min(color[1] + 80, 255),
min(color[2] + 80, 255), alpha),
(6, 6), r2)
screen.blit(glow_surf, (int(x) - 6, int(y) - 6),
special_flags=pg.BLEND_ADD)
# ============================================================================
# Brain Renderer — all drawing logic (futuristic overhaul)
# ============================================================================
class BrainRenderer:
"""Renders the dorsal brain view with gaussian glow, particles, and HUD."""
WIDTH = 800
HEIGHT = 600
# Smoothing
TAU_SMOOTH = 0.12 # exponential smoothing time constant (seconds)
PULSE_FREQ = 2.5 # Hz breathing animation
PULSE_AMP = 0.08 # ±8% intensity modulation
def __init__(self, pygame_module):
self.pg = pygame_module
self.screen = None
self.clock = None
self.font = None
self.font_sm = None
self.font_title = None
self.glow_cache = GlowCache(pygame_module)
# Audio: wing song tones
self._audio_tones = {} # freq -> pygame.Sound
self._current_tone_freq = 0
self._audio_ready = False
self.particles = ParticleSystem()
n = len(REGIONS)
self.raw_intensities = [0.0] * n
self.smoothed = [0.0] * n
self.display_intensity = [0.0] * n
self.phase = [random.random() * math.tau for _ in range(n)]
self.data = {}
self.frame_time = 0.0 # monotonic seconds
self.last_time = None
self.dash_offset = 0.0 # animated dash offset
# Pre-rendered surfaces (built in init_display)
self._hex_grid = None
self._brain_sil = None
self._scanlines = None
self._title_glow = None
# Pre-rendered sidebar bar gradient surfaces
self._bar_gradients = {}
def init_display(self):
"""Initialize pygame display, fonts, caches, pre-rendered assets."""
pg = self.pg
self.screen = pg.display.set_mode(
(self.WIDTH, self.HEIGHT), pg.DOUBLEBUF)
pg.display.set_caption('Drosophila Brain Monitor')
self.clock = pg.time.Clock()
self.font = pg.font.SysFont('consolas', 14)
self.font_sm = pg.font.SysFont('consolas', 11)
self.font_title = pg.font.SysFont('consolas', 18, bold=True)
self.glow_cache.build()
self._build_hex_grid()
self._build_brain_silhouette()
self._build_scanline_overlay()
self._build_title_glow()
self._build_bar_gradients()
self._init_audio()
self.last_time = time.monotonic()
# ── Pre-render: Hex Grid ──────────────────────────────────────────────
def _build_hex_grid(self):
"""Pre-render a subtle hexagonal grid pattern."""
pg = self.pg
surf = pg.Surface((self.WIDTH, self.HEIGHT), pg.SRCALPHA)
hex_r = 20 # hex radius
w = hex_r * 2
h = int(hex_r * math.sqrt(3))
color = (*COL_HEX, 35) # subtle alpha
for row in range(-1, self.HEIGHT // h + 2):
for col in range(-1, self.WIDTH // w + 2):
cx = int(col * w * 0.75)
cy = int(row * h + (col % 2) * h * 0.5)
points = []
for i in range(6):
angle = math.pi / 3 * i + math.pi / 6
px = cx + int(hex_r * math.cos(angle))
py = cy + int(hex_r * math.sin(angle))
points.append((px, py))
if len(points) == 6:
pg.draw.polygon(surf, color, points, 1)
self._hex_grid = surf
# ── Pre-render: Brain Silhouette ──────────────────────────────────────
def _build_brain_silhouette(self):
"""Pre-render brain silhouette with radial gradient via numpy."""
import numpy as np
pg = self.pg
surf = pg.Surface((self.WIDTH, self.HEIGHT), pg.SRCALPHA)
# Three elliptical regions: left optic, right optic, central brain
ellipses = [
(200, 160, 150, 110), # left optic lobe (cx, cy, rx, ry)
(600, 160, 150, 110), # right optic lobe
(400, 350, 170, 160), # central brain
]
y_coords, x_coords = np.mgrid[0:self.HEIGHT, 0:self.WIDTH]
x_f = x_coords.astype(np.float32)
y_f = y_coords.astype(np.float32)
# Combined mask: union of ellipses with soft falloff
combined = np.zeros((self.HEIGHT, self.WIDTH), dtype=np.float32)
for cx, cy, rx, ry in ellipses:
dist = ((x_f - cx) / rx) ** 2 + ((y_f - cy) / ry) ** 2
# Smooth falloff: 1.0 inside, fades to 0 outside
mask = np.clip(1.0 - (dist - 0.7) * 2.5, 0.0, 1.0)
combined = np.maximum(combined, mask)
# Radial gradient: center brighter
center_x, center_y = 400, 300
global_dist = np.sqrt((x_f - center_x) ** 2 +
(y_f - center_y) ** 2)
radial = np.clip(1.0 - global_dist / 350.0, 0.2, 1.0)
alpha = (combined * radial * 45).astype(np.uint8) # subtle
# Color: COL_BRAIN_CENTER
r_arr = np.full_like(alpha, COL_BRAIN_CENTER[0])
g_arr = np.full_like(alpha, COL_BRAIN_CENTER[1])
b_arr = np.full_like(alpha, COL_BRAIN_CENTER[2])
rgba = np.stack([r_arr, g_arr, b_arr, alpha], axis=-1)
pg.surfarray.blit_array(surf, rgba[:, :, :3].transpose(1, 0, 2))
a_view = pg.surfarray.pixels_alpha(surf)
a_view[:] = alpha.T
del a_view
self._brain_sil = surf
# ── Pre-render: Scanline Overlay ──────────────────────────────────────
def _build_scanline_overlay(self):
"""Pre-render CRT scanline effect."""
pg = self.pg
surf = pg.Surface((self.WIDTH, self.HEIGHT), pg.SRCALPHA)
for y in range(0, self.HEIGHT, 3):
pg.draw.line(surf, (0, 0, 0, 18), (0, y), (self.WIDTH, y), 1)
self._scanlines = surf
# ── Pre-render: Title Glow ────────────────────────────────────────────
def _build_title_glow(self):
"""Pre-render title text with glow halo."""
pg = self.pg
text = 'DROSOPHILA BRAIN MONITOR'
base = self.font_title.render(text, True, COL_TITLE)
w, h = base.get_size()
pad = 6
surf = pg.Surface((w + pad * 2, h + pad * 2), pg.SRCALPHA)
# Multi-offset glow
glow_color = (COL_TITLE[0] // 3, COL_TITLE[1] // 3,
COL_TITLE[2] // 3)
for dx in range(-2, 3):
for dy in range(-2, 3):
if dx == 0 and dy == 0:
continue
glow = self.font_title.render(text, True, glow_color)
surf.blit(glow, (pad + dx, pad + dy))
surf.blit(base, (pad, pad))
self._title_glow = surf
# ── Pre-render: Bar Gradients ─────────────────────────────────────────
def _build_bar_gradients(self):
"""Pre-render horizontal gradient bars for sidebar."""
pg = self.pg
bar_w = 70
bar_h = 12
for label, key, color in SIDEBAR_BARS:
surf = pg.Surface((bar_w, bar_h), pg.SRCALPHA)
for x in range(bar_w):
t = x / bar_w
r = int(color[0] * (0.3 + 0.7 * t))
g = int(color[1] * (0.3 + 0.7 * t))
b = int(color[2] * (0.3 + 0.7 * t))
a = int(180 + 75 * t)
pg.draw.line(surf, (min(r, 255), min(g, 255),
min(b, 255), min(a, 255)),
(x, 0), (x, bar_h - 1), 1)
self._bar_gradients[key] = surf
# ── Audio: Wing Song Tones ───────────────────────────────────────────
def _init_audio(self):
"""Initialize pygame.mixer and pre-generate wing song tones."""
pg = self.pg
try:
pg.mixer.init(frequency=22050, size=-16, channels=1, buffer=512)
import numpy as np
sample_rate = 22050
duration = 0.5 # seconds per tone loop chunk
n_samples = int(sample_rate * duration)
t = np.linspace(0, duration, n_samples, endpoint=False)
for freq in [160, 200, 400]:
# Generate sine wave, low volume (10% amplitude)
mono = (np.sin(2.0 * np.pi * freq * t) * 3276).astype(
np.int16)
# Stereo: duplicate to 2 channels (N, 2)
wave = np.column_stack([mono, mono])
sound = pg.sndarray.make_sound(wave)
self._audio_tones[freq] = sound
self._audio_ready = True
print("[BrainMonitor] Audio tones ready (160/200/400 Hz)",
flush=True)
except Exception as e:
print(f"[BrainMonitor] Audio init failed: {e}", flush=True)
self._audio_ready = False
def _update_audio(self, data):
"""Play or stop wing song audio based on current data."""
if not self._audio_ready:
return
freq = int(data.get('wing_freq', 0))
if freq == self._current_tone_freq:
return # no change
# Stop current tone
if self._current_tone_freq > 0:
tone = self._audio_tones.get(self._current_tone_freq)
if tone:
tone.stop()
# Play new tone
self._current_tone_freq = freq
if freq > 0:
tone = self._audio_tones.get(freq)
if tone:
tone.play(loops=-1) # loop indefinitely
# ── Data Update ───────────────────────────────────────────────────────
def update_data(self, data):
"""Update neural data from main process."""
self.data = data
self._compute_raw_intensities()
self._update_audio(data)
def _compute_raw_intensities(self):
"""Map data dict values to raw region intensities [0-1]."""
d = self.data
for key, region_name in _DATA_KEY_MAP.items():
idx = _REGION_IDX.get(region_name)
if idx is not None:
val = d.get(key, 0.0)
if region_name.startswith('Retina'):
dark_key = ('dark_omm_left' if 'left' in key
else 'dark_omm_right')
dark_count = d.get(dark_key, 0)
self.raw_intensities[idx] = _clamp(dark_count / 100.0)
else:
self.raw_intensities[idx] = _clamp(float(val))
# P9_R mirrors forward drive
fwd = d.get('dn_forward', 0.0)
idx_r = _REGION_IDX.get('P9_R')
if idx_r is not None:
self.raw_intensities[idx_r] = _clamp(float(fwd))
# ── Smoothing & Pulse ─────────────────────────────────────────────────
def _smooth_intensities(self, dt):
"""Exponential smoothing toward raw intensities."""
if dt <= 0:
return
alpha = 1.0 - math.exp(-dt / self.TAU_SMOOTH)
for i in range(len(REGIONS)):
self.smoothed[i] += (self.raw_intensities[i] -
self.smoothed[i]) * alpha
def _compute_pulse(self):
"""Apply sinusoidal pulse modulation to smoothed intensities."""
t = self.frame_time
for i in range(len(REGIONS)):
base = self.smoothed[i]
if base > 0.02:
pulse = 1.0 + self.PULSE_AMP * math.sin(
math.tau * self.PULSE_FREQ * t + self.phase[i])
self.display_intensity[i] = _clamp(base * pulse)
else:
self.display_intensity[i] = base
# ── Consciousness Index Visualization ────────────────────────────────
@staticmethod
def _ci_color(ci):
"""Interpolate CI gradient for a value in [0, 1]."""
ci = max(0.0, min(1.0, ci))
for i in range(len(CI_GRADIENT) - 1):
t0, c0 = CI_GRADIENT[i]
t1, c1 = CI_GRADIENT[i + 1]
if ci <= t1:
f = (ci - t0) / (t1 - t0) if t1 > t0 else 0.0
return (
int(c0[0] + (c1[0] - c0[0]) * f),
int(c0[1] + (c1[1] - c0[1]) * f),
int(c0[2] + (c1[2] - c0[2]) * f),
)
return CI_GRADIENT[-1][1]
def _draw_consciousness(self):
"""Draw CI timeline graph and value bar at top of screen (y=38)."""
d = self.data
pg = self.pg
ci = d.get('consciousness_ci', 0.0)
timeline = d.get('consciousness_timeline', [])
if ci == 0.0 and not timeline:
return # no consciousness data yet
y_base = 38
graph_w = 420
graph_h = 30
bar_x = 440
bar_w = 340
bar_h = 16
# ── Timeline graph (left side) ──
if len(timeline) > 1:
# Background
pg.draw.rect(self.screen, (8, 10, 25),
(10, y_base, graph_w, graph_h))
pg.draw.rect(self.screen, (30, 40, 70),
(10, y_base, graph_w, graph_h), 1)
# Plot CI timeline
n = len(timeline)
step = max(1, graph_w / max(n - 1, 1))
points = []
for i, v in enumerate(timeline):
x = 10 + int(i * step)
y = y_base + graph_h - int(v * graph_h * 0.9) - 2
y = max(y_base + 1, min(y_base + graph_h - 1, y))
points.append((x, y))
if len(points) >= 2:
# Draw filled area
fill_points = list(points) + [
(points[-1][0], y_base + graph_h - 1),
(points[0][0], y_base + graph_h - 1)]
# Draw line on top
for i in range(len(points) - 1):
color = self._ci_color(timeline[min(i, len(timeline) - 1)])
pg.draw.line(self.screen, color,
points[i], points[i + 1], 2)
# Peak markers (gold stars)
peaks = d.get('consciousness_peaks', [])
for step_val, peak_ci in peaks:
# Find approximate x position
for i, e in enumerate(timeline):
if abs(e - peak_ci) < 0.01:
x = 10 + int(i * (graph_w / max(len(timeline) - 1, 1)))
y = y_base + graph_h - int(peak_ci * graph_h * 0.9) - 2
y = max(y_base + 2, y)
pg.draw.circle(self.screen, COL_CI_PEAK, (x, y), 3)
break
# Label
lbl = self.font_sm.render('CI TIMELINE', True, (80, 100, 140))
self.screen.blit(lbl, (12, y_base + 1))
# ── CI value bar (right side) ──
ci_color = self._ci_color(ci)
# Label
ci_txt = self.font.render(f'CONSCIOUSNESS: {ci:.3f}', True, ci_color)
self.screen.blit(ci_txt, (bar_x, y_base))
# Bar background
bar_y = y_base + 18
pg.draw.rect(self.screen, (8, 10, 25),
(bar_x, bar_y, bar_w, bar_h))
pg.draw.rect(self.screen, (30, 40, 70),
(bar_x, bar_y, bar_w, bar_h), 1)
# Bar fill with gradient
fill_w = int(bar_w * min(ci, 1.0))
if fill_w > 0:
for x in range(fill_w):
t = x / bar_w
c = self._ci_color(t)
pg.draw.line(self.screen, c,
(bar_x + x, bar_y + 1),
(bar_x + x, bar_y + bar_h - 2), 1)
def _draw_consciousness_sidebar(self):
"""Draw PHI/GWT/SLF/CMP bars below existing sidebar."""
d = self.data
pg = self.pg
ci = d.get('consciousness_ci', 0.0)
if ci == 0.0 and d.get('consciousness_phi', 0.0) == 0.0:
return # no data yet
x_start = 710
bar_w = 70
bar_h = 12
# Position below existing sidebar (14 bars × 20px + header)
y = 50 + len(SIDEBAR_BARS) * (bar_h + 8) + 20
# Header
header = self.font_sm.render('CONSCIOUSNESS', True, COL_HUD)
self.screen.blit(header, (x_start, y - 14))
y += 4
metrics = [
('PHI', d.get('consciousness_phi', 0.0)),
('GWT', d.get('consciousness_gw', 0.0)),
('SLF', d.get('consciousness_self', 0.0)),
('CMP', d.get('consciousness_cmplx', 0.0)),
]
for label, val in metrics:
val = max(0.0, min(1.0, val))
color = self._ci_color(val)
# Label
lbl_color = color if val > 0.05 else (60, 65, 90)
lbl = self.font_sm.render(label, True, lbl_color)
self.screen.blit(lbl, (x_start, y))
# Bar background
bx = x_start + 32
pg.draw.rect(self.screen, (12, 14, 28),
(bx, y, bar_w, bar_h))
pg.draw.rect(self.screen, (30, 35, 60),
(bx, y, bar_w, bar_h), 1)
# Bar fill
fill_w = int(bar_w * val)
if fill_w > 0:
for x in range(fill_w):
t = x / bar_w
c = self._ci_color(t)
pg.draw.line(self.screen, c,
(bx + x, y + 1),
(bx + x, y + bar_h - 2), 1)
# Value text
val_color = color if val > 0.1 else (55, 60, 85)
val_txt = self.font_sm.render(f'{val:.2f}', True, val_color)
self.screen.blit(val_txt, (bx + bar_w + 4, y))
y += bar_h + 8
# ── Render Frame ──────────────────────────────────────────────────────
def render_frame(self):
"""Render one complete frame with the full pipeline."""
# Timing
now = time.monotonic()
dt = now - self.last_time if self.last_time else 1.0 / 30.0
dt = min(dt, 0.1) # cap at 100ms
self.last_time = now
self.frame_time += dt
self.dash_offset += dt * 40.0 # dash animation speed
# 1. Smooth & pulse
self._smooth_intensities(dt)
self._compute_pulse()
# 2. Background
self.screen.fill(COL_BG)
# 3. Hex grid
self.screen.blit(self._hex_grid, (0, 0))
# 4. Brain silhouette
self.screen.blit(self._brain_sil, (0, 0))
# 5. Dashed connections
self._draw_connections()
# 6. Particles
conn_intensities = self._get_conn_intensities()
self.particles.update(dt, conn_intensities)
self.particles.draw(self.screen, self.pg, CONNECTIONS)
# 7. Region glows
self._draw_regions()
# 8. Scanlines
self.screen.blit(self._scanlines, (0, 0),
special_flags=self.pg.BLEND_RGBA_SUB)
# 9. HUD + sidebar
self._draw_hud()
self._draw_sidebar()
# 10. Consciousness overlay (if data present)
self._draw_consciousness()
self._draw_consciousness_sidebar()
# 11. Flip
self.pg.display.flip()
def _get_conn_intensities(self):
"""Get source-region intensity for each connection."""
result = []
for src_name, dst_name in CONNECTIONS:
src_idx = _REGION_IDX[src_name]
result.append(self.display_intensity[src_idx])
return result
# ── Connections (animated dashes) ─────────────────────────────────────
def _draw_connections(self):
"""Draw animated dashed lines between connected regions."""
pg = self.pg
dash_len = 8
gap_len = 5
segment = dash_len + gap_len
for ci, (src_name, dst_name) in enumerate(CONNECTIONS):
src_idx = _REGION_IDX[src_name]
dst_idx = _REGION_IDX[dst_name]
src = REGIONS[src_idx]
dst = REGIONS[dst_idx]
intensity = self.display_intensity[src_idx]
sx, sy = src[1], src[2]
dx, dy = dst[1] - sx, dst[2] - sy
length = math.sqrt(dx * dx + dy * dy)
if length < 1:
continue
# Color
if intensity < 0.01:
color = (20, 25, 45)
width = 1
else:
sr, sg, sb = src[4]
t = intensity * 0.8
color = (
int(sr * t + 20 * (1 - t)),
int(sg * t + 25 * (1 - t)),
int(sb * t + 45 * (1 - t)),
)
width = 2
# Animated dash offset — proportional to activity
speed_mult = 0.3 + intensity * 0.7
offset = (self.dash_offset * speed_mult) % segment
# Walk along line drawing dashes
ux, uy = dx / length, dy / length
pos = -offset # start before 0 so dashes flow in
while pos < length:
start = max(pos, 0)
end = min(pos + dash_len, length)
if end > start:
x1 = int(sx + ux * start)
y1 = int(sy + uy * start)
x2 = int(sx + ux * end)
y2 = int(sy + uy * end)
pg.draw.line(self.screen, color, (x1, y1), (x2, y2),
width)
pos += segment
# ── Region Glows ──────────────────────────────────────────────────────