Skip to content

Commit

Permalink
Add write_dir argument to csv_to_wfdb(). Ref #490.
Browse files Browse the repository at this point in the history
  • Loading branch information
tompollard committed Jul 9, 2024
1 parent 13df748 commit f874b1c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions wfdb/io/convert/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def csv_to_wfdb(
header=True,
delimiter=",",
verbose=False,
write_dir="",
):
"""
Read a WFDB header file and return either a `Record` object with the
Expand Down Expand Up @@ -235,6 +236,10 @@ def csv_to_wfdb(
verbose : bool, optional
Whether to print all the information read about the file (True) or
not (False).
write_dir : str, optional
The directory where the output files will be saved. If write_dir is not
provided, the output files will be saved in the same directory as the
input file.
Returns
-------
Expand Down Expand Up @@ -291,6 +296,7 @@ def csv_to_wfdb(
df_CSV = pd.read_csv(file_name, delimiter=delimiter, header=None)
if verbose:
print("Successfully read CSV")

# Extract the entire signal from the dataframe
p_signal = df_CSV.values
# The dataframe should be in (`sig_len`, `n_sig`) dimensions
Expand All @@ -300,6 +306,7 @@ def csv_to_wfdb(
n_sig = p_signal.shape[1]
if verbose:
print("Number of signals: {}".format(n_sig))

# Check if signal names are valid and set defaults
if not sig_name:
if header:
Expand All @@ -318,15 +325,12 @@ def csv_to_wfdb(
if verbose:
print("Signal names: {}".format(sig_name))

# Set the output header file name to be the same, remove path
if os.sep in file_name:
file_name = file_name.split(os.sep)[-1]
record_name = file_name.replace(".csv", "")
record_name = os.path.splitext(os.path.basename(file_name))[0]
if verbose:
print("Output header: {}.hea".format(record_name))
print("Record name: {}.hea".format(record_name))

# Replace the CSV file tag with DAT
dat_file_name = file_name.replace(".csv", ".dat")
dat_file_name = record_name + ".dat"
dat_file_name = [dat_file_name] * n_sig
if verbose:
print("Output record: {}".format(dat_file_name[0]))
Expand Down Expand Up @@ -450,7 +454,6 @@ def csv_to_wfdb(
if verbose:
print("Record generated successfully")
return record

else:
# Write the information to a record and header file
wrsamp(
Expand All @@ -465,6 +468,7 @@ def csv_to_wfdb(
comments=comments,
base_time=base_time,
base_date=base_date,
write_dir=write_dir,
)
if verbose:
print("File generated successfully")
Expand Down

0 comments on commit f874b1c

Please sign in to comment.