Skip to content

Commit ab91a74

Browse files
committed
Adds Physionet EDF support
1 parent 92a4766 commit ab91a74

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

wfdb/io/record.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,14 +1348,22 @@ def check_np_array(item, field_name, ndim, parent_class, channel_num=None):
13481348
raise TypeError(error_msg)
13491349

13501350

1351-
def edf2mit(record_name, record_only=False):
1351+
def edf2mit(record_name, pn_dir=None, delete_file=True, record_only=False):
13521352
"""
13531353
Convert EDF formatted files to MIT format.
13541354
13551355
Parameters
13561356
----------
13571357
record_name : str
13581358
The name of the input EDF record to be read.
1359+
pn_dir : str, optional
1360+
Option used to stream data from Physionet. The Physionet
1361+
database directory from which to find the required record files.
1362+
eg. For record '100' in 'http://physionet.org/content/mitdb'
1363+
pn_dir='mitdb'.
1364+
delete_file : bool, optional
1365+
Whether to delete the saved EDF file (False) or not (True)
1366+
after being imported.
13591367
record_only : bool, optional
13601368
Whether to only return the record information (True) or not (False).
13611369
If false, this function will generate both a .dat and .hea file.
@@ -1365,11 +1373,33 @@ def edf2mit(record_name, record_only=False):
13651373
record : dict, optional
13661374
All of the record information needed to generate MIT formatted files.
13671375
Only returns if 'record_only' is set to True, else generates the
1368-
corresponding .dat and .hea files.
1376+
corresponding .dat and .hea files. This record file will not match the
1377+
`rdrecord` output since it will only give us the digital signal for now.
1378+
1379+
Examples
1380+
--------
1381+
>>> edf_record = wfdb.edf2mit('SC4002E0-PSG.edf',
1382+
pn_dir='sleep-edfx/sleep-cassette',
1383+
record_only=True)
13691384
13701385
"""
1386+
if pn_dir is not None:
1387+
1388+
if '.' not in pn_dir:
1389+
dir_list = pn_dir.split(os.sep)
1390+
pn_dir = posixpath.join(dir_list[0], get_version(dir_list[0]), *dir_list[1:])
1391+
1392+
file_url = posixpath.join(download.PN_INDEX_URL, pn_dir, record_name)
1393+
# Currently must download file for MNE to read it though can give the
1394+
# user the option to delete it immediately afterwards
1395+
r = requests.get(file_url, allow_redirects=False)
1396+
open(record_name, 'wb').write(r.content)
1397+
13711398
edf_data = mne.io.read_raw_edf(record_name, preload=True)
13721399

1400+
if pn_dir is not None and delete_file:
1401+
os.remove(record_name)
1402+
13731403
record_name_out = edf_data._filenames[0].split(os.sep)[-1].replace('-','_').replace('.edf','')
13741404
n_sig = edf_data._raw_extras[0]['nchan']
13751405
sample_rate = (edf_data._raw_extras[0]['n_samps'] / edf_data._raw_extras[0]['record_length'][0]).astype(np.int16)
@@ -1682,7 +1712,7 @@ def rdrecord(record_name, sampfrom=0, sampto=None, channels=None,
16821712
pn_dir = posixpath.join(dir_list[0], get_version(dir_list[0]), *dir_list[1:])
16831713

16841714
if record_name.endswith('.edf'):
1685-
record = edf2mit(record_name, record_only=True)
1715+
record = edf2mit(record_name, pn_dir=pn_dir, record_only=True)
16861716
else:
16871717
record = rdheader(record_name, pn_dir=pn_dir, rd_segments=False)
16881718

0 commit comments

Comments
 (0)