66import re
77import shutil
88import tempfile
9+ import warnings
910
1011import numpy as np
1112import 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
0 commit comments