# Resampling with TMS artifacts

**URL:** <https://mne.discourse.group/t/resampling-with-tms-artifacts/1425>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [February 14, 2018, 2:49pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425 "2018-02-14T14:49:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 2:49pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/1 "2018-02-14T14:49:25Z")

</div>

Hi all,

&nbsp;&nbsp;&nbsp;&nbsp;I was wondering about the best way to resample data with a high-frequency TMS artifact. I first epoched the raw data so that the TMS events would be kept but when I resample the epochs, a low-pass filter is applied which makes sense but causes a large filter artifact from the TMS pulse. Is there any way to not apply this filter?

Thanks,

Alex  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180214/cffb889e/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180214/cffb889e/attachment-0001.html)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 4:29pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/2 "2018-02-14T16:29:17Z")

</div>

Hi,

How long is the artifact? Probably you could use fix\_stim\_artifact for  
this:  
[https://mne-tools.github.io/dev/generated/mne.preprocessing.fix\_stim\_artifact.html](https://mne-tools.github.io/dev/generated/mne.preprocessing.fix_stim_artifact.html)

or use decim parameter when epoching

Mainak

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 7:11pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/3 "2018-02-14T19:11:41Z")

</div>

Hi Mainak,

&nbsp;&nbsp;&nbsp;&nbsp;Thank you for the suggestion. When I load in the raw data using the string preload option, there isn't a problem, but when try giving a string for preload to the epochs, I get:

Traceback (most recent call last):  
&nbsp;&nbsp;File "\<stdin\>", line 4, in \<module\>  
&nbsp;&nbsp;File "\<string\>", line 2, in \_\_init\_\_  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/utils.py", line 728, in verbose  
&nbsp;&nbsp;&nbsp;&nbsp;return function(\*args, \*\*kwargs)  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 1960, in \_\_init\_\_  
&nbsp;&nbsp;&nbsp;&nbsp;verbose=verbose)  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 386, in \_\_init\_\_  
&nbsp;&nbsp;&nbsp;&nbsp;self.load\_data() # this will do the projection  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 448, in load\_data  
&nbsp;&nbsp;&nbsp;&nbsp;self.\_data = self.\_get\_data()  
&nbsp;&nbsp;File "\<string\>", line 2, in \_get\_data  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/utils.py", line 728, in verbose  
&nbsp;&nbsp;&nbsp;&nbsp;return function(\*args, \*\*kwargs)  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 1212, in \_get\_data  
&nbsp;&nbsp;&nbsp;&nbsp;dtype=epoch\_out.dtype, order='C')  
MemoryError

In order to interpolate the artifact and downsample the data, I am following recommendations that the data be in epochs. Is there any suggestions for how to avoid this?

Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 8:04pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/4 "2018-02-14T20:04:22Z")

</div>

to interpolate the artifact you need to load the data from disk.

it seems that your data are big and you cannot load them in memory.

how big are your data? How much RAM do you have on your machine?

Alex

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 8:11pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/5 "2018-02-14T20:11:17Z")

</div>

Hi Alex,

&nbsp;&nbsp;&nbsp;&nbsp;The raw .fif file is 30 GB and I have 60 GB of RAM. When I load the epochs, it takes up more than the 60 GB I have of RAM. I have tried iterating over the epochs, loading each section and then putting them back into an EpochsArray but then when I go to read them in I get the following error:

&nbsp;&nbsp;&nbsp;Traceback (most recent call last):  
&nbsp;&nbsp;File "\<stdin\>", line 1, in \<module\>  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 448, in load\_data  
&nbsp;&nbsp;&nbsp;&nbsp;self.\_data = self.\_get\_data()  
&nbsp;&nbsp;File "\<string\>", line 2, in \_get\_data  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/utils.py", line 728, in verbose  
&nbsp;&nbsp;&nbsp;&nbsp;return function(\*args, \*\*kwargs)  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 1170, in \_get\_data  
&nbsp;&nbsp;&nbsp;&nbsp;epoch\_noproj = self.\_get\_epoch\_from\_raw(idx)  
&nbsp;&nbsp;File "\<string\>", line 2, in \_get\_epoch\_from\_raw  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/utils.py", line 728, in verbose  
&nbsp;&nbsp;&nbsp;&nbsp;return function(\*args, \*\*kwargs)  
&nbsp;&nbsp;File "/autofs/space/karima\_001/users/alex/software/anaconda2.7/lib/python2.7/site-packages/mne-0.16.dev0-py2.7.egg/mne/epochs.py", line 2606, in \_get\_epoch\_from\_raw  
&nbsp;&nbsp;&nbsp;&nbsp;raise RuntimeError('Correct epoch could not be found, please '  
RuntimeError: Correct epoch could not be found, please contact mne-python developers

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 8:22pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/6 "2018-02-14T20:22:42Z")

</div>

we load the data as float64 for stable numerics so you have data of 30GB in  
float32  
then yes it will blow up...

I will crop the raw, extract epochs in each chunk and then eventually  
concatenate  
the epochs.

HTH  
Alex

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 14, 2018, 8:57pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/7 "2018-02-14T20:57:21Z")

</div>

Any advice on how to avoid the runtime error?

I think it has to do with resampling the epochs to a different rate than the original raw but I was using the MNE examples section about creating an Epochs object from scratch as a template.

This was my attempt at resampling without a low-pass filter so that I could remove the artifact afterwards.

epochs = read\_epochs(os.getcwd() + '/' + subject + '/TMS-epo.fif',preload=False)  
new\_sfreq = 1000.0  
new\_ntimes = int((epochs.tmax-epochs.tmin)\*new\_sfreq)+1  
if int(epochs.info['sfreq'])%int(new\_sfreq) != 0:  
&nbsp;&nbsp;&nbsp;&nbsp;print('Sampling frequencies are not integer multiples, will not work.')  
else:  
&nbsp;&nbsp;&nbsp;&nbsp;factor = int(epochs.info['sfreq'])/int(new\_sfreq)  
times = epochs.times[::factor]  
tind = epochs.time\_as\_index(times)  
epochs\_resampled\_data = np.zeros((len(epochs.events),epochs.info['nchan'],len(times)))  
for i,epoch in enumerate(epochs):  
&nbsp;&nbsp;&nbsp;&nbsp;epochs\_resampled\_data[i] = epoch[:,tind]

ch\_names = [ch['ch\_name'] for ch in epochs.info['chs']]  
ch\_types = []  
for ch in ch\_names:  
&nbsp;&nbsp;&nbsp;&nbsp;if 'EOG' in ch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types.append('eog')  
&nbsp;&nbsp;&nbsp;&nbsp;elif 'ECG' in ch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types.append('ecg')  
&nbsp;&nbsp;&nbsp;&nbsp;elif 'STI' in ch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types.append('stim')  
&nbsp;&nbsp;&nbsp;&nbsp;elif 'EMG' in ch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types.append('emg')  
&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types.append('eeg')  
info = create\_info(ch\_names=ch\_names,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch\_types=ch\_types,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sfreq=new\_sfreq)  
epochs = EpochsArray(epochs\_resampled\_data,epochs.info,events=events2,tmin=tmin)  
epochs.save(os.getcwd() + '/' + subject + '/' + 'TMS\_resampled-epo.fif')

but again when I go to read the epochs in with preload=True, I get the RunTime error.

Thanks for all the help,

Alex

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 15, 2018, 2:00am UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/8 "2018-02-15T02:00:30Z")

</div>

Hi,

I would suggest posting code in gist: [https://gist.github.com/](https://gist.github.com/) so that it  
is easy to follow the bug report. Did you try using the decim parameter  
when using the Epochs constructor? It did not help?

Mainak

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 16, 2018, 1:35pm UTC](https://mne.discourse.group/t/resampling-with-tms-artifacts/1425/9 "2018-02-16T13:35:23Z")

</div>

Thanks Mainak,

&nbsp;&nbsp;&nbsp;&nbsp;The decim parameter in epochs was what I was looking for.

&nbsp;&nbsp;&nbsp;&nbsp;I think I managed to sort out the runtime error as well, it looks like the last epoch didn't have enough data (the recording was stopped early) and when it was dropped there were some loading and saving errors but I just avoided these by preemptively dropping the event.

I appreciate all the help,

Alex
