I want to collect EEG data in real-time and process it, but the final print epochs are always 0. I noticed that the plot .fif file appears as a straight line for each channel, but I don't know what the problem is, can you help me? Below is my code:
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import brainflow
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
import mne
from mne.channels import make_standard_montage
import os
import numpy as np
# filename = str(input("Namefile: "))
start_time = time.time()
def import_to_mne():
global start_time # Add this line to reference the global variable
while True:
# BoardShim.enable_dev_board_logger()
# Set up board
params = BrainFlowInputParams()
# params.sampling_rate = 250 # Modify sampling rate
params.serial_port = "/dev/ttyUSB0"
# board_id = BoardIds.CYTON_BOARD
# board = BoardShim(board_id, params)
board = BoardShim(BoardIds.CYTON_BOARD.value, params)
board.prepare_session()
board.start_stream()
########################### Something here ########################
time.sleep(10)
########################### Something here ########################
data = board.get_board_data() # Select the initial hz for measurement
board.stop_stream()
board.release_session()
eeg_channels = BoardShim.get_eeg_channels(BoardIds.CYTON_BOARD.value)
eeg_data = data[eeg_channels, :]
eeg_data = eeg_data / 1000000
# Creating MNE objects from brainflow data arrays
ch_types = ['eeg'] * len(eeg_channels)
ch_names = BoardShim.get_eeg_names(BoardIds.SYNTHETIC_BOARD.value) # Added line with current channels names
sfreq = BoardShim.get_sampling_rate(BoardIds.CYTON_BOARD.value)
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(eeg_data, info)
scan_durn = raw._data.shape[1] / raw.info['sfreq']
print('Duration of EEG recording = ', scan_durn, 's, or', scan_durn / 60, 'min.')
# Check if 5 seconds have passed
current_time = time.time()
# elapsed_time = current_time - start_time
# if elapsed_time >= 10.0:
# Save the EEG data as a fif file
file_name = f"/home/aiuser/Downloads/data_acquisition/measurements/{int(current_time)}.fif"
raw.save(file_name)
# Perform further processing on the filtered data
raw_filtered = mne.io.read_raw_fif(file_name, preload=True)
# raw.load_data()
low_freq = 1 # Low cutoff frequency in Hz
high_freq = 30 # High cutoff frequency in Hz
picks = raw_filtered.ch_names
# Apply high-pass filter
raw_filtered.filter(l_freq=low_freq, h_freq=high_freq, picks=picks)
print("...................................")
print(raw_filtered)
print("...................................")
# Define the epoch parameters
tmin =-0.5
tmax = 10
event_duration = 1.0 # Duration of each event segment in seconds
event_overlap = 0.5 # Overlap between consecutive event segments in seconds
# Create artificial events
events = mne.make_fixed_length_events(raw_filtered,duration=1.0, overlap=0.5)
# events = mne.find_events(raw_filtered, threshold=None, min_duration=0.1, shortest_event=0.1)
# Print the events
print("...................................")
print(events)
print(events.shape)
print("...................................")
# # Create epochs from the raw data using the artificial events
epochs = mne.Epochs(raw_filtered, events, event_id=None, tmin=tmin,tmax=tmax,proj=True,
picks=picks, baseline=(None, 0), preload=True)
# reject_criteria = dict(eeg=100e-6)
# epochs.drop_bad(reject=reject_criteria)
print("...................................")
X = epochs.get_data()
print(X.shape)
print("...................................")
#Reset the start time for the next 5-second interval
# start_time = time.time()
import_to_mne()
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import brainflow
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
import mne
from mne.channels import make_standard_montage
import os
import numpy as np
# filename = str(input("Namefile: "))
start_time = time.time()
def import_to_mne():
global start_time # Add this line to reference the global variable
while True:
# BoardShim.enable_dev_board_logger()
# Set up board
params = BrainFlowInputParams()
# params.sampling_rate = 250 # Modify sampling rate
params.serial_port = "/dev/ttyUSB0"
# board_id = BoardIds.CYTON_BOARD
# board = BoardShim(board_id, params)
board = BoardShim(BoardIds.CYTON_BOARD.value, params)
board.prepare_session()
board.start_stream()
########################### Something here ########################
time.sleep(10)
########################### Something here ########################
data = board.get_board_data() # Select the initial hz for measurement
board.stop_stream()
board.release_session()
eeg_channels = BoardShim.get_eeg_channels(BoardIds.CYTON_BOARD.value)
eeg_data = data[eeg_channels, :]
eeg_data = eeg_data / 1000000
# Creating MNE objects from brainflow data arrays
ch_types = ['eeg'] * len(eeg_channels)
ch_names = BoardShim.get_eeg_names(BoardIds.SYNTHETIC_BOARD.value) # Added line with current channels names
sfreq = BoardShim.get_sampling_rate(BoardIds.CYTON_BOARD.value)
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(eeg_data, info)
scan_durn = raw._data.shape[1] / raw.info['sfreq']
print('Duration of EEG recording = ', scan_durn, 's, or', scan_durn / 60, 'min.')
# Check if 5 seconds have passed
current_time = time.time()
# elapsed_time = current_time - start_time
# if elapsed_time >= 10.0:
# Save the EEG data as a fif file
file_name = f"/home/aiuser/Downloads/data_acquisition/measurements/{int(current_time)}.fif"
raw.save(file_name)
# Perform further processing on the filtered data
raw_filtered = mne.io.read_raw_fif(file_name, preload=True)
# raw.load_data()
low_freq = 1 # Low cutoff frequency in Hz
high_freq = 30 # High cutoff frequency in Hz
picks = raw_filtered.ch_names
# Apply high-pass filter
raw_filtered.filter(l_freq=low_freq, h_freq=high_freq, picks=picks)
print("...................................")
print(raw_filtered)
print("...................................")
# Define the epoch parameters
tmin =-0.5
tmax = 10
event_duration = 1.0 # Duration of each event segment in seconds
event_overlap = 0.5 # Overlap between consecutive event segments in seconds
# Create artificial events
events = mne.make_fixed_length_events(raw_filtered,duration=1.0, overlap=0.5)
# events = mne.find_events(raw_filtered, threshold=None, min_duration=0.1, shortest_event=0.1)
# Print the events
print("...................................")
print(events)
print(events.shape)
print("...................................")
# # Create epochs from the raw data using the artificial events
epochs = mne.Epochs(raw_filtered, events, event_id=None, tmin=tmin,tmax=tmax,proj=True,
picks=picks, baseline=(None, 0), preload=True)
# reject_criteria = dict(eeg=100e-6)
# epochs.drop_bad(reject=reject_criteria)
print("...................................")
X = epochs.get_data()
print(X.shape)
print("...................................")
#Reset the start time for the next 5-second interval
# start_time = time.time()
import_to_mne()