-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fif2edf #114
Open
lkorczowski
wants to merge
6
commits into
master
Choose a base branch
from
fif
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fif2edf #114
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fdf3b7
modified save_edf for heterogeneous edf
lkorczowski 5cb0652
example fif2edf
lkorczowski 82a7773
add dummy edf and fif for testing
lkorczowski d5074d6
update coverage
lkorczowski c929633
fix test_io
lkorczowski b365c6a
fix units when median lower than 1e5
lkorczowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ omit = | |
*/setup.py | ||
*/tests/* | ||
*/notebooks/* | ||
*/tinnsleep/config.py | ||
*/tinnsleep/config.py | ||
*/tinnsleep/external/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import mne | ||
import os | ||
import numpy as np | ||
import numpy.testing as npt | ||
from tinnsleep.external.save_edf import write_edf | ||
import matplotlib.pyplot as plt | ||
|
||
def test_fif2edf(): | ||
fname = "./dummy_fif" | ||
fname_edf = fname +".edf" | ||
raw = mne.io.read_raw_fif(fname+".fif", preload=True) | ||
|
||
# plt.close("all") | ||
# plt.figure() | ||
# raw.plot() | ||
# plt.show() | ||
|
||
signals = raw.get_data() | ||
N, T = signals.shape | ||
scaling = np.ones((signals.shape[0])) | ||
scaling[np.median(np.abs(signals), axis=1)< 1e-3] = 1e6 | ||
#raw.info["ch_names"] | ||
if os.path.exists(fname_edf): | ||
os.remove(fname_edf) | ||
write_edf(raw, fname_edf) | ||
|
||
raw2 = mne.io.read_raw_edf(fname_edf) | ||
signals2 = raw2.get_data() | ||
is_uV = np.median(np.abs(signals), axis=1)< 1e-3 | ||
npt.assert_allclose(signals2[is_uV,:T], signals[is_uV, :], atol=0.01) | ||
npt.assert_allclose(signals2[~is_uV,:T], signals[~is_uV, :], atol=0.01) | ||
|
||
# plt.figure() | ||
# raw2.plot() | ||
# plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- mne | ||
- pyedflib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Dec 5 12:56:31 2018 | ||
|
||
@author: skjerns | ||
|
||
Gist to save a mne.io.Raw object to an EDF file using pyEDFlib | ||
(https://github.com/holgern/pyedflib) | ||
|
||
Disclaimer: | ||
- Saving your data this way will result in slight | ||
loss of precision (magnitude +-1e-09). | ||
- It is assumed that the data is presented in Volt (V), it will be internally converted to microvolt | ||
- Saving to BDF can be done by changing the file_type variable. | ||
Be aware that you also need to change the dmin and dmax to | ||
the corresponding minimum and maximum integer values of the | ||
file_type: e.g. BDF+ dmin, dmax =- [-8388608, 8388607] | ||
""" | ||
|
||
import pyedflib # pip install pyedflib | ||
from datetime import datetime | ||
import mne | ||
import os | ||
import numpy as np | ||
|
||
|
||
def write_edf(mne_raw, fname, picks=None, tmin=0, tmax=None, overwrite=False): | ||
""" | ||
Saves the raw content of an MNE.io.Raw and its subclasses to | ||
a file using the EDF+ filetype | ||
pyEDFlib is used to save the raw contents of the RawArray to disk | ||
|
||
Parameters | ||
---------- | ||
mne_raw : mne.io.Raw | ||
An object with super class mne.io.Raw that contains the data | ||
to save | ||
fname : string | ||
File name of the new dataset. This has to be a new filename | ||
unless data have been preloaded. Filenames should end with .edf | ||
picks : array-like of int | None | ||
Indices of channels to include. If None all channels are kept. | ||
tmin : float | None | ||
Time in seconds of first sample to save. If None first sample | ||
is used. | ||
tmax : float | None | ||
Time in seconds of last sample to save. If None last sample | ||
is used. | ||
overwrite : bool | ||
If True, the destination file (if it exists) will be overwritten. | ||
If False (default), an error will be raised if the file exists. | ||
""" | ||
if not issubclass(type(mne_raw), mne.io.BaseRaw): | ||
raise TypeError('Must be mne.io.Raw type') | ||
if not overwrite and os.path.exists(fname): | ||
raise OSError('File already exists. No overwrite.') | ||
# static settings | ||
file_type = pyedflib.FILETYPE_EDFPLUS | ||
sfreq = mne_raw.info['sfreq'] | ||
date = datetime.now()#.strftime('%d %b %Y %H:%M:%S') | ||
first_sample = int(sfreq * tmin) | ||
last_sample = int(sfreq * tmax) if tmax is not None else None | ||
|
||
# convert data | ||
channels = mne_raw.get_data(picks, | ||
start=first_sample, | ||
stop=last_sample) | ||
|
||
# convert to microvolts to scale up precision | ||
#is_micro = np.median(channels, axis=1) < 1e-3 | ||
|
||
#channels *= 1e6 | ||
|
||
# set conversion parameters | ||
dmin, dmax = [-32768, 32767] | ||
pmin, pmax = [channels.min(), channels.max()] | ||
n_channels = len(channels) | ||
|
||
# create channel from this | ||
try: | ||
f = pyedflib.EdfWriter(fname, | ||
n_channels=n_channels, | ||
file_type=file_type) | ||
|
||
channel_info = [] | ||
data_list = [] | ||
|
||
for i in range(n_channels): | ||
if np.median(np.abs(channels[i])) < 1e-3: | ||
scale = 1e6 | ||
unit = 'uV' | ||
else: | ||
scale = 1 | ||
unit = 'V' | ||
|
||
ch_dict = {'label': mne_raw.ch_names[i], | ||
'dimension': unit, | ||
'sample_rate': sfreq, | ||
'physical_min': pmin, | ||
'physical_max': pmax, | ||
'digital_min': dmin, | ||
'digital_max': dmax, | ||
'transducer': '', | ||
'prefilter': ''} | ||
|
||
channel_info.append(ch_dict) | ||
data_list.append(scale*channels[i]) | ||
|
||
f.setTechnician('mne-gist-save-edf-skjerns') | ||
f.setSignalHeaders(channel_info) | ||
f.setStartdatetime(date) | ||
f.writeSamples(data_list) | ||
except Exception as e: | ||
print(e) | ||
return False | ||
finally: | ||
f.close() | ||
return True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Micro modif that makes it better:
`
for i in range(n_channels):
`
No longer the cut off saturating threshold ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done