-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_database.py
132 lines (111 loc) · 3.86 KB
/
build_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
import os
import sqlite3
from itertools import product
from utils import OPTSETS, STRUCTSETS, CALCSETS, PROPS
def build_datasets_table(optsets, calcsets):
with sqlite3.connect("database.sqlite") as conn:
conn.execute("DROP TABLE IF EXISTS datasets")
string = """
CREATE TABLE datasets
(
id integer primary key,
optset varchar,
calcset varchar,
parameters varchar
)
"""
conn.execute(string)
data = list(product(optsets, calcsets))
string = "INSERT INTO datasets(optset, calcset) VALUES (?,?)"
conn.executemany(string, data)
conn.commit()
def build_names_table(names):
with sqlite3.connect("database.sqlite") as conn:
conn.execute("DROP TABLE IF EXISTS names")
string = """
CREATE TABLE names
(
id integer primary key,
name varchar,
structset varchar
)
"""
conn.execute(string)
conn.commit()
string = "INSERT INTO names(name, structset) VALUES (?, ?)"
conn.executemany(string, names)
conn.commit()
def build_data_table(data):
with sqlite3.connect("database.sqlite") as conn:
conn.execute("DROP TABLE IF EXISTS data")
string = """
CREATE TABLE data
(
id integer primary key,
name_id integer,
homo float,
lumo float,
excitation float,
dataset_id integer,
FOREIGN KEY(name_id) REFERENCES names(id),
FOREIGN KEY(dataset_id) REFERENCES datasets(id)
)
"""
conn.execute(string)
conn.commit()
string = """
INSERT INTO data(name_id, homo, lumo, excitation, dataset_id)
VALUES
(
?,
?,
?,
?,
(
SELECT id FROM datasets
WHERE optset=? and calcset=?
)
)
"""
conn.executemany(string, data)
conn.commit()
def export_database(fill_null=False):
names, data = load_data_for_db_insert(OPTSETS, STRUCTSETS, CALCSETS,
fill_null=fill_null)
build_datasets_table(OPTSETS, CALCSETS)
build_names_table(names)
build_data_table(data)
def load_data_for_db_insert(optsets, structsets, calcsets, fill_null=False):
names = {}
for optset in optsets:
for structset in structsets:
for calcset in calcsets:
path = os.path.join(optset, structset, calcset + ".txt")
if not os.path.exists(path):
continue
with open(path, 'r') as f:
for line in f:
name, homo, lumo, gap = line.strip().split()
payload = [float(homo), float(lumo), float(gap)]
try:
names[(name, structset)][
(optset, calcset)] = payload
except KeyError:
names[(name, structset)] = {
(optset, calcset): payload}
sorted_names = sorted(names.keys())
data = []
for i, (name, structset) in enumerate(sorted_names):
for optset in optsets:
for calcset in calcsets:
if names[(name, structset)].get((optset, calcset)) is None and not fill_null:
continue
if fill_null:
temp = names[(name, structset)].get(
(optset, calcset), [None, None, None])
else:
temp = names[(name, structset)].get((optset, calcset))
data.append([i + 1] + temp + [optset, calcset])
return sorted_names, data
if __name__ == "__main__":
export_database(fill_null=False)