edfio.Edf#
- class edfio.Edf(signals, *, patient=None, recording=None, starttime=None, data_record_duration=None, annotations=None)[source]#
Python representation of an EDF file.
EDF header fields are exposed as properties with appropriate data types (i.e., string, numeric, date, or time objects). Fields that might break the file on modification (i.e.,
version,bytes_in_header_record,reserved,num_data_records,data_record_duration, andnum_signals) can not be set after instantiation.Note that the startdate has to be set via the parameter
recording.For writing an EDF file with a non-integer seconds duration, currently an appropriate value for
data_record_durationhas to be provided manually.- Parameters:
- signals
Sequence[EdfSignal] The (non-annotation) signals to be contained in the EDF file.
- patient
Patient|None, default:None The “local patient identification”, containing patient code, sex, birthdate, name, and optional additional fields. If
None, the field is set toX X X Xin accordance with EDF+ specs.- recording
Recording|None, default:None The “local recording identification”, containing recording startdate, hospital administration code, investigator/technical code, equipment code, and optional additional fields. If
None, the field is set toStartdate X X X Xin accordance with EDF+ specs.- starttime
datetime.time|None, default:None The starttime of the recording. If
None,00.00.00is used. Ifstarttimecontains microseconds, an EDF+C file is created.- data_record_duration
float|None, default:None The duration of each data record in seconds. If
None, an appropriate value is chosen automatically.- annotations
Iterable[EdfAnnotation] |None, default:None The annotations, consisting of onset, duration (optional), and text. If not
None, an EDF+C file is created.
- signals
Attributes
All annotations contained in the Edf, sorted chronologically.
Number of bytes in the header record.
Duration of each data record in seconds.
Recording duration in seconds.
The labels of all signals contained in the Edf.
Unparsed string representation of the legacy local patient identification.
Unparsed string representation of the legacy local recording identification.
Number of data records in the recording.
Return the number of signals, excluding annotation signals for EDF+.
Parsed object representation of the local patient identification.
Parsed object representation of the local recording identification.
"EDF+C"for an EDF+C file, else"".Ordinary signals contained in the recording.
Recording startdate.
Recording starttime.
EDF version, always
0.Methods
__init__Add annotations to the Edf.
Anonymize a recording.
Append one or more signal(s) to the Edf recording.
Create a deep copy of the Edf.
Drop annotations with a given text.
Drop signals by index or label.
All annotations defined (starting) in a specified time region of the Edf, sorted chronologically.
Retrieve a single signal by its label.
Overwrite all annotations with new ones.
Slice to the interval between two EDF+ annotations.
Slice to the interval between two times.
Convert an Edf to a
bytesobject.Update the data record duration.
Write an Edf to a file or file-like object.
- add_annotations(annotations)[source]#
Add annotations to the Edf.
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:
- annotations
Iterable[EdfAnnotation] The annotations to add.
- annotations
- property annotations[source]#
All annotations contained in the Edf, 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 Xlocal recording identification is set to
Startdate X X X Xstartdate is set to
01.01.85starttime is set to
00.00.00
For EDF+ 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 toX X 01-JAN-YYYY X, whereYYYYis 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.
- keep_agebool, default:
- append_signals(new_signals)[source]#
Append one or more signal(s) to the Edf recording.
Every signal must be compatible with the current
data_record_durationand all signal durations must match the overall recording duration. For recordings containing EDF+ annotation signals, the new signals are inserted after the last ordinary (i.e. non-annotation) signal.
- drop_annotations(text)[source]#
Drop annotations with a given text.
- Parameters:
- text
str All annotations whose text exactly matches this parameter are removed.
- text
- 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.
- get_annotations(start_second=None, stop_second=None)[source]#
All annotations defined (starting) in a specified time region of the Edf, 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.
- 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.
- property local_patient_identification[source]#
Unparsed string representation of the legacy local patient identification.
- property local_recording_identification[source]#
Unparsed string representation of the legacy local recording identification.
- property patient[source]#
Parsed object representation of the local patient identification.
See
Patientfor information on its attributes.
- property recording[source]#
Parsed object representation of the local recording identification.
See
Recordingfor information on its attributes.
- 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:
- annotations
Iterable[EdfAnnotation] The annotations to set.
- annotations
- 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
Edf.append_signals(),Edf.drop_signals(), orEdfSignal.data, respectively.
- slice_between_annotations(start_text, stop_text, *, keep_all_annotations=False)[source]#
Slice to the interval between two EDF+ annotations.
The sample point corresponding to the onset of the annotation identified by
stop_textis excluded.start_textandstop_texteach have to uniquely identify a single annotation, whose onset corresponds exactly to a sample time in all non-annotation signals.
- slice_between_seconds(start, stop, *, keep_all_annotations=False)[source]#
Slice to the interval between two times.
The sample point corresponding to
stopis excluded.startandstopare given in seconds from recording start and have to correspond exactly to a sample time in all non-annotation signals.
- property startdate[source]#
Recording startdate.
If the
local_recording_identificationconforms to the EDF+ standard, the startdate provided there is used. If not, this falls back to the legacystartdatefield. If both differ, a warning is issued and the EDF+ field is preferred. Raises anAnonymizedDateErrorif the EDF+ field is anonymized (i.e., begins withStartdate X).
- to_bytes()[source]#
Convert an Edf to a
bytesobject.- Returns:
bytesThe binary representation of the Edf object (i.e., what a file created with
Edf.writewould 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_duration
float 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 Edf 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)
- data_record_duration
- write(target)[source]#
Write an Edf to a file or file-like object.
- Parameters:
- target
Path|str|io.BufferedWriter|io.BytesIO The file location (path object or string) or file-like object to write to.
- target