Skip to content

Commit

Permalink
Record.set_default: use multiple signal files if needed.
Browse files Browse the repository at this point in the history
It may be necessary to use multiple signal files in various cases:

- signals are to be written in different formats (e.g. 8-bit signals
  stored in format 80 while 12-bit signals are stored in format 212)

- multi-frequency signals are to be written in compressed format (FLAC
  supports only one frequency per signal file)

- more than 8 signals are to be written in compressed format (FLAC
  supports a maximum of 8 channels)

In each of these cases, if the caller didn't specify signal file names
explicitly, generate signal file files by adding a numeric suffix
after the record name.
  • Loading branch information
Benjamin Moody committed Sep 6, 2022
1 parent de1461d commit d1d26ba
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion wfdb/io/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,35 @@ def get_write_fields(self):

return rec_write_fields, sig_write_fields

def _auto_signal_file_names(self):
fmt = self.fmt or [None] * self.n_sig
spf = self.samps_per_frame or [None] * self.n_sig
num_groups = 0
group_number = []
prev_fmt = prev_spf = None
channels_in_group = 0

for ch_fmt, ch_spf in zip(fmt, spf):
if ch_fmt != prev_fmt:
num_groups += 1
channels_in_group = 0
elif ch_fmt in ("508", "516", "524"):
if channels_in_group >= 8 or ch_spf != prev_spf:
num_groups += 1
channels_in_group = 0
group_number.append(num_groups)
prev_fmt = ch_fmt
prev_spf = ch_spf

if num_groups < 2:
return [self.record_name + ".dat"] * self.n_sig
else:
digits = len(str(group_number[-1]))
return [
self.record_name + "_" + str(g).rjust(digits, "0") + ".dat"
for g in group_number
]

def set_default(self, field):
"""
Set the object's attribute to its default value if it is missing
Expand Down Expand Up @@ -394,7 +423,7 @@ def set_default(self, field):

# Specific dynamic case
if field == "file_name" and self.file_name is None:
self.file_name = self.n_sig * [self.record_name + ".dat"]
self.file_name = self._auto_signal_file_names()
return

item = getattr(self, field)
Expand Down

0 comments on commit d1d26ba

Please sign in to comment.