From 50b02c2a4e4e524996a366a7ea0a120fd3472686 Mon Sep 17 00:00:00 2001 From: Brian Gow Date: Mon, 23 Jun 2025 15:41:51 -0400 Subject: [PATCH 1/3] multiheader wr_header_file formatting fixes --- wfdb/io/_header.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/wfdb/io/_header.py b/wfdb/io/_header.py index 0d420521..a45da827 100644 --- a/wfdb/io/_header.py +++ b/wfdb/io/_header.py @@ -756,9 +756,25 @@ 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 = 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]) + ) + + record_line += ( + RECORD_SPECS.loc[field, "delimiter"] + string_field ) + # The 'base_counter' field needs to be closed with ')' + if field == "base_counter": + record_line += ")" header_lines = [record_line] From e355422b2199776e153717e6790a0a4bb293c770 Mon Sep 17 00:00:00 2001 From: Brian Gow Date: Mon, 13 Oct 2025 10:59:39 -0400 Subject: [PATCH 2/3] add header preprocessing to function --- wfdb/io/_header.py | 50 +++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/wfdb/io/_header.py b/wfdb/io/_header.py index a45da827..e2743d9e 100644 --- a/wfdb/io/_header.py +++ b/wfdb/io/_header.py @@ -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): """ @@ -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 ) @@ -756,25 +763,10 @@ 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: - 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 ) - # The 'base_counter' field needs to be closed with ')' - if field == "base_counter": - record_line += ")" header_lines = [record_line] From f8ea36d4cb7c56062f1f87b564973169e54860a7 Mon Sep 17 00:00:00 2001 From: Brian Gow Date: Tue, 28 Oct 2025 14:24:54 -0400 Subject: [PATCH 3/3] improve date formatting robustness --- wfdb/io/_header.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wfdb/io/_header.py b/wfdb/io/_header.py index e2743d9e..8e53ba14 100644 --- a/wfdb/io/_header.py +++ b/wfdb/io/_header.py @@ -1,4 +1,5 @@ import datetime +from dateutil import parser from typing import Any, Dict, List, Optional, Sequence, Tuple import numpy as np @@ -240,7 +241,8 @@ def preprocess_header(self, field): """ Get and process the fields as needed before writing """ - string_field = str(getattr(self, field)) + original_field = getattr(self, field) + string_field = str(original_field) # Certain fields need extra processing if field == "fs" and isinstance(self.fs, float): @@ -249,9 +251,7 @@ def preprocess_header(self, field): 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 = original_field.strftime("%d/%m/%Y") return string_field