edfio.Bdf#

class edfio.Bdf(signals, *, patient=None, recording=None, starttime=None, data_record_duration=None, annotations=None)[source]#

Python representation of a BDF file.

See Edf for information on the header fields and their types.

Note

BDF uses 24-bit integers (compared to 16-bit for EDF) for the digital values. The default for digital_range (and the supported depth) thus differs.

Attributes

annotations

All annotations contained in the Bdf, sorted chronologically.

bytes_in_header_record

Number of bytes in the header record.

data_record_duration

Duration of each data record in seconds.

duration

Recording duration in seconds.

labels

The labels of all signals contained in the Bdf.

local_patient_identification

Unparsed string representation of the legacy local patient identification.

local_recording_identification

Unparsed string representation of the legacy local recording identification.

num_data_records

Number of data records in the recording.

num_signals

Return the number of signals, excluding annotation signals for BDF+.

patient

Parsed object representation of the local patient identification.

recording

Parsed object representation of the local recording identification.

reserved

"BDF+C" for an BDF+C file, else "".

signals

Ordinary signals contained in the recording.

startdate

Recording startdate.

starttime

Recording starttime.

version

The BDF version, always �BIOSEMI.

Methods

__init__

add_annotations

Add annotations to the Bdf.

anonymize

Anonymize a recording.

append_signals

Append one or more signal(s) to the Bdf recording.

copy

Create a deep copy of the Bdf.

drop_annotations

Drop annotations with a given text.

drop_signals

Drop signals by index or label.

get_annotations

All annotations defined (starting) in a specified time region of the Bdf, sorted chronologically.

get_signal

Retrieve a single signal by its label.

set_annotations

Overwrite all annotations with new ones.

slice_between_annotations

Slice to the interval between two BDF+ annotations.

slice_between_seconds

Slice to the interval between two times.

to_bytes

Convert an Bdf to a bytes object.

update_data_record_duration

Update the data record duration.

write

Write an Bdf to a file or file-like object.

add_annotations(annotations)[source]#

Add annotations to the Bdf.

This removes all existing annotation signals and adds a new one containing the old and new annotations as the last signal in the file.

Parameters:
annotationsIterable[EdfAnnotation]

The annotations to add.

property annotations[source]#

All annotations contained in the Bdf, sorted chronologically.

Does not include timekeeping annotations.

anonymize(*, keep_age=False, keep_sex=False, keep_starttime=False)[source]#

Anonymize a recording.

By default, header fields are modified as follows:
  • local patient identification is set to X X X X

  • local recording identification is set to Startdate X X X X

  • startdate is set to 01.01.85

  • starttime is set to 00.00.00

For BDF+ files, subsecond starttimes specified via an annotations signal are removed.

Optionally, parts of the original information can be preserved by setting the corresponding parameters to True (see below).

Parameters:
keep_agebool, default: False

Whether to keep age information relative to the anonymized start date in the local patient identification. If True, the birthdate is set to January 1st of the year that preserves the age relative to 01.01.85. This implies that the local patient identification is set to X X 01-JAN-YYYY X, where YYYY is the age-preserving year.

keep_sexbool, default: False

Whether to keep the sex field in the local patient identification.

keep_starttimebool, default: False

Whether to keep the original start time.

append_signals(new_signals)[source]#

Append one or more signal(s) to the Bdf recording.

Every signal must be compatible with the current data_record_duration and all signal durations must match the overall recording duration. For recordings containing BDF+ annotation signals, the new signals are inserted after the last ordinary (i.e. non-annotation) signal.

Parameters:
new_signalsBdfSignal | Iterable[BdfSignal]

The signal(s) to add.

property bytes_in_header_record[source]#

Number of bytes in the header record.

copy()[source]#

Create a deep copy of the Bdf.

Returns:
Bdf

The copied Bdf object.

property data_record_duration[source]#

Duration of each data record in seconds.

drop_annotations(text)[source]#

Drop annotations with a given text.

Parameters:
textstr

All annotations whose text exactly matches this parameter are removed.

drop_signals(drop)[source]#

Drop signals by index or label.

_Signal indices (int) and labels (str) can be provided in the same iterable. For ambiguous labels, all corresponding signals are dropped. Raises a ValueError if at least one of the provided identifiers does not correspond to a signal.

Parameters:
dropIterable[int | str]

The signals to drop, identified by index or label.

property duration[source]#

Recording duration in seconds.

get_annotations(start_second=None, stop_second=None)[source]#

All annotations defined (starting) in a specified time region of the Bdf, sorted chronologically.

Does not include timekeeping annotations. Will not include annotations that start within the specified time region but are defined in data records outside of that window.

Parameters:
start_secondfloat, optional

The start of the time region in seconds from recording start. If not provided, the start of the recording is used.

stop_secondfloat, optional

The end of the time region in seconds. If not provided, the end of the recording is used.

get_signal(label)[source]#

Retrieve a single signal by its label.

The label has to be unique - a ValueError is raised if it is ambiguous or does not exist.

Parameters:
labelstr

A label identifying a single signal

Returns:
BdfSignal

The signal corresponding to the given label.

property labels[source]#

The labels of all signals contained in the Bdf.

Returns:
tuple[str, …]

The labels, in order of the signals.

property local_patient_identification[source]#

Unparsed string representation of the legacy local patient identification.

See also

patient

Parsed representation, as a Patient object.

property local_recording_identification[source]#

Unparsed string representation of the legacy local recording identification.

See also

recording

Parsed representation, as a Recording object.

property num_data_records[source]#

Number of data records in the recording.

property num_signals[source]#

Return the number of signals, excluding annotation signals for BDF+.

property patient[source]#

Parsed object representation of the local patient identification.

See Patient for information on its attributes.

property recording[source]#

Parsed object representation of the local recording identification.

See Recording for information on its attributes.

property reserved[source]#

"BDF+C" for an BDF+C file, else "".

set_annotations(annotations)[source]#

Overwrite all annotations with new ones.

This removes all existing annotation signals and adds a new one as the last signal in the file.

Parameters:
annotationsIterable[EdfAnnotation]

The annotations to set.

property signals[source]#

Ordinary signals contained in the recording.

Annotation signals are excluded. Individual signals can not be removed, added, or replaced by modifying this property. Use Bdf.append_signals(), Bdf.drop_signals(), or BdfSignal.data, respectively.

slice_between_annotations(start_text, stop_text, *, keep_all_annotations=False)[source]#

Slice to the interval between two BDF+ annotations.

The sample point corresponding to the onset of the annotation identified by stop_text is excluded. start_text and stop_text each have to uniquely identify a single annotation, whose onset corresponds exactly to a sample time in all non-annotation signals.

Parameters:
start_textstr

Text identifying the start annotation.

stop_textstr

Text identifying the stop annotation.

keep_all_annotationsbool, default: False

If set to True, annotations outside the selected time interval are kept.

slice_between_seconds(start, stop, *, keep_all_annotations=False)[source]#

Slice to the interval between two times.

The sample point corresponding to stop is excluded. start and stop are given in seconds from recording start and have to correspond exactly to a sample time in all non-annotation signals.

Parameters:
startfloat

Start time in seconds from recording start.

stopfloat

Stop time in seconds from recording start.

keep_all_annotationsbool, default: False

If set to True, annotations outside the selected time interval are kept.

property startdate[source]#

Recording startdate.

If the local_recording_identification conforms to the BDF+ standard, the startdate provided there is used. If not, this falls back to the legacy startdate field. If both differ, a warning is issued and the BDF+ field is preferred. Raises an AnonymizedDateError if the BDF+ field is anonymized (i.e., begins with Startdate X).

property starttime[source]#

Recording starttime.

In BDF+ files, microsecond accuracy is supported.

to_bytes()[source]#

Convert an Bdf to a bytes object.

Returns:
bytes

The binary representation of the Bdf object (i.e., what a file created with Bdf.write would contain).

update_data_record_duration(data_record_duration, method='strict')[source]#

Update the data record duration.

This operation will fail if the new duration is incompatible with the current sampling frequencies.

Parameters:
data_record_durationfloat

The new data record duration in seconds.

method{"strict", "pad", "truncate"}, default: "strict"

How to handle the case where the new duration does not divide the Bdf duration evenly

  • “strict”: Raise a ValueError

  • “pad”: Pad the data with zeros to the next compatible duration. If zero is outside the physical range, data is padded with the physical minimum.

  • “truncate”: Truncate the data to the previous compatible duration (might lead to loss of data)

property version[source]#

The BDF version, always �BIOSEMI.

write(target)[source]#

Write an Bdf to a file or file-like object.

Parameters:
targetPath | str | io.BufferedWriter | io.BytesIO

The file location (path object or string) or file-like object to write to.