/
run.py
88 lines (71 loc) · 3.36 KB
/
run.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
import DataHandler
import Algorithms
import ChartHandler
import argparse
import io
NO_ARGUMENT = "__none__"
def createAlgoSelect():
userClasses = dict([(name, cls) for name, cls in Algorithms.__dict__.items()
if name in Algorithms.usernames])
script = "window.addEventListener('DOMContentLoaded', function(){\ninnerHTML = \""
text = ""
for username in userClasses:
text += "<optgroup label=\'" + username + "\'>"
usermod = userClasses[username]
for algo,desc in zip(usermod.classNames, usermod.classDescription):
text += "<option value = \'" + algo + "\' data-desc = \'" + desc +"\'>" + algo + "</option>"
text += "</optgroup>"
script += text + "\";\nvar algoselect = document.querySelector('#algoselect');\nalgoselect.innerHTML = innerHTML;\n});"
out_file_path = "algoselect.js"
with io.open(out_file_path, mode='w') as f:
f.write(script)
def runAlgo(username, algoName, algoClass, countriesData):
algo = algoClass(countriesData.countries, countriesData.countryCodes)
algo.fit(countriesData.getTrainData())
trainPred = algo.predict()
trainloss, valloss = countriesData.evaluate(trainPred, True)
algo.fit(countriesData.getPresentData())
pred = algo.predict()
loss = countriesData.evaluate(pred, False)
print(sum(loss)/len(loss))
ChartHandler.makePlot(username, algoName, countriesData.countries, countriesData.countryCodes,
countriesData.getPresentData(), pred)
DataHandler.saveLoss(username, algoName, countriesData.countries, trainloss, valloss, loss)
DataHandler.savePredictions(username, algoName, countriesData.countries, pred)
def main(username, algos):
userClasses = dict([(name, cls) for name, cls in Algorithms.__dict__.items()
if name in Algorithms.usernames])
countriesData = DataHandler.GetData()
if username == NO_ARGUMENT:
createAlgoSelect()
for username in userClasses:
usermod = userClasses[username]
algomod = dict([(name, cls) for name, cls in usermod.__dict__.items() if name in usermod.classNames])
for algo in algomod:
runAlgo(username, algo, algomod[algo], countriesData)
elif username in userClasses:
usermod = userClasses[username]
algomod = dict([(name, cls) for name, cls in usermod.__dict__.items() if name in usermod.classNames])
if algos[0] == NO_ARGUMENT:
for algo in algomod:
runAlgo(username, algo, algomod[algo], countriesData)
else:
for algo in algos:
if algo in algomod:
runAlgo(username, algo, algomod[algo], countriesData)
else:
print(algo + " algorithm not found!")
else:
print(username + " module not found!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create Covid19 prediction graphs for one or all Algorithms")
parser.add_argument('-u', nargs=1, default=NO_ARGUMENT, type = str, help="Your username")
parser.add_argument('-a', nargs='+', default=NO_ARGUMENT, help="List of algorithms")
args = parser.parse_args()
username = args.u
algos = args.a
if type(username) == list:
username = username[0]
if type(algos) == str:
algos = [algos]
main(username, algos)