Skip to content

Commit 1b16c2f

Browse files
committed
Code: Added init source code
1 parent 7f6d41f commit 1b16c2f

File tree

197 files changed

+101161
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+101161
-0
lines changed

cesnet_tszoo/__init__.py

Whitespace-only changes.

cesnet_tszoo/annotation.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import logging
2+
3+
import pandas as pd
4+
import numpy as np
5+
6+
from cesnet_tszoo.utils.enums import AnnotationType
7+
8+
9+
class Annotations():
10+
"""Used as wrapper around dictionaries used for storing annotations data."""
11+
12+
def __init__(self):
13+
self.time_series_annotations = {}
14+
self.time_annotations = {}
15+
self.time_in_series_annotations = {}
16+
17+
self.logger = logging.getLogger("annotations")
18+
19+
def add_annotation(self, annotation: str, annotation_group: str, ts_id: int | None, id_time: int | None) -> None:
20+
"""
21+
Add an annotation to the specified `annotation_group` identified by `ts_id` and `id_time`.
22+
23+
If the `annotation_group` does not already exist, it will be created.
24+
"""
25+
26+
assert ts_id is not None or id_time is not None, "At one of the ids must be set."
27+
assert annotation_group is not None, "Annotation group must be set."
28+
29+
if ts_id is not None and id_time is not None:
30+
self.add_annotation_group(annotation_group, AnnotationType.BOTH, True)
31+
self.time_in_series_annotations[annotation_group][(ts_id, id_time)] = annotation
32+
self.logger.debug("Annotation added to annotation_group: %s in %s.", annotation_group, AnnotationType.BOTH)
33+
elif id_time is not None:
34+
self.add_annotation_group(annotation_group, AnnotationType.ID_TIME, True)
35+
self.time_annotations[annotation_group][id_time] = annotation
36+
self.logger.debug("Annotation added to annotation_group: %s in %s.", annotation_group, AnnotationType.ID_TIME)
37+
elif ts_id is not None:
38+
self.add_annotation_group(annotation_group, AnnotationType.TS_ID, True)
39+
self.time_series_annotations[annotation_group][ts_id] = annotation
40+
self.logger.debug("Annotation added to annotation_group: %s in %s.", annotation_group, AnnotationType.TS_ID)
41+
else:
42+
raise NotImplementedError("Should not happen.")
43+
44+
def clear_time_in_time_series(self):
45+
"""Clears `time_in_series_annotations` dictionary. """
46+
47+
self.time_in_series_annotations = {}
48+
self.logger.debug("Cleared time_in_series_annotations.")
49+
50+
def clear_time(self):
51+
"""Clears `time_annotations` dictionary. """
52+
53+
self.time_annotations = {}
54+
self.logger.debug("Cleared time_annotations.")
55+
56+
def clear_time_series(self):
57+
"""Clears `time_series_annotations` dictionary. """
58+
59+
self.time_series_annotations = {}
60+
self.logger.debug("Cleared time_series_annotations.")
61+
62+
def remove_annotation(self, annotation_group: str, ts_id: int | None, id_time: int | None, silent: bool = False):
63+
""" Removes annotation from `annotation_group` and `ts_id`, `id_time`. """
64+
65+
assert ts_id is not None or id_time is not None, "At one of the ids must be set."
66+
assert annotation_group is not None, "Annotation group must be set."
67+
68+
if ts_id is not None and id_time is not None:
69+
assert annotation_group in self.time_in_series_annotations, "Annotation group must exist in time_in_series_annotations."
70+
if (ts_id, id_time) not in self.time_in_series_annotations[annotation_group]:
71+
if not silent:
72+
self.logger.info("Annotation does not exist.")
73+
74+
return
75+
76+
self.time_in_series_annotations[annotation_group].pop((ts_id, id_time))
77+
78+
elif id_time is not None:
79+
assert annotation_group in self.time_annotations, "Annotation group must exist in time_annotations."
80+
if id_time not in self.time_annotations[annotation_group]:
81+
if not silent:
82+
self.logger.info("Annotation does not exist.")
83+
84+
return
85+
86+
self.time_annotations[annotation_group].pop(id_time)
87+
88+
elif ts_id is not None:
89+
assert annotation_group in self.time_series_annotations, "Annotation group must exist in time_series_annotations."
90+
if ts_id not in self.time_series_annotations[annotation_group]:
91+
if not silent:
92+
self.logger.info("Annotation does not exist.")
93+
94+
return
95+
96+
self.time_series_annotations[annotation_group].pop(ts_id)
97+
98+
def add_annotation_group(self, annotation_group: str, on: AnnotationType, silent: bool = False):
99+
""" Adds `annotation_group` to dictionary based on `on`. """
100+
101+
if on == AnnotationType.TS_ID:
102+
if annotation_group in self.time_series_annotations:
103+
if not silent:
104+
self.logger.info("Annotation group %s already exists.", annotation_group)
105+
106+
return
107+
108+
self.time_series_annotations[annotation_group] = {}
109+
elif on == AnnotationType.ID_TIME:
110+
if annotation_group in self.time_annotations:
111+
if not silent:
112+
self.logger.info("Annotation group %s already exists.", annotation_group)
113+
114+
return
115+
116+
self.time_annotations[annotation_group] = {}
117+
elif on == AnnotationType.BOTH:
118+
if annotation_group in self.time_in_series_annotations:
119+
if not silent:
120+
self.logger.info("Annotation group %s already exists.", annotation_group)
121+
122+
return
123+
124+
self.time_in_series_annotations[annotation_group] = {}
125+
126+
def remove_annotation_group(self, annotation_group: str, on: AnnotationType, silent: bool = False):
127+
""" Removes `annotation_group` from dictionary based on `on`. """
128+
129+
if on == AnnotationType.TS_ID:
130+
if annotation_group not in self.time_series_annotations:
131+
if not silent:
132+
self.logger.info("Annotation group %s does not exist.", annotation_group)
133+
134+
return
135+
136+
self.time_series_annotations.pop(annotation_group)
137+
elif on == AnnotationType.ID_TIME:
138+
if annotation_group not in self.time_annotations:
139+
if not silent:
140+
self.logger.info("Annotation group %s does not exist.", annotation_group)
141+
142+
return
143+
144+
self.time_annotations.pop(annotation_group)
145+
elif on == AnnotationType.BOTH:
146+
if annotation_group not in self.time_in_series_annotations:
147+
if not silent:
148+
self.logger.info("Annotation group %s does not exist.", annotation_group)
149+
150+
return
151+
152+
self.time_in_series_annotations.pop(annotation_group)
153+
154+
def get_annotations(self, on: AnnotationType, ts_id_name: str) -> pd.DataFrame:
155+
"""Returns annotations based on `on` as Pandas [`DataFrame`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html). """
156+
157+
rows = {}
158+
columns = []
159+
annotations = None
160+
point_size = None
161+
162+
if on == AnnotationType.TS_ID:
163+
columns.append(ts_id_name)
164+
annotations = self.time_series_annotations
165+
point_size = 1
166+
167+
elif on == AnnotationType.ID_TIME:
168+
columns.append("id_time")
169+
annotations = self.time_annotations
170+
point_size = 1
171+
172+
elif on == AnnotationType.BOTH:
173+
columns.append(ts_id_name)
174+
columns.append("id_time")
175+
annotations = self.time_in_series_annotations
176+
point_size = 2
177+
178+
for i, group in enumerate(annotations):
179+
columns.append(group)
180+
i = i + point_size # offset for ts_id and id_time
181+
for point in annotations[group]:
182+
if point in rows:
183+
row = rows[point]
184+
else:
185+
row = np.array([None for _ in range(len(annotations) + point_size)])
186+
row[:point_size] = point
187+
rows[point] = row
188+
189+
row[i] = annotations[group][point]
190+
191+
return pd.DataFrame(rows.values(), columns=columns)

0 commit comments

Comments
 (0)