-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_knotinfo.py
205 lines (167 loc) · 6.39 KB
/
extract_knotinfo.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
import xlrd
import json
def dump(o, fout):
json.dump(o, fout, separators=(',', ':'))
def read_poly(s):
"""Puts the (Laurent) poly in polynomial vector notation "{mindeg, maxdeg, coeffs...}"
into the form [mindeg, c0, c1, ..., cn]"""
if s == "1":
return [0,1]
assert(s[0] == "{" and s[-1] == "}")
ps = [int(p) for p in s[1:-1].split(",")]
return [ps[0]] + ps[2:]
def convert_conway(p):
"""Take a conway polynomial in z^2 and make it be in z. """
p2 = [p[0]*2] + [0]*(2*(len(p)-1)-1)
for i,c in enumerate(p[1:]):
p2[1+2*i] = c
return normalize_conway(p2)
def convert_jones(p):
"""Take a jones polynomial for a knot (which is in t) and make it be in x=t^2."""
p2 = [p[0]*2] + [0]*(2*(len(p)-1)-1)
for i,c in enumerate(p[1:]):
p2[1+2*i] = c
return p2
def normalize_conway(p):
cs = p[1:]
minexp = p[0]
while cs and cs[0] == 0:
cs = cs[1:]
minexp += 1
return [minexp] + cs
def is_amphicheiral(symmetry_type):
symmetry_type = symmetry_type.strip()
if symmetry_type in ("", "fully amphicheiral", "negative amphicheiral", "positive amphicheiral"):
return True
elif symmetry_type in ("reversible", "chiral"):
return False
else:
print(symmetry_type)
raise Exception(symmetry_type)
def cut_katlas(url):
if not url.startswith("http://katlas.math.toronto.edu/wiki/"):
raise Exception(url)
return url[len("http://katlas.math.toronto.edu/wiki/"):]
data = []
print("Processing knotinfo")
with xlrd.open_workbook("data/knotinfo_data_complete.xls") as book:
sheet = book.sheet_by_index(0)
cols = {}
for i, cell in enumerate(sheet.row(0)):
cols[cell.value] = i
for row_idx in range(2, sheet.nrows):
def get(colname):
return sheet.cell(row_idx, cols[colname])
entry={}
entry['name'] = get('name').value
if get('knot_atlas_anon').value:
entry['katlas'] = cut_katlas(get('knot_atlas_anon').value)
else:
entry['katlas'] = ""
entry['components'] = 1
entry['crossing_number'] = int(get('crossing_number').value)
entry['genus'] = int(get('three_genus').value)
entry['signature'] = int(get('signature').value)
entry['conway'] = convert_conway(read_poly(get('conway_polynomial_vector').value))
entry['jones'] = convert_jones(read_poly(get('jones_polynomial_vector').value))
entry['turaev_genus'] = json.loads(get('turaev_genus').value) # might be [lo, hi]
entry['bridge_number'] = int(get('bridge_index').value)
if get('pd_notation').value == "":
entry['pd'] = [[1, 1]]
else:
entry['pd'] = json.loads(get('pd_notation').value)
data.append(entry)
# if not is_amphicheiral(get('symmetry_type').value):
# entry = entry.copy()
# entry['name'] = 'm' + entry['name']
# entry['signature'] = -entry['signature']
# entry['jones'] = poly_reverse(entry['jones'])
# data.append(entry)
print("Processing linkinfo")
with open("data/linkinfo_conway_coeffs.json") as fin:
conway = json.load(fin)
with xlrd.open_workbook("data/linkinfo_data_complete_resave.xls") as book:
sheet = book.sheet_by_index(0)
cols = {}
for i, cell in enumerate(sheet.row(0)):
cols[cell.value] = i
for row_idx in range(2, sheet.nrows):
def get(colname):
return sheet.cell(row_idx, cols[colname])
entry={}
entry['name'] = get('name').value
if not entry['name']:
break
if get('knot_atlas_anon').value:
entry['katlas'] = cut_katlas(get('knot_atlas_anon').value)
entry['components'] = int(get('components').value)
entry['pd'] = json.loads(get('pd_notation_vector').value.replace("{","[").replace("}","]"))
entry['crossing_number'] = int(get('crossing_number').value)
entry['signature'] = int(get('signature').value)
entry['conway'] = normalize_conway([0] + conway[entry['name']])
entry['jones'] = read_poly(get('jones_polynomial_vector').value)
data.append(entry)
print("Writing data files")
# Lazy loader description file
f_defs = open("build/knotinfo.js", "w")
f_defs.write("// Generated by extract_knotinfo.py from KnotInfo database\n")
f_defs.write("// Descriptions of available data files\n")
def write_knots(filename, crossings, components, properties):
f_defs.write("provides_knot_data(")
dump(filename, f_defs)
f_defs.write(", ")
dump(components, f_defs)
f_defs.write(", ")
dump(crossings, f_defs)
f_defs.write(", ")
dump(properties, f_defs)
f_defs.write(");\n")
fout = open("build/" + filename, "w")
fout.write("// generated by extract_knotinfo.py from KnotInfo database\n")
fout.write("add_knot_data(")
dump(properties, fout)
fout.write(",[\n")
first = True
for entry in data:
if entry['crossing_number'] in crossings and entry['components'] in components:
if not first:
fout.write(",\n")
first = False
dump(entry['name'], fout)
for p in properties:
fout.write(",")
dump(entry[p], fout)
fout.write("]);\n")
fout.write("loaded_knot_data(")
dump(components, fout)
fout.write(", ")
dump(crossings, fout)
fout.write(", ")
dump(properties, fout)
fout.write(");\n")
fout.close()
properties = ['crossing_number',
'components',
'katlas',
'genus',
'signature',
'conway',
'jones',
'turaev_genus',
'bridge_number']
write_knots("knotinfo_0-10.js", [0,1,2,3,4,5,6,7,8,9,10], [1], properties);
write_knots("knotinfo_11-12.js", [11,12], [1], properties)
write_knots("knotinfo_0-10_pd.js", [0,1,2,3,4,5,6,7,8,9,10], [1], ["pd"])
write_knots("knotinfo_11-12_pd.js", [11,12], [1], ["pd"])
properties = ['crossing_number',
'components',
'katlas',
'signature',
'conway',
'jones']
write_knots("linkinfo_0-10.js", [0,1,2,3,4,5,6,7,8,9,10], [2,3,4,5], properties);
write_knots("linkinfo_11.js", [11], [2,3,4,5], properties);
write_knots("linkinfo_0-10_pd.js", [0,1,2,3,4,5,6,7,8,9,10], [2,3,4,5], ["pd"]);
write_knots("linkinfo_11_pd.js", [11], [2,3,4,5], ["pd"]);
f_defs.close()
print("Done")