[b48499]: / test / test_databases / test_sph.py

Download this file

149 lines (125 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
TestSPH: accomplished
subsampling: NOT tested
"""
import shutil
from pathlib import Path
import numpy as np
import pytest
from torch_ecg.databases import SPH, DataBaseInfo
from torch_ecg.utils.download import http_get
###############################################################################
# set paths
_CWD = Path(__file__).absolute().parents[2] / "tmp" / "test-db" / "sph"
try:
shutil.rmtree(_CWD)
except FileNotFoundError:
pass
_CWD.mkdir(parents=True, exist_ok=True)
http_get(
url="https://www.dropbox.com/s/og877l6d4bh4vew/SPH-Mini.tar.gz?dl=1",
dst_dir=_CWD,
extract=True,
)
###############################################################################
reader = SPH(_CWD)
class TestSPH:
def test_len(self):
assert len(reader) == 100
def test_subsample(self):
ss_ratio = 0.3
reader_ss = SPH(_CWD, subsample=ss_ratio, verbose=0)
assert len(reader_ss) == pytest.approx(len(reader) * ss_ratio, abs=1)
ss_ratio = 0.1 / len(reader)
reader_ss = SPH(_CWD, subsample=ss_ratio)
assert len(reader_ss) == 1
with pytest.raises(AssertionError, match="`subsample` must be in \\(0, 1\\], but got `.+`"):
SPH(_CWD, subsample=0.0)
with pytest.raises(AssertionError, match="`subsample` must be in \\(0, 1\\], but got `.+`"):
SPH(_CWD, subsample=1.01)
with pytest.raises(AssertionError, match="`subsample` must be in \\(0, 1\\], but got `.+`"):
SPH(_CWD, subsample=-0.1)
def test_load_data(self):
for rec in reader:
data = reader.load_data(rec)
data_1 = reader.load_data(rec, leads=[1, 7])
assert data.shape[0] == 12
assert data_1.shape[0] == 2
assert np.allclose(data[[1, 7], :], data_1)
data_1 = reader.load_data(rec, units="uV")
assert np.allclose(data_1, data * 1000)
data_1 = reader.load_data(rec, data_format="lead_last")
assert data.shape == data_1.T.shape
data_1, data_1_fs = reader.load_data(rec, return_fs=True)
assert data_1_fs == reader.fs
with pytest.raises(AssertionError, match="Invalid data_format: `flat`"):
reader.load_data(rec, data_format="flat")
with pytest.raises(AssertionError, match="Invalid units: `kV`"):
reader.load_data(rec, units="kV")
def test_load_ann(self):
for rec in reader:
ann = reader.load_ann(rec, ann_format="c")
ann_1 = reader.load_ann(rec, ann_format="f")
assert isinstance(ann, list) and isinstance(ann_1, list)
assert len(ann) == len(ann_1)
ann_1 = reader.load_ann(rec, ann_format="c", ignore_modifier=False)
assert len(ann) == len(ann_1)
ann_1 = reader.load_ann(rec, ann_format="f", ignore_modifier=False)
assert len(ann) == len(ann_1)
with pytest.raises(ValueError, match="Unknown annotation format: `flat`"):
reader.load_ann(rec, ann_format="flat")
with pytest.raises(NotImplementedError, match="Abbreviations are not supported yet"):
reader.load_ann(rec, ann_format="a")
def test_get_subject_info(self):
for rec in reader:
info = reader.get_subject_info(rec)
assert isinstance(info, dict)
assert info.keys() == {"age", "sex"}
def test_get_subject_id(self):
for rec in reader:
sid = reader.get_subject_id(rec)
assert isinstance(sid, str)
def test_get_age(self):
for rec in reader:
age = reader.get_age(rec)
assert isinstance(age, int)
assert age > 0
def test_get_sex(self):
for rec in reader:
sex = reader.get_sex(rec)
assert isinstance(sex, str)
assert sex in ["M", "F"]
def test_get_siglen(self):
for rec in reader:
siglen = reader.get_siglen(rec)
data = reader.load_data(rec)
assert isinstance(siglen, int)
assert siglen == data.shape[1]
def test_meta_data(self):
assert isinstance(reader.url, dict)
assert reader.get_citation() is None # printed
isinstance(reader.database_info, DataBaseInfo)
def test_plot(self):
waves = {
"p_onsets": [100, 1100],
"p_offsets": [110, 1110],
"q_onsets": [115, 1115],
"s_offsets": [130, 1130],
"t_onsets": [150, 1150],
"t_offsets": [190, 1190],
}
reader.plot(0, leads="II", ticks_granularity=2, waves=waves)
waves = {
"p_peaks": [105, 1105],
"q_peaks": [120, 1120],
"s_peaks": [125, 1125],
"t_peaks": [170, 1170],
}
reader.plot(0, leads=["II", 7], ticks_granularity=1, waves=waves)
waves = {
"p_peaks": [105, 1105],
"r_peaks": [122, 1122],
"t_peaks": [170, 1170],
}
data = reader.load_data(0)
reader.plot(0, data=data, ticks_granularity=0, waves=waves)