Skip to content

Commit 9256de0

Browse files
committed
Add csv loader
1 parent b49d264 commit 9256de0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/goofi/nodes/inputs/eegrecording.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import os
12
import threading
23
import time
34
from typing import Any, Dict, Tuple
45

56
import mne
7+
import pandas as pd
68
from mne.datasets import eegbci
79
from mne_realtime import MockLSLStream
810

@@ -19,6 +21,10 @@ def config_params():
1921

2022
def stream_thread(self):
2123
"""Load the appropriate data and start the stream. Then wait until running is set to False."""
24+
while not self.params.recording.use_example_data.value and not os.path.exists(self.params.recording.file_path.value):
25+
print("File path cannot be empty if 'Use Example Data' is False.")
26+
time.sleep(1)
27+
2228
if self.params.recording.use_example_data.value:
2329
raw = mne.concatenate_raws(
2430
[mne.io.read_raw(p, preload=True, verbose=False) for p in eegbci.load_data(1, [1, 2])],
@@ -27,6 +33,15 @@ def stream_thread(self):
2733
eegbci.standardize(raw)
2834
# scale the data for better default behavior
2935
raw.apply_function(lambda x: x * 1e4)
36+
elif self.params.recording.file_path.value.endswith(".csv"):
37+
# load data from csv file
38+
df = pd.read_csv(self.params.recording.file_path.value, index_col=0)
39+
df = df.select_dtypes(include=["float"])
40+
data = df.transpose().to_numpy()
41+
42+
# TODO: make sfreq a parameter
43+
info = mne.create_info(ch_names=df.columns.tolist(), ch_types=["eeg"] * data.shape[0], sfreq=256)
44+
raw = mne.io.RawArray(data, info)
3045
else:
3146
# load data from file
3247
raw = mne.io.read_raw(self.params.recording.file_path.value, preload=True)
@@ -60,9 +75,7 @@ def setup(self, init: bool = True):
6075
# both use_example_data and file_path are set
6176
# TODO: add proper logging
6277
print("Both 'use_example_data' and 'file_path' are set. Using example data.")
63-
elif self.params.recording.file_path.value == "":
64-
# either use example data or a file path must be set
65-
raise ValueError("File path cannot be empty if 'Use Example Data' is False.")
78+
6679
assert self.params.recording.stream_name.value != "", "Stream name cannot be empty."
6780

6881
# start the stream

0 commit comments

Comments
 (0)