forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_expression.py
More file actions
853 lines (804 loc) · 27.3 KB
/
Copy pathtest_expression.py
File metadata and controls
853 lines (804 loc) · 27.3 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
import pytest
import iarray as ia
import numpy
import numpy as np
# Expression
@pytest.mark.parametrize(
"method, shape, chunks, blocks, dtype, expression, xcontiguous, xurlpath, ycontiguous, yurlpath",
[
(
ia.Eval.ITERBLOSC,
[100, 100],
[23, 32],
[10, 10],
np.float64,
"cos(x)",
True,
"test_expression_xcontiguous.iarr",
True,
"test_expression_ycontiguous.iarr",
), # TODO: fix this
(
ia.Eval.ITERBLOSC,
[100, 100],
[10, 99],
[4, 12],
np.float64,
"x",
False,
None,
False,
None,
),
(ia.Eval.ITERBLOSC, [1000], [110], [55], np.float32, "x", True, None, True, None),
(
ia.Eval.ITERBLOSC,
[1000],
[100],
[30],
np.float64,
"(cos(x) - 1.35) * (sin(x) - 4.45) * tan(x - 8.5)",
False,
"test_expression_xsparse.iarr",
False,
"test_expression_ysparse.iarr",
),
(
ia.Eval.AUTO,
[1000],
[100],
[25],
np.float64,
"(cos(x) - 1.35) * (sin(x) - 4.45) * tan(x - 8.5)",
True,
None,
False,
"test_expression_ysparse.iarr",
),
(
ia.Eval.ITERCHUNK,
[1000],
[367],
[77],
np.float32,
"(abs(-x) - 1.35) * ceil(x) * floor(x - 8.5)",
False,
None,
True,
None,
),
pytest.param(
ia.Eval.ITERBLOSC,
[100, 100, 100],
[25, 25, 33],
[12, 16, 8],
np.float64,
"sinh(x) + (cosh(x) - 1.35) - tanh(x + .2)",
True,
"test_expression_xcontiguous.iarr",
True,
None,
marks=pytest.mark.heavy,
),
pytest.param(
ia.Eval.ITERBLOSC,
[223],
[100],
[30],
np.float64,
"sinh(x) + (cosh(x) - 1.35) - tanh(x + .2)",
True,
"test_expression_xcontiguous.iarr",
False,
"test_expression_ysparse.iarr",
marks=pytest.mark.heavy,
),
pytest.param(
ia.Eval.ITERCHUNK,
[100, 100, 55],
[10, 5, 10],
[3, 4, 3],
np.float64,
"asin(x) + (acos(x) - 1.35) - atan(x + .2)",
False,
None,
True,
"test_expression_ycontiguous.iarr",
marks=pytest.mark.heavy,
),
(
ia.Eval.ITERCHUNK,
[50, 10, 55],
[10, 5, 10],
[3, 4, 3],
np.float64,
"asin(x) + (acos(x) - 1.35) - atan(x + .2)",
False,
None,
True,
"test_expression_ycontiguous.iarr",
),
(
ia.Eval.ITERCHUNK,
[100, 55],
[10, 10],
[3, 3],
np.float64,
"arcsin(x) + (arccos(x) - 1.35) - arctan(x + .2)",
True,
None,
True,
"test_expression_ycontiguous.iarr",
),
pytest.param(
ia.Eval.ITERCHUNK,
[100, 100, 55],
[10, 5, 10],
[3, 4, 3],
np.float64,
"arcsin(x) + (arccos(x) - 1.35) - arctan(x + .2)",
True,
None,
True,
"test_expression_ycontiguous.iarr",
marks=pytest.mark.heavy,
), # check NumPy naming convention for ufuncs
(
ia.Eval.AUTO,
[1000],
[500],
[100],
np.float64,
"exp(x) + (log(x) - 1.35) - log10(x + .2)",
True,
None,
False,
None,
),
(
ia.Eval.ITERCHUNK,
[1000],
[500],
[200],
np.float32,
"sqrt(x) + atan2(x, x) + pow(x, x)",
False,
None,
True,
None,
),
(
ia.Eval.AUTO,
[1000],
[500],
[250],
np.float32,
"sqrt(x) + arctan2(x, x) + power(x, x)",
False,
"test_expression_xsparse.iarr",
True,
"test_expression_ycontiguous.iarr",
), # NumPy conventions
(
ia.Eval.AUTO,
[100, 100],
[50, 50],
[25, 25],
np.float64,
"(x - cos(1)) * 2",
True,
None,
False,
"test_expression_ysparse.iarr",
),
pytest.param(
ia.Eval.ITERCHUNK,
[8, 6, 7, 4, 5],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
np.float32,
"(x - cos(y)) * (sin(x) + y) + 2 * x + y",
False,
"test_expression_xsparse.iarr",
True,
None,
marks=pytest.mark.heavy,
),
(
ia.Eval.ITERBLOSC,
[15, 8],
[4, 5],
[4, 3],
np.float64,
"(x - cos(y)) * (sin(x) + y) + 2 * x + y",
False,
None,
False,
"test_expression_ysparse.iarr",
),
pytest.param(
ia.Eval.ITERBLOSC,
[17, 12, 15, 15, 8],
[8, 6, 7, 4, 5],
[4, 3, 3, 4, 5],
np.float64,
"(x - cos(y)) * (sin(x) + y) + 2 * x + y",
False,
None,
False,
"test_expression_ysparse.iarr",
marks=pytest.mark.heavy
),
(
ia.Eval.ITERBLOSC,
[17, 12],
[8, 6],
[4, 3],
np.float64,
"(x - cos(0.5)) * (sin(.1) + y) + 2 * x + y",
True,
"test_expression_xcontiguous.iarr",
False,
"test_expression_ysparse.iarr",
),
pytest.param(
ia.Eval.ITERBLOSC,
[17, 12, 15, 15, 8],
[8, 6, 7, 4, 5],
[4, 3, 3, 4, 5],
np.float64,
"(x - cos(0.5)) * (sin(.1) + y) + 2 * x + y",
True,
"test_expression_xcontiguous.iarr",
False,
"test_expression_ysparse.iarr",
marks=pytest.mark.heavy,
),
(
ia.Eval.ITERBLOSC,
[100, 100],
[23, 32],
[10, 10],
np.float32,
"2.71828**y / x",
True,
"test_expression_xcontiguous.iarr",
True,
"test_expression_ycontiguous.iarr",
)
],
)
def test_expression(
method, shape, chunks, blocks, dtype, expression, xcontiguous, xurlpath, ycontiguous, yurlpath
):
# The ranges below are important for not overflowing operations
ia.remove_urlpath(xurlpath)
ia.remove_urlpath(yurlpath)
ia.remove_urlpath("test_expression_zarray.iarr")
x = ia.linspace(shape, 0.1, 0.2, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=xcontiguous,
urlpath=xurlpath)
y = ia.linspace(shape, 0, 1, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=ycontiguous, urlpath=yurlpath)
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
expr = ia.expr_from_string(expression, {"x": x, "y": y}, chunks=chunks, blocks=blocks, contiguous=xcontiguous,
urlpath="test_expression_zarray.iarr", eval_method=method)
with pytest.raises(IOError):
expr.cfg.mode = "r"
expr.eval()
with pytest.raises(IOError):
expr.cfg.mode = "r+"
expr.eval()
expr.cfg.mode = "w-"
iout = expr.eval()
npout = ia.iarray2numpy(iout)
# Evaluate using a different engine (numpy)
ufunc_repls = {
"asin": "arcsin",
"acos": "arccos",
"atan": "arctan",
"atan2": "arctan2",
"pow": "power",
}
for ufunc in ufunc_repls.keys():
if ufunc in expression:
if ufunc == "pow" and "power" in expression:
# Don't do a replacement twice
break
expression = expression.replace(ufunc, ufunc_repls[ufunc])
for ufunc in ia.MATH_FUNC_LIST:
if ufunc in expression:
idx = expression.find(ufunc)
# Prevent replacing an ufunc with np.ufunc twice (not terribly solid, but else, test will crash)
if "np." not in expression[idx - len("np.arc"): idx]:
expression = expression.replace(ufunc + "(", "np." + ufunc + "(")
npout2 = eval(expression, {"x": npx, "y": npy, "np": numpy})
tol = 1e-6 if dtype is np.float32 else 1e-14
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
ia.remove_urlpath(x.cfg.urlpath)
ia.remove_urlpath(y.cfg.urlpath)
ia.remove_urlpath(iout.cfg.urlpath)
# ufuncs
@pytest.mark.parametrize(
"ufunc, ia_expr, xcontiguous, xurlpath, ycontiguous, yurlpath",
[
("abs(x)", "absolute(x)", True, None, True, None),
("arccos(x)", "acos(x)", False, None, False, None),
("arcsin(x)", "asin(x)", True, "test_expression_xcontiguous.iarr", True, None),
(
"arctan(x)",
"atan(x)",
False,
"test_expression_xsparse.iarr",
True,
"test_expression_ycontiguous.iarr",
),
(
"arctan2(x, y)",
"atan2(x, y)",
True,
"test_expression_xcontiguous.iarr",
False,
"test_expression_ysparse.iarr",
),
("ceil(x)", "ceil(x)", False, None, True, None),
("cos(x)", "cos(x)", True, None, False, None),
("cosh(x)", "cosh(x)", True, "test_expression_xcontiguous.iarr", False, None),
(
"exp(x)",
"exp(x)",
False,
"test_expression_xsparse.iarr",
True,
"test_expression_ycontiguous.iarr",
),
("floor(x)", "floor(x)", False, None, False, None),
("log(x)", "log(x)", True, None, False, None),
("log10(x)", "log10(x)", True, "test_expression_xcontiguous.iarr", False, None),
("negative(x)", "negate(x)", True, "test_expression_xcontiguous.iarr", False, None),
(
"power(x, y)",
"pow(x, y)",
False,
"test_expression_xsparse.iarr",
False,
"test_expression_ysparse.iarr",
),
("sin(x)", "sin(x)", True, None, True, None),
("sinh(x)", "sinh(x)", False, None, False, None),
("sqrt(x)", "sqrt(x)", True, "test_expression_xcontiugous.iarr", True, None),
("tan(x)", "tan(x)", True, None, True, "test_expression_ycontiguous.iarr"),
("tanh(x)", "tanh(x)", False, None, False, "test_expression_ysparse.iarr"),
],
)
def test_ufuncs(ufunc, ia_expr, xcontiguous, xurlpath, ycontiguous, yurlpath):
shape = [100, 200]
chunks = [40, 40]
bshape = [10, 17]
ia.remove_urlpath(xurlpath)
ia.remove_urlpath(yurlpath)
ia.remove_urlpath("test_expression_res.iarr")
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
x = ia.linspace(shape, 0.1, 0.9, dtype=dtype, chunks=chunks, blocks=bshape, contiguous=xcontiguous,
urlpath=xurlpath)
y = ia.linspace(shape, 0, 1, dtype=dtype, chunks=chunks, blocks=bshape, contiguous=ycontiguous,
urlpath=yurlpath)
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
if y.cfg.urlpath is not None:
expr = ia.expr_from_string(ia_expr, {"x": x, "y": y}, chunks=chunks,
blocks=bshape,
contiguous=ycontiguous,
urlpath="test_expression_res.iarr")
else:
expr = ia.expr_from_string(ia_expr, {"x": x, "y": y}, y.cfg)
iout = expr.eval()
npout = ia.iarray2numpy(iout)
tol = 1e-5 if dtype is np.float32 else 1e-13
# Lazy expression eval
lazy_expr = eval("ia." + ufunc, {"ia": ia, "x": x, "y": y})
iout2 = lazy_expr.eval()
npout2 = ia.iarray2numpy(iout2)
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
# Lazy expression eval, but via numpy ufunc machinery
# TODO: the next ufuncs still have some problems with the numpy machinery (bug?)
# abs(x) : TypeError: bad operand type for abs(): 'IArray'
# ceil(x) : TypeError: must be real number, not IArray
# floor(x): TypeError: must be real number, not IArray
# negative(x) : TypeError: bad operand type for unary -: 'IArray'
# power(x,y) : TypeError: unsupported operand type(s) for ** or pow(): 'IArray' and 'IArray'
if ufunc not in ("abs(x)", "ceil(x)", "floor(x)", "negative(x)", "power(x, y)"):
lazy_expr = eval("np." + ufunc, {"np": np, "x": x, "y": y})
iout2 = lazy_expr.eval()
npout2 = ia.iarray2numpy(iout2)
else:
npout2 = eval("np." + ufunc, {"np": np, "x": x.data, "y": y.data})
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
npout2 = eval("np." + ufunc, {"np": np, "x": npx, "y": npy}) # pure numpy
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
ia.remove_urlpath(x.cfg.urlpath)
ia.remove_urlpath(y.cfg.urlpath)
ia.remove_urlpath(iout.cfg.urlpath)
# ufuncs inside of expressions
@pytest.mark.parametrize(
"ufunc, xcontiguous, xurlpath, ycontiguous, yurlpath",
[
("abs", True, None, True, None),
("arccos", False, "test_expression_xsparse.iarr", True, None),
("arcsin", False, None, True, "test_expression_ycontiguous.iarr"),
("arctan", False, None, False, None),
("arctan2", True, None, True, "test_expression_ycontiguous.iarr"),
("ceil", False, None, False, "test_expression_ysparse.iarr"),
(
"cos",
True,
"test_expression_xcontiguous.iarr",
True,
"test_expression_ycontiguous.iarr",
),
("cosh", True, None, False, None),
("exp", False, None, True, None),
("floor", False, "test_expression_xsparse.iarr", False, "test_expression_ysparse.iarr"),
("log", False, "test_expression_xsparse.iarr", True, "test_expression_ycontiguous.iarr"),
("log10", True, "test_expression_xcontiguous.iarr", False, "test_expression_ysparse.iarr"),
("negative", True, "test_expression_xcontiguous.iarr", False, "test_expression_ysparse.iarr"),
("power", True, "test_expression_xcontiguous.iarr", True, None),
("sin", True, "test_expression_xcontiguous.iarr", False, None),
("sinh", True, None, False, "test_expression_ysparse.iarr"),
("sqrt", False, None, True, None),
("tan", False, "test_expression_xsparse.iarr", False, "test_expression_ysparse"),
("tanh", True, None, True, None),
],
)
def test_expr_ufuncs(ufunc, xcontiguous, xurlpath, ycontiguous, yurlpath):
shape = [200, 300]
cshape = [40, 50]
bshape = [20, 20]
xcfg = ia.Config(chunks=cshape, blocks=bshape, contiguous=xcontiguous, urlpath=xurlpath)
x32cfg = ia.Config(chunks=cshape, blocks=bshape, contiguous=xcontiguous,
urlpath="x32urlpath")
if xcfg.urlpath is None:
x32cfg.urlpath = None
ycfg = ia.Config(chunks=cshape, blocks=bshape, contiguous=ycontiguous, urlpath=yurlpath)
y32cfg = ia.Config(chunks=cshape, blocks=bshape, contiguous=ycontiguous, urlpath="y32urlpath")
if ycfg.urlpath is None:
y32cfg.urlpath = None
ia.remove_urlpath(xcfg.urlpath)
ia.remove_urlpath(ycfg.urlpath)
ia.remove_urlpath(x32cfg.urlpath)
ia.remove_urlpath(y32cfg.urlpath)
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
if dtype == np.float32:
x = ia.linspace(shape, 0.1, 0.9, dtype=dtype, cfg=x32cfg)
assert x.cfg.dtype == dtype
y = ia.linspace(shape, 0.5, 1, dtype=dtype, cfg=y32cfg)
else:
x = ia.linspace(shape, 0.1, 0.9, dtype=dtype, cfg=xcfg)
assert x.cfg.dtype == dtype
y = ia.linspace(shape, 0.5, 1, dtype=dtype, cfg=ycfg)
# NumPy computation
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
if ufunc in ("arctan2", "power"):
npout = eval("1 + 2 * np.%s(x, y)" % ufunc, {"np": np, "x": npx, "y": npy})
else:
npout = eval("1 + 2 * np.%s(x)" % ufunc, {"np": np, "x": npx})
# Lazy expression eval
if ufunc in ("arctan2", "power"):
lazy_expr = eval("1 + 2* x.%s(y)" % ufunc, {"x": x, "y": y})
else:
lazy_expr = eval("1 + 2 * x.%s()" % ufunc, {"x": x})
iout2 = lazy_expr.eval()
npout2 = ia.iarray2numpy(iout2)
tol = 1e-5 if dtype is np.float32 else 1e-13
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
ia.remove_urlpath(xcfg.urlpath)
ia.remove_urlpath(ycfg.urlpath)
ia.remove_urlpath(x32cfg.urlpath)
ia.remove_urlpath(y32cfg.urlpath)
# Different operand fusions inside expressions
@pytest.mark.parametrize(
"expr, np_expr, xcontiguous, xurlpath, ycontiguous, yurlpath, zcontiguous, zurlpath, tcontiguous, turlpath",
[
(
"x + y",
"x + y",
True,
"test_expression_xcontiguous.iarr",
True,
None,
True,
None,
True,
None,
),
(
"(x + y) + z",
"(x + y) + z",
False,
"test_expression_xsparse.iarr",
True,
"test_expression_ycontiguous.iarr",
True,
"test_expression_zcontiguous.iarr",
False,
None,
),
(
"(x + y) * (x + z)",
"(x + y) * (x + z)",
False,
None,
False,
"test_expression_ysparse.iarr",
False,
None,
False,
None,
),
(
"(x + y + z) * (x + z)",
"(x + y + z) * (x + z)",
True,
None,
False,
None,
False,
None,
False,
None,
),
(
"(x + y - z) * (x + y + t)",
"(x + y - z) * (x + y + t)",
False,
None,
False,
None,
False,
None,
False,
None,
),
(
"(x - z + t) * (x + y - z)",
"(x - z + t) * (x + y - z)",
False,
None,
False,
"test_expression_ysparse.iarr",
False,
"test_expression_zsparse.iarr",
True,
None,
),
pytest.param(
"(x - z + t) * (z + t - x)",
"(x - z + t) * (z + t - x)",
True,
"test_expression_xcontiguous.iarr",
True,
"test_expression_ycontiguous.iarr",
True,
"test_expression_zcontiguous.iarr",
True,
"test_expression_tcontiguous.iarr",
marks=pytest.mark.heavy
),
pytest.param(
"(x - z + t + y) * (t - y + z - x)",
"(x - z + t + y) * (t - y + z - x)",
False,
"test_expression_xsparse.iarr",
False,
"test_expression_ysparse.iarr",
False,
"test_expression_zsparse.iarr",
False,
"test_expression_tsparse.iarr",
marks=pytest.mark.heavy,
),
(
"(x - z + t + y) * (t - y + z - x)",
"(x - z + t + y) * (t - y + z - x)",
True,
None,
True,
None,
True,
None,
True,
None,
),
# transcendental functions
(
"x.cos() + y",
"np.cos(x) + y",
True,
"test_expression_xcontiguous.iarr",
True,
"test_expression_ycontiguous.iarr",
True,
None,
True,
None,
),
("ia.cos(x) + y", "np.cos(x) + y", False, None, False, None, False, None, False, None),
(
"x.sin() * x.sin() + y.cos()",
"np.sin(x) * np.sin(x) + np.cos(y)",
False,
"test_expression_xsparse.iarr",
True,
"test_expression_xcontiguous.iarr",
False,
None,
False,
None,
),
pytest.param(
"x.tan() * (y.sin() * y.sin() + z.cos()) + (t.sqrt() * 2)",
"np.tan(x) * (np.sin(y) * np.sin(y) + np.cos(z)) + (np.sqrt(t) * 2)",
True,
None,
False,
"test_expression_ysparse.iarr",
False,
"test_expression_zsparse.iarr",
True,
"test_expression_tsparse.iarr",
marks=pytest.mark.heavy,
),
# Use another order than before (precision needs to be relaxed a bit)
(
"t.tan() * (x.sin() * x.sin() + y.cos()) + (z.sqrt() * 2)",
"np.tan(t) * (np.sin(x) * np.sin(x) + np.cos(y)) + (np.sqrt(z) * 2)",
False,
None,
True,
None,
False,
None,
True,
None,
),
],
)
def test_expr_fusion(
expr,
np_expr,
xcontiguous,
xurlpath,
ycontiguous,
yurlpath,
zcontiguous,
zurlpath,
tcontiguous,
turlpath,
):
shape = [200, 300]
chunks = [40, 50]
bshape = [20, 20]
xcfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=xcontiguous, urlpath=xurlpath)
x32cfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=xcontiguous, urlpath="x32urlpath")
ycfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=ycontiguous, urlpath=yurlpath)
y32cfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=ycontiguous, urlpath="y32urlpath")
zcfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=zcontiguous, urlpath=zurlpath)
z32cfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=zcontiguous, urlpath="z32urlpath")
tcfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=tcontiguous, urlpath=turlpath)
t32cfg = ia.Config(chunks=chunks, blocks=bshape, contiguous=tcontiguous, urlpath="t32urlpath")
if xcfg.urlpath is None:
x32cfg.urlpath = None
if ycfg.urlpath is None:
y32cfg.urlpath = None
if zcfg.urlpath is None:
z32cfg.urlpath = None
if tcfg.urlpath is None:
t32cfg.urlpath = None
ia.remove_urlpath(xcfg.urlpath)
ia.remove_urlpath(ycfg.urlpath)
ia.remove_urlpath(x32cfg.urlpath)
ia.remove_urlpath(y32cfg.urlpath)
ia.remove_urlpath(zcfg.urlpath)
ia.remove_urlpath(tcfg.urlpath)
ia.remove_urlpath(z32cfg.urlpath)
ia.remove_urlpath(t32cfg.urlpath)
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
if dtype == np.float32:
x = ia.linspace(shape, 0.1, 0.9, dtype=dtype, cfg=x32cfg)
y = ia.linspace(shape, 0.5, 1, dtype=dtype, cfg=y32cfg)
z = ia.linspace(shape, 0.1, 0.9, dtype=dtype, cfg=z32cfg)
t = ia.linspace(shape, 0.5, 1, dtype=dtype, cfg=t32cfg)
else:
x = ia.linspace(shape, 0.1, 0.9, dtype=dtype, cfg=xcfg)
y = ia.linspace(shape, 0.5, 1, dtype=dtype, cfg=ycfg)
z = ia.linspace(shape, 1.0, 2.0, dtype=dtype, cfg=zcfg)
t = ia.linspace(shape, 1.5, 3.0, dtype=dtype, cfg=tcfg)
# NumPy computation
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
npz = ia.iarray2numpy(z)
npt = ia.iarray2numpy(t)
npout = eval("%s" % np_expr, {"np": np, "x": npx, "y": npy, "z": npz, "t": npt})
# High-level ironarray eval
lazy_expr = eval(expr, {"ia": ia, "x": x, "y": y, "z": z, "t": t})
iout2 = lazy_expr.eval()
npout2 = ia.iarray2numpy(iout2)
tol = 1e-6 if dtype is np.float32 else 1e-14
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
ia.remove_urlpath(xcfg.urlpath)
ia.remove_urlpath(ycfg.urlpath)
ia.remove_urlpath(x32cfg.urlpath)
ia.remove_urlpath(y32cfg.urlpath)
ia.remove_urlpath(zcfg.urlpath)
ia.remove_urlpath(tcfg.urlpath)
ia.remove_urlpath(z32cfg.urlpath)
ia.remove_urlpath(t32cfg.urlpath)
@pytest.mark.parametrize(
"expression, contiguous, zcontiguous, zurlpath",
[
(
"x + y",
True,
True,
None,
)
])
def test_chunks_blocks_params(expression, contiguous, zurlpath, zcontiguous):
shape = [200]
chunks = [40]
blocks = [20]
ia.remove_urlpath(zurlpath)
# First with default chunks and blocks when operands chunks and blocks are equal
xcfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=contiguous)
ycfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=contiguous)
zcfg = ia.Config(contiguous=zcontiguous, urlpath=zurlpath)
x = ia.linspace(shape, 0.1, 0.2, cfg=xcfg)
y = ia.linspace(shape, 0, 1, cfg=ycfg)
expr = ia.expr_from_string(expression, {"x": x, "y": y}, cfg=zcfg)
iout = expr.eval()
assert(iout.cfg.chunks == chunks)
assert(iout.cfg.blocks == blocks)
ia.remove_urlpath(zcfg.urlpath)
# Now with default chunks and blocks when operands chunks and blocks are not equal
ycfg = ia.Config(chunks=[30], blocks=blocks, contiguous=contiguous)
zcfg = ia.Config(contiguous=zcontiguous, urlpath=zurlpath)
y = ia.linspace(shape, 0, 1, cfg=ycfg)
expr = ia.expr_from_string(expression, {"x": x, "y": y}, cfg=zcfg)
iout = expr.eval()
assert (iout.cfg.chunks != chunks)
assert (iout.cfg.blocks != blocks)
ia.remove_urlpath(zcfg.urlpath)
# Check that the provided chunks and blocks are used
ycfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=contiguous)
zcfg = ia.Config(chunks=[10], blocks=[5], contiguous=zcontiguous, urlpath=zurlpath)
y = ia.linspace(shape, 0, 1, cfg=ycfg)
expr = ia.expr_from_string(expression, {"x": x, "y": y}, cfg=zcfg)
iout = expr.eval()
assert (iout.cfg.chunks == zcfg.chunks)
assert (iout.cfg.blocks == zcfg.blocks)
@pytest.mark.parametrize(
"expression, operands",
[
("x + y", ("x", "y")),
("(3 + a) -c -1", ("a", "c")),
("(3.+a)/c", ("a", "c")),
("2.3e9 + b + atan2(a + c / b) - 2**z + 12 % 3 * sin(9 - 9.9) - C", ("C", "a", "b", "c", "z")),
])
def test_get_operands(expression, operands):
assert ia.expr_get_operands(expression) == operands
@pytest.mark.parametrize(
"sexpr, sexpr_scalar, inputs",
[
("x + 1", "x + y", {"x": ia.arange((10,)), "y": 1}),
("x + y + 1.35", "x + y + z", {"x": ia.arange((10,)), "y": 1, "z": 1.35}),
])
def test_scalar_params(sexpr, sexpr_scalar, inputs):
expr = ia.expr_from_string(sexpr, inputs)
expr_scalar = ia.expr_from_string(sexpr_scalar, inputs)
out = expr.eval()
out_udf = expr_scalar.eval()
tol = 1e-14
np.testing.assert_allclose(out.data, out_udf.data, rtol=tol, atol=tol)