5

Okay so I have a datafile from an EEG scan (a binary file, data.eeg), and in matlab the code to read the file and plot a section of the data goes like this:

sr=400;                                                     % Sample Rate
Nyq_freq=sr/2;                                              % Nyquist Frequency
fneeg=input('Filename  (with path and extension) :', 's');  
t=input('How many seconds in total of EEG ? : ');
ch=input('How many channels of EEG ? : ');
le=t*sr;                                                    % Length of the Recording
fid=fopen(fneeg, 'r', 'l');                                 % Open the file to read 
EEG=fread(fid,[ch,le],'int16');                             % Read Data -> EEG Matrix
fclose ('all');     
plot(EEG(:,3))

Here is my attempt to "translate"

from numpy import *
from matplotlib.pylab import *

sample_rate = 400
Nyquist = sample_rate/2.
fneeg = raw_input("Filename (full path & extension): ")
t = int(raw_input("How many secs in total of EEG?: "))
ch = int(raw_input("How many channels of EEG?: "))
le = t*sample_rate
fid = open(fneeg, 'r')
EEG = fromfile(fneeg, int16)

Here's where things get confusing to me. According to the documentation, matlab's fread is a method of reading binary files via fread(loaded_file, size, data_type). The alternative in python is using numpy's fromfile and reshaping (according to this thread here: MATLAB to Python fread) using the built in reshape function. I'm not sure how this works, or even relates to the matlab method? I'm sorry if my question is confusing, matlab is still very new to me

Edit: If you wanna look have a look at the file here it is: https://www.dropbox.com/s/zzm6uvjfm9gpamk/data.eeg

Edit2: The answers to the raw inputs are t=10, ch=32. In fact I'm not sure why I'm even asking for user input now that I think about it..

10
  • Can you post a sample of the input file ? Commented Nov 12, 2013 at 19:36
  • 1
    It sounds like you've already answered your own question. As near as I can tell, you just want eeg = numpy.fromfile(filename, 'int16').reshape(ch, le). Also, you can skip explicitly opening the file, if you'd like. fromfile will accept a filename as well as a file object. Commented Nov 12, 2013 at 19:38
  • I've added a link to the data in the edit. @JoeKington, that was my reasoning too, but why is it that we need to reshape? I don't understand the reasoning, you know what I mean? Commented Nov 12, 2013 at 19:55
  • 1
    @Norman, np.fromfile just returns a 1d array (of length ch*le, but you want it to be a ch X le matrix. reshape simply makes this change, without touching the data itself. Commented Nov 12, 2013 at 20:39
  • @NormanB, do you happen to be using Neuroscan? I'm trying to import Neuroscan files (.cnt or .eeg) to python and running into a lot of issues. Commented Jun 13, 2014 at 22:40

1 Answer 1

2

As discussed in the comments by yourself and @JoeKington, this should work (I removed the input stuff for testing)

import numpy as np

sample_rate = 400
Nyquist = sample_rate/2.0
fneeg = 'data.eeg'
t = 10 
ch = 32
le = t*sample_rate
EEG = np.fromfile(fneeg, 'int16').reshape(ch, le, order='F')

Without the reshape, you get:

In [45]: EEG
Out[45]: array([ -39,  -25,  -22, ..., -168, -586,  -46], dtype=int16)

In [46]: EEG.shape
Out[46]: (128000,)

With reshaping:

In [47]: EEG.reshape(ch, le, order='F')
Out[47]: 
array([[ -39,  -37,  -12, ...,    5,   19,   21],
       [ -25,  -20,    7, ...,   20,   36,   36],
       [ -22,  -20,    0, ...,   18,   34,   36],
       ..., 
       [ 104,  164,   44, ...,   60,  -67, -168],
       [ 531,  582,   88, ...,   29, -420, -586],
       [ -60,  -63,  -92, ...,  -17,  -44,  -46]], dtype=int16)

In [48]: EEG.reshape(ch, le, order='F').shape
Out[48]: (32, 4000)
Sign up to request clarification or add additional context in comments.

7 Comments

Haha, you're welcome, but the credit is due to you and @Joe :P
NormanB - Just on a side note, it's possible (? I can't remember.) that matlab uses column-major ordering instead of row major... en.wikipedia.org/wiki/Row-major_order In that case, the results were probably written to disk in column-major order, and you'd do fromfile(...).reshape(le, ch).T (note that le and ch are swapped). Just something to be aware of, at any rate. If your data looks garbled, give it a try.
Oh yeah you're right it does(en.wikipedia.org/wiki/Row-major_order). Good catch!
That's actually copied and pasted from ipython (not notebook). A nice compromise between ipython notebook and the standard python interpreter.
Nice catch, @Joe! An easier fix is to use order='F'. I've edited to reflect it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.