Skip to content

Commit 4d718ec

Browse files
committed
Fix style.
1 parent 4253562 commit 4d718ec

File tree

5 files changed

+80
-49
lines changed

5 files changed

+80
-49
lines changed

tests/test_archive.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import os
2-
import numpy as np
3-
import pytest
42
import tempfile
53
import zipfile
64

5+
import numpy as np
6+
import pytest
7+
78
from wfdb import rdrecord, wrsamp
89
from wfdb.io.archive import WFDBArchive
910

10-
1111
np.random.seed(1234)
1212

1313

@@ -132,8 +132,9 @@ def test_wfdb_archive_round_trip(temp_record):
132132
assert record.p_signal.shape == original_signal.shape
133133

134134
# Add tolerance to account for loss of precision during archive round-trip
135-
np.testing.assert_allclose(record.p_signal, original_signal, rtol=1e-2,
136-
atol=3e-3)
135+
np.testing.assert_allclose(
136+
record.p_signal, original_signal, rtol=1e-2, atol=3e-3
137+
)
137138

138139

139140
def test_archive_read_subset_channels(temp_record):
@@ -149,8 +150,9 @@ def test_archive_read_subset_channels(temp_record):
149150
assert record.p_signal.shape[0] == original_signal.shape[0]
150151

151152
# Add tolerance to account for loss of precision during archive round-trip
152-
np.testing.assert_allclose(record.p_signal[:, 0], original_signal[:, 1],
153-
rtol=1e-2, atol=3e-3)
153+
np.testing.assert_allclose(
154+
record.p_signal[:, 0], original_signal[:, 1], rtol=1e-2, atol=3e-3
155+
)
154156

155157

156158
def test_archive_read_partial_samples(temp_record):
@@ -164,7 +166,9 @@ def test_archive_read_partial_samples(temp_record):
164166
record = rdrecord(archive_path, sampfrom=start, sampto=stop)
165167

166168
assert record.p_signal.shape == (stop - start, original_signal.shape[1])
167-
np.testing.assert_allclose(record.p_signal, original_signal[start:stop], rtol=1e-2, atol=1e-3)
169+
np.testing.assert_allclose(
170+
record.p_signal, original_signal[start:stop], rtol=1e-2, atol=1e-3
171+
)
168172

169173

170174
def test_archive_missing_file_error(temp_record):
@@ -178,8 +182,9 @@ def test_archive_missing_file_error(temp_record):
178182
zf_name = [name for name in zf.namelist() if name.endswith(".dat")][0]
179183
zf.fp = None # Prevent auto-close bug in some zipfile implementations
180184
os.rename(archive_path, archive_path + ".bak")
181-
with zipfile.ZipFile(archive_path + ".bak", "r") as zin, \
182-
zipfile.ZipFile(archive_path, "w") as zout:
185+
with zipfile.ZipFile(archive_path + ".bak", "r") as zin, zipfile.ZipFile(
186+
archive_path, "w"
187+
) as zout:
183188
for item in zin.infolist():
184189
if not item.filename.endswith(".dat"):
185190
zout.writestr(item, zin.read(item.filename))

wfdb/io/_header.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import numpy as np
66
import pandas as pd
77

8-
from wfdb.io import _signal
9-
from wfdb.io import util
8+
from wfdb.io import _signal, util
109
from wfdb.io.header import HeaderSyntaxError, rx_record, rx_segment, rx_signal
1110

1211
"""
@@ -326,8 +325,12 @@ def wrheader(self, write_dir="", expanded=True, wfdb_archive=None):
326325
self.check_field_cohesion(rec_write_fields, list(sig_write_fields))
327326

328327
# Write the header file using the specified fields
329-
self.wr_header_file(rec_write_fields, sig_write_fields, write_dir,
330-
wfdb_archive=wfdb_archive)
328+
self.wr_header_file(
329+
rec_write_fields,
330+
sig_write_fields,
331+
write_dir,
332+
wfdb_archive=wfdb_archive,
333+
)
331334

332335
def get_write_fields(self):
333336
"""
@@ -510,8 +513,9 @@ def check_field_cohesion(self, rec_write_fields, sig_write_fields):
510513
"Each file_name (dat file) specified must have the same byte offset"
511514
)
512515

513-
def wr_header_file(self, rec_write_fields, sig_write_fields, write_dir,
514-
wfdb_archive=None):
516+
def wr_header_file(
517+
self, rec_write_fields, sig_write_fields, write_dir, wfdb_archive=None
518+
):
515519
"""
516520
Write a header file using the specified fields. Converts Record
517521
attributes into appropriate WFDB format strings.

wfdb/io/_signal.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import math
21
import io
2+
import math
33
import os
44
import posixpath
55
import sys
66

77
import fsspec
88
import numpy as np
99

10-
from wfdb.io import download, _coreio, util
10+
from wfdb.io import _coreio, download, util
1111
from wfdb.io._coreio import CLOUD_PROTOCOLS
1212

1313
MAX_I32 = 2147483647
@@ -163,8 +163,9 @@ def wr_dats(self, expanded, write_dir, wfdb_archive=None):
163163
self.check_sig_cohesion([], expanded)
164164

165165
# Write each of the specified dat files
166-
self.wr_dat_files(expanded=expanded, write_dir=write_dir,
167-
wfdb_archive=wfdb_archive)
166+
self.wr_dat_files(
167+
expanded=expanded, write_dir=write_dir, wfdb_archive=wfdb_archive
168+
)
168169

169170
def check_sig_cohesion(self, write_fields, expanded):
170171
"""
@@ -1401,8 +1402,13 @@ def _rd_dat_signals(
14011402
)
14021403
else:
14031404
data_to_read = _rd_dat_file(
1404-
file_name, dir_name, pn_dir, fmt, start_byte, n_read_samples,
1405-
wfdb_archive=wfdb_archive
1405+
file_name,
1406+
dir_name,
1407+
pn_dir,
1408+
fmt,
1409+
start_byte,
1410+
n_read_samples,
1411+
wfdb_archive=wfdb_archive,
14061412
)
14071413

14081414
if extra_flat_samples:
@@ -1642,8 +1648,9 @@ def _required_byte_num(mode, fmt, n_samp):
16421648
return int(n_bytes)
16431649

16441650

1645-
def _rd_dat_file(file_name, dir_name, pn_dir, fmt, start_byte, n_samp,
1646-
wfdb_archive=None):
1651+
def _rd_dat_file(
1652+
file_name, dir_name, pn_dir, fmt, start_byte, n_samp, wfdb_archive=None
1653+
):
16471654
"""
16481655
Read data from a dat file, either local or remote, into a 1d numpy
16491656
array.

wfdb/io/archive.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class WFDBArchive:
1717
- .wfdb is included in the record_name explicitly, or
1818
- .wfdb is passed directly to the file loading function.
1919
"""
20+
2021
def __init__(self, record_name, mode="r"):
2122
"""
2223
Initialize a WFDBArchive for a given record name (without extension).
@@ -35,7 +36,9 @@ def __init__(self, record_name, mode="r"):
3536

3637
if mode == "r":
3738
if not os.path.exists(self.archive_path):
38-
raise FileNotFoundError(f"Archive not found: {self.archive_path}")
39+
raise FileNotFoundError(
40+
f"Archive not found: {self.archive_path}"
41+
)
3942
if not zipfile.is_zipfile(self.archive_path):
4043
raise ValueError(f"Invalid WFDB archive: {self.archive_path}")
4144
self.zipfile = zipfile.ZipFile(self.archive_path, mode="r")
@@ -60,16 +63,17 @@ def open(self, filename, mode="r"):
6063
Mode 'r' (text) or 'rb' (binary) supported.
6164
"""
6265
if self.zipfile and filename in self.zipfile.namelist():
63-
with self.zipfile.open(filename, 'r') as f:
66+
with self.zipfile.open(filename, "r") as f:
6467
if "b" in mode:
6568
yield f
6669
else:
6770
import io
71+
6872
yield io.TextIOWrapper(f)
6973
else:
7074
raise FileNotFoundError(
7175
f"Could not find '{filename}' as loose file or inside '{self.archive_path}'."
72-
)
76+
)
7377

7478
def close(self):
7579
"""
@@ -111,14 +115,17 @@ def create_archive(self, file_list, output_path=None):
111115
if file.endswith((".hea", ".hea.json", ".hea.yml"))
112116
else zipfile.ZIP_DEFLATED
113117
)
114-
zf.write(file, arcname=os.path.basename(file), compress_type=compress)
118+
zf.write(
119+
file, arcname=os.path.basename(file), compress_type=compress
120+
)
115121

116122

117123
def get_archive(record_base_name, mode="r"):
118124
"""
119125
Get or create a WFDBArchive for the given record base name.
120126
"""
121127
if record_base_name not in _archive_cache:
122-
_archive_cache[record_base_name] = WFDBArchive(record_base_name,
123-
mode=mode)
128+
_archive_cache[record_base_name] = WFDBArchive(
129+
record_base_name, mode=mode
130+
)
124131
return _archive_cache[record_base_name]

wfdb/io/record.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import datetime
22
import multiprocessing.dummy
3-
import posixpath
43
import os
4+
import posixpath
55
import re
66

77
import fsspec
88
import numpy as np
99
import pandas as pd
1010

11-
from wfdb.io import _header
12-
from wfdb.io import _signal
13-
from wfdb.io import _url
14-
from wfdb.io.archive import get_archive
15-
from wfdb.io import download
16-
from wfdb.io import header
17-
from wfdb.io import util
11+
from wfdb.io import _header, _signal, _url, download, header, util
1812
from wfdb.io._coreio import CLOUD_PROTOCOLS
19-
13+
from wfdb.io.archive import get_archive
2014

2115
# -------------- WFDB Signal Calibration and Classification ---------- #
2216

@@ -935,13 +929,17 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
935929

936930
# Perform field validity and cohesion checks, and write the
937931
# header file.
938-
self.wrheader(write_dir=write_dir, expanded=expanded,
939-
wfdb_archive=wfdb_archive)
932+
self.wrheader(
933+
write_dir=write_dir, expanded=expanded, wfdb_archive=wfdb_archive
934+
)
940935
if self.n_sig > 0:
941936
# Perform signal validity and cohesion checks, and write the
942937
# associated dat files.
943-
self.wr_dats(expanded=expanded, write_dir=write_dir,
944-
wfdb_archive=wfdb_archive)
938+
self.wr_dats(
939+
expanded=expanded,
940+
write_dir=write_dir,
941+
wfdb_archive=wfdb_archive,
942+
)
945943

946944
def _arrange_fields(self, channels, sampfrom, smooth_frames):
947945
"""
@@ -2046,10 +2044,13 @@ def rdrecord(
20462044
hea_file = os.path.basename(record_base) + ".hea"
20472045

20482046
import tempfile
2047+
20492048
with wfdb_archive.open(hea_file, "r") as f:
20502049
header_str = f.read()
20512050

2052-
with tempfile.NamedTemporaryFile("w+", suffix=".hea", delete=False) as tmpf:
2051+
with tempfile.NamedTemporaryFile(
2052+
"w+", suffix=".hea", delete=False
2053+
) as tmpf:
20532054
tmpf.write(header_str)
20542055
tmpf.flush()
20552056
record = rdheader(tmpf.name)
@@ -2074,7 +2075,9 @@ def rdrecord(
20742075
if "." not in pn_dir:
20752076
dir_list = pn_dir.split("/")
20762077
pn_dir = posixpath.join(
2077-
dir_list[0], download.get_version(dir_list[0]), *dir_list[1:]
2078+
dir_list[0],
2079+
download.get_version(dir_list[0]),
2080+
*dir_list[1:],
20782081
)
20792082

20802083
record = rdheader(record_name, pn_dir=pn_dir, rd_segments=False)
@@ -2783,7 +2786,9 @@ def wfdbtime(record_name, input_times, pn_dir=None):
27832786
)
27842787
if not times.startswith("s"):
27852788
sample_num = int(
2786-
sum(x * 60**i for i, x in enumerate([seconds, minutes, hours]))
2789+
sum(
2790+
x * 60**i for i, x in enumerate([seconds, minutes, hours])
2791+
)
27872792
* record.fs
27882793
)
27892794
sample_num = "s" + str(sample_num)
@@ -3080,11 +3085,14 @@ def wrsamp(
30803085
expanded = False
30813086

30823087
if wfdb_archive:
3083-
wfdb_archive = get_archive(os.path.join(write_dir, record_name),
3084-
mode="w")
3088+
wfdb_archive = get_archive(
3089+
os.path.join(write_dir, record_name), mode="w"
3090+
)
30853091

30863092
# Write the record files - header and associated dat
3087-
record.wrsamp(write_dir=write_dir, expanded=expanded, wfdb_archive=wfdb_archive)
3093+
record.wrsamp(
3094+
write_dir=write_dir, expanded=expanded, wfdb_archive=wfdb_archive
3095+
)
30883096

30893097

30903098
def dl_database(

0 commit comments

Comments
 (0)