-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plot.py
More file actions
452 lines (425 loc) · 16.5 KB
/
test_plot.py
File metadata and controls
452 lines (425 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# -*- coding: utf-8 -*-
"""
Unit tests from mathics.builtin.drawing.plot
"""
from test.helper import SESSIONS, check_evaluation
import pytest
from mathics.core.expression import Expression
from mathics.core.symbols import Symbol
from mathics.core.util import print_expression_tree
def test__listplot():
"""tests for module builtin.drawing.plot._ListPlot"""
for str_expr, msgs, str_expected, fail_msg in (
(
"ListPlot[5]",
("5 is not a list of numbers or pairs of numbers.",),
"ListPlot[5]",
"ListPlot with invalid list of point",
),
(
"ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]",
(
"{{}, {{1., 1.}}, {{1., 2.}}, {}} is not a list of numbers or pairs of numbers.",
),
"ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]",
"ListLinePlot with invalid list of point",
),
):
check_evaluation(
str_expr,
str_expected,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=fail_msg,
expected_messages=msgs,
use_vectorized=False,
)
@pytest.mark.parametrize(
("str_expr", "msgs", "str_expected", "fail_msg"),
[
("Plot[1 / x, {x, -1, 1}]", None, "-Graphics-", None),
("Plot[x, {y, 0, 2}]", None, "-Graphics-", None),
(
"Plot[{f[x],-49x/12+433/108},{x,-6,6}, PlotRange->{-10,10}, AspectRatio->{1}]",
None,
"-Graphics-",
None,
),
(
"Plot[Sin[t], {t, 0, 2 Pi}, PlotPoints -> 1]",
("Value of option PlotPoints -> 1 is not an integer >= 2.",),
"Plot[Sin[t], {t, 0, 2 Pi}, PlotPoints -> 1]",
None,
),
("Plot[x*y, {x, -1, 1}]", None, "-Graphics-", None),
("Plot3D[z, {x, 1, 20}, {y, 1, 10}]", None, "-Graphics3D-", None),
(
"Plot3D[Exp[x] Cos[y], {x, -2, 1}, {y, -Pi, 2 Pi}]",
None,
"-Graphics3D-",
None,
),
("Plot3D[1, {x,-1,1}, {y,-1,1}]", None, "-Graphics3D-", None),
(
"Plot3D[]",
("Plot3D called with 0 arguments; 3 arguments are expected.",),
"Plot3D[]",
None,
),
(
"Plot3D[1, 2, 3]",
(
"Plot range 2 must be of the form {variable, min, max}, where max > min.",
),
"Plot3D[1, 2, 3]",
None,
),
(
"Plot3D[1, {2, 3}, {3, 4}]",
(
"Plot range {2, 3} must be of the form {variable, min, max}, where max > min.",
),
"Plot3D[1, {2, 3}, {3, 4}]",
None,
),
(
"Plot3D[1, {1, 2, 3}, {1, 3, 4}]",
(
"Plot range {1, 2, 3} must be of the form {variable, min, max}, where max > min.",
),
"Plot3D[1, {1, 2, 3}, {1, 3, 4}]",
None,
),
(
"Plot3D[1, {x, 3, 2}, {1, 3, 4}]",
(
"Plot range {x, 3, 2} must be of the form {variable, min, max}, where max > min.",
),
"Plot3D[1, {x, 3, 2}, {1, 3, 4}]",
None,
),
(
"Graphics[{Disk[]}, Background->RGBColor[1,.1,.1]]//TeXForm//ToString",
None,
(
'\n\\begin{asy}\nusepackage("amsmath");\nsize(5.8333cm, 5.8333cm);\n'
"filldraw(box((0,0), (350,350)), rgb(1, 0.1, 0.1));\n"
"filldraw(ellipse((175,175),175,175), rgb(0, 0, 0), nullpen);\n"
"clip(box((0,0), (350,350)));\n\\end{asy}\n"
),
"Background 2D",
),
## MaxRecursion Option
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> 0]",
None,
"-Graphics3D-",
None,
),
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> 15]",
None,
"-Graphics3D-",
None,
),
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> 16]",
(
"MaxRecursion must be a non-negative integer; the recursion value is limited to 15. Using MaxRecursion -> 15.",
),
"-Graphics3D-",
None,
),
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> -1]",
(
"MaxRecursion must be a non-negative integer; the recursion value is limited to 15. Using MaxRecursion -> 0.",
),
"-Graphics3D-",
None,
),
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> a]",
(
"MaxRecursion must be a non-negative integer; the recursion value is limited to 15. Using MaxRecursion -> 0.",
),
"-Graphics3D-",
None,
),
(
"Plot3D[0, {x, -2, 2}, {y, -2, 2}, MaxRecursion -> Infinity]",
(
"MaxRecursion must be a non-negative integer; the recursion value is limited to 15. Using MaxRecursion -> 15.",
),
"-Graphics3D-",
None,
),
(
"Plot3D[x ^ 2 + 1 / y, {x, -1, 1}, {y, 1, z}]",
("Limiting value z in {y, 1, z} is not a machine-size real number.",),
"Plot3D[x ^ 2 + 1 / y, {x, -1, 1}, {y, 1, z}]",
None,
),
(
"StringTake[Plot3D[x + 2y, {x, -2, 2}, {y, -2, 2}] // TeXForm//ToString,80]",
None,
"\n\\begin{asy}\nimport three;\nimport solids;\nimport tube;\nsize(6.6667cm, 6.6667cm);",
None,
),
(
"Graphics3D[{Sphere[]}, Background->RGBColor[1,.1,.1]]//TeXForm//ToString",
None,
(
"\n\\begin{asy}\n"
"import three;\n"
"import solids;\n"
"import tube;\n"
"size(6.6667cm, 6.6667cm);\n"
"currentprojection=perspective(2.6,-4.8,4.0);\n"
"currentlight=light(rgb(0.5,0.5,0.5), background=rgb(1, 0.1, 0.1), specular=red, (2,0,2), (2,2,2), (0,2,2));\n"
"// Sphere3DBox\n"
"draw(surface(sphere((0, 0, 0), 1)), rgb(1,1,1)+opacity(1));\n"
"draw(((-1,-1,-1)--(1,-1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,1,-1)--(1,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,-1,1)--(1,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,1,1)--(1,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,-1,-1)--(-1,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((1,-1,-1)--(1,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,-1,1)--(-1,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((1,-1,1)--(1,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,-1,-1)--(-1,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((1,-1,-1)--(1,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-1,1,-1)--(-1,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((1,1,-1)--(1,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"\\end{asy}\n"
),
"Background 3D",
),
(
"Graphics3D[Point[Table[{Sin[t], Cos[t], 0}, {t, 0, 2. Pi, Pi / 15.}]]] //Chop//TeXForm//ToString",
None,
(
"\n\\begin{asy}\nimport three;\n"
"import solids;\n"
"import tube;\n"
"size(6.6667cm, 6.6667cm);\n"
"currentprojection=perspective(2.6,-4.8,4.0);\n"
"currentlight=light(rgb(0.5,0.5,0.5), specular=red, (2,0,2), (2,2,2), (0,2,2));\n"
"// Point3DBox\npath3 g=(0,1,0)--(0.20791,0.97815,0)--(0.40674,0.91355,0)--"
"(0.58779,0.80902,0)--(0.74314,0.66913,0)--(0.86603,0.5,0)--(0.95106,0.30902,0)--"
"(0.99452,0.10453,0)--(0.99452,-0.10453,0)--(0.95106,-0.30902,0)--(0.86603,-0.5,0)"
"--(0.74314,-0.66913,0)--(0.58779,-0.80902,0)--(0.40674,-0.91355,0)--"
"(0.20791,-0.97815,0)--(0,-1,0)--(-0.20791,-0.97815,0)--"
"(-0.40674,-0.91355,0)--(-0.58779,-0.80902,0)--(-0.74314,-0.66913,0)--"
"(-0.86603,-0.5,0)--(-0.95106,-0.30902,0)--(-0.99452,-0.10453,0)--"
"(-0.99452,0.10453,0)--(-0.95106,0.30902,0)--(-0.86603,0.5,0)--"
"(-0.74314,0.66913,0)--(-0.58779,0.80902,0)--(-0.40674,0.91355,0)--"
"(-0.20791,0.97815,0)--(0,1,0)--cycle;dot(g, rgb(0, 0, 0));\n"
"draw(((-0.99452,-1,-1)--(0.99452,-1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,1,-1)--(0.99452,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,-1,1)--(0.99452,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,1,1)--(0.99452,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,-1,-1)--(-0.99452,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((0.99452,-1,-1)--(0.99452,1,-1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,-1,1)--(-0.99452,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((0.99452,-1,1)--(0.99452,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,-1,-1)--(-0.99452,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((0.99452,-1,-1)--(0.99452,-1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((-0.99452,1,-1)--(-0.99452,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n"
"draw(((0.99452,1,-1)--(0.99452,1,1)), rgb(0.4, 0.4, 0.4)+linewidth(1));\n\\end{asy}\n"
),
None,
),
],
)
def test_plot(str_expr, msgs, str_expected, fail_msg):
"""tests for module builtin.drawing.plot"""
check_evaluation(
str_expr,
str_expected,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=fail_msg,
expected_messages=msgs,
use_vectorized=False,
)
#
# NOTE: I think the following tests have been superseded by test_plot_detail.py which
# does similar (actually, more stringent) tests much less laboriously. Keeping these
# for now just in case, but probably better to add new tests to test_plot_detail.py
#
# Call plotting functions and examine the structure of the output
# In case of error trees are printed with an embedded >>> marker showing location of error
#
def print_expression_tree_with_marker(expr):
print_expression_tree(expr, marker=lambda expr: getattr(expr, "_marker", ""))
def check_structure(
result, expected, parent_result=None, parent_expected=None, parent_i=None
):
"""Check that expected is a prefix of result at every node"""
def error(msg):
if parent_result and parent_expected:
def mark(parent_expr, marker):
parent_expr._elements = list(parent_expr.elements)
parent_expr.elements[parent_i] = Expression(
Symbol(marker), parent_expr.elements[parent_i]
)
# parent_expr.elements[parent_i]._marker = marker
mark(parent_result, "RESULT >>> ")
mark(parent_expected, "EXPECTED >>> ")
raise AssertionError(msg)
# do the heads match?
if result.get_head() != expected.get_head():
error(f"heads {result.get_head()} and {expected.get_head()} don't match")
# does the structure match?
if hasattr(expected, "elements"):
if not hasattr(result, "elements"):
error("expected elements but result has none")
for i, e in enumerate(expected.elements):
if len(result.elements) <= i:
error("result has too few elements")
check_structure(result.elements[i], e, result, expected, i)
else:
if str(result) != str(expected):
error("leaves don't match")
def eval_and_check_structure(str_expr, str_expected):
session = SESSIONS[False]
session.reset()
expr = session.parse(str_expr)
result = expr.evaluate(session.evaluation)
expected = session.parse(str_expected)
try:
check_structure(result, expected)
except AssertionError as oops:
print(f"\nERROR: {oops} (error is marked with >>> below)")
print("=== result:")
print_expression_tree_with_marker(result)
print("=== expected:")
print_expression_tree_with_marker(expected)
raise
def test_plot3d_default():
eval_and_check_structure(
"""
Plot3D[
x+y,
{x,0,1}, {y,0,1},
PlotPoints->{2,2},
MaxRecursion->0
]
""",
"""
Graphics3D[
{
Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}],
Polygon[{{}}], Polygon[{{}}], Polygon[{{}}], Polygon[{{}}], Polygon[{{}}], Polygon[{{}}], Polygon[{{}}],
(* mesh lines for default Mesh->Full *)
Line[{{0.0,0.0,0.0},{0.0,0.5,0.5},{0.0,1.0,1.0}}],
Line[{{}}], Line[{{}}], Line[{{}}], Line[{{}}], Line[{{}}]
},
System`AlignmentPoint -> Center,
AspectRatio -> 1,
Axes -> True
]
""",
)
def test_plot3d_nondefault():
eval_and_check_structure(
"""
Plot3D[
x+y,
{x,0,1}, {y,0,1},
PlotPoints->{2,2},
AspectRatio -> 0.5,
Axes -> False
]
""",
"""
Graphics3D[
{
Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}],
Polygon[{{}}]
},
AlignmentPoint -> Center,
AspectRatio -> 0.5,
Axes -> False
]
""",
)
def test_densityplot_default():
eval_and_check_structure(
"""
DensityPlot[
x+y, {x,0,1}, {y,0,1},
PlotPoints-> {2,2},
MaxRecursion->0
]
""",
"""
Graphics[
{
Polygon[
{
{{0.0,0.0},{0.0,0.5},{0.5,0.0}},
{{0.0,0.5},{0.5,0.0},{0.5,0.5}}
},
VertexColors -> {
{
RGBColor[0.293416, 0.0574044, 0.529412],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999]
},
{
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.663226, 0.6872815, 0.9117649999999999]
}
}
]
},
AlignmentPoint -> Center,
AspectRatio -> 1,
Axes -> False
]
""",
)
def test_densityplot_nondefault():
eval_and_check_structure(
"""
DensityPlot[
x+y, {x,0,1}, {y,0,1},
PlotPoints-> {2,2},
MaxRecursion->0
AspectRatio -> 0.5,
Axes -> True
]
""",
"""
Graphics[
{
Polygon[
{
{{0.0,0.0},{0.0,0.5},{0.5,0.0}},
{{0.0,0.5},{0.5,0.0},{0.5,0.5}}
},
VertexColors -> {
{
RGBColor[0.293416, 0.0574044, 0.529412],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999]
},
{
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.49621975000000007, 0.41002484999999994, 0.8144772499999999],
RGBColor[0.663226, 0.6872815, 0.9117649999999999]
}
}
]
},
System`AlignmentPoint -> Center,
AspectRatio -> 1, (* TODO: why not passed through? *)
Axes -> True
]
""",
)