Blame view

MLTD/src/Utils.py 6.85 KB
0d8c0f816   Thanasis Naskos   initial commit
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  # -*- coding: utf-8 -*-
  import sqlite3
  import re
  import math
  import logging
  import os
  
  try:
      import cPickle as pickle
  except:
      import pickle
  
  from sqlite3 import Error
  
  logger = logging.getLogger(__name__)
  
  
  def create_sqlite_connection(db_file):
      conn = None
      try:
          conn = sqlite3.connect(db_file)
      except Error as e:
          logger.exception("Can't connect to SQLite")
      return conn
  
  
  def select_model_attribute(conn, trainID, attribute):
      time_seg = ""
      cur = conn.cursor()
      cur.execute(
          "SELECT " + attribute + " FROM models WHERE model_id=?", (int(trainID),)
      )
      rows = cur.fetchone()
      if rows != None:
          if len(rows) > 0:
              time_seg = rows[0]
      return time_seg
  
  def check_pid(pid):
      """ Check For the existence of a unix pid. """
      try:
          os.kill(pid, 0)
      except OSError:
          return False
      else:
          return True
  
  
  # def load_data(train_id, train_dir=DEFAULT_TRAIN_DIR, only_aem=False):
  #     filename = "train_" + str(train_id) + ".dat"
  #     train_path = os.path.join(train_dir, filename)
  #     infile = open(train_path, "rb")
  #     weak_bins_mapping = pickle.load(infile)
  #     profile_index = pickle.load(infile)
  #     mp = pickle.load(infile)
  #     train_dataset_values = np.array(pickle.load(infile))
  #     art_events_dataset = pickle.load(infile)
  #     processed_art_events_dataset = ""
  #     regr = ""
  #     feature_importance = ""
  #     if not only_aem:
  #         processed_art_events_dataset = pickle.load(infile)
  #         regr = pickle.load(infile)
  #         feature_importance = pickle.load(infile)
  #     infile.close()
  #     return (
  #         weak_bins_mapping,
  #         profile_index,
  #         mp,
  #         train_dataset_values,
  #         art_events_dataset,
  #         processed_art_events_dataset,
  #         regr,
  #         feature_importance,
  #     )
  
  
  def create_table(conn, create_table_sql):
      try:
          c = conn.cursor()
          c.execute(create_table_sql)
      except Error as e:
          print(e)
  
  
  def sigmoid_risk(s, midpoint, t):
      try:
          return 1 / (1 + math.exp(s * (t - midpoint)))
      except OverflowError as ex:
          logger.warning(f"Timedelta to big: {t}. " f"Returning 0 risk")
      return 0
  
  
  def sigmoid_mins(v, s, midpoint, hours_before):
      if v > 1:
          logger.error(f"SIGMOID Risk can't be more than one {v}")
          raise
      elif v == 1:
          return 0
      elif v == 0:
          return convert_hours_to_mins(strtime_to_hours(hours_before))
      return (math.log(1 / v - 1) / s) + midpoint
  
  
  def convert_hours_to_mins(hours):
      return hours * 60
  
  
  def strtime_to_hours(time_segments):
      time_segments_digits = float(re.findall(r"\d+\.\d+|\d+", time_segments)[0])
      time_segments_hours = 0.0
      if "H" in time_segments:
          time_segments_hours = time_segments_digits
      elif "T" in time_segments or "min" in time_segments:
          time_segments_hours = time_segments_digits / 60.0
      elif "S" in time_segments:
          time_segments_hours = time_segments_digits / 60.0 / 60.0
      return time_segments_hours
  
  
  def inBufferWindow(actual_hours, buffer_time):
      """
      Converts the buffer_size, which is in hours, into segments and checks
      whether t  is higher than ep_length-buffer
  
      Parameters
      ----------
      actual_hours : int
          the current segment
      buffer_time : str
          the size of the buffer window in hours
  
      Returns
      -------
      Returns true if t inside the buffer window
  
      """
      if buffer_time is not None:
          buffer_time_mins = convert_hours_to_mins(strtime_to_hours(buffer_time))
          return strtime_to_hours(convert_hours_to_mins(actual_hours)) <= buffer_time_mins
      return False
  
      time_segments_hours = time_segments_to_hours(chunks_type)
      last_segments_count = math.ceil(buffer_time / time_segments_hours)
      return ep_length - last_segments_count <= t
  
  
  def time_segments_to_hours(time_segments):
      time_segments_digits = float(re.findall(r"\d+", time_segments)[0])
      time_segments_hours = 0.0
      if "H" in time_segments:
          time_segments_hours = time_segments_digits
      elif "T" in time_segments or "min" in time_segments:
          time_segments_hours = time_segments_digits / 60.0
      elif "S" in time_segments:
          time_segments_hours = time_segments_digits / 60.0 / 60.0
      return time_segments_hours
  
  
  def fetch_influxdb_data(
      inf_client, start_date, end_date, dbname, measurement, multidimensional_data
  ):
      dataset_dates = []
      dataset_values = []
  
      if multidimensional_data:
          query = (
              "SELECT * FROM "
              + measurement
              + " WHERE "
              + "time>='"
              + start_date
              + "' AND time <= '"
              + end_date
              + "'"
          )
          result = inf_client.query(query, database=dbname)
  
          if len(result.items()) > 0:
              for c in result.items()[0][1]:
                  ae = [0] * (len(c.keys()) - 1)
                  for key in c.keys():
                      if key == "time":
                          time = c["time"]
                      else:
                          ae[int(key)] = c[key]
                  if len(ae) == 1 or sum(ae) == 0:
                      continue
                  dataset_dates.append(time)
                  dataset_values.append(ae)
      else:
          query = (
              "SELECT * FROM "
              + measurement
              + " WHERE "
              + "time>='"
              + start_date
              + "' AND time <= '"
              + end_date
              + "'"
          )
          result = inf_client.query(query, database=dbname)
  
          if len(result.items()) > 0:
              for c in result.items()[0][1]:
                  ae = [0] * (len(c.keys()) - 1)
                  for key in c.keys():
                      if key == "time":
                          time = c["time"]
                      else:
                          ae[int(re.findall(r"\d+\.\d+|\d+", key)[0])] = c[key]
                  if len(ae) == 1 or sum(ae) == 0:
                      continue
                  dataset_dates.append(time)
                  dataset_values.append(max(ae))
      return dataset_dates, dataset_values
  
  
  def compute_thresholds(
      warning_hours_thres, prediction_threshold, s, midpoint, hours_before, buffer_time
  ):
      buffer_time_mins = 0
      if buffer_time is not None:
          buffer_time_mins = convert_hours_to_mins(strtime_to_hours(buffer_time))
      if warning_hours_thres is not None:
          prediction_threshold = sigmoid_risk(
              s,
              convert_hours_to_mins(strtime_to_hours(midpoint)),
              convert_hours_to_mins(strtime_to_hours(warning_hours_thres))
              - buffer_time_mins,
          )
      else:
          warning_hours_thres = (
              sigmoid_mins(
                  prediction_threshold,
                  s,
                  convert_hours_to_mins(strtime_to_hours(midpoint)),
                  hours_before,
              )
              / 60.0
          )
      return warning_hours_thres, prediction_threshold