10
10
11
11
from wfdb .io import _header , _signal , _url , download , header , util
12
12
from wfdb .io ._coreio import CLOUD_PROTOCOLS
13
- from wfdb .io .archive import get_archive
13
+ from wfdb .io .archive import get_archive , WFDBArchive
14
14
15
15
# -------------- WFDB Signal Calibration and Classification ---------- #
16
16
@@ -911,6 +911,10 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
911
911
of the uniform signal (d_signal).
912
912
write_dir : str, optional
913
913
The directory in which to write the files.
914
+ wfdb_archive : str or WFDBArchive, optional
915
+ If provided, writes the record to a .wfdb archive. Can be either:
916
+ - A string path to the archive file (e.g., 'record.wfdb')
917
+ - A WFDBArchive object for more advanced usage
914
918
915
919
Returns
916
920
-------
@@ -927,6 +931,15 @@ def wrsamp(self, expanded=False, write_dir="", wfdb_archive=None):
927
931
checksums [ch ] = old_val
928
932
self .checksum = checksums
929
933
934
+ # Handle wfdb_archive parameter
935
+ if wfdb_archive :
936
+ if isinstance (wfdb_archive , str ):
937
+ # If a string path is provided, create a WFDBArchive object
938
+ from wfdb .io .archive import get_archive
939
+ wfdb_archive = get_archive (wfdb_archive , mode = "w" )
940
+ elif not isinstance (wfdb_archive , WFDBArchive ):
941
+ raise TypeError ("wfdb_archive must be either a string path or WFDBArchive object" )
942
+
930
943
# Perform field validity and cohesion checks, and write the
931
944
# header file.
932
945
self .wrheader (
@@ -1975,7 +1988,6 @@ def rdrecord(
1975
1988
Option used to stream data from Physionet. The Physionet
1976
1989
database directory from which to find the required record files.
1977
1990
eg. For record '100' in 'http://physionet.org/content/mitdb'
1978
- pn_dir='mitdb'.
1979
1991
m2s : bool, optional
1980
1992
Used when reading multi-segment records. Specifies whether to
1981
1993
directly return a WFDB MultiRecord object (False), or to convert
@@ -2033,15 +2045,19 @@ def rdrecord(
2033
2045
--------
2034
2046
>>> record = wfdb.rdrecord('sample-data/test01_00s', sampfrom=800,
2035
2047
channels=[1, 3])
2036
-
2037
2048
"""
2038
2049
wfdb_archive = None
2039
2050
is_wfdb_archive = record_name .endswith (".wfdb" )
2040
2051
2041
2052
if is_wfdb_archive :
2042
2053
record_base = record_name [:- 5 ] # remove ".wfdb"
2043
2054
wfdb_archive = get_archive (record_base )
2044
- hea_file = os .path .basename (record_base ) + ".hea"
2055
+
2056
+ # Find any .hea file in the archive
2057
+ hea_files = [f for f in wfdb_archive .zipfile .namelist () if f .endswith ('.hea' )]
2058
+ if not hea_files :
2059
+ raise FileNotFoundError (f"No header file found in archive { record_name } " )
2060
+ hea_file = hea_files [0 ] # Use the first header file found
2045
2061
2046
2062
import tempfile
2047
2063
@@ -2954,6 +2970,10 @@ def wrsamp(
2954
2970
setting both `base_date` and `base_time`.
2955
2971
write_dir : str, optional
2956
2972
The directory in which to write the files.
2973
+ wfdb_archive : str or WFDBArchive, optional
2974
+ If provided, writes the record to a .wfdb archive. Can be either:
2975
+ - A string path to the archive file (e.g., 'record.wfdb')
2976
+ - A WFDBArchive object for more advanced usage
2957
2977
2958
2978
Returns
2959
2979
-------
@@ -2978,6 +2998,10 @@ def wrsamp(
2978
2998
>>> # Write a local WFDB record (manually inserting fields)
2979
2999
>>> wfdb.wrsamp('ecgrecord', fs = 250, units=['mV', 'mV'],
2980
3000
sig_name=['I', 'II'], p_signal=signals, fmt=['16', '16'])
3001
+ >>> # Write to a .wfdb archive using a string path
3002
+ >>> wfdb.wrsamp('ecgrecord', fs = 250, units=['mV', 'mV'],
3003
+ sig_name=['I', 'II'], p_signal=signals, fmt=['16', '16'],
3004
+ wfdb_archive='ecgrecord.wfdb')
2981
3005
2982
3006
"""
2983
3007
# Check for valid record name
@@ -3082,10 +3106,14 @@ def wrsamp(
3082
3106
else :
3083
3107
expanded = False
3084
3108
3109
+ # Handle wfdb_archive parameter
3085
3110
if wfdb_archive :
3086
- wfdb_archive = get_archive (
3087
- os .path .join (write_dir , record_name ), mode = "w"
3088
- )
3111
+ if isinstance (wfdb_archive , str ):
3112
+ # If a string path is provided, create a WFDBArchive object
3113
+ from wfdb .io .archive import get_archive
3114
+ wfdb_archive = get_archive (wfdb_archive , mode = "w" )
3115
+ elif not isinstance (wfdb_archive , WFDBArchive ):
3116
+ raise TypeError ("wfdb_archive must be either a string path or WFDBArchive object" )
3089
3117
3090
3118
# Write the record files - header and associated dat
3091
3119
record .wrsamp (
0 commit comments