as_mask
openseize.file_io.annotations.as_mask(annotations, size, fs, include=True)
Creates a boolean mask from a sequence of annotation dataclass instances..
Producers of EEG data may receive an optional boolean array mask. This function creates a boolean mask from a sequence of annotations and is therefore useful for filtering EEG data by annotation label during processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
annotations |
Sequence[Annotation]
|
A sequence of annotation dataclass instances to convert to a mask. |
required |
size |
int
|
The length of the boolean array to return. |
required |
fs |
float
|
The sampling rate in Hz of the digital system. |
required |
include |
bool
|
Boolean determining if annotations should be set to True or False in the returned array. True means all values are False in the returned array except for samples where the annotations are located. |
True
|
Returns:
Type | Description |
---|---|
npt.NDArray[np.bool_]
|
A 1-D boolean array of length size. |
Examples:
>>> # read the annotations from the demo annotation file
>>> from openseize.demos import paths
>>> filepath = paths.locate('annotations_001.txt')
>>> from openseize.io.annotations import Pinnacle
>>> # read the 'rest' annotations
>>> with Pinnacle(filepath, start=6) as pinnacle:
>>> annotations = pinnacle.read(labels=['rest'])
>>> # create a mask measuring 3700 secs at 5000 Hz
>>> mask = as_mask(annotations, size=3700*5000, fs=5000)
>>> # measure the total time in secs of 'rest' annotation
>>> print(np.count_nonzero(mask) / 5000)
15.0
Source code in openseize/file_io/annotations.py
openseize.file_io.bases.Annotation
dataclass
An object for storing a predefined set of annotation attributes that can be updated with user defined attributes after object creation.
Attributes:
Name | Type | Description |
---|---|---|
label |
str
|
The string name of this annotation. |
time |
float
|
The time this annotation was made in seconds relative to the recording start. |
duration |
float
|
The duration of this annotation in seconds. |
channel |
Any
|
The string name or integer index of the channel this annotation created from. |