Path: blob/main/scripts/artin_representations/import_art_nf.py
1128 views
#!/usr/local/bin/sage -python12# This version writes the data to a file, deletes all records from the database,3# then reloads from the files.4import sys5import os6import re7import json8import numpy910# find lmfdb and the top of the tree11mypath = os.path.realpath(__file__)12while os.path.basename(mypath) != 'lmfdb':13mypath = os.path.dirname(mypath)14# now move up one more time...15#mypath = os.path.dirname(mypath)16sys.path.append(mypath)1718from lmfdb import db19from psycodict.encoding import copy_dumps2021rep=db.artin_reps22nfgal=db.artin_field_data2324count = 02526nottest = False27nottest = True2829#utilities3031# turn poly coefs into a string32def makels(li):33li2 = [str(x) for x in li]34return ','.join(li2)3536maxint = (2**63)-13738def int_or_string(a):39if abs(a)< maxint:40return a41return str(a)4243def fix_local_factors(gconj):44gconj['LocalFactors'] = [ [ [int_or_string(a) for a in b ] for b in c] for c in gconj['LocalFactors']]45return gconj4647# Main programs4849# There are two parts since we need to deal with two files/databases50# The two functions below take our for one entry as a dictionary, and reformats51# the dictionary5253outrecs = []5455def artrepload(l):56global count57global outrecs58l['Conductor'] = int(l['Conductor'])59l['GaloisConjugates'] = [fix_local_factors(z) for z in l['GaloisConjugates']]60sortord = numpy.argsort([z['GalOrbIndex'] for z in l['GaloisConjugates']])61l['GaloisConjugates'] = [l['GaloisConjugates'][z] for z in sortord]62assert [z['GalOrbIndex'] for z in l['GaloisConjugates']] == [u for u in range(1,len(l['GaloisConjugates'])+1)]63# Extract containing representation from the label64cont = l['Baselabel'].split('.')[2]65l['Container'] = cont66for s in ['BadPrimes', 'HardPrimes']:67l[s] = [int(z) for z in l[s]]68l['Galn'] = l['Galois_nt'][0]69l['Galt'] = l['Galois_nt'][1]70l['GaloisLabel'] = "%sT%s"%(str(l['Galois_nt'][0]),str(l['Galois_nt'][1]))71del l['Galois_nt']72l['GalConjSigns'] = [z['Sign'] for z in l['GaloisConjugates']]73l['Dets'] = [z['Det'] for z in l['GaloisConjugates']]74for j in range(len(l['GaloisConjugates'])):75del l['GaloisConjugates'][j]['Det']76aproj = l['Proj']77l['Proj_GAP'] = aproj[0]78l['Proj_nTj'] = aproj[1]79l['Proj_Polynomial'] = aproj[2]80del l['Proj']81chival = int(l['Chi_of_complex'])82dim = int(l['Dim'])83minusones = (dim - chival)/284iseven = (minusones % 2) == 085l['Is_Even'] = iseven86#print str(l)87count +=188outrecs.append(l)89if count % 10000==0:90print("Count %s" % count)91return9293def nfgalload(l):94global count95global outrecs9697artreps=l['ArtinReps']98artreps=[{'Baselabel': z[0][0], 'GalConj': z[0][1], 'CharacterField': z[1],99'Character': z[2]} for z in artreps]100l['ArtinReps']=artreps101l['Size'] = int(l['Size'])102outrecs.append(l)103count +=1104if count % 10000==0:105print("Count %s" % count)106return107108def strx(val, k):109if k == 'Algorithm':110return '"'+str(val)+'"'111if k == 'Baselabel':112return '"'+str(val)+'"'113return str(val)114115def fixdict(d):116kys = d.keys()117start = ['"'+str(k)+'": '+strx(d[k],k) for k in kys]118return "{"+','.join(start)+"}"119120def fixlist(d):121return [str(k) for k in d]122123reloadme = []124# processing file names125for path in sys.argv[1:]:126print(path)127count = 0128outrecs = []129filename = os.path.basename(path)130fn = open(path)131if re.match(r'^nfgal', filename):132case = 'nfgal'133if re.match(r'^art', filename):134case = 'art rep'135for line in fn.readlines():136line = line.strip()137if re.match(r'\S',line):138l = json.loads(line)139if case == 'nfgal':140nfgalload(l)141if case == 'art rep':142artrepload(l)143# We have loaded the file, now dump it144if outrecs:145if case == 'nfgal':146fnout = open("nfgal.dump", "w")147cols = nfgal.col_type148del cols['id']149head1 = [str(z) for z in cols.keys()]150fnout.write('|'.join(head1)+"\n")151fnout.write('|'.join([str(cols[z]) for z in head1])+"\n\n")152for ent in outrecs:153for kk in head1:154if isinstance(ent[kk], str):155ent[kk] = str(ent[kk])156if not isinstance(ent[kk], str):157ent[kk] = json.dumps(ent[kk])158fnout.write('|'.join([ent[z].replace("'",'"') for z in head1])+'\n')159fnout.close()160reloadme.append('nfgal')161if case == 'art rep':162fnout = open("art.dump", "w")163cols = rep.col_type164del cols['id']165head1 = [str(z) for z in cols.keys()]166fnout.write('|'.join(head1)+"\n")167fnout.write('|'.join([str(cols[z]) for z in head1])+"\n\n")168for ent in outrecs:169for kk in head1:170if isinstance(ent[kk], str):171ent[kk] = str(ent[kk])172if kk == 'Dets':173ent[kk] = copy_dumps(ent[kk], 'text[]', recursing=False)174elif kk in ['Proj_GAP', 'Proj_nTj', 'Proj_Polynomial']:175ent[kk] = copy_dumps(ent[kk], 'int[]', recursing=False)176elif not isinstance(ent[kk], str):177ent[kk] = json.dumps(ent[kk])178fnout.write('|'.join([ent[z] for z in head1])+'\n')179fnout.close()180reloadme.append('art')181print("%s entries" % count)182fn.close()183184if nottest:185for k in reloadme:186if k == 'nfgal':187nfgal.reload('nfgal.dump', sep='|')188if k == 'art':189rep.reload('art.dump', sep='|')190191192