Creating a raw object manually

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

See: Creating MNE-Python data structures from scratch — MNE 1.6.1 documentation


1 Like

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

# Create the Raw object
raw =, 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

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 )
—> 23 raw =, info)
24 raw.plot()

in init(self, data, info, first_samp, copy, verbose)

/usr/local/lib/python3.10/dist-packages/mne/io/array/ 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

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.