Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/artin_representations/import_art_nf.py
1128 views
1
#!/usr/local/bin/sage -python
2
3
# This version writes the data to a file, deletes all records from the database,
4
# then reloads from the files.
5
import sys
6
import os
7
import re
8
import json
9
import numpy
10
11
# find lmfdb and the top of the tree
12
mypath = os.path.realpath(__file__)
13
while os.path.basename(mypath) != 'lmfdb':
14
mypath = os.path.dirname(mypath)
15
# now move up one more time...
16
#mypath = os.path.dirname(mypath)
17
sys.path.append(mypath)
18
19
from lmfdb import db
20
from psycodict.encoding import copy_dumps
21
22
rep=db.artin_reps
23
nfgal=db.artin_field_data
24
25
count = 0
26
27
nottest = False
28
nottest = True
29
30
#utilities
31
32
# turn poly coefs into a string
33
def makels(li):
34
li2 = [str(x) for x in li]
35
return ','.join(li2)
36
37
maxint = (2**63)-1
38
39
def int_or_string(a):
40
if abs(a)< maxint:
41
return a
42
return str(a)
43
44
def fix_local_factors(gconj):
45
gconj['LocalFactors'] = [ [ [int_or_string(a) for a in b ] for b in c] for c in gconj['LocalFactors']]
46
return gconj
47
48
# Main programs
49
50
# There are two parts since we need to deal with two files/databases
51
# The two functions below take our for one entry as a dictionary, and reformats
52
# the dictionary
53
54
outrecs = []
55
56
def artrepload(l):
57
global count
58
global outrecs
59
l['Conductor'] = int(l['Conductor'])
60
l['GaloisConjugates'] = [fix_local_factors(z) for z in l['GaloisConjugates']]
61
sortord = numpy.argsort([z['GalOrbIndex'] for z in l['GaloisConjugates']])
62
l['GaloisConjugates'] = [l['GaloisConjugates'][z] for z in sortord]
63
assert [z['GalOrbIndex'] for z in l['GaloisConjugates']] == [u for u in range(1,len(l['GaloisConjugates'])+1)]
64
# Extract containing representation from the label
65
cont = l['Baselabel'].split('.')[2]
66
l['Container'] = cont
67
for s in ['BadPrimes', 'HardPrimes']:
68
l[s] = [int(z) for z in l[s]]
69
l['Galn'] = l['Galois_nt'][0]
70
l['Galt'] = l['Galois_nt'][1]
71
l['GaloisLabel'] = "%sT%s"%(str(l['Galois_nt'][0]),str(l['Galois_nt'][1]))
72
del l['Galois_nt']
73
l['GalConjSigns'] = [z['Sign'] for z in l['GaloisConjugates']]
74
l['Dets'] = [z['Det'] for z in l['GaloisConjugates']]
75
for j in range(len(l['GaloisConjugates'])):
76
del l['GaloisConjugates'][j]['Det']
77
aproj = l['Proj']
78
l['Proj_GAP'] = aproj[0]
79
l['Proj_nTj'] = aproj[1]
80
l['Proj_Polynomial'] = aproj[2]
81
del l['Proj']
82
chival = int(l['Chi_of_complex'])
83
dim = int(l['Dim'])
84
minusones = (dim - chival)/2
85
iseven = (minusones % 2) == 0
86
l['Is_Even'] = iseven
87
#print str(l)
88
count +=1
89
outrecs.append(l)
90
if count % 10000==0:
91
print("Count %s" % count)
92
return
93
94
def nfgalload(l):
95
global count
96
global outrecs
97
98
artreps=l['ArtinReps']
99
artreps=[{'Baselabel': z[0][0], 'GalConj': z[0][1], 'CharacterField': z[1],
100
'Character': z[2]} for z in artreps]
101
l['ArtinReps']=artreps
102
l['Size'] = int(l['Size'])
103
outrecs.append(l)
104
count +=1
105
if count % 10000==0:
106
print("Count %s" % count)
107
return
108
109
def strx(val, k):
110
if k == 'Algorithm':
111
return '"'+str(val)+'"'
112
if k == 'Baselabel':
113
return '"'+str(val)+'"'
114
return str(val)
115
116
def fixdict(d):
117
kys = d.keys()
118
start = ['"'+str(k)+'": '+strx(d[k],k) for k in kys]
119
return "{"+','.join(start)+"}"
120
121
def fixlist(d):
122
return [str(k) for k in d]
123
124
reloadme = []
125
# processing file names
126
for path in sys.argv[1:]:
127
print(path)
128
count = 0
129
outrecs = []
130
filename = os.path.basename(path)
131
fn = open(path)
132
if re.match(r'^nfgal', filename):
133
case = 'nfgal'
134
if re.match(r'^art', filename):
135
case = 'art rep'
136
for line in fn.readlines():
137
line = line.strip()
138
if re.match(r'\S',line):
139
l = json.loads(line)
140
if case == 'nfgal':
141
nfgalload(l)
142
if case == 'art rep':
143
artrepload(l)
144
# We have loaded the file, now dump it
145
if outrecs:
146
if case == 'nfgal':
147
fnout = open("nfgal.dump", "w")
148
cols = nfgal.col_type
149
del cols['id']
150
head1 = [str(z) for z in cols.keys()]
151
fnout.write('|'.join(head1)+"\n")
152
fnout.write('|'.join([str(cols[z]) for z in head1])+"\n\n")
153
for ent in outrecs:
154
for kk in head1:
155
if isinstance(ent[kk], str):
156
ent[kk] = str(ent[kk])
157
if not isinstance(ent[kk], str):
158
ent[kk] = json.dumps(ent[kk])
159
fnout.write('|'.join([ent[z].replace("'",'"') for z in head1])+'\n')
160
fnout.close()
161
reloadme.append('nfgal')
162
if case == 'art rep':
163
fnout = open("art.dump", "w")
164
cols = rep.col_type
165
del cols['id']
166
head1 = [str(z) for z in cols.keys()]
167
fnout.write('|'.join(head1)+"\n")
168
fnout.write('|'.join([str(cols[z]) for z in head1])+"\n\n")
169
for ent in outrecs:
170
for kk in head1:
171
if isinstance(ent[kk], str):
172
ent[kk] = str(ent[kk])
173
if kk == 'Dets':
174
ent[kk] = copy_dumps(ent[kk], 'text[]', recursing=False)
175
elif kk in ['Proj_GAP', 'Proj_nTj', 'Proj_Polynomial']:
176
ent[kk] = copy_dumps(ent[kk], 'int[]', recursing=False)
177
elif not isinstance(ent[kk], str):
178
ent[kk] = json.dumps(ent[kk])
179
fnout.write('|'.join([ent[z] for z in head1])+'\n')
180
fnout.close()
181
reloadme.append('art')
182
print("%s entries" % count)
183
fn.close()
184
185
if nottest:
186
for k in reloadme:
187
if k == 'nfgal':
188
nfgal.reload('nfgal.dump', sep='|')
189
if k == 'art':
190
rep.reload('art.dump', sep='|')
191
192