Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/lattices/lattice.py
1127 views
1
# -*- coding: utf-8 -*-
2
r""" Import integral lattices data.
3
4
Note: This code can be run on all files in any order. Even if you
5
rerun this code on previously entered files, it should have no affect.
6
This code checks if the entry exists, if so returns that and updates
7
with new information. If the entry does not exist then it creates it
8
and returns that.
9
10
"""
11
12
import sys
13
import re
14
import json
15
import os
16
import gzip
17
from sage.all import matrix
18
from lmfdb.lattice.isom import isom
19
from lmfdb.base import getDBConnection
20
21
C= getDBConnection()
22
import yaml
23
pw_dict = yaml.load(open(os.path.join(os.getcwd(), "passwords.yaml")))
24
username = pw_dict['data']['username']
25
password = pw_dict['data']['password']
26
C['Lattices'].authenticate('editor', password)
27
lat = C.Lattices.lat
28
29
saving = True
30
31
def sd(f):
32
for k in f.keys():
33
print('%s ---> %s'%(k, f[k]))
34
35
def makels(li):
36
li2 = [str(x) for x in li]
37
return ','.join(li2)
38
39
def string2list(s):
40
s = str(s)
41
if s=='': return []
42
return [int(a) for a in s.split(',')]
43
44
def base_label(dimension,determinant,level,class_number):
45
return ".".join([str(dimension),str(determinant),str(level),str(class_number)])
46
47
def last_label(base_label, n):
48
return ".".join([str(base_label),str(n)])
49
50
# The following create_index command checks if there is an index on
51
# label, dimension, determinant and level.
52
53
lat.create_index('label')
54
lat.create_index('dim')
55
lat.create_index('det')
56
lat.create_index('level')
57
lat.create_index('aut')
58
lat.create_index('class_number')
59
60
print("finished indices")
61
62
63
## Main importing function
64
65
label_dict={}
66
67
def label_lookup(base_label):
68
if base_label in label_dict:
69
n=label_dict[base_label]+1
70
label_dict[base_label]=n
71
return n
72
label_dict[base_label]=1
73
return 1
74
75
def do_import(ll):
76
dim,det,level,gram,density,hermite,minimum,kissing,shortest,aut,theta_series,class_number,genus_reps,name,comments = ll
77
mykeys = ['dim','det','level','gram','density','hermite', 'minimum','kissing','shortest','aut','theta_series','class_number','genus_reps','name','comments']
78
data = {}
79
for j in range(len(mykeys)):
80
data[mykeys[j]] = ll[j]
81
82
blabel = base_label(data['dim'],data['det'],data['level'], data['class_number'])
83
data['base_label'] = blabel
84
data['index'] = label_lookup(blabel)
85
label= last_label(blabel, data['index'])
86
data['label'] = label
87
88
lattice = lat.find_one({'label': label})
89
90
if lattice is None:
91
print("new lattice")
92
print("***********")
93
print("check for isometries...")
94
A = data['gram']
95
n = len(A[0])
96
d = matrix(A).determinant()
97
result = [B for B in lat.find({'dim': int(n), 'det': int(d)})
98
if isom(A, B['gram'])]
99
if result:
100
print("... the lattice with base label "+ blabel + " is isometric to " + str(result[0]['gram']))
101
print("***********")
102
else:
103
lattice = data
104
else:
105
print("lattice already in the database")
106
lattice.update(data)
107
if saving:
108
lat.update({'label': label} , {"$set": lattice}, upsert=True)
109
110
111
112
# Loop over files
113
114
for path in sys.argv[1:]:
115
print(path)
116
filename = os.path.basename(path)
117
fn = gzip.open(path) if filename[-3:] == '.gz' else open(path)
118
for line in fn.readlines():
119
line.strip()
120
if re.match(r'\S',line):
121
l = json.loads(line)
122
do_import(l)
123
124