Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions wfdb/io/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ def get_write_subset(self, spec_type):

return write_fields

def preprocess_header(self, field):
"""
Get and process the fields as needed before writing
"""
string_field = str(getattr(self, field))

# Certain fields need extra processing
if field == "fs" and isinstance(self.fs, float):
if round(self.fs, 8) == float(int(self.fs)):
string_field = str(int(self.fs))
elif field == "base_time" and "." in string_field:
string_field = string_field.rstrip("0")
elif field == "base_date":
string_field = "/".join(
(string_field[8:], string_field[5:7], string_field[:4])
)

return string_field


class HeaderMixin(BaseHeaderMixin):
"""
Expand Down Expand Up @@ -534,19 +553,7 @@ def wr_header_file(self, rec_write_fields, sig_write_fields, write_dir):
for field in RECORD_SPECS.index:
# If the field is being used, add it with its delimiter
if field in rec_write_fields:
string_field = str(getattr(self, field))

# Certain fields need extra processing
if field == "fs" and isinstance(self.fs, float):
if round(self.fs, 8) == float(int(self.fs)):
string_field = str(int(self.fs))
elif field == "base_time" and "." in string_field:
string_field = string_field.rstrip("0")
elif field == "base_date":
string_field = "/".join(
(string_field[8:], string_field[5:7], string_field[:4])
)

string_field = self.preprocess_header(field)
record_line += (
RECORD_SPECS.loc[field, "delimiter"] + string_field
)
Expand Down Expand Up @@ -756,8 +763,9 @@ def wr_header_file(self, write_fields, write_dir):
for field in RECORD_SPECS.index:
# If the field is being used, add it with its delimiter
if field in write_fields:
record_line += RECORD_SPECS.loc[field, "delimiter"] + str(
getattr(self, field)
string_field = self.preprocess_header(field)
record_line += (
RECORD_SPECS.loc[field, "delimiter"] + string_field
)

header_lines = [record_line]
Expand Down
Loading