Repository navigation
Expand file tree
/
Copy pathpython_sonication_pipeline.py
More file actions
509 lines (411 loc) · 15.6 KB
/
Copy pathpython_sonication_pipeline.py
File metadata and controls
509 lines (411 loc) · 15.6 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
from __future__ import annotations
import logging
import math
import os
import sys
import threading
import time
import csv
from pathlib import Path
if os.name == 'nt':
import msvcrt
else:
import select
import numpy as np
from pylsl import StreamInlet, local_clock, resolve_byprop, StreamInfo, StreamOutlet
from hash_func import hash_and_test
import gpype as gp
from openlifu.bf.pulse import Pulse
from openlifu.bf.sequence import Sequence
from openlifu.db import Database
from openlifu.geo import Point
from openlifu.io.LIFUInterface import LIFUInterface
from openlifu.plan.solution import Solution
# main pipeline from theta detection to LIFU triggering, sends markers to psychopy and EEG files
# logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Prevent duplicate handlers and cluttered terminal output
if not logger.hasHandlers():
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
logger.propagate = False
# Sending markers to EEG
eeg_trigger_info = StreamInfo('EEG_LIFU_events', 'Markers', 1, 0, 'string')
eeg_trigger_outlet = StreamOutlet(eeg_trigger_info)
logger.info("LIFU to PsychoPy LSL outlet created.")
#sending markers to psychopy
lifu_num_info = StreamInfo('PsychoPy_numeric', 'Markers', 1, 0, 'float32')
lifu_num_outlet = StreamOutlet(lifu_num_info)
logger.info("LIFU to PsychoPy LSL outlet created.")
#global variables for threads
RUNNING = True
#saving markers to csv
def record_lifu_numeric():
print("Waiting for LIFU_numeric stream...")
streams = resolve_byprop("name", "EEG_LIFU_events", timeout=30)
if not streams:
print("No LIFU_numeric stream found.")
return
inlet = StreamInlet(streams[0])
print("Connected to LIFU_numeric stream.")
with open(f"lifu_markers_1_{hash_and_test}.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Time", "marker", "LSL_timestamp"]) # Header
while RUNNING:
sample, ts= inlet.pull_sample(timeout=1.0)
if sample is None:
continue
if sample:
relative_ts = ts - eeg_start_lsl
writer.writerow([relative_ts, sample[0],ts])
f.flush() # <--- forces Python to write
os.fsync(f.fileno()) # <--- forces OS to write
print("Wrote marker:", sample[0])
def record_eeg_lsl():
"""
Record EEG data from LSL to CSV for offline processing.
This is separate from the g.Pype pipeline's own CSV writing.
"""
print("Waiting for EEG LSL stream...")
streams = resolve_byprop('type', 'EEG', timeout=30)
if not streams:
print("No EEG LSL stream found.")
return
inlet = StreamInlet(streams[0])
print("Connected to EEG LSL stream.")
with open(f"eeg_LSL_gpype{hash_and_test}.csv", "w", newline="") as f:
writer = csv.writer(f)
header_written = False
while RUNNING:
sample, ts = inlet.pull_sample(timeout=1.0)
if sample is None:
continue
if not header_written:
header = ["Time"] + [f"Ch{i:02d}" for i in range(1, len(sample)+1)]
writer.writerow(header)
header_written = True
writer.writerow([ts] + sample)
f.flush()
os.fsync(f.fileno())
#print(f"Wrote EEG sample at {ts:.6f}s")
# beamforming parameters (demo code)
xInput = 0
yInput = 0
zInput = 50
frequency_kHz = 400
voltage = 20.0
duration_msec = 3
interval_msec = 10
num_modules = 1
use_external_power_supply = False
peak_to_peak_voltage = voltage * 2
db_path = Path(r"C:\Users\jshin\Downloads\OpenLIFU-python\OpenLIFU-python\db_dvc")
db = Database(db_path)
arr = db.load_transducer(f"openlifu_{num_modules}x400_evt1")
arr.sort_by_pin()
target = Point(position=(xInput, yInput, zInput), units="mm")
focus = target.get_position(units="mm")
positions = arr.get_positions(units="mm")
distances = np.sqrt(np.sum((focus - positions) ** 2, axis=1)).reshape(1, -1)
speed_of_sound = 1500
tof = distances * 1e-3 / speed_of_sound
delays = tof.max() - tof
apodizations = np.ones((1, arr.numelements()))
logger.info("Starting LIFU Test Script...")
interface = LIFUInterface(ext_power_supply=use_external_power_supply)
tx_connected, hv_connected = interface.is_device_connected()
interface.hvcontroller.turn_12v_on()
time.sleep(0.8)
interface.stop_monitoring()
del interface
interface = LIFUInterface(ext_power_supply=False)
tx_connected, hv_connected = interface.is_device_connected()
if not tx_connected:
raise RuntimeError("TX not connected after 12V power-up")
interface.hvcontroller.turn_hv_on()
time.sleep(0.5)
if not use_external_power_supply and not tx_connected:
logger.warning("TX device not connected. Attempting to turn on 12V...")
interface.hvcontroller.turn_hv_on()
time.sleep(2)
interface.hvcontroller.turn_12v_on()
time.sleep(2)
interface.stop_monitoring()
del interface
time.sleep(1)
logger.info("Reinitializing LIFU interface after powering 12V...")
interface = LIFUInterface(ext_power_supply=use_external_power_supply)
tx_connected, hv_connected = interface.is_device_connected()
if not use_external_power_supply:
if hv_connected:
logger.info(f" HV Connected: {hv_connected}")
else:
logger.error("HV NOT fully connected.")
sys.exit(1)
else:
logger.info("Using external power supply")
if tx_connected:
logger.info(f" TX Connected: {tx_connected}")
logger.info("LIFU Device fully connected.")
else:
logger.error("TX NOT fully connected.")
sys.exit(1)
if not interface.txdevice.ping():
logger.error("Failed to ping the transmitter device.")
sys.exit(1)
if not use_external_power_supply and not interface.hvcontroller.ping():
logger.error("Failed to ping the console device.")
sys.exit(1)
if not use_external_power_supply:
try:
console_firmware_version = interface.hvcontroller.get_version()
logger.info(f"Console Firmware Version: {console_firmware_version}")
except Exception as e:
logger.error(f"Error querying console firmware version: {e}")
try:
tx_firmware_version = interface.txdevice.get_version()
logger.info(f"TX Firmware Version: {tx_firmware_version}")
except Exception as e:
logger.error(f"Error querying TX firmware version: {e}")
logger.info("Enumerate TX7332 chips")
num_tx_devices = interface.txdevice.enum_tx7332_devices()
if num_tx_devices == 0:
raise ValueError("No TX7332 devices found.")
elif num_tx_devices == num_modules * 2:
logger.info(f"Number of TX7332 devices found: {num_tx_devices}")
numelements = 32 * num_tx_devices
else:
raise Exception(f"Number of TX7332 devices found: {num_tx_devices} != 2x{num_modules}")
logger.info(f'Apodizations: {apodizations}')
logger.info(f'Delays: {delays}')
pulse = Pulse(frequency=frequency_kHz * 1e3, duration=duration_msec * 1e-3)
sequence = Sequence(
pulse_interval=interval_msec * 1e-3,
pulse_count=int(60 / (interval_msec * 1e-3)),
pulse_train_interval=0,
pulse_train_count=1
)
pin_order = np.argsort([el.pin for el in arr.elements])
solution = Solution(
delays=delays[:, pin_order],
apodizations=apodizations[:, pin_order],
transducer=arr,
pulse=pulse,
voltage=voltage,
sequence=sequence
)
interface.set_solution(
solution=solution,
profile_index=1,
profile_increment=False,
trigger_mode="continuous"
)
logger.info("Beamforming solution loaded.")
# theta sonication loop (eeg + demo code)
SONICATION_TIME = 5 #seconds i believe
COOLDOWN_TIME = 7 #sonication time + cooldown time
THETA_THRESHOLD_Z = 1.5 # z-score threshold
MU = 2.32
SIGMA = 4.18
MAD_THRESHOLD = 60 # for artifact rejection in baseline collection
INITIAL_CUTOFF = 100.0 # initial power threshold to exclude extreme artifacts
BUFFER_SIZE = 500
sonication_enabled = True
def listen_for_start():
global sonication_enabled
inlet = StreamInlet(resolve_byprop("name", "PsychoPyMarkers")[0])
while True:
sample, ts = inlet.pull_sample(timeout=0.1)
if sample and sample[0] == "START_EXPERIMENT":
print("Experiment started — enabling LIFU.")
eeg_trigger_outlet.push_sample(["START_EXPERIMENT_RECEIVED"])
sonication_enabled = True
break
def theta_trigger_loop():
logger.info("Waiting for theta LSL stream (type='EEG')...")
streams = resolve_byprop('name', 'EEG_gpype', timeout=30)
if not streams:
logger.error("No EEG LSL stream found for theta.")
return
inlet = StreamInlet(streams[0])
logger.info("Connected to EEG LSL stream for theta.")
#theta_history = []
last_trigger_time = 0
last_theta_val = None
logger.info("Starting theta-based closed-loop monitoring...")
buffer = []
while RUNNING:
sample, ts = inlet.pull_sample(timeout=1.0)
if sample is None:
break
theta_val = sample[11] # Smoothed Power channel
if last_theta_val is not None and theta_val == last_theta_val:
continue
last_theta_val = theta_val
# update rolling buffer
# not enough data yet → just collect
if len(buffer) <= 200:
if theta_val < INITIAL_CUTOFF:
buffer.append(theta_val)
eeg_trigger_outlet.push_sample(["collecting_baseline"])
continue
if len(buffer) > BUFFER_SIZE:
buffer.pop(0)
arr = np.array(buffer)
median = np.median(arr)
mad = np.median(np.abs(arr - median)) + 1e-6
z = abs(theta_val - median) / mad
if z > MAD_THRESHOLD:
logger.info(
f"Artifact detected: {theta_val:.1f} (median={median:.1f}, MAD={mad:.1f}, z={z:.1f})"
)
continue # skip adding this sample to baseline
# clean sample → keep
buffer.append(theta_val)
#theta_z =np.abs(theta_val - MU) / SIGMA
#ts_rel = ts - eeg_start_lsl
# with open(f"theta_z_values_{hash_and_test}.csv", "a") as f:
# f.write(f"{ts_rel},{theta_z}\n")
now = ts
print(f"sonication_enabled={sonication_enabled}")
if sonication_enabled and theta_val < MAD_THRESHOLD and theta_val > THETA_THRESHOLD_Z and (now - last_trigger_time) > COOLDOWN_TIME:
logger.info(f"Theta threshold crossed: z={theta_val:.2f}. Triggering LIFU.")
eeg_trigger_outlet.push_sample(["LIFU_ON"])
if interface.txdevice.start_trigger():
logger.info("Trigger Running...") #once ttl works we will take this out
lifu_num_outlet.push_sample([1.0])
for i in range(int(SONICATION_TIME),0,-1):
logger.info(f"Sonication stopping in {i} seconds")
time.sleep(1)
# Wait for threads to finish
# user_input.join()
# time.sleep(0.5) # Give the logging thread time to finish
if interface.txdevice.stop_trigger():
eeg_trigger_outlet.push_sample(["LIFU_OFF"])
lifu_num_outlet.push_sample([0.0])
last_trigger_time = now
logger.info("Theta-triggered sonication complete.")
else:
logger.error("Failed to stop trigger.")
else:
logger.error("Failed to get trigger setting.")
# gp pipeline for EEG headset
fs = 250
def run_pipeline():
global eeg_start_lsl
app = gp.MainApp()
p = gp.Pipeline()
source = gp.BCICore8()
bandpass = gp.Bandpass(f_lo = 1.0, f_hi = 30.0, order = 4)
theta_filter = gp.Bandpass(f_lo=4.0, f_hi=7.0, order=4)
notch60 = gp.Bandstop(f_lo=58, f_hi=62, order=4)
power = gp.Equation("in**2")
moving_average = gp.MovingAverage(window_size=50)
decimator = gp.Decimator(decimation_factor=10)
hold = gp.Hold()
theta_z_eq = gp.Equation("(in - 2.32) / 4.18")
trigger_scaling = gp.Equation("in/2")
merger = gp.Router(
input_channels={
"channel_1": [0],
"channel_2": [1],
"channel_3": [2],
"channel_4": [3],
"channel_5": [4],
"channel_6": [5],
"channel_7": [6],
"theta_filter": [0],
"power": [0],
"moving_average": [0],
"theta_z": [0],
"hold": [0],
"channel_8": [7]
},
output_channels=[gp.Router.ALL],
)
scope = gp.TimeSeriesScope(
amplitude_limit=20, time_window=10,
channel_names=[
"Channel 1",
"Channel 2",
"Channel 3",
"Channel 4",
"Channel 5",
"Channel 6",
"Channel 7",
"Theta Filter (4-7Hz)",
"Instantaneous Power",
"Smoothed Power",
"Theta Z-Score",
"Decimated Power",
"Trigger Channel"
]
)
sender = gp.LSLSender(stream_name = "EEG_gpype") # default name/type; we’ll resolve by type='EEG'
online_writer = gp.CsvWriter(file_name=f"thetaEEG_gpype_{hash_and_test}.csv")
offline_writer = gp.CsvWriter(file_name=f"thetaEEG_full_{hash_and_test}.csv")
p.connect(source, notch60)
p.connect(notch60, bandpass)
p.connect(bandpass,theta_filter)
p.connect(theta_filter, power)
p.connect(power, moving_average)
p.connect(moving_average, theta_z_eq)
p.connect(theta_z_eq, decimator)
p.connect(decimator, hold)
p.connect(bandpass, trigger_scaling)
p.connect(source, merger["channel_1"])
p.connect(source, merger["channel_2"])
p.connect(source, merger["channel_3"])
p.connect(source, merger["channel_4"])
p.connect(source, merger["channel_5"])
p.connect(source, merger["channel_6"])
p.connect(source, merger["channel_7"])
p.connect(theta_filter, merger["theta_filter"])
p.connect(power, merger["power"])
p.connect(moving_average, merger["moving_average"])
p.connect(hold, merger["hold"])
p.connect(theta_z_eq, merger["theta_z"])
p.connect(trigger_scaling, merger["channel_8"])
p.connect(merger, scope)
p.connect(merger, sender)
p.connect(merger, online_writer)
p.connect(source, offline_writer)
app.add_widget(scope)
p.start()
eeg_start_lsl = local_clock() # set global start time for LSL relative timestamps
try:
app.run() # blocks until GUI close or Ctrl+C
except KeyboardInterrupt:
logger.info("Pipeline interrupted, stopping g.Pype...")
finally:
p.stop()
# p.start()
# app.run()
# p.stop()
if __name__ == "__main__":
try:
# Start thread to listen for experiment start trigger from PsychoPy
listen_for_psychopy_thread = threading.Thread(target=listen_for_start, daemon=True)
listen_for_psychopy_thread.start()
# Start theta closed-loop thread
theta_thread = threading.Thread(target=theta_trigger_loop, daemon=False)
theta_thread.start()
# Start LIFU marker recording thread
lifu_record_thread = threading.Thread(target=record_lifu_numeric, daemon=False)
lifu_record_thread.start()
# Start EEG recording thread
eeg_record_thread = threading.Thread(target=record_eeg_lsl, daemon=False)
eeg_record_thread.start()
# Start g.Pype pipeline
run_pipeline()
finally:
# ALWAYS stop threads when pipeline stops
RUNNING = False
try:
interface.hvcontroller.turn_hv_off()
except:
pass
theta_thread.join()
lifu_record_thread.join()