Skip to content

Commit e355422

Browse files
committed
add header preprocessing to function
1 parent 50b02c2 commit e355422

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

wfdb/io/_header.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ def get_write_subset(self, spec_type):
236236

237237
return write_fields
238238

239+
def preprocess_header(self, field):
240+
"""
241+
Get and process the fields as needed before writing
242+
"""
243+
string_field = str(getattr(self, field))
244+
245+
# Certain fields need extra processing
246+
if field == "fs" and isinstance(self.fs, float):
247+
if round(self.fs, 8) == float(int(self.fs)):
248+
string_field = str(int(self.fs))
249+
elif field == "base_time" and "." in string_field:
250+
string_field = string_field.rstrip("0")
251+
elif field == "base_date":
252+
string_field = "/".join(
253+
(string_field[8:], string_field[5:7], string_field[:4])
254+
)
255+
256+
return string_field
257+
239258

240259
class HeaderMixin(BaseHeaderMixin):
241260
"""
@@ -534,19 +553,7 @@ def wr_header_file(self, rec_write_fields, sig_write_fields, write_dir):
534553
for field in RECORD_SPECS.index:
535554
# If the field is being used, add it with its delimiter
536555
if field in rec_write_fields:
537-
string_field = str(getattr(self, field))
538-
539-
# Certain fields need extra processing
540-
if field == "fs" and isinstance(self.fs, float):
541-
if round(self.fs, 8) == float(int(self.fs)):
542-
string_field = str(int(self.fs))
543-
elif field == "base_time" and "." in string_field:
544-
string_field = string_field.rstrip("0")
545-
elif field == "base_date":
546-
string_field = "/".join(
547-
(string_field[8:], string_field[5:7], string_field[:4])
548-
)
549-
556+
string_field = self.preprocess_header(field)
550557
record_line += (
551558
RECORD_SPECS.loc[field, "delimiter"] + string_field
552559
)
@@ -756,25 +763,10 @@ def wr_header_file(self, write_fields, write_dir):
756763
for field in RECORD_SPECS.index:
757764
# If the field is being used, add it with its delimiter
758765
if field in write_fields:
759-
string_field = str(getattr(self, field))
760-
761-
# Certain fields need extra processing
762-
if field == "fs" and isinstance(self.fs, float):
763-
if round(self.fs, 8) == float(int(self.fs)):
764-
string_field = str(int(self.fs))
765-
elif field == "base_time" and "." in string_field:
766-
string_field = string_field.rstrip("0")
767-
elif field == "base_date":
768-
string_field = "/".join(
769-
(string_field[8:], string_field[5:7], string_field[:4])
770-
)
771-
766+
string_field = self.preprocess_header(field)
772767
record_line += (
773768
RECORD_SPECS.loc[field, "delimiter"] + string_field
774769
)
775-
# The 'base_counter' field needs to be closed with ')'
776-
if field == "base_counter":
777-
record_line += ")"
778770

779771
header_lines = [record_line]
780772

0 commit comments

Comments
 (0)