@@ -33,6 +33,7 @@ def csv_to_wfdb(
33
33
header = True ,
34
34
delimiter = "," ,
35
35
verbose = False ,
36
+ output_dir = None ,
36
37
):
37
38
"""
38
39
Read a WFDB header file and return either a `Record` object with the
@@ -235,6 +236,9 @@ def csv_to_wfdb(
235
236
verbose : bool, optional
236
237
Whether to print all the information read about the file (True) or
237
238
not (False).
239
+ output_dir : str, optional
240
+ The directory where the output files will be saved. If not provided,
241
+ the output files will be saved in the same directory as the input file.
238
242
239
243
Returns
240
244
-------
@@ -291,6 +295,7 @@ def csv_to_wfdb(
291
295
df_CSV = pd .read_csv (file_name , delimiter = delimiter , header = None )
292
296
if verbose :
293
297
print ("Successfully read CSV" )
298
+
294
299
# Extract the entire signal from the dataframe
295
300
p_signal = df_CSV .values
296
301
# The dataframe should be in (`sig_len`, `n_sig`) dimensions
@@ -300,6 +305,7 @@ def csv_to_wfdb(
300
305
n_sig = p_signal .shape [1 ]
301
306
if verbose :
302
307
print ("Number of signals: {}" .format (n_sig ))
308
+
303
309
# Check if signal names are valid and set defaults
304
310
if not sig_name :
305
311
if header :
@@ -318,15 +324,23 @@ def csv_to_wfdb(
318
324
if verbose :
319
325
print ("Signal names: {}" .format (sig_name ))
320
326
321
- # Set the output header file name to be the same, remove path
322
- if os .sep in file_name :
323
- file_name = file_name .split (os .sep )[- 1 ]
324
- record_name = file_name .replace (".csv" , "" )
327
+ # Determine the output directory
328
+ if output_dir :
329
+ if not os .path .exists (output_dir ):
330
+ os .makedirs (output_dir )
331
+ output_base = os .path .join (
332
+ output_dir , os .path .basename (file_name ).replace (".csv" , "" )
333
+ )
334
+ else :
335
+ if os .sep in file_name :
336
+ file_name = file_name .split (os .sep )[- 1 ]
337
+ output_base = file_name .replace (".csv" , "" )
338
+
325
339
if verbose :
326
- print ("Output header : {}.hea " .format (record_name ))
340
+ print ("Output base : {}" .format (output_base ))
327
341
328
342
# Replace the CSV file tag with DAT
329
- dat_file_name = file_name . replace ( ".csv" , ".dat" )
343
+ dat_file_name = output_base + ".dat"
330
344
dat_file_name = [dat_file_name ] * n_sig
331
345
if verbose :
332
346
print ("Output record: {}" .format (dat_file_name [0 ]))
@@ -419,7 +433,7 @@ def csv_to_wfdb(
419
433
if record_only :
420
434
# Create the record from the input and generated values
421
435
record = Record (
422
- record_name = record_name ,
436
+ record_name = output_base ,
423
437
n_sig = n_sig ,
424
438
fs = fs ,
425
439
samps_per_frame = samps_per_frame ,
@@ -454,7 +468,7 @@ def csv_to_wfdb(
454
468
else :
455
469
# Write the information to a record and header file
456
470
wrsamp (
457
- record_name = record_name ,
471
+ record_name = output_base ,
458
472
fs = fs ,
459
473
units = units ,
460
474
sig_name = sig_name ,
0 commit comments