Path: blob/main/scripts/hypergm/import_hgmfam.py
1455 views
#!/usr/local/bin/sage -python1# -*- coding: utf-8 -*-23import sys4import json5import os6import gzip7import re8from collections import Counter as mset9from sage.all import euler_phi, valuation1011sys.path.append('/home/jj/data/lmfdb/data_mgt/utilities')12pw_path = "../../"13pw_filename = "xyzzy"14password = open(pw_path+pw_filename, "r").readlines()[0].strip()1516#from pymongo import *1718from pymongo.mongo_client import MongoClient19C= MongoClient(port=37010, host='lmfdb-ib')20C['hgm'].authenticate('editor', password)2122hgm = C.hgm.newfamilies23hgm = C.hgm.families # now this is the new one2425saving = True2627count = 02829#hgm.drop()3031# New game plan:32# - make a list of python dictionaries33# - insert them all in one shot!3435newrecs = []3637def list2string(li):38li2 = [str(x) for x in li]39return ','.join(li2)4041def fixname(s):42a = re.sub(r'C(\d+)', r'C_{\1}',str(s))43a = re.sub(r'S(\d+)', r'S_{\1}',a)44a = re.sub(r'A(\d+)', r'A_{\1}',a)45a = re.sub(r'D(\d+)', r'D_{\1}',a)46return a4748def pnotp(a,p):49pv = p**valuation(a,p)50return [pv, a/pv]5152def modvec(A,p):53Ap = []54for a in A:55ap, aprime = pnotp(a,p)56Ap.extend([ap]*euler_phi(aprime))57Ap.sort(reverse=True)58return Ap5960def notpvec(A,p):61Ap = []62for a in A:63ap, aprime = pnotp(a,p)64Ap.extend([aprime]*euler_phi(ap))65Ap.sort(reverse=True)66return Ap6768def orderAB(A,B):69if 1 in B:70return [A,B]71if 1 in A:72return [B,A]73if A[-1]<B[-1]:74return [A,B]75return [B,A]7677def killdup(A,B):78aa=mset(A)79bb=mset(B)80cc=aa & bb81aa.subtract(cc)82bb.subtract(cc)83aa = list(aa.elements())84aa.sort(reverse=True)85bb = list(bb.elements())86bb.sort(reverse=True)87cc = list(cc.elements())88cc.sort(reverse=True)89return([aa,bb,cc])9091def galmunge(gg):92if gg[1]==0: # means group not computed93return gg94gg[1][2] = fixname(gg[1][2])95return gg9697def fixsort(gg):98if gg[1]==0: # means group not computed99return gg100for k in range(3):101gg[1][3][k] = sorted(gg[1][3][k], reverse=True)102return gg103104def modpair(A,B,p):105vecs= [modvec(A,p),modvec(B,p)]106return vecs107108def do_addrec(F):109global newrecs110degree, weight, A, B, hodge, imprim, bezout, snf, det, gal2, gal3, gal5, gal7 = F111A.sort(reverse=True)112B.sort(reverse=True)113A,B = orderAB(A,B)114Astr = '.'.join([str(x) for x in A])115Bstr = '.'.join([str(x) for x in B])116label = "A%s_B%s" % (Astr, Bstr)117mono = [[2,gal2], [3,gal3], [5,gal5], [7,gal7]]118mono = [galmunge(z) for z in mono]119mono = [fixsort(z) for z in mono]120data = {121'label': label,122'degree': degree,123'weight': weight,124'A': list2string(A),125'B': list2string(B),126'Arev': list2string(B),127'Brev': list2string(A),128'famhodge': list2string(hodge),129'bezout': bezout,130'snf': list2string(snf),131'imprim': imprim,132'det': det,133'mono': mono134}135for p in [2,3,5,7]:136mod = modpair(A,B,p)137mod = killdup(mod[0],mod[1])138data['A'+str(p)] = list2string(mod[0])139data['B'+str(p)] = list2string(mod[1])140data['C'+str(p)] = list2string(mod[2])141mod = modpair(B,A,p)142mod = killdup(mod[0],mod[1])143data['A'+str(p)+'rev'] = list2string(mod[0])144data['B'+str(p)+'rev'] = list2string(mod[1])145# We don't search on C reversed146mod = [notpvec(A,p),notpvec(B,p)]147mod = killdup(mod[0],mod[1])148data['Au'+str(p)] = list2string(mod[0])149data['Bu'+str(p)] = list2string(mod[1])150data['Cu'+str(p)] = list2string(mod[2])151data['Au'+str(p)+'rev'] = list2string(mod[1])152data['Bu'+str(p)+'rev'] = list2string(mod[0])153154is_new = True155for field in hgm.find({'label': label}):156is_new = False157break158159for k in newrecs:160if k['label'] == label:161is_new = False162break163164if is_new:165#print "new family"166newrecs.append(data)167#else:168#print "Have this one"169170for path in sys.argv[1:]:171print(path)172filename = os.path.basename(path)173fn = gzip.open(path) if filename[-3:] == '.gz' else open(path)174dat = fn.read().replace('\n', ' ')175dat = dat.replace('>',']')176dat = dat.replace('<','[')177l = json.loads(dat)178for motfam in l:179do_addrec(motfam)180count += 1181#print "Count %d"%(count)182fn.close()183184hgm.insert_many(newrecs)185186187188