Skip to content

Commit 998888c

Browse files
committed
Warn when a renamed record would replace another record's signal files
Record.wrsamp names the header after record_name but the signal files by file_name, so a record read and given a new record_name, then written next to the original, silently replaced the original's signal files. Warn when writing would replace an existing signal file whose name does not start with record_name, and explain this, with an example, in the documentation of wrsamp and file_name, which said only "the name of the file used for analysis". Fixes #153.
1 parent f59c445 commit 998888c

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**Warn when a renamed record would replace another record's signal files**
2+
``Record.wrsamp`` names the header after ``record_name`` and the signal
3+
files by ``file_name``, so writing a record that was read under a new
4+
``record_name``, next to the original, replaced the original's signal
5+
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
7+
documentation of ``wrsamp`` and ``file_name`` now explains this, with an
8+
example of saving a copy under a new name.

‎src/wfdb/io/record.py‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import shutil
88
import tempfile
9+
import warnings
910

1011
import numpy as np
1112
import pandas as pd
@@ -724,8 +725,10 @@ class Record(BaseRecord, _header.HeaderMixin, _signal.SignalMixin):
724725
base_datetime : datetime.datetime, optional
725726
The date and time at the beginning of the record, equivalent to
726727
`datetime.combine(base_date, base_time)`.
727-
file_name : str, optional
728-
The name of the file used for analysis.
728+
file_name : list, optional
729+
The name of the signal file of each signal. The signals are written
730+
to these files, which are not renamed when `record_name` is changed:
731+
to save a copy of a record under a new name, set both.
729732
fmt : list, optional
730733
A list of strings giving the WFDB format of each file used to store each
731734
channel. Accepted formats are: '80','212','16','24', and '32'. There are
@@ -897,6 +900,13 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
897900
`skew` attribute is not changed, and a value assigned to it after
898901
reading is written to the header.
899902
903+
The header file is named after `record_name`, and the signal files are
904+
named by `file_name`. A record that was read keeps the signal file
905+
names of the original, so to save a copy under a new name, change
906+
both, or the copy's signals are written to the original's files. A
907+
warning is given when writing would replace an existing signal file
908+
whose name does not start with `record_name`.
909+
900910
Parameters
901911
----------
902912
expanded : bool, optional
@@ -915,6 +925,15 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
915925
-------
916926
N/A
917927
928+
Examples
929+
--------
930+
Save a copy of record 100 as record '100_copy':
931+
932+
>>> record = wfdb.rdrecord('sample-data/100', physical=False)
933+
>>> record.record_name = '100_copy'
934+
>>> record.file_name = ['100_copy.dat'] * record.n_sig
935+
>>> record.wrsamp()
936+
918937
"""
919938
converted = None
920939
if not expanded and self.d_signal is None and self.p_signal is not None:
@@ -961,6 +980,9 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
961980
checksums[ch] = old_val
962981
self.checksum = checksums
963982

983+
if wfdb_archive is None and has_signal_files and self.file_name:
984+
self._warn_about_replacing_other_files(write_dir)
985+
964986
with archive_for_writing(wfdb_archive, write_dir) as archive:
965987
# Perform field validity and cohesion checks, and write the
966988
# header file.
@@ -978,6 +1000,41 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
9781000
if converted:
9791001
setattr(self, converted, None)
9801002

1003+
def _warn_about_replacing_other_files(self, write_dir):
1004+
"""Warn if writing would replace signal files of another record.
1005+
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`.
1009+
1010+
Parameters
1011+
----------
1012+
write_dir : str
1013+
The directory in which the files are to be written.
1014+
1015+
Returns
1016+
-------
1017+
N/A
1018+
1019+
"""
1020+
record_name = self.record_name or ""
1021+
others = [
1022+
file_name
1023+
for file_name in dict.fromkeys(self.file_name)
1024+
if file_name
1025+
and file_name != "~"
1026+
and not file_name.startswith(record_name)
1027+
and os.path.exists(os.path.join(write_dir, file_name))
1028+
]
1029+
if others:
1030+
warnings.warn(
1031+
f"Writing record {record_name!r} replaces existing signal files "
1032+
f"{', '.join(others)}, which are named by `file_name`, not "
1033+
"`record_name`. To write a copy of a record under a new name, "
1034+
"set `file_name` too.",
1035+
stacklevel=3,
1036+
)
1037+
9811038
def _arrange_fields(self, channels, sampfrom, smooth_frames):
9821039
"""Arrange/edit object fields to reflect user channel and/or signal range input.
9831040

‎tests/test_record.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,3 +2086,54 @@ def test_multi_segment(self, tmp_path):
20862086
np.testing.assert_array_equal(
20872087
written.segments[0].d_signal, record.segments[0].d_signal
20882088
)
2089+
2090+
2091+
class TestWarnAboutReplacingOtherFiles:
2092+
"""Writing a renamed copy next to the original warns (#153)."""
2093+
2094+
@staticmethod
2095+
def read_100(tmp_path):
2096+
record = wfdb.rdrecord("sample-data/100", physical=False, sampto=100)
2097+
record.wrsamp(write_dir=str(tmp_path))
2098+
return record
2099+
2100+
def test_renamed_copy_with_the_original_file_names_warns(self, tmp_path):
2101+
record = self.read_100(tmp_path)
2102+
record.record_name = "100_cloned"
2103+
2104+
with pytest.warns(
2105+
UserWarning, match=r"replaces existing signal files 100\.dat"
2106+
):
2107+
record.wrsamp(write_dir=str(tmp_path))
2108+
2109+
@pytest.mark.parametrize(
2110+
"change", ["none", "rename both", "rename in another folder", "archive"]
2111+
)
2112+
def test_no_warning(self, tmp_path, recwarn, change):
2113+
record = self.read_100(tmp_path)
2114+
write_dir = tmp_path
2115+
archive = None
2116+
if change != "none":
2117+
record.record_name = "100_copy"
2118+
if change == "rename both":
2119+
record.file_name = ["100_copy.dat"] * record.n_sig
2120+
elif change == "rename in another folder":
2121+
write_dir = tmp_path / "other"
2122+
write_dir.mkdir()
2123+
elif change == "archive":
2124+
archive = "100_copy.wfdb"
2125+
2126+
record.wrsamp(write_dir=str(write_dir), wfdb_archive=archive)
2127+
2128+
assert not [w for w in recwarn if "replaces existing" in str(w.message)]
2129+
2130+
def test_signal_files_named_with_a_suffix_do_not_warn(self, tmp_path, recwarn):
2131+
"""Signal files whose names start with the record name are its own."""
2132+
record = wfdb.rdrecord("sample-data/100", physical=False, sampto=100)
2133+
record.record_name = "rec_0001"
2134+
record.file_name = ["rec_0001e.dat"] * record.n_sig
2135+
record.wrsamp(write_dir=str(tmp_path))
2136+
2137+
record.wrsamp(write_dir=str(tmp_path))
2138+
2139+
assert not [w for w in recwarn if "replaces existing" in str(w.message)]

0 commit comments

Comments
 (0)