Spaces:
Build error
Build error
Yann Bouteiller
commited on
Commit
·
c57f4b2
1
Parent(s):
0c284d8
fixed some bugs
Browse files- portiloop/capture.py +19 -15
- portiloop/hardware/frontend.py +3 -3
portiloop/capture.py
CHANGED
|
@@ -5,6 +5,7 @@ from time import sleep
|
|
| 5 |
import time
|
| 6 |
import numpy as np
|
| 7 |
import os
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
import multiprocessing as mp
|
|
@@ -60,7 +61,7 @@ DEFAULT_FRONTEND_CONFIG = [
|
|
| 60 |
FRONTEND_CONFIG = [
|
| 61 |
0x3E, # ID (RO)
|
| 62 |
0x95, # CONFIG1 [95] [1, DAISY_EN(bar), CLK_EN, 1, 0, DR[2:0]] : Datarate = 500 SPS
|
| 63 |
-
|
| 64 |
0xFC, # CONFIG3 [E0] [PD_REFBUF(bar), 1, 1, BIAS_MEAS, BIASREF_INT, PD_BIAS(bar), BIAS_LOFF_SENS, BIAS_STAT] : Power-down reference buffer, no bias
|
| 65 |
0x00, # No lead-off
|
| 66 |
0x62, # CH1SET [60] [PD1, GAIN1[2:0], SRB2, MUX1[2:0]] set to measure BIAS signal
|
|
@@ -106,6 +107,8 @@ LEADOFF_CONFIG = [
|
|
| 106 |
0x00, # Lead-off negative status (RO)
|
| 107 |
0x00, # All GPIOs as output ?
|
| 108 |
0x20, # Enable SRB1
|
|
|
|
|
|
|
| 109 |
]
|
| 110 |
|
| 111 |
EDF_PATH = Path.home() / 'workspace' / 'edf_recording'
|
|
@@ -164,7 +167,7 @@ def mod_config(config, datarate, channel_modes):
|
|
| 164 |
|
| 165 |
|
| 166 |
def filter_24(value):
|
| 167 |
-
return (value * 4.5) / (2**23 - 1) # 23 because 1 bit is lost for sign
|
| 168 |
|
| 169 |
|
| 170 |
def filter_2scomplement_np(value):
|
|
@@ -1206,23 +1209,21 @@ class Capture:
|
|
| 1206 |
return x & 1 << n != 0
|
| 1207 |
|
| 1208 |
try:
|
| 1209 |
-
frontend.write_regs(0x00,
|
| 1210 |
frontend.start()
|
| 1211 |
start_time = time.time()
|
| 1212 |
current_time = time.time()
|
| 1213 |
while current_time - start_time < 2:
|
| 1214 |
current_time = time.time()
|
| 1215 |
-
|
| 1216 |
-
leadoff_p = new_config[18]
|
| 1217 |
-
leadoff_n = new_config[19]
|
| 1218 |
|
| 1219 |
# Check if any of the negative bits are set and initialize the impedance array
|
| 1220 |
-
|
| 1221 |
-
|
| 1222 |
-
# Check all other values for all electrodes
|
| 1223 |
-
for i in range(2, 9):
|
| 1224 |
-
impedance_check.append(is_set(leadoff_p, i))
|
| 1225 |
|
|
|
|
|
|
|
|
|
|
| 1226 |
def print_impedance(impedance):
|
| 1227 |
names = ["Ref", "Ch2", "Ch3", "Ch4", "Ch5", "Ch6", "Ch7", "Ch8"]
|
| 1228 |
vals = [' Y ' if val else ' N ' for val in impedance]
|
|
@@ -1378,19 +1379,22 @@ class Capture:
|
|
| 1378 |
continue
|
| 1379 |
|
| 1380 |
n_array = np.array([point])
|
| 1381 |
-
|
| 1382 |
|
| 1383 |
if filter:
|
| 1384 |
-
n_array = fp.filter(
|
|
|
|
|
|
|
| 1385 |
|
| 1386 |
filtered_point = n_array.tolist()
|
| 1387 |
|
| 1388 |
if lsl:
|
| 1389 |
-
|
|
|
|
| 1390 |
lsl_outlet.push_sample(filtered_point[-1])
|
| 1391 |
|
| 1392 |
if stimulation_delayer is not None:
|
| 1393 |
-
stimulation_delayer.add_point(
|
| 1394 |
|
| 1395 |
with self._pause_detect_lock:
|
| 1396 |
pause = self._pause_detect
|
|
|
|
| 5 |
import time
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
+
from copy import deepcopy
|
| 9 |
from pathlib import Path
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
import multiprocessing as mp
|
|
|
|
| 61 |
FRONTEND_CONFIG = [
|
| 62 |
0x3E, # ID (RO)
|
| 63 |
0x95, # CONFIG1 [95] [1, DAISY_EN(bar), CLK_EN, 1, 0, DR[2:0]] : Datarate = 500 SPS
|
| 64 |
+
0xD0, # CONFIG2 [C0] [1, 1, 0, INT_CAL, 0, CAL_AMP0, CAL_FREQ[1:0]]
|
| 65 |
0xFC, # CONFIG3 [E0] [PD_REFBUF(bar), 1, 1, BIAS_MEAS, BIASREF_INT, PD_BIAS(bar), BIAS_LOFF_SENS, BIAS_STAT] : Power-down reference buffer, no bias
|
| 66 |
0x00, # No lead-off
|
| 67 |
0x62, # CH1SET [60] [PD1, GAIN1[2:0], SRB2, MUX1[2:0]] set to measure BIAS signal
|
|
|
|
| 107 |
0x00, # Lead-off negative status (RO)
|
| 108 |
0x00, # All GPIOs as output ?
|
| 109 |
0x20, # Enable SRB1
|
| 110 |
+
0x00,
|
| 111 |
+
0x02,
|
| 112 |
]
|
| 113 |
|
| 114 |
EDF_PATH = Path.home() / 'workspace' / 'edf_recording'
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
def filter_24(value):
|
| 170 |
+
return (value * 4.5) / (2**23 - 1) / 24.0 * 1e6 # 23 because 1 bit is lost for sign
|
| 171 |
|
| 172 |
|
| 173 |
def filter_2scomplement_np(value):
|
|
|
|
| 1209 |
return x & 1 << n != 0
|
| 1210 |
|
| 1211 |
try:
|
| 1212 |
+
frontend.write_regs(0x00, LEADOFF_CONFIG)
|
| 1213 |
frontend.start()
|
| 1214 |
start_time = time.time()
|
| 1215 |
current_time = time.time()
|
| 1216 |
while current_time - start_time < 2:
|
| 1217 |
current_time = time.time()
|
| 1218 |
+
reading = frontend.read()
|
|
|
|
|
|
|
| 1219 |
|
| 1220 |
# Check if any of the negative bits are set and initialize the impedance array
|
| 1221 |
+
# impedance_check = [any([is_set(leadoff_n, i) for i in range(2, 9)])]
|
| 1222 |
+
impedance_check = [any([reading.loff_n(i) for i in range(7)])]
|
|
|
|
|
|
|
|
|
|
| 1223 |
|
| 1224 |
+
for i in range(7):
|
| 1225 |
+
impedance_check.append(reading.loff_p(i))
|
| 1226 |
+
|
| 1227 |
def print_impedance(impedance):
|
| 1228 |
names = ["Ref", "Ch2", "Ch3", "Ch4", "Ch5", "Ch6", "Ch7", "Ch8"]
|
| 1229 |
vals = [' Y ' if val else ' N ' for val in impedance]
|
|
|
|
| 1379 |
continue
|
| 1380 |
|
| 1381 |
n_array = np.array([point])
|
| 1382 |
+
n_array_raw = filter_np(n_array)
|
| 1383 |
|
| 1384 |
if filter:
|
| 1385 |
+
n_array = fp.filter(deepcopy(n_array_raw))
|
| 1386 |
+
else:
|
| 1387 |
+
n_array = n_array_raw
|
| 1388 |
|
| 1389 |
filtered_point = n_array.tolist()
|
| 1390 |
|
| 1391 |
if lsl:
|
| 1392 |
+
raw_point = n_array_raw.tolist()
|
| 1393 |
+
lsl_outlet_raw.push_sample(raw_point[-1])
|
| 1394 |
lsl_outlet.push_sample(filtered_point[-1])
|
| 1395 |
|
| 1396 |
if stimulation_delayer is not None:
|
| 1397 |
+
stimulation_delayer.add_point(filtered_point[channel-1])
|
| 1398 |
|
| 1399 |
with self._pause_detect_lock:
|
| 1400 |
pause = self._pause_detect
|
portiloop/hardware/frontend.py
CHANGED
|
@@ -33,15 +33,15 @@ class Reading:
|
|
| 33 |
def channels(self):
|
| 34 |
return self._channels
|
| 35 |
|
| 36 |
-
def gpio(idx: int):
|
| 37 |
assert 0 <= idx <= 3, "Invalid gpio index"
|
| 38 |
return (self.gpios >> idx) & 0x01 == 0x01
|
| 39 |
|
| 40 |
-
def loff_p(idx: int):
|
| 41 |
assert 0 <= idx <= 7, "Invalid loff index"
|
| 42 |
return (self.loff_statp >> idx) & 0x01 == 0x01
|
| 43 |
|
| 44 |
-
def loff_n(idx: int):
|
| 45 |
assert 0 <= idx <= 7, "Invalid loff index"
|
| 46 |
return (self.loff_statn >> idx) & 0x01 == 0x01
|
| 47 |
|
|
|
|
| 33 |
def channels(self):
|
| 34 |
return self._channels
|
| 35 |
|
| 36 |
+
def gpio(self, idx: int):
|
| 37 |
assert 0 <= idx <= 3, "Invalid gpio index"
|
| 38 |
return (self.gpios >> idx) & 0x01 == 0x01
|
| 39 |
|
| 40 |
+
def loff_p(self, idx: int):
|
| 41 |
assert 0 <= idx <= 7, "Invalid loff index"
|
| 42 |
return (self.loff_statp >> idx) & 0x01 == 0x01
|
| 43 |
|
| 44 |
+
def loff_n(self, idx: int):
|
| 45 |
assert 0 <= idx <= 7, "Invalid loff index"
|
| 46 |
return (self.loff_statn >> idx) & 0x01 == 0x01
|
| 47 |
|