Issue Creating a raw data object from scratch

Hey guys
I hope youre all well
My name is Julian and I am attempting a BCI project for my undergrad final major project in robotics.

I have been given an ADinstruments Powerlab system as my EEG device, this only interfaces with a program called labchart, which only exports .adicht files

This file is not supported by MNE, However, I found an SDK that converts the .adicht file to a NumPy array, and as MNE is built on NumPy, I figured there must be way to use MNE as normal using this generated NumPy array.

I followed the tutorial for Creating MNE-Python data structures from scratch

Unfortunately, MNE comprehends the Array as such :

When using matplotlib on the array straight from the SDK it looks like this :
Plotting the results from the AD instruments SDK
These signals were taken very quickly as I just needed to get an .adicht file so i could figure out interfacing it with MNE, dont worry about how wrong they look, the matplot representation is ‘correct’, as in it was how they appeared on the Labchart software.

I hope that I am just doing something wrong, although I fear that the format of the numpy array from the SDK just wont be interpretable by MNE.
Here are as much details as I can think to give, hopefully somebody can spot a fault or suggest a possible solution, any help would be greatly appreciated

I am a new user, cannot embed more than 3 images or share more than 5 links, so apologies for the broken imgur links, Id advise tpying the imgur part, and copying the rest

The SDK captures the channels from the .adicht file individually
imgur. com/s5rdt1B

Printing the Array looks like this, the NumPy shape is (14850,)
imgur. com/PxIVaCX

In the turorial, they build the array using np.array :
imgur. com/Ecysy6M

so although the data I have is already an array, I attempted this
imgur. com/bw3HfhU

When matplot’ing the data, it looks like this :
imgur. com/XfJnZhD

and the MNE plot looks like this :
imgur. com/xLI1RBX

As the data is already in array format, I figured i could just assign it straight to the data variable in
simulated_raw = mne.io.RawArray(data, info)
However my EEG data is in two seperate arrays.
When i try to concatenate these arrays into one, i get the following error :
imgur. com/W5kkUDx

So im not sure where to go from here, I need to find a way to convert these two Arrays to a single one, or find another way to get the .adicht arrays to work with MNE.

Ive already spent 2 months learning everything I need to know about BCI, EEG, signal processing and classification, Im confident about it all, but if this doesnt work out, ill be at a dead end, with too little time till my submission to start a new project from scratch.

Any help would be greatly appreicated, and im happy to provide any further details

Kind regards,
Julian

as the shape of the NumPy Array is (14850,) I cant plug it into the raw array function
Cant plug it directly

I was able to merge the two 1D arrays using np.vstack(FP1, FP2) which resulted in the shape (14850,2)
when i matplot this new 2D array i get the normal data :
Plotting the results from the AD instruments SDK

But as the 2D array must be of shape (n_channels, n_samples) I need to flip the array.
So using np.transpose i was able to change it to the shape (2, 14850) which should be correct.
However when i matplot this transposed array, the data looks like this :
Making an array from both channels

and the mne interprets it in the same incorrect way.

Mabye this data is correct? i just need to redefine the vertical axis, but im not sure how to do that

any help appreciated

The first Matplotlib plot indicates that your signals are in µV, but MNE expects them to be in V. Therefore, multiply the array you get directly from your system by 1e-6 and proceed with creating the RawArray. I think this should solve your issue.

Youre a lifesaver!!!
I ended up multiplying by 1e-4 to get a recognisable signal, as 1e-6 returned flat lines

I believe the signals are switched, the Fp1 signal should be Fp2, probably just a result of transposing the numpy array.

My data file was for a 148 second sample.
The MNE plot only shows 10 seconds, would you know how to change this time scale so it shows the whole thing?

again, thank you so much, you have restored hope in my ability to fulfil this project

1 Like

If you use the raw browsers (i.e. raw.plot()), you can press “?” to see a list of available keyboard (and mouse) shortcuts. There are shortcuts for changing the time scale. You can also pass duration=148 to your raw.plot() call to immediately show the desired time range.

Hi,

I have similar issue with adicht files. I am also recording EcoG using bipolar electrodes. But I don’t know how to do it as I am not familiar with both phyton and MNE software. I’ve just started learning the Python program. I will be glad if you can help me on how to convert my adicht files to MNE compliant. I would appreciate it if you could share the source document for the next steps.

Sure thing! I will try to send you my code with instructive comments tomorrow!

I will be very grateful. Thank you.

# Setting the MNE info for the EEG data
sampling_freq = 1000  
ch_names = ['C3', 'C4']
ch_types = ['eeg'] * 2
info = mne.create_info(ch_names, ch_types=ch_types, sfreq=sampling_freq)
info['description'] = 'My custom dataset'
info.set_montage('standard_1020')

# Converting the EEG data to NumPy array from .adicht file using SDK
f = adi.read_file(r'C:\Users\jkill\Desktop\session3_16.adicht')
C3 = f.channels[2].get_data(1)  
C4 = f.channels[3].get_data(1)  

# Scaling the values appropriately to be interpreted by the CSP (SDK always returns incorrect units)
C3 = C3 * (10000)
C4 = C4 * (10000)

# Creating an array of (number_of_channels, number_of_samples) which looks like (2, 286749)
EEG = np.array([C3, C4]) 
# Creating MNE data
raw = mne.io.RawArray(EEG, info)


# Specifying the onset times and duration of each trial, and labelling them with strings and classification codes (left, right) (1, -1)
event_on = np.array([69, 80, 91, 104, 117, 129, 142, 153, 166, 178, 191, 202, 214, 227, 240, 253, 264, 276, 287, 299, 310, 322, 335, 347, 358, 371, 382, 395, 409, 420, 431, 444, 456, 468, 479, 492, 503, 516, 528, 540])
duration=[6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], 
event_desc=['Right', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Right', 'Right', 'Left' , 'Right', 'Left', 'Left', 'Right', 'Right', 'Left', 'Left', 'Right', 'Left']
desc_codes=[1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1 , 1, -1, -1, 1, 1, -1, -1, 1, -1]

Hi, I tried the codes. I have three channels for ECoG recording. How to edit these codes?

https://github.com/JimHokanson/adinstruments_sdk_python

you must type :
pip install adi-reader
Into your command prompt application on your computer / CMD

Hi, Before your last message, I installed SDK. However, I have three channels for ECoG recording. How to edit these codes?

Ecog1 = f.channels[0].get_data(1) Ecog2 = f.channels[1].get_data(1)
Ecog3 = f.channels[2].get_data(1)

If your Ecog recordings are on the first 3 channels on LabChart then this is fine, if the first channel is blank and the recordings are on channels 2, 3 and 4 then change the [0], [1], [2] to 1, 2 ,3

In python everything begins at 0
So 1 = 0
2 = 1
3 = 2

Then you make an array

EcogData = np.array([Ecog1, Ecog2, Ecog3])

If that doesn’t work

Then try

EcogArray = np.array([Ecog1, Ecog2])
EcogData = np.array([EcogArray, Ecog3])

Then you create the MNE data

raw = mne.io.RawArray(EcogData, info)

Then once you have the data in MNE, just follow tutorials on the MNE website to do what you want , and google any error codes you get.

You will need to install the MNE and NumPy packages

Pip install mne
Pip install numpy

Once they are installed at the top of your code you need to write

Import mne
Import numpy
Import adi-reader

Thank you very much. I will try.

Hi, I rearranged the codes as follows:

import mne
import numpy as np
import adi

# Setting the MNE info for the ecog data
sampling_freq = 1000  
ch_names = ['C1', 'C2', 'C3']
ch_types = ['ecog'] * 3
info = mne.create_info(ch_names, ch_types=ch_types, sfreq=sampling_freq)
info['description'] = 'My custom dataset'
info.set_montage('standard_1020')

# Converting the ecog data to NumPy array from .adicht file using SDK
f = adi.read_file(r'C:\Users\Mehmet\Desktop\scz_56e.adicht')
ecog1 = f.channels[0].get_data(1) 
ecog2 = f.channels[1].get_data(1)
ecog3 = f.channels[2].get_data(1)  

# Scaling the values appropriately to be interpreted by the CSP (SDK always returns incorrect units)
ecog1 = ecog1 * (10000)
ecog2 = ecog2 * (10000)
ecog3 = ecog3 * (10000)

# Creating an array of (number_of_channels, number_of_samples) which looks like (2, 286749)
EcogData = np.array([ecog1, ecog2, ecog3])
# Creating MNE data
raw = mne.io.RawArray(EcogData, info)


# Specifying the onset times and duration of each trial, and labelling them with strings and classification codes (left, right) (1, -1)
event_on = np.array([69, 80, 91, 104, 117, 129, 142, 153, 166, 178, 191, 202, 214, 227, 240, 253, 264, 276, 287, 299, 310, 322, 335, 347, 358, 371, 382, 395, 409, 420, 431, 444, 456, 468, 479, 492, 503, 516, 528, 540])
duration=[6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], 
event_desc=['Right', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Left', 'Right', 'Left', 'Right', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Left', 'Right', 'Right', 'Left' , 'Right', 'Left', 'Left', 'Right', 'Right', 'Left', 'Left', 'Right', 'Left']
desc_codes=[1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1 , 1, -1, -1, 1, 1, -1, -1, 1, -1]

And output:

runfile('C:/Users/Mehmet/.spyder-py3/untitled0.py', wdir='C:/Users/Mehmet/.spyder-py3')
Creating RawArray with float64 data, n_channels=3, n_times=584785
    Range : 0 ... 584784 =      0.000 ...   584.784 secs
Ready.

However, I couldn’t find the file. Is it converted?

yes, the output :

Creating RawArray with float64 data, n_channels=3, n_times=584785
Range : 0 … 584784 = 0.000 … 584.784 secs
Ready.

tells you that MNE has successfully loaded the raw data.

use

raw.plot(duration=585, show_scrollbars=False, show_scalebars=False, title='Raw data')

to create a graph of the signal

Now please browse the MNE forums and mne.tools for tutorials on how to do what you want.

1 Like

Ok. I think you’re busy. Thank you very much for your help.

1 Like

@mtaskiran If you have any remaining questions, it would make sense to create a new topic :+1:

Thank you for your suggestion, Richard. I just started learning Python and MNE. So I have many questions. I am trying to find solution to my problems from forum and google. I will consider your suggestion.

1 Like