Skip to content

Commit 4c784fa

Browse files
authored
Move some general functions to a util module and clean up some imports (#396)
1 parent 225d2a2 commit 4c784fa

File tree

6 files changed

+113
-105
lines changed

6 files changed

+113
-105
lines changed

wfdb/io/_header.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import datetime
2-
import os
32
import re
43
from typing import List, Tuple
54

65
import numpy as np
76
import pandas as pd
87

98
from wfdb.io import _signal
9+
from wfdb.io import util
1010

1111

1212
"""
@@ -603,7 +603,7 @@ def wr_header_file(self, rec_write_fields, sig_write_fields, write_dir):
603603
comment_lines = ["# " + comment for comment in self.comments]
604604
header_lines += comment_lines
605605

606-
lines_to_file(self.record_name + ".hea", write_dir, header_lines)
606+
util.lines_to_file(self.record_name + ".hea", write_dir, header_lines)
607607

608608

609609
class MultiHeaderMixin(BaseHeaderMixin):
@@ -795,7 +795,7 @@ def wr_header_file(self, write_fields, write_dir):
795795
comment_lines = ["# " + comment for comment in self.comments]
796796
header_lines += comment_lines
797797

798-
lines_to_file(self.record_name + ".hea", header_lines, write_dir)
798+
util.lines_to_file(self.record_name + ".hea", header_lines, write_dir)
799799

800800
def get_sig_segments(self, sig_name=None):
801801
"""
@@ -1122,25 +1122,3 @@ def _read_segment_lines(segment_lines):
11221122

11231123
class HeaderSyntaxError(ValueError):
11241124
"""Invalid syntax found in a WFDB header file."""
1125-
1126-
1127-
def lines_to_file(file_name, write_dir, lines):
1128-
"""
1129-
Write each line in a list of strings to a text file.
1130-
1131-
Parameters
1132-
----------
1133-
write_dir : str
1134-
The output directory in which the header is written.
1135-
lines : list
1136-
The lines to be written to the text file.
1137-
1138-
Returns
1139-
-------
1140-
N/A
1141-
1142-
"""
1143-
f = open(os.path.join(write_dir, file_name), "w")
1144-
for l in lines:
1145-
f.write("%s\n" % l)
1146-
f.close()

wfdb/io/_signal.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np
66

7-
from wfdb.io import download, _coreio
7+
from wfdb.io import download, _coreio, util
88

99

1010
MAX_I32 = 2147483647
@@ -1563,14 +1563,14 @@ def _required_byte_num(mode, fmt, n_samp):
15631563

15641564
if n_extra == 2:
15651565
if fmt == "310":
1566-
n_bytes = upround(n_samp * 4 / 3, 4)
1566+
n_bytes = util.upround(n_samp * 4 / 3, 4)
15671567
# 311
15681568
else:
15691569
if mode == "read":
15701570
n_bytes = math.ceil(n_samp * 4 / 3)
15711571
# Have to write more bytes for WFDB c to work
15721572
else:
1573-
n_bytes = upround(n_samp * 4 / 3, 4)
1573+
n_bytes = util.upround(n_samp * 4 / 3, 4)
15741574
# 0 or 1
15751575
else:
15761576
n_bytes = math.ceil(n_samp * 4 / 3)
@@ -2506,42 +2506,3 @@ def _infer_sig_len(
25062506

25072507
return sig_len
25082508

2509-
2510-
def downround(x, base):
2511-
"""
2512-
Round <x> down to nearest <base>.
2513-
2514-
Parameters
2515-
---------
2516-
x : str, int, float
2517-
The number that will be rounded down.
2518-
base : int, float
2519-
The base to be rounded down to.
2520-
2521-
Returns
2522-
-------
2523-
float
2524-
The rounded down result of <x> down to nearest <base>.
2525-
2526-
"""
2527-
return base * math.floor(float(x) / base)
2528-
2529-
2530-
def upround(x, base):
2531-
"""
2532-
Round <x> up to nearest <base>.
2533-
2534-
Parameters
2535-
---------
2536-
x : str, int, float
2537-
The number that will be rounded up.
2538-
base : int, float
2539-
The base to be rounded up to.
2540-
2541-
Returns
2542-
-------
2543-
float
2544-
The rounded up result of <x> up to nearest <base>.
2545-
2546-
"""
2547-
return base * math.ceil(float(x) / base)

wfdb/io/record.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from wfdb.io import _signal
1212
from wfdb.io import _url
1313
from wfdb.io import download
14+
from wfdb.io import util
1415

1516

1617
# -------------- WFDB Signal Calibration and Classification ---------- #
@@ -468,7 +469,7 @@ def check_field(self, field, required_channels="all"):
468469
"File names should only contain alphanumerics, hyphens, and an extension. eg. record-100.dat"
469470
)
470471
# Check that dat files are grouped together
471-
if not is_monotonic(self.file_name):
472+
if not util.is_monotonic(self.file_name):
472473
raise ValueError(
473474
"Signals in a record that share a given file must be consecutive."
474475
)
@@ -2920,37 +2921,6 @@ def wrsamp(
29202921
record.wrsamp(write_dir=write_dir)
29212922

29222923

2923-
def is_monotonic(full_list):
2924-
"""
2925-
Determine whether elements in a list are monotonic. ie. unique
2926-
elements are clustered together.
2927-
2928-
ie. [5,5,3,4] is, [5,3,5] is not.
2929-
2930-
Parameters
2931-
----------
2932-
full_list : list
2933-
The input elements used for the analysis.
2934-
2935-
Returns
2936-
-------
2937-
bool
2938-
Whether the elements are monotonic (True) or not (False).
2939-
2940-
"""
2941-
prev_elements = set({full_list[0]})
2942-
prev_item = full_list[0]
2943-
2944-
for item in full_list:
2945-
if item != prev_item:
2946-
if item in prev_elements:
2947-
return False
2948-
prev_item = item
2949-
prev_elements.add(item)
2950-
2951-
return True
2952-
2953-
29542924
def dl_database(
29552925
db_dir,
29562926
dl_dir,

wfdb/io/util.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
A module for general utility functions
3+
"""
4+
import math
5+
import os
6+
7+
from typing import Sequence
8+
9+
10+
def lines_to_file(file_name: str, write_dir: str, lines: Sequence[str]):
11+
"""
12+
Write each line in a list of strings to a text file.
13+
14+
Parameters
15+
----------
16+
file_name: str
17+
The base name of the file
18+
write_dir : str
19+
The output directory in which the file is to be written.
20+
lines : list
21+
The lines to be written to the text file.
22+
23+
Returns
24+
-------
25+
N/A
26+
27+
"""
28+
with open(os.path.join(write_dir, file_name), "w", encoding="utf-8") as f:
29+
for l in lines:
30+
f.write(f"{l}\n")
31+
32+
33+
def is_monotonic(items: Sequence) -> bool:
34+
"""
35+
Determine whether elements in a list are monotonic. ie. unique
36+
elements are clustered together.
37+
38+
ie. [5,5,3,4] is, [5,3,5] is not.
39+
40+
Parameters
41+
----------
42+
items : Sequence
43+
The input elements to be checked.
44+
45+
Returns
46+
-------
47+
bool
48+
Whether the elements are monotonic (True) or not (False).
49+
50+
"""
51+
prev_elements = set({items[0]})
52+
prev_item = items[0]
53+
54+
for item in items:
55+
if item != prev_item:
56+
if item in prev_elements:
57+
return False
58+
prev_item = item
59+
prev_elements.add(item)
60+
61+
return True
62+
63+
64+
def downround(x, base):
65+
"""
66+
Round <x> down to nearest <base>.
67+
68+
Parameters
69+
---------
70+
x : str, int, float
71+
The number that will be rounded down.
72+
base : int, float
73+
The base to be rounded down to.
74+
75+
Returns
76+
-------
77+
float
78+
The rounded down result of <x> down to nearest <base>.
79+
80+
"""
81+
return base * math.floor(float(x) / base)
82+
83+
84+
def upround(x, base):
85+
"""
86+
Round <x> up to nearest <base>.
87+
88+
Parameters
89+
---------
90+
x : str, int, float
91+
The number that will be rounded up.
92+
base : int, float
93+
The base to be rounded up to.
94+
95+
Returns
96+
-------
97+
float
98+
The rounded up result of <x> up to nearest <base>.
99+
100+
"""
101+
return base * math.ceil(float(x) / base)

wfdb/plot/plot.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import numpy as np
21
import os
3-
import pdb
2+
3+
import numpy as np
44

55
from wfdb.io.record import Record, rdrecord
6-
from wfdb.io._header import float_types
7-
from wfdb.io._signal import downround, upround
6+
from wfdb.io.util import downround, upround
87
from wfdb.io.annotation import Annotation
98

109

wfdb/processing/basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
2-
from scipy import signal
32
import pandas as pd
4-
import pdb
3+
from scipy import signal
54

65
from wfdb.io.annotation import Annotation
76

0 commit comments

Comments
 (0)