I have some signals recorded in a text file where there are 2 columns and many rows, 1 column is time in ms, 2 column is amplitude. Is it possible to manually combine these signals so that I end up creating a raw object where the first signal is in the first channel, the second in the second channel, etc. And to then be able to work normally with the raw object, for example, to expose the montage, etc. This is quite an important question for me, and I would be immensely grateful if anyone knows anything about this. Thanks in advance
Thanks, but I am having problems in the same place ācreating raw objectsā. The problem is that the tutorial shows how to create raw using just the sine and cosine signals that were obtained in the program via np.sin and np.cos. In my case, I load text files through numpy, which of course turns them into np array, and then I canāt create raw because of this. I specify second columns from several files instead of āvaluesā like in the tutorial
You mention that your file only has two columns (time and amplitude), but later you state that you have multiple channels. Does that mean you have one file per channel?
Yes, each file is the channels and I need to put them together in raw
Hereās a solution proposed by ChatGPT:
import mne
import numpy as np
# Placeholder for where you'd load your data
# Let's assume you have N channels and each file is named 'channel_X.txt'
num_channels = 2 # Example for 2 channels, adjust accordingly
sampling_frequency = 1000 # Adjust based on your data
# Initialize an empty list to store data from each channel
data_list = []
for i in range(num_channels):
# Load data from text file
# Assuming the format is: time(ms) amplitude
data = np.loadtxt(f'channel_{i+1}.txt')
# Append amplitude data to the list, discard time since it's uniform across channels
data_list.append(data[:, 1])
# Convert the list of arrays into a 2D numpy array (channels x timepoints)
data_array = np.vstack(data_list)
# Convert data from (channels x timepoints) to (timepoints x channels) as required by MNE
data_array = data_array.T
# Creating an MNE Info object
info = mne.create_info(
ch_names=[f'ch_{i}' for i in range(num_channels)],
ch_types=['eeg'] * num_channels, # Adjust this if your channels have different types
sfreq=sampling_frequency
)
# Create the Raw object
raw = mne.io.RawArray(data_array, info)
# Now, you can work with the raw object as usual, e.g., plotting, filtering
# If you have montage information, you can set it here
# montage = mne.channels.make_standard_montage('standard_1020')
# raw.set_montage(montage)
# Example: Plot the data
raw.plot()
Interesting, but the code gives an error. Apparently it tries to take all lines of files as channels. I specified that the number of channels is 3, because Iām testing the code on 3 files so far. I also inserted a small condition to check that the number of rows in the arrays match, because it turns out that one file can have 50000 rows and the other 60000, and it gives an error in the line data_list.append(data[:, 1]). If they donāt match, I trim the larger array to the size of the smaller array.
My addition:
for i in range(num_channels):
data = np.loadtxt(fā1_class_{i+1}.datā)
if i == 0:
length_check = len(data)
elif len(data) != length_check:
min_length = min(len(data), length_check)
data = data[:min_length]
data_list.append(data[:, 1])
The error:
ValueError Traceback (most recent call last)
in <cell line: 23>()
21 )
22
ā> 23 raw = mne.io.RawArray(data_array, info)
24 raw.plot()
in init(self, data, info, first_samp, copy, verbose)
/usr/local/lib/python3.10/dist-packages/mne/io/array/array.py in init(self, data, info, first_samp, copy, verbose)
67 )
68 if len(data) != len(info[āch_namesā]):
ā> 69 raise ValueError(
70 "len(data) (%s) does not match "
71 ālen(info[āch_namesā]) (%s)ā % (len(data), len(info[āch_namesā]))
ValueError: len(data) (529992) does not match len(info[āch_namesā]) (3)
Maybe this transpose operation is incorrect, then.
Yes, you are right. Thank you very much, youāve done a lot for me
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.