Skip to content

Commit 17b9349

Browse files
committed
Add write_dir argument to csv_to_wfdb(). Ref #490.
1 parent 13df748 commit 17b9349

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

wfdb/io/convert/csv.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def csv_to_wfdb(
3333
header=True,
3434
delimiter=",",
3535
verbose=False,
36+
write_dir="",
3637
):
3738
"""
3839
Read a WFDB header file and return either a `Record` object with the
@@ -235,6 +236,10 @@ def csv_to_wfdb(
235236
verbose : bool, optional
236237
Whether to print all the information read about the file (True) or
237238
not (False).
239+
write_dir : str, optional
240+
The directory where the output files will be saved. If write_dir is not
241+
provided, the output files will be saved in the same directory as the
242+
input file.
238243
239244
Returns
240245
-------
@@ -291,6 +296,7 @@ def csv_to_wfdb(
291296
df_CSV = pd.read_csv(file_name, delimiter=delimiter, header=None)
292297
if verbose:
293298
print("Successfully read CSV")
299+
294300
# Extract the entire signal from the dataframe
295301
p_signal = df_CSV.values
296302
# The dataframe should be in (`sig_len`, `n_sig`) dimensions
@@ -300,6 +306,7 @@ def csv_to_wfdb(
300306
n_sig = p_signal.shape[1]
301307
if verbose:
302308
print("Number of signals: {}".format(n_sig))
309+
303310
# Check if signal names are valid and set defaults
304311
if not sig_name:
305312
if header:
@@ -318,15 +325,12 @@ def csv_to_wfdb(
318325
if verbose:
319326
print("Signal names: {}".format(sig_name))
320327

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", "")
328+
record_name = os.path.splitext(os.path.basename(file_name))[0]
325329
if verbose:
326-
print("Output header: {}.hea".format(record_name))
330+
print("Record name: {}.hea".format(record_name))
327331

328332
# Replace the CSV file tag with DAT
329-
dat_file_name = file_name.replace(".csv", ".dat")
333+
dat_file_name = record_name + ".dat"
330334
dat_file_name = [dat_file_name] * n_sig
331335
if verbose:
332336
print("Output record: {}".format(dat_file_name[0]))
@@ -450,7 +454,6 @@ def csv_to_wfdb(
450454
if verbose:
451455
print("Record generated successfully")
452456
return record
453-
454457
else:
455458
# Write the information to a record and header file
456459
wrsamp(
@@ -465,6 +468,7 @@ def csv_to_wfdb(
465468
comments=comments,
466469
base_time=base_time,
467470
base_date=base_date,
471+
write_dir=write_dir
468472
)
469473
if verbose:
470474
print("File generated successfully")

0 commit comments

Comments
 (0)