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

Download this file

109 lines (82 with data), 3.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
"""
"""
from pathlib import Path
import pytest
from torch_ecg.databases import AFDB, list_databases
from torch_ecg.databases.base import (
BeatAnn,
CPSCDataBase,
DataBaseInfo,
NSRRDataBase,
PhysioNetDataBase,
WFDB_Beat_Annotations,
WFDB_Non_Beat_Annotations,
WFDB_Rhythm_Annotations,
_DataBase,
)
from torch_ecg.databases.datasets import list_datasets
def test_base_database():
with pytest.raises(
TypeError,
match=f"Can't instantiate abstract class {_DataBase.__name__}",
):
db = _DataBase()
with pytest.raises(
TypeError,
match=f"Can't instantiate abstract class {PhysioNetDataBase.__name__}",
):
db = PhysioNetDataBase()
with pytest.raises(
TypeError,
match=f"Can't instantiate abstract class {NSRRDataBase.__name__}",
):
db = NSRRDataBase()
with pytest.raises(
TypeError,
match=f"Can't instantiate abstract class {CPSCDataBase.__name__}",
):
db = CPSCDataBase()
def test_beat_ann():
index = 100
for symbol, name in WFDB_Beat_Annotations.items():
ba = BeatAnn(index, symbol)
assert ba.index == index
assert ba.symbol == symbol
assert ba.name == name
for symbol, name in WFDB_Non_Beat_Annotations.items():
ba = BeatAnn(index, symbol)
assert ba.index == index
assert ba.symbol == symbol
assert ba.name == name
ba = BeatAnn(index, "XXX")
assert ba.index == index
assert ba.symbol == "XXX"
assert ba.name == "XXX"
def test_get_arrhythmia_knowledge():
assert _DataBase.get_arrhythmia_knowledge("AF") is None # printed
assert _DataBase.get_arrhythmia_knowledge(["AF", "PVC"]) is None # printed
def test_database_meta():
with pytest.warns(RuntimeWarning, match="`db_dir` is not specified"):
reader = AFDB()
assert reader.db_dir == Path("~").expanduser() / ".cache" / "torch_ecg" / "data" / "afdb"
assert reader.helper() is None # printed
for item in ["attributes", "methods", "beat", "non-beat", "rhythm"]:
assert reader.helper(item) is None # printed
assert reader.helper(["methods", "beat"]) is None # printed
for k in WFDB_Beat_Annotations:
assert reader.helper(k) is None # printed: `{k}` stands for `{WFDB_Beat_Annotations[k]}`
for k in WFDB_Non_Beat_Annotations:
assert reader.helper(k) is None # printed: `{k}` stands for `{WFDB_Non_Beat_Annotations[k]}`
for k in WFDB_Rhythm_Annotations:
assert reader.helper(k) is None # printed: `{k}` stands for `{WFDB_Rhythm_Annotations[k]}`
def test_database_info():
with pytest.warns(RuntimeWarning, match="`db_dir` is not specified"):
reader = AFDB()
assert isinstance(reader.database_info, DataBaseInfo)
def test_list_databases():
assert isinstance(list_databases(), list)
assert len(list_databases()) > 0
def test_list_datasets():
assert isinstance(list_datasets(), list)
assert len(list_datasets()) > 0
assert all([item.endswith("Dataset") for item in list_datasets()]), list_datasets()