Skip to content

Commit a989032

Browse files
committed
Tell a record's own files by its header, not by their names
The warning treated any signal file whose name started with the record name as the record's own, so renaming record 100_extra to 100 replaced 100_extra.dat without a warning. A file is now the record's own only if the record's existing header in write_dir lists it, as when a record is written again. Two tests in TestRecord wrote records whose signal file is shared with another sample record (test01_00s.dat) into the directory shared by the class, so they replaced a file written by another test and triggered the warning. Give them their own directories.
1 parent 998888c commit a989032

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎changelog.d/warn-replacing-other-files.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
files by ``file_name``, so writing a record that was read under a new
44
``record_name``, next to the original, replaced the original's signal
55
files without any sign. It now warns when writing would replace an existing
6-
signal file whose name does not start with ``record_name``. The
6+
signal file that the record's own header in ``write_dir`` does not list. The
77
documentation of ``wrsamp`` and ``file_name`` now explains this, with an
88
example of saving a copy under a new name.

‎src/wfdb/io/record.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
905905
names of the original, so to save a copy under a new name, change
906906
both, or the copy's signals are written to the original's files. A
907907
warning is given when writing would replace an existing signal file
908-
whose name does not start with `record_name`.
908+
that is not listed by this record's own header in `write_dir`.
909909
910910
Parameters
911911
----------
@@ -1003,9 +1003,11 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
10031003
def _warn_about_replacing_other_files(self, write_dir):
10041004
"""Warn if writing would replace signal files of another record.
10051005
1006-
This is the case when a signal file already exists and its name does
1007-
not start with the record name, as when a record that was read is
1008-
given a new `record_name` but keeps the original's `file_name`.
1006+
A signal file that already exists is this record's own if the
1007+
record's header in `write_dir` lists it, as when a record is written
1008+
again. Otherwise it belongs to another record, as when a record that
1009+
was read is given a new `record_name` but keeps the original's
1010+
`file_name`.
10091011
10101012
Parameters
10111013
----------
@@ -1018,14 +1020,22 @@ def _warn_about_replacing_other_files(self, write_dir):
10181020
10191021
"""
10201022
record_name = self.record_name or ""
1021-
others = [
1023+
existing = [
10221024
file_name
10231025
for file_name in dict.fromkeys(self.file_name)
10241026
if file_name
10251027
and file_name != "~"
1026-
and not file_name.startswith(record_name)
10271028
and os.path.exists(os.path.join(write_dir, file_name))
10281029
]
1030+
if not existing:
1031+
return
1032+
try:
1033+
header = rdheader(os.path.join(write_dir, record_name))
1034+
own = set(getattr(header, "file_name", None) or [])
1035+
except Exception:
1036+
# No readable header of this record, so no file is its own yet
1037+
own = set()
1038+
others = [file_name for file_name in existing if file_name not in own]
10291039
if others:
10301040
warnings.warn(
10311041
f"Writing record {record_name!r} replaces existing signal files "

‎tests/test_record.py‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ def test_wrsamp_physical_only_record_second_write(self, temp_path):
636636
np.testing.assert_array_equal(record_write.d_signal, expected_d_signal)
637637
assert record.d_signal is None
638638

639-
def test_wrsamp_expanded_physical_only_record(self, temp_path):
639+
def test_wrsamp_expanded_physical_only_record(self, tmp_path):
640+
# Its own directory: the record shares its signal file name with others
640641
record = wfdb.rdrecord("sample-data/test01_00s_frame", smooth_frames=False)
641642
expected_e_d_signal = wfdb.rdrecord(
642643
"sample-data/test01_00s_frame",
@@ -646,9 +647,9 @@ def test_wrsamp_expanded_physical_only_record(self, temp_path):
646647

647648
assert record.e_d_signal is None
648649

649-
record.wrsamp(write_dir=temp_path, expanded=True)
650+
record.wrsamp(write_dir=str(tmp_path), expanded=True)
650651
record_write = wfdb.rdrecord(
651-
os.path.join(temp_path, "test01_00s_frame"),
652+
os.path.join(tmp_path, "test01_00s_frame"),
652653
smooth_frames=False,
653654
physical=False,
654655
)
@@ -765,12 +766,13 @@ def test_3b(self, source):
765766

766767
# -------------- 4. Skew and multiple samples/frame -------------- #
767768

768-
def test_4a(self, temp_path):
769+
def test_4a(self, tmp_path):
769770
"""Format 16, multi-samples per frame, skew, digital.
770771
771772
Target file created with:
772773
rdsamp -r sample-data/test01_00s_skewframe | cut -f 2- > record-4a
773774
"""
775+
# Its own directory: the record shares its signal file name with others
774776
record = wfdb.rdrecord("sample-data/test01_00s_skewframe", physical=False)
775777
sig = record.d_signal
776778
# The WFDB library rdsamp does not return the final N samples for all
@@ -788,10 +790,10 @@ def test_4a(self, temp_path):
788790
smooth_frames=False,
789791
ignore_skew=True,
790792
)
791-
record_no_skew.wrsamp(write_dir=temp_path, expanded=True)
793+
record_no_skew.wrsamp(write_dir=str(tmp_path), expanded=True)
792794
# Read the written record
793795
record_write = wfdb.rdrecord(
794-
os.path.join(temp_path, "test01_00s_skewframe"),
796+
os.path.join(tmp_path, "test01_00s_skewframe"),
795797
physical=False,
796798
)
797799

@@ -2127,6 +2129,33 @@ def test_no_warning(self, tmp_path, recwarn, change):
21272129

21282130
assert not [w for w in recwarn if "replaces existing" in str(w.message)]
21292131

2132+
def test_renamed_record_whose_name_is_a_prefix_warns(self, tmp_path):
2133+
"""A file named like the new record name can still be another's."""
2134+
record = wfdb.rdrecord("sample-data/100", physical=False, sampto=100)
2135+
record.record_name = "100_extra"
2136+
record.file_name = ["100_extra.dat"] * record.n_sig
2137+
record.wrsamp(write_dir=str(tmp_path))
2138+
record.record_name = "100"
2139+
2140+
with pytest.warns(
2141+
UserWarning, match=r"replaces existing signal files 100_extra"
2142+
):
2143+
record.wrsamp(write_dir=str(tmp_path))
2144+
2145+
def test_own_header_listing_other_files_warns(self, tmp_path):
2146+
"""Files that this record's existing header does not list are not its."""
2147+
self.read_100(tmp_path)
2148+
record = wfdb.rdrecord("sample-data/100", physical=False, sampto=100)
2149+
record.record_name = "other"
2150+
record.file_name = ["other.dat"] * record.n_sig
2151+
record.wrsamp(write_dir=str(tmp_path))
2152+
record.file_name = ["100.dat"] * record.n_sig
2153+
2154+
with pytest.warns(
2155+
UserWarning, match=r"replaces existing signal files 100\.dat"
2156+
):
2157+
record.wrsamp(write_dir=str(tmp_path))
2158+
21302159
def test_signal_files_named_with_a_suffix_do_not_warn(self, tmp_path, recwarn):
21312160
"""Signal files whose names start with the record name are its own."""
21322161
record = wfdb.rdrecord("sample-data/100", physical=False, sampto=100)

0 commit comments

Comments
 (0)