Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/hypergm/import_hgmfam.py
1455 views
1
#!/usr/local/bin/sage -python
2
# -*- coding: utf-8 -*-
3
4
import sys
5
import json
6
import os
7
import gzip
8
import re
9
from collections import Counter as mset
10
from sage.all import euler_phi, valuation
11
12
sys.path.append('/home/jj/data/lmfdb/data_mgt/utilities')
13
pw_path = "../../"
14
pw_filename = "xyzzy"
15
password = open(pw_path+pw_filename, "r").readlines()[0].strip()
16
17
#from pymongo import *
18
19
from pymongo.mongo_client import MongoClient
20
C= MongoClient(port=37010, host='lmfdb-ib')
21
C['hgm'].authenticate('editor', password)
22
23
hgm = C.hgm.newfamilies
24
hgm = C.hgm.families # now this is the new one
25
26
saving = True
27
28
count = 0
29
30
#hgm.drop()
31
32
# New game plan:
33
# - make a list of python dictionaries
34
# - insert them all in one shot!
35
36
newrecs = []
37
38
def list2string(li):
39
li2 = [str(x) for x in li]
40
return ','.join(li2)
41
42
def fixname(s):
43
a = re.sub(r'C(\d+)', r'C_{\1}',str(s))
44
a = re.sub(r'S(\d+)', r'S_{\1}',a)
45
a = re.sub(r'A(\d+)', r'A_{\1}',a)
46
a = re.sub(r'D(\d+)', r'D_{\1}',a)
47
return a
48
49
def pnotp(a,p):
50
pv = p**valuation(a,p)
51
return [pv, a/pv]
52
53
def modvec(A,p):
54
Ap = []
55
for a in A:
56
ap, aprime = pnotp(a,p)
57
Ap.extend([ap]*euler_phi(aprime))
58
Ap.sort(reverse=True)
59
return Ap
60
61
def notpvec(A,p):
62
Ap = []
63
for a in A:
64
ap, aprime = pnotp(a,p)
65
Ap.extend([aprime]*euler_phi(ap))
66
Ap.sort(reverse=True)
67
return Ap
68
69
def orderAB(A,B):
70
if 1 in B:
71
return [A,B]
72
if 1 in A:
73
return [B,A]
74
if A[-1]<B[-1]:
75
return [A,B]
76
return [B,A]
77
78
def killdup(A,B):
79
aa=mset(A)
80
bb=mset(B)
81
cc=aa & bb
82
aa.subtract(cc)
83
bb.subtract(cc)
84
aa = list(aa.elements())
85
aa.sort(reverse=True)
86
bb = list(bb.elements())
87
bb.sort(reverse=True)
88
cc = list(cc.elements())
89
cc.sort(reverse=True)
90
return([aa,bb,cc])
91
92
def galmunge(gg):
93
if gg[1]==0: # means group not computed
94
return gg
95
gg[1][2] = fixname(gg[1][2])
96
return gg
97
98
def fixsort(gg):
99
if gg[1]==0: # means group not computed
100
return gg
101
for k in range(3):
102
gg[1][3][k] = sorted(gg[1][3][k], reverse=True)
103
return gg
104
105
def modpair(A,B,p):
106
vecs= [modvec(A,p),modvec(B,p)]
107
return vecs
108
109
def do_addrec(F):
110
global newrecs
111
degree, weight, A, B, hodge, imprim, bezout, snf, det, gal2, gal3, gal5, gal7 = F
112
A.sort(reverse=True)
113
B.sort(reverse=True)
114
A,B = orderAB(A,B)
115
Astr = '.'.join([str(x) for x in A])
116
Bstr = '.'.join([str(x) for x in B])
117
label = "A%s_B%s" % (Astr, Bstr)
118
mono = [[2,gal2], [3,gal3], [5,gal5], [7,gal7]]
119
mono = [galmunge(z) for z in mono]
120
mono = [fixsort(z) for z in mono]
121
data = {
122
'label': label,
123
'degree': degree,
124
'weight': weight,
125
'A': list2string(A),
126
'B': list2string(B),
127
'Arev': list2string(B),
128
'Brev': list2string(A),
129
'famhodge': list2string(hodge),
130
'bezout': bezout,
131
'snf': list2string(snf),
132
'imprim': imprim,
133
'det': det,
134
'mono': mono
135
}
136
for p in [2,3,5,7]:
137
mod = modpair(A,B,p)
138
mod = killdup(mod[0],mod[1])
139
data['A'+str(p)] = list2string(mod[0])
140
data['B'+str(p)] = list2string(mod[1])
141
data['C'+str(p)] = list2string(mod[2])
142
mod = modpair(B,A,p)
143
mod = killdup(mod[0],mod[1])
144
data['A'+str(p)+'rev'] = list2string(mod[0])
145
data['B'+str(p)+'rev'] = list2string(mod[1])
146
# We don't search on C reversed
147
mod = [notpvec(A,p),notpvec(B,p)]
148
mod = killdup(mod[0],mod[1])
149
data['Au'+str(p)] = list2string(mod[0])
150
data['Bu'+str(p)] = list2string(mod[1])
151
data['Cu'+str(p)] = list2string(mod[2])
152
data['Au'+str(p)+'rev'] = list2string(mod[1])
153
data['Bu'+str(p)+'rev'] = list2string(mod[0])
154
155
is_new = True
156
for field in hgm.find({'label': label}):
157
is_new = False
158
break
159
160
for k in newrecs:
161
if k['label'] == label:
162
is_new = False
163
break
164
165
if is_new:
166
#print "new family"
167
newrecs.append(data)
168
#else:
169
#print "Have this one"
170
171
for path in sys.argv[1:]:
172
print(path)
173
filename = os.path.basename(path)
174
fn = gzip.open(path) if filename[-3:] == '.gz' else open(path)
175
dat = fn.read().replace('\n', ' ')
176
dat = dat.replace('>',']')
177
dat = dat.replace('<','[')
178
l = json.loads(dat)
179
for motfam in l:
180
do_addrec(motfam)
181
count += 1
182
#print "Count %d"%(count)
183
fn.close()
184
185
hgm.insert_many(newrecs)
186
187
188