-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
159 lines (133 loc) · 5.64 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import json
import os
import math
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import matplotlib.dates as mdates
import matplotlib.cm as cm
from tqdm.notebook import trange, tqdm
from datetime import datetime, timedelta
def get_terminal_width():
return int(os.popen('stty size', 'r').read().split()[1])
def resize_jupyter(width=80):
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:" + str(width) +"% !important; }</style>"))
def load_dt(path):
with open(path) as file:
data = file.read()
dt = [datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S") for ts_str in data.split('\n') if ts_str != '']
return dt
def load_labels(path):
with open(path, 'r') as openfile:
labels = json.load(openfile)
for label_type in labels:
for database_type in labels[label_type]:
for interval_idx in range(len(labels[label_type][database_type])):
interval = labels[label_type][database_type][interval_idx]
start = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S")
end = datetime.strptime(interval[1], "%Y-%m-%d %H:%M:%S")
labels[label_type][database_type][interval_idx] = [start, end]
return labels
def plot_self(ts_list, times, down, st, end, labels, yLabel="", figTitle="", toSave=False, addDay=False, flag_thresh=None):
col = ['deepskyblue', 'peachpuff', 'darkgrey', 'cornflowerblue']
legendLabels = ['Flags', 'Self-Labels', 'Self-Labels [C]', 'DownTimes']
lines = [Line2D([0], [0], color=c, linewidth=3, linestyle='-') for c in col]
if(addDay):
curDay = times[int(st+(end-st)/2)]
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
months = ['Dum', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
curDay = times[int(st+(end-st)/2)]
weekDay = days[curDay.weekday()]
year = "2020"
month = months[curDay.month]
day = curDay.day
prefixStr = " [{:02d} {}, {}] [{}]".format(day,month,year,weekDay)
figTitle += prefixStr
if(end-st <= 180): marker = '.'
else: marker = ""
plt.figure(figsize=[16,7])
plt.legend(lines, legendLabels)
ts_col_list = ['black', 'blueviolet', 'mediumseagreen', 'peru']
for idx in range(len(ts_list)):
plt.plot(times[st:end], ts_list[idx][st:end], linestyle='-', zorder=10, color=ts_col_list[idx], marker=marker)
plt.grid()
plt.title(figTitle)
plt.xlabel("Time")
plt.ylabel(yLabel)
# plt.yticks(np.arange(0.0,7,0.5) * 1e11)
# plt.xticks(np.arange(times[st],times[end], timedelta(minutes=60)))
# Flags
if(flag_thresh != None):
plt.axhline(flag_thresh, linestyle='--', color='deepskyblue', zorder=1)
flag_points = times[ts < flag_thresh]
for point in flag_points:
if(point < times[end-1] and point > times[st]):
plt.axvline(point, color=col[0], zorder=1, ymin=0, ymax=1)
# SelfLabels Standard
selfLabels = labels['selfLabels']
for db in selfLabels.keys():
if(db in down):
intervals = selfLabels[db]
for interval in intervals:
points = fill_dips(interval)
for point in points:
if(point < times[end-1] and point > times[st]):
plt.axvline(point, color=col[1], zorder=1, ymin=0, ymax=0.95)
# SelfLabels Challenging
selfLabels_challenge = labels['selfLabels_challenge']
for db in selfLabels_challenge.keys():
if(db in down):
intervals = selfLabels_challenge[db]
for interval in intervals:
points = fill_dips(interval)
for point in points:
if(point < times[end-1] and point > times[st]):
plt.axvline(point, color=col[2], zorder=1, ymin=0, ymax=0.95)
# Clubbed Downtimes
clubbedDownTimes = labels['clubbedDownTimes']
for db in clubbedDownTimes.keys():
if(db in down):
intervals = clubbedDownTimes[db]
for interval in intervals:
points = fill_dips(interval)
for point in points:
if(point < times[end-1] and point > times[st]):
plt.axvline(point, color=col[3], zorder=1, ymin=0.05, ymax=1)
ax = plt.axes()
ax2 = ax.twiny()
ax2.set_xlim(0,1)
locator = mdates.AutoDateLocator(minticks=5, maxticks=20)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
if(toSave):
path = os.path.join(folderGraphs2, figTitle + ".png")
plt.savefig(path, dpi=200)
plt.show()
def fill_dips(dips):
start, end = dips[0], dips[-1]
newDips = []
while(start <= end):
newDips.append(start)
start = start + timedelta(seconds=30)
return newDips
def get_stend(start=None, delay=None, anchor=None, delta=None, l=None):
globalStart = datetime(2020,4,27,0,0,0)
globalStop = datetime(2020,6,30,23,59,59)
if(start != None and delay != None):
curStart = start
curStop = start + delay
elif(anchor != None and delta != None):
curStart = anchor - delta
curStop = anchor + delta
else:
curStart = globalStart
curStop = globalStop
s = max((curStart - globalStart)/(globalStop - globalStart),0)
e = min((curStop - globalStart)/(globalStop - globalStart),1)
st = int(l*s)
end = int(l*e)
return st, end