-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move conversion functions to their own modules and remove cyclic impo…
…rts (#370) * Move wav, edf, mat, and csv conversion functions to their own modules * Move content to remove cyclic dependencies * Move get_version for a PhysioNet database, to download.py * Remove rdrecord's ability to read edf and wav files * Update documentation
- Loading branch information
Showing
25 changed files
with
3,191 additions
and
3,202 deletions.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Format Conversions | ||
================== | ||
|
||
The wfdb.io.convert subpackage contains functions to read and write | ||
non-WFDB files commonly used for waveforms. | ||
|
||
CSV | ||
--- | ||
|
||
.. automodule:: wfdb.io.convert.csv | ||
:members: csv_to_wfdb | ||
|
||
EDF | ||
--- | ||
|
||
.. automodule:: wfdb.io.convert.edf | ||
:members: | ||
|
||
Matlab | ||
------ | ||
|
||
.. automodule:: wfdb.io.convert.matlab | ||
:members: | ||
|
||
TFF | ||
--- | ||
|
||
.. automodule:: wfdb.io.convert.tff | ||
:members: | ||
|
||
WAV | ||
--- | ||
|
||
.. automodule:: wfdb.io.convert.wav | ||
:members: |
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ Other Content | |
|
||
installation | ||
wfdb-specifications | ||
convert | ||
|
||
|
||
Indices and tables | ||
|
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
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,110 @@ | ||
import numpy as np | ||
|
||
from wfdb.io.record import rdrecord | ||
from wfdb.io.convert.edf import read_edf | ||
|
||
|
||
class TestConvert: | ||
def test_edf_uniform(self): | ||
""" | ||
EDF format conversion to MIT for uniform sample rates. | ||
""" | ||
# Uniform sample rates | ||
record_MIT = rdrecord("sample-data/n16").__dict__ | ||
record_EDF = read_edf("sample-data/n16.edf").__dict__ | ||
|
||
fields = list(record_MIT.keys()) | ||
# Original MIT format method of checksum is outdated, sometimes | ||
# the same value though | ||
fields.remove("checksum") | ||
# Original MIT format units are less comprehensive since they | ||
# default to mV if unknown.. therefore added more default labels | ||
fields.remove("units") | ||
|
||
test_results = [] | ||
for field in fields: | ||
# Signal value will be slightly off due to C to Python type conversion | ||
if field == "p_signal": | ||
true_array = np.array(record_MIT[field]).flatten() | ||
pred_array = np.array(record_EDF[field]).flatten() | ||
# Prevent divide by zero warning | ||
for i, v in enumerate(true_array): | ||
if v == 0: | ||
true_array[i] = 1 | ||
pred_array[i] = 1 | ||
sig_diff = np.abs((pred_array - true_array) / true_array) | ||
sig_diff[sig_diff == -np.inf] = 0 | ||
sig_diff[sig_diff == np.inf] = 0 | ||
sig_diff = np.nanmean(sig_diff, 0) | ||
# 5% tolerance | ||
if np.max(sig_diff) <= 5: | ||
test_results.append(True) | ||
else: | ||
test_results.append(False) | ||
elif field == "init_value": | ||
signal_diff = [ | ||
abs(record_MIT[field][i] - record_EDF[field][i]) | ||
for i in range(len(record_MIT[field])) | ||
] | ||
if abs(max(min(signal_diff), max(signal_diff), key=abs)) <= 2: | ||
test_results.append(True) | ||
else: | ||
test_results.append(False) | ||
else: | ||
test_results.append(record_MIT[field] == record_MIT[field]) | ||
|
||
target_results = len(fields) * [True] | ||
assert np.array_equal(test_results, target_results) | ||
|
||
def test_edf_non_uniform(self): | ||
""" | ||
EDF format conversion to MIT for non-uniform sample rates. | ||
""" | ||
# Non-uniform sample rates | ||
record_MIT = rdrecord("sample-data/wave_4").__dict__ | ||
record_EDF = read_edf("sample-data/wave_4.edf").__dict__ | ||
|
||
fields = list(record_MIT.keys()) | ||
# Original MIT format method of checksum is outdated, sometimes | ||
# the same value though | ||
fields.remove("checksum") | ||
# Original MIT format units are less comprehensive since they | ||
# default to mV if unknown.. therefore added more default labels | ||
fields.remove("units") | ||
|
||
test_results = [] | ||
for field in fields: | ||
# Signal value will be slightly off due to C to Python type conversion | ||
if field == "p_signal": | ||
true_array = np.array(record_MIT[field]).flatten() | ||
pred_array = np.array(record_EDF[field]).flatten() | ||
# Prevent divide by zero warning | ||
for i, v in enumerate(true_array): | ||
if v == 0: | ||
true_array[i] = 1 | ||
pred_array[i] = 1 | ||
sig_diff = np.abs((pred_array - true_array) / true_array) | ||
sig_diff[sig_diff == -np.inf] = 0 | ||
sig_diff[sig_diff == np.inf] = 0 | ||
sig_diff = np.nanmean(sig_diff, 0) | ||
# 5% tolerance | ||
if np.max(sig_diff) <= 5: | ||
test_results.append(True) | ||
else: | ||
test_results.append(False) | ||
elif field == "init_value": | ||
signal_diff = [ | ||
abs(record_MIT[field][i] - record_EDF[field][i]) | ||
for i in range(len(record_MIT[field])) | ||
] | ||
if abs(max(min(signal_diff), max(signal_diff), key=abs)) <= 2: | ||
test_results.append(True) | ||
else: | ||
test_results.append(False) | ||
else: | ||
test_results.append(record_MIT[field] == record_MIT[field]) | ||
|
||
target_results = len(fields) * [True] | ||
assert np.array_equal(test_results, target_results) |
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
Oops, something went wrong.