-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdatabase.py
185 lines (178 loc) · 5.86 KB
/
database.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
import psycopg2
from config import config
# class for database functions
class Database:
def __init__(self):
conn = None
try:
params = config()
self.conn = psycopg2.connect(**params)
print("Connection success")
except (Exception, psycopg2.DatabaseError) as error:
print('error')
print(error)
# insert one agent into database
def insert_agent(self, data):
""" insert a new agent into the trace table """
sql = """
INSERT INTO agent(
uuid,
age_group,
sex_group,
vaccine_willingness,
incubation_time,
dwelling_time,
recovery_time,
prob_contagion,
mortality_value,
severity_value,
curr_dwelling,
curr_incubation,
curr_recovery,
curr_asymptomatic,
isolated,
isolated_but_inefficient,
test_chance,
in_isolation,
in_distancing,
in_testing,
astep,
tested,
occupying_bed,
cumul_private_value,
cumul_public_value,
employed,
tested_traced,
tracing_delay,
tracing_counter,
vaccinated,
safetymultiplier,
current_effectiveness,
vaccination_day,
vaccine_count,
dosage_eligible,
fully_vaccinated,
variant
) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
try:
self.cur = self.conn.cursor()
self.cur.executemany(sql, data)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
# insert one model into the database
def insert_model(self, data):
""" insert a new model into the experiment table """
sql = """
INSERT INTO experiment(
uuid,
test_cost,
alpha_private,
alpha_public,
fully_vaccinated_count,
prop_initial_infected,
generally_infected,
cumul_vaccine_count,
cumul_test_cost,
total_costs,
vaccination_chance,
vaccination_stage,
vaccine_cost,
day_vaccination_begin,
day_vaccination_end,
effective_period,
effectiveness,
distribution_rate,
vaccine_count,
vaccinated_count,
vaccinated_percent,
vaccine_dosage,
effectiveness_per_dosage,
dwell_15_day,
avg_dwell,
avg_incubation,
repscaling,
prob_contagion_base,
kmob,
rate_inbound,
prob_contagion_places,
prob_asymptomatic,
avg_recovery,
testing_rate,
testing_start,
testing_end,
tracing_start,
tracing_end,
tracing_now,
isolation_rate,
isolation_start,
isolation_end,
after_isolation,
prob_isolation_effective,
distancing,
distancing_start,
distancing_end,
new_agent_num,
new_agent_start,
new_agent_end,
new_agent_age_mean,
new_agent_prop_infected,
vaccination_start,
vaccination_end,
vaccination_now,
prob_severe,
max_bed_available,
bed_count
) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""
try:
self.cur = self.conn.cursor()
self.cur.executemany(sql, data)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
# insert one summary into the database
def insert_summary(self, data):
""" insert a new summary into the summary table """
sql = """
INSERT INTO summary(
uuid,
cumul_priv_value,
cumul_publ_value,
cumul_test_cost,
rt,
employed,
unemployed,
tested,
traced,
cumul_vaccine_cost,
cumul_cost,
step,
n,
isolated,
vaccinated,
vaccines,
v,
data_time,
step_time,
generally_infected,
fully_vaccinated,
vaccine_1,
vaccine_2,
vaccine_willing
) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""
try:
self.cur = self.conn.cursor()
self.cur.executemany(sql, data)
except (Exception, psycopg2.DatabaseError) as error:
print('error')
print(error)
# commit changes to database
def commit(self):
self.conn.commit()
# close connection to database
def close(self):
if self.cur is not None:
self.cur.close()
if self.conn is not None:
self.conn.close()