11import math
2+ import io
23import os
34import posixpath
45import sys
@@ -120,7 +121,7 @@ class SignalMixin(object):
120121
121122 """
122123
123- def wr_dats (self , expanded , write_dir ):
124+ def wr_dats (self , expanded , write_dir , wfdb_archive = None ):
124125 """
125126 Write all dat files associated with a record
126127 expanded=True to use e_d_signal instead of d_signal.
@@ -132,6 +133,8 @@ def wr_dats(self, expanded, write_dir):
132133 the `d_signal` attribute (False).
133134 write_dir : str
134135 The directory to write the output file to.
136+ wfdb_archive : WFDBArchive, optional
137+ If set, writes to a .wfdb archive instead of the filesystem.
135138
136139 Returns
137140 -------
@@ -160,7 +163,8 @@ def wr_dats(self, expanded, write_dir):
160163 self .check_sig_cohesion ([], expanded )
161164
162165 # Write each of the specified dat files
163- self .wr_dat_files (expanded = expanded , write_dir = write_dir )
166+ self .wr_dat_files (expanded = expanded , write_dir = write_dir ,
167+ wfdb_archive = wfdb_archive )
164168
165169 def check_sig_cohesion (self , write_fields , expanded ):
166170 """
@@ -958,7 +962,7 @@ def calc_checksum(self, expanded=False):
958962 cs = [int (c ) for c in cs ]
959963 return cs
960964
961- def wr_dat_files (self , expanded = False , write_dir = "" ):
965+ def wr_dat_files (self , expanded = False , write_dir = "" , wfdb_archive = None ):
962966 """
963967 Write each of the specified dat files.
964968
@@ -969,6 +973,8 @@ def wr_dat_files(self, expanded=False, write_dir=""):
969973 the `d_signal` attribute (False).
970974 write_dir : str, optional
971975 The directory to write the output file to.
976+ wfdb_archive : WFDBArchive, optional
977+ If set, writes to a .wfdb archive instead of the local filesystem.
972978
973979 Returns
974980 -------
@@ -1003,6 +1009,7 @@ def wr_dat_files(self, expanded=False, write_dir=""):
10031009 [self .e_d_signal [ch ] for ch in dat_channels [fn ]],
10041010 [self .samps_per_frame [ch ] for ch in dat_channels [fn ]],
10051011 write_dir = write_dir ,
1012+ wfdb_archive = wfdb_archive ,
10061013 )
10071014 else :
10081015 dsig = self .d_signal
@@ -1013,6 +1020,7 @@ def wr_dat_files(self, expanded=False, write_dir=""):
10131020 dsig [:, dat_channels [fn ][0 ] : dat_channels [fn ][- 1 ] + 1 ],
10141021 dat_offsets [fn ],
10151022 write_dir = write_dir ,
1023+ wfdb_archive = wfdb_archive ,
10161024 )
10171025
10181026 def smooth_frames (self , sigtype = "physical" ):
@@ -2322,6 +2330,7 @@ def wr_dat_file(
23222330 e_d_signal = None ,
23232331 samps_per_frame = None ,
23242332 write_dir = "" ,
2333+ wfdb_archive = None ,
23252334):
23262335 """
23272336 Write a dat file. All bytes are written one at a time to avoid
@@ -2519,16 +2528,30 @@ def wr_dat_file(
25192528 else :
25202529 raise ValueError (f"unknown format ({ fmt } )" )
25212530
2522- sf = soundfile .SoundFile (
2523- file_path ,
2524- mode = "w" ,
2525- samplerate = 96000 ,
2526- channels = n_sig ,
2527- subtype = subtype ,
2528- format = "FLAC" ,
2529- )
2530- with sf :
2531- sf .write (d_signal )
2531+ if wfdb_archive :
2532+ with io .BytesIO () as f :
2533+ with soundfile .SoundFile (
2534+ f ,
2535+ mode = "w" ,
2536+ samplerate = 96000 ,
2537+ channels = n_sig ,
2538+ subtype = subtype ,
2539+ format = "FLAC" , # required for file-like
2540+ ) as sf :
2541+ sf .write (d_signal )
2542+ wfdb_archive .write (os .path .basename (file_name ), f .getvalue ())
2543+ return
2544+ else :
2545+ sf = soundfile .SoundFile (
2546+ file_path ,
2547+ mode = "w" ,
2548+ samplerate = 96000 ,
2549+ channels = n_sig ,
2550+ subtype = subtype ,
2551+ format = "FLAC" ,
2552+ )
2553+ with sf :
2554+ sf .write (d_signal )
25322555 return
25332556
25342557 else :
@@ -2549,8 +2572,13 @@ def wr_dat_file(
25492572 b_write = np .append (np .zeros (byte_offset , dtype = "uint8" ), b_write )
25502573
25512574 # Write the bytes to the file
2552- with open (file_path , "wb" ) as f :
2553- b_write .tofile (f )
2575+ if wfdb_archive :
2576+ with io .BytesIO () as f :
2577+ b_write .tofile (f )
2578+ wfdb_archive .write (os .path .basename (file_name ), f .getvalue ())
2579+ else :
2580+ with open (file_path , "wb" ) as f :
2581+ b_write .tofile (f )
25542582
25552583
25562584def describe_list_indices (full_list ):
0 commit comments