-
Notifications
You must be signed in to change notification settings - Fork 1
/
makedatabase.py
executable file
·109 lines (81 loc) · 2.73 KB
/
makedatabase.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
#!/usr/bin/env python
#
# Read a list of polygons from stdin in Castryck format and enter them
# in a database.
import os, sys
from sqlite3 import connect
from multiprocessing import Pool
from betti.polygon import normalize_vertices
from sage.all import Polyhedron, ZZ, walltime
NUMPROCESSES=12
def polygon_values(vertices):
vertices = normalize_vertices(vertices)
vertices_str = ",".join(repr(pt).replace(" ", "") for pt in vertices)
polygon = Polyhedron(vertices)
volume = float(polygon.volume())
points = polygon.integral_points()
num_points = len(points)
num_interior = len([pt for pt in points if polygon.interior_contains(pt)])
length = int(max(x for x, y in vertices))
width = int(max(y for x, y in vertices))
SymmQQ = polygon.restricted_automorphism_group(output="matrixlist")
if len(SymmQQ) == 1:
# Fast path for trivial symmetry group
symm = 1
else:
# Restrict symmetries to symmetries in GL(3, ZZ)
Symm = [s for s in SymmQQ if abs(s.determinant()) == 1 and all(x in ZZ for x in s.list())]
if all(s.determinant() > 0 for s in Symm):
symm = len(Symm)
else:
symm = -len(Symm)
return (vertices_str, len(vertices), volume,
num_points, num_interior, num_points - num_interior,
width, length, symm)
def process_data(t):
counter, vertices = t
v = (counter,) + polygon_values(vertices)
vstr = " ".join(repr(x) for x in v)
t1 = walltime()
sys.stdout.write("({:5.1f}Hz) {}\n".format(counter/float(t1 - t0), vstr))
return v
def raw_data(f):
counter = 0
for line in f:
if '<' not in line:
continue
line = line.replace("<", "(")
line = line.replace(">", ")")
# Drop all characters except these:
line = "".join(c for c in line if c in "[](),-0123456789")
# Drop trailing comma
if line.endswith(","):
line = line[:-1]
counter += 1
yield (counter, eval(line))
def create_database(f, db):
global t0
t0 = walltime()
P = Pool(NUMPROCESSES)
it = P.imap_unordered(process_data, raw_data(f), chunksize=100)
con = connect(db)
con.execute('''
CREATE TABLE polygons(
rowid INTEGER PRIMARY KEY,
vertices TEXT,
num_vertices INTEGER,
volume REAL,
num_points INTEGER,
num_interior INTEGER,
num_border INTEGER,
width INTEGER,
length INTEGER,
symm INTEGER)
''')
con.executemany('INSERT INTO polygons VALUES (?,?,?,?,?,?,?,?,?,?)', it)
con.commit()
con.close()
db = sys.argv[1]
if os.path.exists(db):
os.remove(db)
create_database(sys.stdin, db)