moeinrazavi / EEG-ASR-Python

Clean ASR (Artifact Subspace Reconstruction) in Python
4 stars 1 forks source link

How to clean continuous data (resting state EEG) #1

Open apavlo89 opened 3 years ago

apavlo89 commented 3 years ago

Hello,

Fantastic job importing ASR in Python. I live for the day everything gets ported to Python and everyone dumps Matlab for good! Paying to use a programming language is bullshit!

I basically want to clean eeg data by removing bad segments from continuous EEG recording. I would greatly appreciate some help in how to do this. This is an example code I have up to now

#BASELINE
# Read the CSV file as a NumPy array
data = np.loadtxt('E:\location\file.txt', delimiter=',')
ch_names = ['AF7', 'AF8']  

# Sampling rate of MUSE
sfreq = 256  # Hz

# Create the info structure needed by MNE
info = mne.create_info(ch_names, sfreq)

data = np.transpose(data)

# Finally, create the Raw object
raw_baseline = mne.io.RawArray(data, info)

data_baseline = raw_baseline._data #* 1e6

sf_baseline = raw_baseline.info['sfreq']
chan_baseline = raw_baseline.ch_names
times_baseline = np.arange(data_baseline.shape[1]) / sf_baseline
print(data_baseline.shape, chan_baseline, times_baseline)

I've installed the package using git clone https://github.com/moeinrazavi/EEG-ASR-Python

moeinrazavi commented 3 years ago

Hi,

Simply do the following:

  1. put the CleanASR.py into your python code folder.
  2. import CleanASR in your python code.
  3. Use Clean_EEG = CleanASR.clean_asr(EEG, sfreq) your code --> The EEG format must be a NumPy array with the shape (Number of Channels, Number of Samples), sfreq is the EEG sampling frequency.

The Clean_EEG would a NumPy array with the shape (Number of Channels, Number of Samples).

You can find an example in Main.py. On Sun, Mar 21, 2021 at 6:45 PM apavlo89 @.***> wrote:

Hello,

Fantastic job importing ASR in Python. I live for the day everything gets ported to Python and everyone dumps Matlab for good! Paying to use a programming language is bullshit!

I basically want to clean eeg data by removing bad segments from continuous EEG recording. I would greatly appreciate some help in how to do this. This is an example code I have up to now

BASELINE

Read the CSV file as a NumPy array

data = np.loadtxt('E:\location\file.txt', delimiter=',') ch_names = ['AF7', 'AF8']

Sampling rate of MUSE

sfreq = 256 # Hz

Create the info structure needed by MNE

info = mne.create_info(ch_names, sfreq)

data = np.transpose(data)

Finally, create the Raw object

raw_baseline = mne.io.RawArray(data, info)

data_baseline = raw_baseline._data #* 1e6

sf_baseline = raw_baseline.info['sfreq'] chan_baseline = raw_baseline.ch_names times_baseline = np.arange(data_baseline.shape[1]) / sf_baseline print(data_baseline.shape, chan_baseline, times_baseline)

I've installed the package using git clone https://github.com/moeinrazavi/EEG-ASR-Python https://urldefense.com/v3/__https://github.com/moeinrazavi/EEG-ASR-Python__;!!KwNVnqRv!TuWMRg6O5JEkJ9tR_Hpj7W8WsCNxb5_EO5l2wNCBALu4CrOy72X-xwX2xMLNhV-lMoc$

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/moeinrazavi/EEG-ASR-Python/issues/1__;!!KwNVnqRv!TuWMRg6O5JEkJ9tR_Hpj7W8WsCNxb5_EO5l2wNCBALu4CrOy72X-xwX2xMLNJ7sZPDs$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AIVHXMK7VMHRPP4SKT4RAETTE2AJJANCNFSM4ZSCT6AA__;!!KwNVnqRv!TuWMRg6O5JEkJ9tR_Hpj7W8WsCNxb5_EO5l2wNCBALu4CrOy72X-xwX2xMLNo0CW1AQ$ .

--

Moein Razavi Ph.D. Student in Cognitive Neuroscience (CN) Master's Student in Computer Engineering (CE) Brain-Computer Interface Graduate Research Assistant

4235 TAMU

Psychological and Brain Sciences Department Texas A&M University

College Station, TX 77840

Email: @.***

Tel: (979)-676-7486

apavlo89 commented 3 years ago

I've added this line to my code:

import CleanASR
Clean_EEG = CleanASR.clean_asr(data_baseline, sf_baseline)

and I get ModuleNotFoundError: No module named 'CleanASR'

I'm using a virtual environment with anaconda if that helps. Like I mentioned earlier I installed it using git clone

apavlo89 commented 3 years ago

I've now tried the following code:

import sys
sys.path.append('C:/Users/apavl/EEG-ASR-Python/')

import CleanASR

Clean_EEG = CleanASR.clean_asr(data_baseline, sf_baseline)

and I get the following error

Finding a clean section of the data... Estimating calibration statistics; this may take a while... Determining per-component thresholds... Compiling cost function... Optimizing... f: +1.969944e+01 |grad|: 8.042798e-15 C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py:1267: RuntimeWarning: k >= N - 1 for N N square matrix. Attempting to use scipy.linalg.eig instead. warnings.warn("k >= N - 1 for N N square matrix. " Traceback (most recent call last):

File "", line 121, in Clean_EEG = CleanASR.clean_asr(data_baseline, sf_baseline)

File "C:/Users/apavl/EEG-ASR-Python\CleanASR.py", line 610, in clean_asr state = asr_calibrate_r(ref_section,SRate,Cutoff)

File "C:/Users/apavl/EEG-ASR-Python\CleanASR.py", line 445, in asr_calibrate_r [V,D] = rasr_nonlinear_eigenspace(M, C)

File "C:/Users/apavl/EEG-ASR-Python\CleanASR.py", line 336, in rasr_nonlinear_eigenspace Xopt = solver.solve(problem, X0)

File "C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\pymanopt\solvers\trust_regions.py", line 170, in solve eta, Heta, numit, stop_inner = self._truncated_conjugate_gradient(

File "C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\pymanopt\solvers\trust_regions.py", line 552, in _truncated_conjugate_gradient return eta, Heta, j, stop_tCG

UnboundLocalError: local variable 'j' referenced before assignment

moeinrazavi commented 3 years ago

You need to replace the file "trust_regions.py" in the folder "C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\pymanopt\solvers\" with the one available in the repository. You may also need to replace "_tensorflow.py" in the folder "C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\pymanopt\tools\autodiff" with one available in the repository

On Sun, Mar 21, 2021 at 7:41 PM apavlo89 @.***> wrote:

I've now tried the following code:

import sys sys.path.append('C:/Users/apavl/EEG-ASR-Python/')

import CleanFlatlines, CleanDrifts, CleanASR

Clean_EEG = CleanASR.clean_asr(data_baseline, sf_baseline)

and I get the following error

File "C:\Users\apavl\anaconda3\envs\neuro\lib\site-packages\pymanopt\solvers\trust_regions.py", line 552, in _truncated_conjugate_gradient return eta, Heta, j, stop_tCG

UnboundLocalError: local variable 'j' referenced before assignment

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/moeinrazavi/EEG-ASR-Python/issues/1*issuecomment-803693307__;Iw!!KwNVnqRv!THrUS9xiXculOQhCdsGraR4zwNeJFzqVJyqTbEqVwczux3oPCqARUGfPWm3GUAZ8OIU$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AIVHXMNTF3IRCB36HL3HCITTE2G4XANCNFSM4ZSCT6AA__;!!KwNVnqRv!THrUS9xiXculOQhCdsGraR4zwNeJFzqVJyqTbEqVwczux3oPCqARUGfPWm3GI3W10bY$ .

--

Moein Razavi Ph.D. Student in Cognitive Neuroscience (CN) Master's Student in Computer Engineering (CE) Brain-Computer Interface Graduate Research Assistant

4235 TAMU

Psychological and Brain Sciences Department Texas A&M University

College Station, TX 77840

Email: @.***

Tel: (979)-676-7486