Skip to content

events

Utilities for loading and parsing analysis result.

parse_events(results, merge_consequtive=True)

Parse events from a JSON result.

Parse a list of Event-compatible dictionaries, or a dictionary with a top-level key 'events' pointing to such list, to a list of Event models.

Parameters:

Name Type Description Default
results dict | list

The result JSON.

required
merge_consequtive bool

Whether to merge consequtive events of the same type into one event.

True

Returns:

Type Description
list[Event]

The parsed list of Event models.

Raises:

Type Description
ValueError

If an event dictionary is not a valid Event model.

Examples:

>>> parse_events({"events": []})
[]
>>> parse_events([])
[]
>>> parse_events([{"start_time_ms": 0, "end_time_ms": 1000, "label": "Sleep stage W"}])
[Event(start_time_ms=0, end_time_ms=1000, label=<EventLabel.SLEEP_STAGE_W: 'Sleep stage W'>, probability=None, value=None, channels=[])]

read_and_parse_events(input_file_or_str, merge_consequtive=False)

Read and parse the events result from the given events JSON file or JSON string.

Parameters:

Name Type Description Default
input_file_or_str TextIOWrapper | str

The open file or JSON string to read the events from.

required
merge_consequtive bool

Whether to merge consequtive events of the same type into one event.

False

Returns:

Type Description
list[Event]

List of parsed events.

read_events(input_file_or_str)

Read events from a JSON file or JSON string.

Parameters:

Name Type Description Default
input_file_or_str TextIOWrapper | str

The open file or JSON string to read the events from.

required

Returns:

Type Description
Any

The parsed result JSON.

Raises:

Type Description
JSONDecodeError

If the file is not a valid JSON file.

write_events(events, out_file)

Write a list of Event or Event-compatible dictionaries to a JSON file.

Parameters:

Name Type Description Default
events list[Event | dict]

The list of events to write.

required
out_file TextIOWrapper

The open file to write the events to.

required

Raises:

Type Description
ValueError

If the event is not a valid Event model.

Returns:

Type Description
None

None