Skip to content

Move conversion functions to their own modules and remove cyclic imports #370

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

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WFDB Records
---------------

.. automodule:: wfdb.io
:members: rdrecord, rdheader, rdsamp, wrsamp, edf2mit, mit2edf, wav2mit, mit2wav, csv2mit
:members: rdrecord, rdheader, rdsamp, wrsamp, read_edf, wfdb_to_edf, read_wav, wfdb_to_wav, csv_to_wfdb, wfdb_to_mat

.. autoclass:: wfdb.io.Record
:members: wrsamp, adc, dac
Expand Down
5 changes: 5 additions & 0 deletions docs/processing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Peaks
.. automodule:: wfdb.processing
:members: find_peaks, find_local_peaks, correct_peaks

Filters
-------

.. automodule:: wfdb.processing
:members: sigavg

QRS Detectors
-------------
Expand Down
2 changes: 1 addition & 1 deletion docs/wfdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WFDB Records
---------------

.. automodule:: wfdb
:members: rdrecord, rdheader, rdsamp, wrsamp, edf2mit, mit2edf, wav2mit, mit2wav, csv2mit
:members: rdrecord, rdheader, rdsamp, wrsamp

.. autoclass:: wfdb.Record
:members: wrsamp, adc, dac
Expand Down
110 changes: 110 additions & 0 deletions tests/io/test_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import numpy as np

import wfdb
from wfdb.io import read_edf


class TestConvert:
def test_edf_uniform(self):
"""
EDF format conversion to MIT for uniform sample rates.

"""
# Uniform sample rates
record_MIT = wfdb.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 = wfdb.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)
106 changes: 1 addition & 105 deletions tests/test_record.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import pdb
import shutil
import unittest

import numpy as np

import wfdb


Expand Down Expand Up @@ -339,110 +339,6 @@ def test_2e(self):
sig_target = sig_target.reshape([977, 1])
assert np.array_equal(sig, sig_target)

def test_2f(self):
"""
EDF format conversion to MIT for uniform sample rates.

"""
# Uniform sample rates
record_MIT = wfdb.rdrecord("sample-data/n16").__dict__
record_EDF = wfdb.edf2mit("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_2g(self):
"""
EDF format conversion to MIT for non-uniform sample rates.

"""
# Non-uniform sample rates
record_MIT = wfdb.rdrecord("sample-data/wave_4").__dict__
record_EDF = wfdb.edf2mit("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)

# --------------------- 3. Multi-dat records --------------------- #

def test_3a(self):
Expand Down
7 changes: 0 additions & 7 deletions wfdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@
rdsamp,
wrsamp,
dl_database,
edf2mit,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions for the extra formats should not be part of the top level module.

mit2edf,
wav2mit,
mit2wav,
wfdb2mat,
csv2mit,
sampfreq,
signame,
wfdbdesc,
wfdbtime,
sigavg,
)
from wfdb.io.annotation import (
Annotation,
Expand Down
13 changes: 5 additions & 8 deletions wfdb/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@
rdsamp,
wrsamp,
dl_database,
edf2mit,
mit2edf,
wav2mit,
mit2wav,
wfdb2mat,
csv2mit,
sampfreq,
signame,
wfdbdesc,
wfdbtime,
sigavg,
SIGNAL_CLASSES,
)
from wfdb.io._signal import est_res, wr_dat_file
Expand All @@ -38,4 +31,8 @@
dl_files,
set_db_index_url,
)
from wfdb.io.tff import rdtff
from wfdb.io.convert.csv import csv_to_wfdb
from wfdb.io.convert.edf import read_edf, wfdb_to_edf
from wfdb.io.convert.matlab import wfdb_to_mat
from wfdb.io.convert.tff import rdtff
from wfdb.io.convert.wav import wfdb_to_wav, read_wav
5 changes: 2 additions & 3 deletions wfdb/io/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import pandas as pd
import re
import posixpath
import pdb
import struct
import sys

from wfdb.io import download
from wfdb.io import _header
from wfdb.io import record

from wfdb.io.convert.edf import read_edf

class Annotation(object):
"""
Expand Down Expand Up @@ -3026,7 +3025,7 @@ def rdedfann(
# "The coding is EDF compatible in the sense that old EDF software would
# simply treat this 'EDF Annotations' signal as if it were a (strange-
# looking) ordinary signal"
rec = record.edf2mit(
rec = read_edf(
record_name,
pn_dir=pn_dir,
delete_file=delete_file,
Expand Down
Empty file added wfdb/io/convert/__init__.py
Empty file.
Loading