Events (dv.events)¶
Event markers for instants and periods, and their conversion to flags and plot overlays.
Imported as diive.events, reached as dv.events after import diive as dv.
EVENTS: TIME-STAMPED EVENT MARKERS¶
Represent things that happened at a point in time or over a period — anything the user wants to mark (management interventions, instrument swaps, field campaigns, weather events, …) — as 0/1 data columns and as overlays on time-series plots.
Part of the diive library: https://github.com/holukas/diive
- class diive.events.Event(name, start, end=None, category='', description='', color=None)[source]¶
Bases:
objectA single time-stamped event.
An event is either an instant (
end is None— a single point in time, e.g. something that happened on one day) or a period (endset — e.g. an interval, or a start date plus a duration).start/endare coerced topandas.Timestamp; a period withend < startis rejected.- Parameters:
name (
str) – Short label shown on plots and used to build the flag column name.start (
Timestamp) – When the event began (or the instant it occurred).end (
Timestamp|None) – When the event ended;Nonefor an instant event.category (
str) – Optional grouping (e.g."category1") that drives the default colour viaCATEGORY_COLORS.description (
str) – Free-text note.color (
str|None) – Explicit colour; whenNoneit is resolved from the category.
- property flag_name: str¶
The 0/1 flag column name for this event (see
make_event_flag_name()).
- resolved_color(fallback_index=0, colors=None)[source]¶
The colour to draw this event in: explicit, else by category, else a cycled fallback (indexed by
fallback_indexso a list of events with no category still get distinct colours).colorsis an optional{category: hex}override map consulted before the built-inCATEGORY_COLORS(category match is case-insensitive), so a caller can supply its own category palette without mutating the event.
- diive.events.event_to_flag(event, index, name=None)[source]¶
Build the yes/no (0/1) flag series for an event, aligned to
index.The result is 1 where the event was active and 0 everywhere else — a normal data column that can be saved, plotted, and processed like any other variable.
Period event (
event.endset): 1 for every record in[start, end](inclusive).Instant event (
event.end is None): 1 at the single record nearest toevent.start(so a point event always marks exactly one record, even when its timestamp falls between two records).
- Parameters:
event (
Event) – The event to encode.index (
DatetimeIndex) – Target timestamp index (the dataset’s index).name (
str|None) – Optional series name; defaults toevent.flag_name.
- Return type:
- Returns:
Integer
Seriesof 0/1 indexed byindex.
- diive.events.make_event_flag_name(name)[source]¶
Column name for an event’s 0/1 flag:
EVENT_<sanitized name>.Whitespace collapses to single underscores so the result is a clean column label; other characters are kept (diive column names are otherwise free-form).
- diive.events.overlay_events(ax, events, *, axis='x', show_labels=True, label_fontsize=8, line_alpha=0.9, span_alpha=0.16, linewidth=1.3, colors=None)[source]¶
Draw events onto an existing matplotlib axes.
Instant events become a dashed line; period events become a translucent shaded span bordered by thin start/end lines. Positions are converted with
matplotlib.dates.date2num(), which matches the float units of any diive datetime axis — both the value-vs-time line panels (axis='x') and the date/time heatmap, where the date is the y-axis (axis='y').This only adds artists; it never clears or rescales the axes, so it composes on top of an already-rendered plot. The overlays are drawn above any image or mesh already on the axes, so they stay visible on an opaque heatmap.
- Parameters:
ax – Target axes (already drawn).
events – Iterable of
Event.axis (
str) –'x'to draw against time on the x-axis (line/cumulative/daily panels),'y'to draw against date on the y-axis (heatmap).show_labels (
bool) – Annotate each event with its name.label_fontsize (
int) – Font size for the labels.line_alpha (
float) – Opacity of instant/border lines.span_alpha (
float) – Opacity of the period shading.linewidth (
float) – Width of instant/border lines.colors (
dict[str,str] |None) – Optional{category: hex}override map (seeEvent.resolved_color()).
- Return type: