header
Reading and validating the header of an EDF or EDF+ recording.
The header is a 256-byte fixed part followed by 256 bytes per signal. The
functions here parse both parts, validate them, and turn them into a
:class:~yousleep_common.models.RecordingHeader. They read the header only,
never the signal data.
EDF_FIXED_HEADER_BYTES = 256
module-attribute
¶
The size of the fixed part of an EDF header, in bytes.
EDF_FIXED_HEADER_SPEC = ((8, 'version', lambda s: s.strip()), (80, 'patient_id', lambda s: s.strip()), (80, 'recording_id', lambda s: s.strip()), (8, 'startdate', lambda s: parse_edf_start_date(s.strip())), (8, 'starttime', lambda s: parse_edf_start_time(s.strip())), (8, 'header_n_bytes', int), (44, 'reserved', lambda s: s.strip()), (8, 'n_records', int), (8, 'record_duration', float), (4, 'n_signals', int))
module-attribute
¶
The fixed header's fields: their size in bytes, their name, and how the text is read.
EDF_VARIABLE_HEADER_SIZE_PER_SIGNAL = 256
module-attribute
¶
The size of the header part each signal adds, in bytes.
EDF_VARIABLE_HEADER_SPEC = ((16, 'label', lambda s: s.strip()), (80, 'transducer', lambda s: s.strip()), (8, 'dimension', lambda s: s.strip()), (8, 'pmin', float), (8, 'pmax', float), (8, 'dmin', int), (8, 'dmax', int), (80, 'prefilter', lambda s: s.strip()), (8, 'n_samples', int), (32, 'reserved', lambda s: s.strip()))
module-attribute
¶
Each signal's header fields: their size in bytes per signal, their name, and how the text is read.
Readable
¶
Bases: Protocol
Anything a header can be read from: a file, a stream, a response body.
read(size)
¶
Read up to size bytes.
get_validated_edf_headers(fixed_header, signal_headers)
¶
Validate a parsed header and return the recording it describes.
The fixed header is refused if its version is not 0, if it marks an
interrupted recording (EDF+D), or if its header size does not match
its number of signals. A signal is refused if its number of samples per
record is not a positive multiple of the record duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixed_header
|
dict[str, Any]
|
The fixed header, as :func: |
required |
signal_headers
|
list[dict[str, Any]]
|
The signal headers, as :func: |
required |
Returns:
| Type | Description |
|---|---|
RecordingHeader
|
The recording's length, start and channels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the header is refused. |
parse_edf_fixed_header(header_bytes)
¶
Parse the 256-byte fixed part of an EDF header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header_bytes
|
bytes
|
The first 256 bytes of an EDF file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The fields by name, as :data: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
parse_edf_signal_headers(header_bytes)
¶
Parse the per-signal part of an EDF header.
The part stores each field for every signal in turn: all labels, then all transducers, and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header_bytes
|
bytes
|
The 256 bytes per signal that follow the fixed header. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
One dictionary of fields per signal, as :data: |
list[dict[str, Any]]
|
reads them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
read_edf_raw_headers_from_readable(readable)
¶
Read and parse an EDF header from the start of a file or stream.
Reads the header only, leaving readable at the start of the data
records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readable
|
Readable
|
The file or stream, positioned at its first byte. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any], list[dict[str, Any]]]
|
The fixed header and the signal headers. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either part cannot be parsed. |
read_edf_recording_header_from_readable(readable)
¶
Read, parse and validate an EDF header from the start of a file or stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readable
|
Readable
|
The file or stream, positioned at its first byte. |
required |
Returns:
| Type | Description |
|---|---|
RecordingHeader
|
The recording's length, start and channels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the header cannot be parsed or is refused. |
validate_edf_recording(readable, full_file_size)
¶
Validate the start of an EDF recording against the size of the whole file.
The header must parse, must be valid, and must account for the whole file: its size plus two bytes per sample of every data record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readable
|
Readable
|
The start of the recording, at least its full header, positioned at its first byte. |
required |
full_file_size
|
int
|
The size of the whole file in bytes. |
required |
Returns:
| Type | Description |
|---|---|
RecordingHeader
|
The recording's length, start and channels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is too small, its header cannot be parsed or is refused, or its size does not match the header. |