Skip to content

Commit 5dde4cd

Browse files
authored
Merge pull request LMFDB#7103 from rvisser7/random_seed
Set random seed before generating the code snippet log files
2 parents 065027c + 8ae1c91 commit 5dde4cd

5 files changed

Lines changed: 53 additions & 29 deletions

File tree

lmfdb/tests/generate_snippet_tests.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
comment_dict = {'magma': '//', 'sage': '#', 'sage_gap': '#',
4040
'gp': '\\\\', 'pari': '\\\\', 'oscar': '#', 'gap': '#'}
4141

42+
# To ensure output is fully deterministic and the log files don't change between runs,
43+
# we set the random seed to 1 before each test run
44+
SEED = 1
45+
seed_dict = {'sage': f'set_random_seed({SEED}); gap.set_seed({SEED}); libgap.set_seed({SEED})',
46+
'sage_gap': f'set_random_seed({SEED}); gap.set_seed({SEED}); libgap.set_seed({SEED})',
47+
'magma': f'SetSeed({SEED});',
48+
'oscar': f'import Random; Random.seed!({SEED}); Oscar.set_seed!({SEED}); Oscar.randseed!({SEED});',
49+
'gap': f'Reset(GlobalMersenneTwister, {SEED}); Reset(GlobalRandomSource, {SEED});',
50+
'gp': f'setrand({SEED});',
51+
}
4252

4353
def _setup_test_dir(yaml_file_path=None):
4454
""" Return dictionary with pair(s) 'yaml-file-path': 'test-file-path'.
@@ -55,7 +65,8 @@ def _setup_test_dir(yaml_file_path=None):
5565
raise Exception("Please run in same directory as test.sh")
5666

5767
if yaml_file_path is None:
58-
code_paths = lmfdb_dir.rglob("code*.yaml")
68+
# Ensure snippet files are evaluated in the same order every time (for deterministic output)
69+
code_paths = sorted(lmfdb_dir.rglob("code*.yaml"))
5970
else:
6071
code_paths = [Path(yaml_file_path)]
6172
assert code_paths[0].exists(), f"Specified path {yaml_file_path} does not exist"
@@ -150,6 +161,16 @@ def _eval_code_file(data, lang, proc, logfile):
150161
"""
151162
cmt = comment_dict[lang]
152163
lines = [l for l in data.splitlines() if l != '' and cmt not in l[:len(cmt)+1]]
164+
165+
# Reset the random state before every snippet file (to ensure log files are generated deterministically)
166+
seed_cmd = seed_dict.get(lang)
167+
if seed_cmd is not None:
168+
try:
169+
proc.run_command(seed_cmd, timeout=60)
170+
except Exception as exc:
171+
# Raise error if unable to set the random seed
172+
raise RuntimeError(f"Error: could not reset random state in {lang} with {seed_cmd!r}") from exc
173+
153174
with logfile.open('w') as f:
154175
proc.child.logfile = f
155176
proc.run_command(cmt + " snippet evaluation file generated by generate_snippet_tests.py")
@@ -160,6 +181,8 @@ def _eval_code_file(data, lang, proc, logfile):
160181
print("Timeout while running line:")
161182
print(line)
162183

184+
proc.child.logfile = None
185+
163186
# Matches ANSI escape sequences (colour codes etc.), see e.g. https://en.wikipedia.org/wiki/ANSI_escape_code
164187
# E.g. this sometimes occurs in the Gap snippet log files
165188
ANSI_ESCAPE_RE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')

lmfdb/tests/snippet_tests/ecnf/code-81.1-CMa1-oscar.log

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ with equation
1212
julia> conductor(E)
1313
Ideal of maximal order of number field of degree 2 over QQ
1414
of norm 81
15-
with 9-normal generators [81, 9*_a - 9]
15+
of minimum 9
16+
with 3-normal generators [9, 9*_a - 9]
1617

1718
julia> norm(conductor(E))
1819
81

lmfdb/tests/snippet_tests/elliptic_curves/code-37.a1-magma.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Mapping from: Abelian Group isomorphic to Z
99
Defined on 1 generator (free) to Set of points of E with coordinates in Rational Field given by a rule [no inverse]
1010
true true
1111
magma> Generators(E);
12-
[ (0 : -1 : 1) ]
12+
[ (0 : 0 : 1) ]
1313
true true
1414
magma> TorsionSubgroup(E);
1515
Abelian Group of order 1
1616
magma> IntegralPoints(E);
17-
[ (-1 : 0 : 1), (0 : -1 : 1), (1 : -1 : 1), (2 : 2 : 1), (6 : -15 : 1) ]
18-
[ <(-1 : 0 : 1), 1>, <(0 : -1 : 1), 1>, <(1 : -1 : 1), 1>, <(2 : 2 : 1), 1>, <(6 : -15 : 1), 1> ]
17+
[ (-1 : -1 : 1), (0 : 0 : 1), (1 : 0 : 1), (2 : -3 : 1), (6 : 14 : 1) ]
18+
[ <(-1 : -1 : 1), 1>, <(0 : 0 : 1), 1>, <(1 : 0 : 1), 1>, <(2 : -3 : 1), 1>, <(6 : 14 : 1), 1> ]
1919
magma> Conductor(E);
2020
37
2121
magma> Discriminant(E);
@@ -42,7 +42,7 @@ magma> Order(TorsionSubgroup(E));
4242
1
4343
magma> MordellWeilShaInformation(E);
4444
[ 1, 1 ]
45-
[ (0 : -1 : 1) ]
45+
[ (0 : 0 : 1) ]
4646
[
4747
<2, [ 0, 0 ]>
4848
]

lmfdb/tests/snippet_tests/groups/abstract/code-A5-magma.log

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ magma> AutomorphismGroup(G);
88
A group of automorphisms of GrpPerm: G, Degree 5, Order 2^2 * 3 * 5
99
Generators:
1010
Automorphism of GrpPerm: G, Degree 5, Order 2^2 * 3 * 5 which maps:
11-
(3, 4, 5) |--> (1, 2, 4)
12-
(1, 2, 3) |--> (2, 5, 3)
13-
Automorphism of GrpPerm: G, Degree 5, Order 2^2 * 3 * 5 which maps:
14-
(3, 4, 5) |--> (2, 3, 5)
15-
(1, 2, 3) |--> (1, 4, 3)
11+
(3, 4, 5) |--> (3, 5, 4)
12+
(1, 2, 3) |--> (1, 4, 2)
1613
Automorphism of GrpPerm: G, Degree 5, Order 2^2 * 3 * 5 which maps:
1714
(3, 4, 5) |--> (2, 4, 3)
18-
(1, 2, 3) |--> (1, 3, 5)
15+
(1, 2, 3) |--> (1, 5, 2)
16+
Automorphism of GrpPerm: G, Degree 5, Order 2^2 * 3 * 5 which maps:
17+
(3, 4, 5) |--> (2, 4, 5)
18+
(1, 2, 3) |--> (1, 3, 2)
1919
magma> CompositionFactors(G);
2020
G
2121
| Alternating(5)
@@ -310,20 +310,20 @@ Id($)
310310
[2] Order 2 Length 15
311311
Permutation group acting on a set of cardinality 5
312312
Order = 2
313-
(1, 2)(4, 5)
313+
(1, 2)(3, 5)
314314
[3] Order 3 Length 10
315315
Permutation group acting on a set of cardinality 5
316316
Order = 3
317-
(2, 5, 4)
317+
(2, 5, 3)
318318
[4] Order 4 Length 5
319319
Permutation group acting on a set of cardinality 5
320320
Order = 4 = 2^2
321-
(1, 2)(4, 5)
322-
(1, 4)(2, 5)
321+
(1, 2)(3, 5)
322+
(1, 3)(2, 5)
323323
[5] Order 5 Length 6
324324
Permutation group acting on a set of cardinality 5
325325
Order = 5
326-
(1, 4, 3, 2, 5)
326+
(1, 3, 4, 2, 5)
327327
[6] Order 6 Length 10
328328
Permutation group acting on a set of cardinality 5
329329
Order = 6 = 2 * 3
@@ -332,19 +332,19 @@ Order = 6 = 2 * 3
332332
[7] Order 10 Length 6
333333
Permutation group acting on a set of cardinality 5
334334
Order = 10 = 2 * 5
335-
(2, 3)(4, 5)
336-
(1, 4, 3, 2, 5)
335+
(2, 4)(3, 5)
336+
(1, 3, 4, 2, 5)
337337
[8] Order 12 Length 5
338338
Permutation group acting on a set of cardinality 5
339339
Order = 12 = 2^2 * 3
340-
(2, 5, 4)
341-
(1, 2)(4, 5)
342-
(1, 4)(2, 5)
340+
(2, 5, 3)
341+
(1, 2)(3, 5)
342+
(1, 3)(2, 5)
343343
[9] Order 60 Length 1
344344
Permutation group acting on a set of cardinality 5
345345
Order = 60 = 2^2 * 3 * 5
346346
(1, 2)(3, 4)
347-
(1, 3, 5)
347+
(1, 4, 5)
348348
magma> Center(G);
349349
Permutation group acting on a set of cardinality 5
350350
Order = 1

lmfdb/tests/snippet_tests/groups/abstract/code-D4-magma.log

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,27 @@ Conjugacy classes of subgroups
145145
[1] Order 1 Length 1
146146
Permutation group acting on a set of cardinality 4
147147
Order = 1
148-
[2] Order 2 Length 1
148+
[2] Order 2 Length 2
149149
Permutation group acting on a set of cardinality 4
150150
Order = 2
151-
(1, 3)(2, 4)
152-
[3] Order 2 Length 2
151+
(2, 4)
152+
[3] Order 2 Length 1
153153
Permutation group acting on a set of cardinality 4
154154
Order = 2
155-
(2, 4)
155+
(1, 3)(2, 4)
156156
[4] Order 2 Length 2
157157
Permutation group acting on a set of cardinality 4
158158
Order = 2
159159
(1, 2)(3, 4)
160160
[5] Order 4 Length 1
161161
Permutation group acting on a set of cardinality 4
162162
Order = 4 = 2^2
163-
(1, 2)(3, 4)
163+
(2, 4)
164164
(1, 3)(2, 4)
165165
[6] Order 4 Length 1
166166
Permutation group acting on a set of cardinality 4
167167
Order = 4 = 2^2
168-
(2, 4)
168+
(1, 2)(3, 4)
169169
(1, 3)(2, 4)
170170
[7] Order 4 Length 1
171171
Permutation group acting on a set of cardinality 4

0 commit comments

Comments
 (0)