-
Notifications
You must be signed in to change notification settings - Fork 0
/
RH_Library.py
326 lines (255 loc) · 12.4 KB
/
RH_Library.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import datetime
import math
import os
import sqlite3
import plotly.plotly as py
import matplotlib.pyplot as plt
#
#
# Time
def timeFormatting(time):
if len(time) == 19:
return time + ".000"
else:
return time
def elapsedTimeCalculating(startTime, endTime):
startTime = datetime.datetime.strptime(startTime, "%Y.%m.%d_%H.%M.%S.%f")
endTime = datetime.datetime.strptime(endTime, "%Y.%m.%d_%H.%M.%S.%f")
elapsedTime = endTime - startTime
return elapsedTime.total_seconds()
def secToTimeFormatting(sec):
if len(str(datetime.timedelta(seconds=sec))) <= 8:
return str(datetime.timedelta(seconds=sec))+".000"
else:
return str(datetime.timedelta(seconds=sec))[:-3]
def timeToSecFormating(tim):
HOUR, MIN, SEC = tim.split(":")
SEC, MIL = SEC.split(".")
return datetime.timedelta(hours=int(HOUR), minutes=int(MIN), seconds=int(SEC), milliseconds=int(MIL)).total_seconds()
def timeBinning(startTime, endTime):
startTime = datetime.datetime.strptime(startTime, "%Y.%m.%d_%H.%M.%S.%f")
endTime = datetime.datetime.strptime(endTime, "%Y.%m.%d_%H.%M.%S.%f")
bins = []
while startTime < endTime:
bins.append([startTime.strftime("%Y.%m.%d_%H.%M.%S.%f")[:-3], (startTime + datetime.timedelta(minutes=5)).strftime("%Y.%m.%d_%H.%M.%S.%f")[:-3]])
startTime += datetime.timedelta(minutes=5)
return bins
def fileExisting(file):
if os.path.isfile(file):
os.remove(file)
def dayCalculating(time):
time = datetime.datetime.strptime(time, "%Y.%m.%d_%H.%M.%S.%f")
return time.weekday()
#
#
# DataMining
def entropy(valueArray):
sumValues = sum(valueArray)
probArray = [round(value/sumValues, 3) for value in valueArray]
# print(">>", probArray)
result = [round(-1 * prob * math.log10(prob), 3) for prob in probArray]
# print(">>>>>", result)
return round(sum(result), 3)
#
#
# Database
def APPDATABASEMAKING(file, appArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE APPTABLE(TIMESTAMP TEXT, RUNNING TEXT, PACKAGE TEXT)")
for row in appArray:
cursor.execute("INSERT INTO APPTABLE(TIMESTAMP, RUNNING, PACKAGE) VALUES(?, ?, ?);", (row[0], row[1], row[2]))
connect.commit()
connect.close()
def NOTIDATABASEMAKING(file, logArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE NOTIFICATIONTABLE(TIMESTAMP TEXT, POSTREMOVALRINGER TEXT, PACKAGE TEXT, TITLE TEXT, TEXT TEXT, SUBTEXT TEXT, SOUND TEXT, VIBRATE TEXT, DEFAULTS INT, LEDON INT, LEDOFF INT, LEDRGB INT)")
for row in logArray:
cursor.execute("INSERT INTO NOTIFICATIONTABLE(TIMESTAMP, POSTREMOVALRINGER, PACKAGE, TITLE, TEXT, SUBTEXT, SOUND, VIBRATE, DEFAULTS, LEDON, LEDOFF, LEDRGB) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
connect.commit()
connect.close()
def APNODATABASEMAKING(file, appArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE APNOTABLE(TIMESTAMP TEXT, TYPE TEXT, PACKAGE TEXT, TITLE TEXT, TEXT TEXT, SUBTEXT TEXT, SOUND TEXT, VIBRATE TEXT, DEFAULTS INT, LEDON INT, LEDOFF INT, LEDRGB INT)")
for row in appArray:
cursor.execute("INSERT INTO APNOTABLE(TIMESTAMP, TYPE, PACKAGE, TITLE, TEXT, SUBTEXT, SOUND, VIBRATE, DEFAULTS, LEDON, LEDOFF, LEDRGB) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
connect.commit()
connect.close()
def USAGEDATABASEMAKING(file, usageArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE USAGETABLE(TYPE TEXT, STARTTIME TEXT, ENDTIME TEXT, ELAPSEDTIME TEXT)")
for row in usageArray:
cursor.execute("INSERT INTO USAGETABLE(TYPE, STARTTIME, ENDTIME, ELAPSEDTIME) VALUES(?, ?, ?, ?);", (row[0], row[1], row[2], row[3]))
connect.commit()
connect.close()
def CLASSUSAGEDATABASEMAKING(file, usageArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE CLASSUSAGETABLE(CLASSNAME TEXT, ENTRANCETIME TEXT, EXITTIME TEXT, TYPE TEXT, STARTTIME TEXT, ENDTIME TEXT, ELAPSEDTIME TEXT)")
for row in usageArray:
cursor.execute("INSERT INTO CLASSUSAGETABLE(CLASSNAME, ENTRANCETIME, EXITTIME, TYPE, STARTTIME, ENDTIME, ELAPSEDTIME) VALUES(?, ?, ?, ?, ?, ?, ?)", (row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
connect.commit()
connect.close()
def CLASSAPNODATABASEMAKING(file, classApnoArray):
fileExisting(file)
connect = sqlite3.connect(file)
cursor = connect.cursor().execute("CREATE TABLE CLASSAPNOTABLE(Z TEXT, TIMESTAMP TEXT, TYPE TEXT, PACKAGE TEXT)")
for row in classApnoArray:
cursor.execute("INSERT INTO CLASSAPNOTABLE(Z, TIMESTAMP, TYPE, PACKAGE) VALUES(?, ?, ?, ?)", (row[0], row[1], row[2], row[3]))
connect.commit()
connect.close()
#
#
# ploting
def PLOTING3D(X, Y, Z):
scatter = dict(
mode="markers", name="y", type="scatter3d",
x=X, y=Y, z=Z,
marker=dict(size=2, color="rgb(23, 190, 207)")
)
clusters = dict(
alphahull=7, name="y", opacity=0.1, type="mesh3d",
x=X, y=Y, z=Z
)
layout = dict(
title='3d point clustering',
scene=dict(
xaxis=dict(zeroline=False),
yaxis=dict(zeroline=False),
zaxis=dict(zeroline=False),
)
)
fig = dict(data=[scatter, clusters], layout=layout)
py.plotly.tools.set_credentials_file(username='dhgkjdasf', api_key='cURhfnaDsgSDmOc0OHQw')
py.plot(fig, filename='3d point clustering')
plt.show()
# def elapsedTimeArrayCalculating(startTimeArray, endTimeArray):
# elapsedTimeArray = []
# for index in range(0, len(startTimeArray)):
# startTIme = datetime.datetime.strptime(startTimeArray[index][0][0:], "%Y.%m.%d_%H.%M.%S")
# endTime = datetime.datetime.strptime(endTimeArray[index][0][0:], "%Y.%m.%d_%H.%M.%S")
# elapsedTimeArray.append(str(endTime - startTIme))
#
# return elapsedTimeArray
#
# def sumUpTimeCalculating(startTime, endTime):
# startHour, startMin, startSec = startTime.split(":")
# startSec, startMil = startSec.split(".")
#
# endHour, endMin, endSec = endTime.split(":")
# endSec, endMil = endSec.split(".")
#
# return str(int(startHour) + int(endHour)) + ":" + str(int(startMin) + int(endMin)) + ":" + str(int(startSec) + int(endSec)) + "." + str(int(startMil) + int(endMil))
# # APPSESSUIBDATABASE
# def APPSESSIONDATABASEMAKING(file, appSessionArray):
# fileExisting(file)
#
# connect = sqlite3.connect(file)
# cursor = connect.cursor().execute("CREATE TABLE APPSESSIONTABLE(ID TEXT, CLASSNAME TEXT, STARTTIME TEXT, ENDTIME TEXT, SESSIONTIME TEXT, SESSIONTYPE TEXT, SESSIONPACKAGE TEXT)")
#
# for row in appSessionArray:
# cursor.execute("""INSERT INTO APPSESSIONTABLE(ID, CLASSNAME, STARTTIME, ENDTIME, SESSIONTIME, SESSIONTYPE, SESSIONPACKAGE) VALUES(?, ?, ?, ?, ?, ?, ?);""",
# (row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
#
# connect.commit()
# connect.close()
#
# def APPSESSIONDATABASEMERGING(file, appSessionArray):
# fileExisting(file)
#
# connect = sqlite3.connect(file)
# cursor = connect.cursor().execute("CREATE TABLE APPSESSIONTABLE(ID TEXT, CLASSNAME TEXT, STARTTIME TEXT, ENDTIME TEXT, SESSIONTIME TEXT, SESSIONTYPE TEXT, SESSIONPACKAGE TEXT)")
#
# for row in appSessionArray:
# cursor.execute("""INSERT INTO APPSESSIONTABLE(ID, CLASSNAME, STARTTIME, ENDTIME, SESSIONTIME, SESSIONTYPE, SESSIONPACKAGE) VALUES(?, ?, ?, ?, ?, ?, ?);""",
# (row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
#
# connect.commit()
# connect.close()
#
# def APPSESSIONCNTDATABASEMERGING(file ,appSessionArray):
# fileExisting(file)
#
# connect = sqlite3.connect(file)
# cursor = connect.cursor().execute(
# "CREATE TABLE APPSESSIONTABLE(ID TEXT, CLASSNAME TEXT, USAGESTARTTIME TEXT, USAGEENDTIME TEXT, SESSIONPACKAGE TEXT, SESSIONSTARTTIME TEXT, SESSIONENDTIME TEXT, SHORTCOUNT, LONGCOUNT, SCROLLCOUNT, KEYCOUNT)")
#
# for row in appSessionArray:
# cursor.execute(
# """INSERT INTO APPSESSIONTABLE(ID, CLASSNAME, USAGESTARTTIME, USAGEENDTIME, SESSIONPACKAGE, SESSIONSTARTTIME, SESSIONENDTIME, SHORTCOUNT, LONGCOUNT, SCROLLCOUNT, KEYCOUNT) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,?);""",
# (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
#
# connect.commit()
# connect.close()
#
#
# def AGGREGATIONDATABASEMAKING(file, aggregationArray):
# fileExisting(file)
#
# connect = sqlite3.connect(file)
# cursor = connect.cursor().execute(
# "CREATE TABLE AGGREGATIONTABLE(ID TEXT, " +
# "CLASS_NAME TEXT, CLASS_ENTRANCE TEXT, CLASS_EXIT TEXT, " +
# "USAGE_START TEXT, USAGE_EXIT TEXT, USAGE_ELAPSED TEXT, " +
# "SESSION_NAME TEXT, SESSION_START, SESSION_EXIT, SESSION_ELAPSED, SHORT, LONG, SCROLL, KEY)")
#
# for row in aggregationArray:
# cursor.execute(
# """INSERT INTO AGGREGATIONTABLE(ID, CLASS_NAME, CLASS_ENTRANCE, CLASS_EXIT, USAGE_START, USAGE_EXIT, USAGE_ELAPSED,
# SESSION_NAME, SESSION_START, SESSION_EXIT, SESSION_ELAPSED, SHORT, LONG, SCROLL, KEY) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);""",
# (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
#
# connect.commit()
# connect.close()
# def activityTableMerging(path, activityArray):
# fileExisting(path, "ACTIVITYDATABASE.db")
#
# connect = sqlite3.connect(path + "\\ACTIVITYDATABASE.db")
# cursor = connect.cursor().execute("CREATE TABLE ACTIVITYTABLE(TIMESTAMP TEXT, VEHICLE INTEGER, BICYCLE INTEGER, FOOT INTEGER, RUNNING INTEGER, STILL INTEGER, WALKING INTEGER, UNKNOWN INTEGER)")
# for row in activityArray:
# cursor.execute("""INSERT INTO ACTIVITYTABLE(TIMESTAMP, VEHICLE, BICYCLE, FOOT, RUNNING, STILL, WALKING, UNKNOWN) VALUES(?, ?, ?, ?, ?, ?, ?, ?);""", (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
# connect.commit()
# connect.close()
#
# def specificActivityTableMerging(path, name, timeArray, activityArray):
# fileExisting(path, name + ".db")
#
# connect = sqlite3.connect(path + "\\" + name + ".db")
# cursor = connect.cursor().execute("CREATE TABLE ACTIVITYTABLE(TIME TEXT, ACTIVITY TEXT)")
# for index in range(0, len(activityArray)):
# cursor.execute("""INSERT INTO ACTIVITYTABLE(TIME, ACTIVITY) VALUES(?, ?);""", (timeArray[index], activityArray[index]))
# connect.commit()
# connect.close()
#
# def specificAppTableMerging(path, name, startTimeArray, endTimeArray, elapsedTimeArray):
# fileExisting(path, name + ".db")
#
# connect = sqlite3.connect(path + "\\" + name + ".db")
# cursor = connect.cursor().execute("CREATE TABLE APPTABLE(STARTTIME TEXT, ENDTIME TEXT, ELAPSEDTIME TEXT)")
# for index in range(0, len(startTimeArray)):
# cursor.execute("""INSERT INTO APPTABLE(STARTTIME, ENDTIME, ELAPSEDTIME) VALUES(?, ?, ?);""", (startTimeArray[index][0], endTimeArray[index][0], str(elapsedTimeArray[index])))
# connect.commit()
# connect.close()
# def LOGDATABASEMAKING(file, logArray):
# fileExisting(file)
#
# connect = sqlite3.connect(file)
# cursor = connect.cursor().execute("CREATE TABLE LOGTABLE(TIMESTAMP TEXT, CALLSMS TEXT, NUMBER TEXT, TYPE TEXT, DURATIONBODY TEXT, READ TEXT)")
#
# for row in logArray:
# cursor.execute("""INSERT INTO LOGTABLE(TIMESTAMP, CALLSMS, NUMBER, TYPE, DURATIONBODY, READ) VALUES(?, ?, ?, ?, ?, ?);""", (row[0], row[1], row[2], row[3], row[4], row[5]))
#
# connect.commit()
# connect.close()
# def specificLogTableMerging(path, name, logArray):
# fileExisting(path, name + ".db")
#
# connect = sqlite3.connect(path + "\\" + name + ".db")
# cursor = connect.cursor().execute("CREATE TABLE LOGTABLE(TIMESTAMP TEXT, CALLSMS TEXT, NUMBER TEXT, TYPE TEXT, DURATIONBODY TEXT, READ TEXT)")
# for row in logArray:
# cursor.execute("""INSERT INTO LOGTABLE(TIMESTAMP, CALLSMS, NUMBER, TYPE, DURATIONBODY, READ) VALUES(?, ?, ?, ?, ?, ?);""", (row[0], row[1], row[2], row[3], row[4], row[5]))
# connect.commit()
# connect.close()