digyt / asrpy Goto Github PK
View Code? Open in Web Editor NEWArtifact Subspace Reconstruction for Python
License: BSD 3-Clause "New" or "Revised" License
Artifact Subspace Reconstruction for Python
License: BSD 3-Clause "New" or "Revised" License
Hi - Thanks for making this available! I'm wondering if you have a version of this that works on numpy arrays rather than mne epochs? Also, do you have any recommendations for riemann asr implementations in python?
Thanks very much!
Note: I saw your comments here: mne-tools/mne-python#7479 and your comparison of artifact removal methods here: https://digyt.github.io/automated_EEG_cleaning_comparison/. As I'm interested in on-line methods of artifact removal, have you run a comparison of your riemann vs. euclidean implementations?
Hello, I greatly appreciate your code. I encountered the following error with some local EEG files while using your code, which might be related to block_covariance(X, window=blocksize). I'm not sure if you have experienced this issue during your usage. Thank you very much for your time and assistance. Below is the error message:
ValueError Traceback (most recent call last)
Cell In[109], line 1
----> 1 asr.fit(raw)
File F:\anaconda\lib\site-packages\asrpy\asr.py:233, in ASR.fit(self, raw, picks, start, stop, return_clean_window)
223 clean, sample_mask = clean_windows(
224 X,
225 sfreq=self.sfreq,
(...)
229 min_clean_fraction=self.min_clean_fraction,
230 max_dropout_fraction=self.max_dropout_fraction)
232 # Perform calibration
--> 233 self.M, self.T = asr_calibrate(
234 clean,
235 sfreq=self.sfreq,
236 cutoff=self.cutoff,
237 blocksize=self.blocksize,
238 win_len=self.win_len,
239 win_overlap=self.win_overlap,
240 max_dropout_fraction=self.max_dropout_fraction,
241 min_clean_fraction=self.min_clean_fraction,
242 ab=(self.A, self.B),
243 method=self.method)
245 self._fitted = True
247 # return data if required
File F:\anaconda\lib\site-packages\asrpy\asr.py:431, in asr_calibrate(X, sfreq, cutoff, blocksize, win_len, win_overlap, max_dropout_fraction, min_clean_fraction, ab, method)
428 N = int(np.round(win_len * sfreq))
430 # get block covariances
--> 431 U = block_covariance(X, window=blocksize)
433 # get geometric median for each block
434 # Note: riemann mode is not yet supported, else this could be:
435 # Uavg = pyriemann.utils.mean_covariance(U, metric='riemann')
436 Uavg = geometric_median(U.reshape((-1, nc * nc)) / blocksize)
File F:\anaconda\lib\site-packages\asrpy\asr_utils.py:509, in block_covariance(data, window)
506 for k in range(0, window):
507 idx_range = np.minimum(n_times - 1,
508 np.arange(k, n_times + k - 2, window))
--> 509 U = U + np.reshape(data[idx_range].reshape([-1, 1, n_ch]) *
510 data[idx_range].reshape(-1, n_ch, 1), U.shape)
512 return np.array(U)
File <array_function internals>:180, in reshape(*args, **kwargs)
File F:\anaconda\lib\site-packages\numpy\core\fromnumeric.py:298, in reshape(a, newshape, order)
198 @array_function_dispatch(_reshape_dispatcher)
199 def reshape(a, newshape, order='C'):
200 """
201 Gives a new shape to an array without changing its data.
202
(...)
296 [5, 6]])
297 """
--> 298 return _wrapfunc(a, 'reshape', newshape, order=order)
File F:\anaconda\lib\site-packages\numpy\core\fromnumeric.py:57, in _wrapfunc(obj, method, *args, **kwds)
54 return _wrapit(obj, method, *args, **kwds)
56 try:
---> 57 return bound(*args, **kwds)
58 except TypeError:
59 # A TypeError occurs if the object does have such a method in its
60 # class, but its signature is not identical to that of NumPy's. This
(...)
64 # Call _wrapit from within the except clause to ensure a potential
65 # exception has a traceback chain.
66 return _wrapit(obj, method, *args, **kwds)
ValueError: cannot reshape array of size 10820304 into shape (75142,144)
`
Line 782 in 15cda19
An additional line
clean = X.copy()
should probably added after line 782 in asr.py. Otherwise, the variable clean
will be missing when the whole data is clean.
Hi there - Thanks again for your directions previously re: running asr_calibrate() on numpy arrays. I tried testing this on a slice of EEG data and am receiving the following error:
Geometric median could converge in 500 iterations with a tolerance of 1e-05
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/bg/b08l9ly13yb_4hsjwlrvx0dm0000gn/T/ipykernel_5374/3832493090.py in <module>
5 # calibrate
6 calibration_data = bandpassed_eeg_data[:, start_calibration: end_calibration]
----> 7 M, T = asr_calibrate(X = calibration_data, sfreq = baseline_eeg_srate, cutoff = asr_cutoff, method = 'euclidean')
8
9 # clean
~/Documents/EEGHRV/asr.py in asr_calibrate(X, sfreq, cutoff, blocksize, win_len, win_overlap, max_dropout_fraction, min_clean_fraction, ab, method)
431 # Uavg = pyriemann.utils.mean_covariance(U, metric='riemann')
432 Uavg = geometric_median(U.reshape((-1, nc * nc)) / blocksize)
--> 433 Uavg = Uavg.reshape((nc, nc))
434
435 # get the mixing matrix M
AttributeError: 'NoneType' object has no attribute 'reshape'
My guess is that the script output: "Geometric median could converge" actually means the geometric median could "not" converge.
When I run the helper functions in asr_utils.py individually for the calibration data:
U = block_covariance(calibration_data)
print(U.shape)
U = U.reshape((-1, 19 * 19))
print(U.shape)
temp = geometric_median(U)
It seems that the geometric_median() function is returning 'None' because of failure to converge.
Any thoughts on how to fix this? I see in the asr_calibrate() documentation that it expects a "zero-mean array". Does this mean that each channel should be zero-mean'd? Thanks!
Hello, can you clarify about dataset format? for example_asr.py
My dataset has the next structure in txt or excel formats and put this data to NumPy
ch1 ch2 ch3 ch4 ch5 ch6 ch7 ch8
1 1 1 1 1 1 1 1
But is not works
asrpy/asr.py
#L727: mask1 = swz[-(np.int(max_bad_chans) + 1), :] > np.max(zthresholds)
#L729: mask2 = (swz[1 + np.int(max_bad_chans - 1), :] < np.min(zthresholds))
I suggest changing np.int for int
Line 504 in 14956f4
When data has shape of (4,17302), window = 100 gives error
ValueError: cannot reshape array of size 2768 into shape (174,16)
And it does the same with every n_samples in the shape of
xxxx02
I think it is an approximation problem when creating the matrix U.
I patched it by creating the matrix U during the first iteration, like this
for k in range(0, window):
idx_range = np.minimum(n_times - 1,
np.arange(k, n_times + k - 2, window))
if k == 0:
U = np.zeros([data[idx_range].shape[0], n_ch**2])
U = U + np.reshape(data[idx_range].reshape([-1, 1, n_ch])*data[idx_range].reshape(-1, n_ch, 1), U.shape)
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
L#269: np.exp(np.fft.fft(Rwindow * np.fft.ifft(np.log(Ss, dtype=np.complex128)))) # noqa
Suggest changing np,complex to np.complex128
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.