Problems in concatenating EEGs

:question: If you have a question or issue with MNE-Python, please include the following info:

  • MNE version: 1.6.0
  • operating system: Windows 10

Hi everyone,
I hope you are doing well.

I am trying to concatenate all the scalp EEG edf files (3-10; maximum 48 hrs) using the following code.
It generally works but I noticed that occasional errors that has an inappropriately scales EEGs.
Please let me know if you have any suggestions.

Thank you very much for your help.

import os
import numpy as np
import matplotlib.pyplot as plt
import mne
import matplotlib
import glob
from natsort import natsorted

#!!change these three
path = r’path to PT _DIR’

group=‘Control’# or ‘Case’
filename=‘filename’

Collect .edf files

all_edf_files = glob.glob(os.path.join(path, ‘*.edf’))

Extract filenames from the paths

all_edf_names = [os.path.basename(file) for file in all_edf_files]

Filter out names starting with ‘._’

filtered_file_names = [name for name in all_edf_names if not name.startswith(‘._’)]

#to sort the data add function
s_filtered_file_names=natsorted(filtered_file_names)

Initialize an empty array (if needed for EEG data)

EEG_list=
time_list=
print(s_filtered_file_names)

for filei in range(len(s_filtered_file_names)):

currentEdf=s_filtered_file_names[filei]
edfpath=os.path.join(path, currentEdf)
raw= mne.io.read_raw_edf(edfpath)

# Assuming edfIdx and currentEdfName are defined earlier in your code
# if filei == 1:
#     record_start=raw.info['meas_date']
time_list.append(raw.info['meas_date'])


raw.drop_channels(['POL E', 'POL PG1', 'POL PG2',  'POL TI', 'POL T2', 'POL EOGR', 'POL EKG1', 'POL EKG2', 'POL RESP','POL RESP1', 'POL RESP2', 'POL EOGL', 'POL A32', 'POL DC01', 'POL DC02', 'POL DC03', 'POL DC04', 'POL $A1', 'POL $A2', 'POL A47'], on_missing='warn')
print(raw.ch_names)

new_names = {ch: ch.replace('POL', '').replace(' ', '').replace('EEG', '').replace('$', '').replace('-Ref', '').replace('T7','T3').replace('T8','T4').replace('P7','T5').replace('P8','T6').replace('FZ','Fz').replace('CZ','Cz').replace('PZ','Pz') for ch in raw.ch_names}

raw.rename_channels(new_names)

#down sample all the data
sf=raw.info['sfreq']


#sort the channels in order.
targetChannelList =['Fp1', 'Fp2', 'F3', 'F4', 'C3', 'C4', 'P3', 'P4', 'O1', 'O2', 'F7', 'F8', 'T3', 'T4', 'T5', 'T6', 'Fz', 'Cz', 'Pz', 'A1', 'A2']
sort_raw=raw.pick_channels(targetChannelList)

if sf>200:#remember that down wampling after cleaning up the eeg channels are important
    sort_raw.resample(200)
    sf=sort_raw.info['sfreq']

EEG_list.append(sort_raw)

EEG_data = [data.get_data() for data in EEG_list]
concat_data = np.concatenate(EEG_data, axis=1)

for eeg in EEG_list:

print(eeg.info)

concat_raw=mne.concatenate_raws(EEG_list)

if concat_raw.get_data().shape[1]>35460000:
print(‘files are cropped to 48h’)
concat_raw = concat_raw.crop(tmin=0, tmax=172800)

for i in range(len(time_list)):
print(f’{s_filtered_file_names[i]}:{time_list[i]}')

filename=os.path.join(group,filename+‘.edf’)
concat_raw.export(filename, fmt=‘edf’,overwrite=True)

In this picture left side is correct amplitude but right side is inappropriately scaled.
EEG in this patient is recorded in 200 and 2000 Hz. I am guessing that this sf might be the issue.

:point_right: :point_right: :point_right: Please edit or remove the above text before submitting your posting. :point_left: :point_left: :point_left: