Frederic-vW / eeg_microstates

EEG microstate analysis
MIT License
37 stars 18 forks source link

error at the filter: invalid syntax when executing from eeg_microstates import * #3

Closed apavlo89 closed 12 months ago

apavlo89 commented 4 years ago

Hello,

I'm trying to go through the tutorial but I get an error when I execute eeg_microstates import *

Traceback (most recent call last):

  File "C:\Users\apavl\anaconda3\envs\neuroscience\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-27-0c38fff94fca>", line 1, in <module>
    from eeg_microstates import *

  File "C:\Users\apavl\Documents\GitHub\eeg_microstates\eeg_microstates.py", line 122
    def bp_filter(data, (f_lo, f_hi), fs):
                        ^
SyntaxError: invalid syntax

Any help would be greatly appreciated

Frederic-vW commented 4 years ago

Hi, you are probably using Python3, which doesn't allow tuples as function arguments. Two options: 1) run the tutorial with Python2.7 2) if you use Python3, don't to use the (f_lo, f_hi) tuple in the function, re-write the filter function as def bp_filter(data, f_lo, f_hi, fs) and the function call as: data = bp_filter(data_raw, 1, 35, fs) The rest of the script should be runnable with Python3. Let me know if not, and I will upload a new Python3 version. Thanks, Fred

apavlo89 commented 4 years ago

Its a whole mess to get it to work for python 2.7... It would be super appreciated if you updated it to work with python3... For python3 instructions I did the following:

I use Anaconda and loaded up my environment

git clone https://github.com/Frederic-vW/eeg_microstates.git

changed

def bp_filter(data, (f_lo, f_hi), fs):

# (2) band-pass filter 1-35 Hz (6-th order Butterworth)
os.system("clear")
print("\n\t[2] Apply band-pass filter...")
data = bp_filter(data_raw,(1,35),fs) # (1, 35)

in eeg_microstates.py

to

def bp_filter(data, f_lo, f_hi, fs):

os.system("clear")
print("\n\t[2] Apply band-pass filter...")
 data = bp_filter(data_raw,1,35,fs) # (1, 35)

opened up notebook and found the notebook file.

at from eeg_microstates import *

I get

Traceback (most recent call last):

File "C:\Users\apavl\anaconda3\envs\neuroscience\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in from eeg_microstates import *

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 903 for i, j in np.ndindex(p12.shape): ^ TabError: inconsistent use of tabs and spaces in indentation

Frederic-vW commented 4 years ago

Sorry for the troubles. I just uploaded the script eeg_microstates3.py which runs without problems with my Python3 installation. I haven't updated the notebook yet. If you put the new script in a folder with the test data set (test.edf), it should run. The tab errors are probably due to the editor, have you opened the script in an editor? (which?). I hope the problem doesn't occur with the new script, please let me know.

apavlo89 commented 4 years ago

Hello,

Thanks for updating it and taking the time to look at this.

It now errors at

for ch in chs: print "\t{:s}".format(ch),

producing the following error:

for ch in chs: print "\t{:s}".format(ch), ^ SyntaxError: invalid syntax

Not sure what is wrong but as its not very crucial I've commented it out.

It then errors at

maps, x, gfp_peaks, gev = clustering(data, fs, chs, locs, mode, n_maps, doplot=True)

giving the following error:

TypeError: kmeans() got an unexpected keyword argument 'doplot'

removing doplot argument doesn't fix the issue...

Frederic-vW commented 4 years ago

Hi, no worries, thanks to you for your help in finding errors. I could reproduce the 2nd error, the doplot argument was a bug in the new code, I removed it and updated the script. The first error is a bit strange as this line was commented out in the script I uploaded yesterday. The loop starting in line 1555 has the old (commented) Python2 syntax in line 1557:

print "\t{:s}".format(ch), # Python-2

The ending comma is not correct in Python-3, the correct line is 1558: print("\t{:s}".format(ch.decode()), end='') # Python-3 Could you please confirm that your error originated in that loop? Just to be sure we are running the same script (eeg_microstates3.py)

apavlo89 commented 4 years ago

the code for ch in chs: print "\t{:s}".format(ch), still gives same error

 File "<ipython-input-57-09a26087ed9a>", line 7
    for ch in chs: print "\t{:s}".format(ch),
SyntaxError: invalid syntax

also the following code:

p_hat = p_empirical(x, n_maps)
T_hat = T_empirical(x, n_maps)
print("\n\t\tEmpirical symbol distribution (RTT):\n")
for i in range(n_maps): print("\t\tp_{:d} = {:.3f}".format(i, p_hat[i]))
print("\n\t\tEmpirical transition matrix:\n")
print_matrix(T_hat)
pps = len(gfp_peaks) / (len(x)/fs)  # peaks per second
print("\n\t\tGFP peaks per sec.: {:.2f}".format(pps))
print("\n\t\tGlobal explained variance (GEV) per map:")
print "\t\t" + str(gev)
print("\n\t\ttotal GEV: {:.2f}".format(gev.sum()))

gives out error

File "<ipython-input-59-1718e1ec1dbe>", line 10
    print "\t\t" + str(gev)
SyntaxError: invalid syntax

this code now runs fine!

n_maps = 4
locs = []
maps, x, gfp_peaks, gev = clustering(data, fs, chs, locs, mode, n_maps, doplot=True)

One other question. Where did you get your cap cartesian coordinates from? I'm trying to find the same coordinates distances for a 64 channel eeg but cannot find one that produces the same cartesian coordinates :/ Thank you for your help in the matter

Frederic-vW commented 4 years ago

Hi, you keep getting errors because you are running the notebook. I wrote in a previous post that the ipython/jupyter notebook isn't updated yet. You have to run the python3 code, that runs fine. The line you refer to: for ch in chs: print "\t{:s}".format(ch), doesn't exist in the eeg_microstates3.py. Anyway, as stated in the previous answer, you can translate that into Python3 by using: for ch in chs: print("\t{:s}".format(ch.decode()), end='') The other one is Python2 syntax also: print "\t\t" + str(gev), the solution here would be to write print("\t\t", str(gev)) But the main message is, don't mix Python2 (notebook) with the Python3 code I provided now. There are other things that will not work properly (visually re-ordering the maps for example, graphics will depend on your OS (Windows, Linux...), "raw_input" has to be changed to "input"). I will work on a new notebook, meanwhile, you can get all the correct results by just running: > python3 eeg_microstates3.py, no notebook needed.

The electrode positions came from the output of the EEG hardware/software system, they were written in the header. You can use standard locations from other sources, the interpolated maps (topo plots) may look different, but will not affect any quantitative results, as these are calculated from the 30-channel data vectors.

apavlo89 commented 4 years ago

Got it, I'm now running only the .py file.

I get the following error - keep in mind that i've deleted the py file that runs on previous python version and renamed eeg_microstates3.py. to eeg_microstates.py

Traceback (most recent call last):

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 1780, in main()

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 1590, in main n_win, doplot=True)

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 415, in clustering axarr[imap].imshow(eeg2map(maps[imap, :]), cmap=cm, origin='lower')

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 252, in eeg2map top = topo(data, n_grid)

File "C:\Users\apavl\eeg_microstates\eeg_microstates.py", line 238, in topo data_ip = griddata((X, Y), data, (Xi[None,:], Yi[:,None]), method='cubic')

File "C:\Users\apavl\anaconda3\envs\neuroscience\lib\site-packages\scipy\interpolate\ndgriddata.py", line 225, in griddata rescale=rescale)

File "interpnd.pyx", line 843, in scipy.interpolate.interpnd.CloughTocher2DInterpolator.init

File "interpnd.pyx", line 80, in scipy.interpolate.interpnd.NDInterpolatorBase.init

File "interpnd.pyx", line 191, in scipy.interpolate.interpnd._check_init_shape

ValueError: different number of values and points

Frederic-vW commented 4 years ago

Good, that means that the clustering has run without problems, this is the visualization step. The error comes from the scipy function griddata, and something is wrong with the array shapes X, Y, data. What do you get if you print the array shapes: print(X.shape, Y.shape, data.shape, Xi.shape, Yi.shape)? How many electrodes does your recording have, and have you loaded other electrode coordinates? I am assuming you are not running my test.edf file, but your own. Please let me know if the test.edf file runs correctly, it would be strange if it didn't. Thanks.