-
Notifications
You must be signed in to change notification settings - Fork 46
/
test_array_functions.py
1690 lines (1353 loc) · 43.2 KB
/
test_array_functions.py
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# tests for NumPy __array_function__ support
import re
from importlib.metadata import version
import numpy as np
import pytest
from packaging.version import Version
from unyt import A, K, cm, degC, degF, delta_degC, g, km, rad, s
from unyt._array_functions import (
_HANDLED_FUNCTIONS as HANDLED_FUNCTIONS,
_UNSUPPORTED_FUNCTIONS as UNSUPPORTED_FUNCTIONS,
)
from unyt.array import unyt_array, unyt_quantity
from unyt.exceptions import (
InvalidUnitOperation,
UnitConversionError,
UnitInconsistencyError,
UnytError,
)
from unyt.testing import assert_array_equal_units
NUMPY_VERSION = Version(version("numpy"))
# this is a subset of NOT_HANDLED_FUNCTIONS for which there's nothing to do
# because they don't apply to (real) numeric types
# or they work as expected out of the box
# This is not necessarilly complete !
NOOP_FUNCTIONS = {
np.all, # expects booleans
np.alltrue, # expects booleans
np.amax, # works out of the box (tested)
np.amin, # works out of the box (tested)
np.angle, # expects complex numbers
np.any, # works out of the box (tested)
np.append, # we get it for free with np.concatenate (tested)
np.apply_along_axis, # works out of the box (tested)
np.argmax, # returns pure numbers
np.argmin, # returns pure numbers
np.argpartition, # returns pure numbers
np.argsort, # returns pure numbers
np.argwhere, # returns pure numbers
np.array_repr, # hooks into __repr__
np.array_str, # hooks into __str__
np.atleast_1d, # works out of the box (tested)
np.atleast_2d, # works out of the box (tested)
np.atleast_3d, # works out of the box (tested)
np.average, # works out of the box (tested)
np.can_cast, # works out of the box (tested)
np.common_type, # works out of the box (tested)
np.result_type, # works out of the box (tested)
np.iscomplex, # works out of the box (tested)
np.iscomplexobj, # works out of the box (tested)
np.isreal, # works out of the box (tested)
np.isrealobj, # works out of the box (tested)
np.nan_to_num, # works out of the box (tested)
np.nanargmax, # return pure numbers
np.nanargmin, # return pure numbers
np.nanmax, # works out of the box (tested)
np.nanmean, # works out of the box (tested)
np.nanmedian, # works out of the box (tested)
np.nanmin, # works out of the box (tested)
np.trim_zeros, # works out of the box (tested)
np.max, # works out of the box (tested)
np.mean, # works out of the box (tested)
np.median, # works out of the box (tested)
np.min, # works out of the box (tested)
np.ndim, # return pure numbers
np.shape, # returns pure numbers
np.size, # returns pure numbers
np.sort, # works out of the box (tested)
np.sum, # works out of the box (tested)
np.repeat, # works out of the box (tested)
np.tile, # works out of the box (tested)
np.shares_memory, # works out of the box (tested)
np.sometrue, # works out of the box (tested)
np.nonzero, # works out of the box (tested)
np.count_nonzero, # returns pure numbers
np.flatnonzero, # works out of the box (tested)
np.isneginf, # works out of the box (tested)
np.isposinf, # works out of the box (tested)
np.empty_like, # works out of the box (tested)
np.full_like, # works out of the box (tested)
np.ones_like, # works out of the box (tested)
np.zeros_like, # works out of the box (tested)
np.copy, # works out of the box (tested)
np.meshgrid, # works out of the box (tested)
np.transpose, # works out of the box (tested)
np.reshape, # works out of the box (tested)
np.resize, # works out of the box (tested)
np.roll, # works out of the box (tested)
np.rollaxis, # works out of the box (tested)
np.rot90, # works out of the box (tested)
np.expand_dims, # works out of the box (tested)
np.squeeze, # works out of the box (tested)
np.flip, # works out of the box (tested)
np.fliplr, # works out of the box (tested)
np.flipud, # works out of the box (tested)
np.delete, # works out of the box (tested)
np.partition, # works out of the box (tested)
np.broadcast_to, # works out of the box (tested)
np.broadcast_arrays, # works out of the box (tested)
np.split, # works out of the box (tested)
np.array_split, # works out of the box (tested)
np.dsplit, # works out of the box (tested)
np.hsplit, # works out of the box (tested)
np.vsplit, # works out of the box (tested)
np.swapaxes, # works out of the box (tested)
np.moveaxis, # works out of the box (tested)
np.nansum, # works out of the box (tested)
np.std, # works out of the box (tested)
np.nanstd, # works out of the box (tested)
np.nanvar, # works out of the box (tested)
np.nanprod, # works out of the box (tested)
np.diag, # works out of the box (tested)
np.diag_indices_from, # returns pure numbers
np.diagflat, # works out of the box (tested)
np.diagonal, # works out of the box (tested)
np.ravel, # returns pure numbers
np.ravel_multi_index, # returns pure numbers
np.unravel_index, # returns pure numbers
np.fix, # works out of the box (tested)
np.round, # is implemented via np.around
np.round_, # is implemented via np.around
np.may_share_memory, # returns pure numbers (booleans)
np.linalg.matrix_power, # works out of the box (tested)
np.linalg.cholesky, # works out of the box (tested)
np.linalg.multi_dot, # works out of the box (tested)
np.linalg.matrix_rank, # returns pure numbers
np.linalg.qr, # works out of the box (tested)
np.linalg.slogdet, # undefined units
np.linalg.cond, # works out of the box (tested)
np.gradient, # works out of the box (tested)
np.cumsum, # works out of the box (tested)
np.nancumsum, # works out of the box (tested)
np.nancumprod, # we get it for free with np.cumprod (tested)
np.bincount, # works out of the box (tested)
np.unique, # works out of the box (tested)
np.take, # works out of the box (tested)
np.min_scalar_type, # returns dtypes
np.extract, # works out of the box (tested)
np.setxor1d, # we get it for free with previously implemented functions (tested)
np.lexsort, # returns pure numbers
np.digitize, # returns pure numbers
np.tril_indices_from, # returns pure numbers
np.triu_indices_from, # returns pure numbers
np.imag, # works out of the box (tested)
np.real, # works out of the box (tested)
np.real_if_close, # works out of the box (tested)
np.einsum_path, # returns pure numbers
np.cov, # returns pure numbers
np.corrcoef, # returns pure numbers
np.compress, # works out of the box (tested)
np.take_along_axis, # works out of the box (tested)
}
# Functions for which behaviour is intentionally left to default
IGNORED_FUNCTIONS = {
np.i0,
# IO functions (no way to add units)
np.save,
np.savez,
np.savez_compressed,
}
DEPRECATED_FUNCTIONS = {
"alen", # deprecated in numpy 1.18, removed in 1.22
"asscalar", # deprecated in numpy 1.18, removed in 1.22
"fv", # deprecated in numpy 1.18, removed in 1.20
"ipmt", # deprecated in numpy 1.18, removed in 1.20
"irr", # deprecated in numpy 1.18, removed in 1.20
"mirr", # deprecated in numpy 1.18, removed in 1.20
"nper", # deprecated in numpy 1.18, removed in 1.20
"npv", # deprecated in numpy 1.18, removed in 1.20
"pmt", # deprecated in numpy 1.18, removed in 1.20
"ppmt", # deprecated in numpy 1.18, removed in 1.20
"pv", # deprecated in numpy 1.18, removed in 1.20
"rank", # deprecated in numpy 1.10, removed in 1.18
"rate", # deprecated in numpy 1.18, removed in 1.20
"msort", # deprecated in numpy 1.24
"product", # deprecated in numpy 1.25
"cumproduct", # deprecated in numpy 1.25
}
NOT_HANDLED_FUNCTIONS = NOOP_FUNCTIONS | UNSUPPORTED_FUNCTIONS | IGNORED_FUNCTIONS
for func in DEPRECATED_FUNCTIONS:
if hasattr(np, func):
NOT_HANDLED_FUNCTIONS.add(getattr(np, func))
def get_wrapped_functions(*modules):
"""get functions that support __array_function__ in modules
This was adapted from astropy's tests
"""
wrapped_functions = {}
for mod in modules:
for name, f in mod.__dict__.items():
if callable(f) and hasattr(f, "__wrapped__"):
if f is np.printoptions or f.__name__.startswith("_"):
continue
wrapped_functions[mod.__name__ + "." + name] = f
return dict(sorted(wrapped_functions.items()))
def test_wrapping_completeness():
"""Ensure we wrap all numpy functions that support __array_function__"""
handled_numpy_functions = set(HANDLED_FUNCTIONS.keys())
# ensure no functions appear in both NOT_HANDLED_FUNCTIONS and HANDLED_FUNCTIONS
assert NOT_HANDLED_FUNCTIONS.isdisjoint(
handled_numpy_functions
), NOT_HANDLED_FUNCTIONS.intersection(handled_numpy_functions)
# get list of functions that support wrapping by introspection on numpy module
wrappable_functions = get_wrapped_functions(np, np.fft, np.linalg)
for function in HANDLED_FUNCTIONS:
# ensure we only have wrappers for functions that support wrapping
assert function in wrappable_functions.values()
all_funcs = NOT_HANDLED_FUNCTIONS.union(handled_numpy_functions)
# ensure all functions in numpy that support wrapping either have wrappers
# or are explicitly whitelisted
for function in wrappable_functions.values():
assert function in all_funcs
def test_array_repr():
arr = [1, 2, 3] * cm
assert np.array_repr(arr) == "unyt_array([1, 2, 3], units='cm')"
def test_dot_vectors():
a = [1, 2, 3] * cm
b = [1, 2, 3] * s
res = np.dot(a, b)
assert res.units == cm * s
assert res.d == 14
# NOTE: explicitly setting the dtype of out arrays as `dtype=np.int_` is the
# cross-platform way to guarantee that their dtype matches that of np.arange(x)
# (on POSIX system it's int64, while on windows it's int32)
@pytest.mark.parametrize(
"out",
[
None,
np.empty((3, 3), dtype=np.int_),
np.empty((3, 3), dtype=np.int_, order="C") * cm * s,
np.empty((3, 3), dtype=np.int_, order="C") * km * s,
],
ids=[
"None",
"pure ndarray",
"same units",
"convertible units",
],
)
def test_dot_matrices(out):
a = np.arange(9) * cm
a.shape = (3, 3)
b = np.arange(9) * s
b.shape = (3, 3)
res = np.dot(a, b, out=out)
if out is not None:
np.testing.assert_array_equal(res, out)
assert np.shares_memory(res, out)
assert isinstance(res, unyt_array)
assert isinstance(out, np.ndarray)
if isinstance(out, unyt_array):
# check that the result can be converted to predictible units
res.in_units("cm * s")
assert out.units == res.units
def test_dot_mixed_ndarray_unyt_array():
a = np.ones((3, 3))
b = np.ones((3, 3)) * cm
res = np.dot(a, b)
assert isinstance(res, unyt_array)
assert res.units == cm
out = np.zeros((3, 3))
res = np.dot(a, b, out=out)
assert isinstance(res, unyt_array)
assert type(out) is np.ndarray
assert np.shares_memory(out, res)
np.testing.assert_array_equal(out, res)
out = np.zeros((3, 3)) * km
res = np.dot(a, b, out=out)
assert isinstance(res, unyt_array)
assert isinstance(out, unyt_array)
assert res.units == out.units == cm
assert np.shares_memory(res, out)
# check this works with an ndarray as the first operand
out = np.zeros((3, 3)) * km
res = np.dot(b, a, out=out)
assert isinstance(res, unyt_array)
assert isinstance(out, unyt_array)
assert res.units == out.units == cm
assert np.shares_memory(res, out)
def test_invalid_dot_matrices():
a = np.arange(9) * cm
a.shape = (3, 3)
b = np.arange(9) * s
b.shape = (3, 3)
out = np.empty((3, 3), dtype=np.int_, order="C") * s**2
res = np.dot(a, b, out=out)
np.testing.assert_array_equal(res, out)
assert out.units == res.units == cm * s
def test_vdot():
a = np.arange(9) * cm
b = np.arange(9) * s
res = np.vdot(a, b)
assert res.units == cm * s
def test_inner():
a = np.array([1, 2, 3]) * cm
b = np.array([0, 1, 0]) * s
res = np.inner(a, b)
assert res.d == 2
assert res.units == cm * s
def test_outer():
a = np.array([1, 2, 3]) * cm
b = np.array([0, 1, 0]) * s
res = np.outer(a, b)
expected = np.array([[0, 1, 0], [0, 2, 0], [0, 3, 0]])
np.testing.assert_array_equal(res.ndview, expected)
assert res.units == cm * s
def test_kron():
a = np.eye(2) * cm
b = np.ones((2, 2)) * s
res = np.kron(a, b)
assert res.units == cm * s
def test_linalg_inv():
arr = np.random.random_sample((3, 3)) * cm
iarr = np.linalg.inv(arr)
assert 1 * iarr.units == 1 / cm
def test_linalg_tensorinv():
a = np.eye(4 * 6) * cm
a.shape = (4, 6, 8, 3)
ia = np.linalg.tensorinv(a)
assert 1 * ia.units == 1 / cm
def test_linalg_pinv():
a = np.random.randn(9, 6) * cm
B = np.linalg.pinv(a)
assert 1 * B.units == 1 / cm
np.testing.assert_allclose(a, np.dot(a, np.dot(B, a)))
np.testing.assert_allclose(B, np.dot(B, np.dot(a, B)))
# see https://github.com/numpy/numpy/issues/22444
@pytest.mark.xfail(
reason=(
"as of numpy 1.21.2, the __array_function__ protocol doesn't let "
"us overload np.linalg.pinv for stacks of matrices"
)
)
def test_matrix_stack_linalg_pinv():
stack = [np.eye(4) * g for _ in range(3)]
B = np.linalg.pinv(stack)
assert 1 * B.units == 1 / g
# see https://github.com/numpy/numpy/issues/22444
@pytest.mark.xfail(
reason=(
"as of numpy 1.21.2, the __array_function__ protocol doesn't let "
"us overload np.linalg.pinv for stacks of matrices"
)
)
def test_invalid_matrix_stack_linalg_pinv():
stack = [np.eye(4) * g, np.eye(4) * s]
with pytest.raises(
TypeError,
match=re.escape(
"numpy.linalg.pinv cannot operate on a stack "
"of matrices with different units."
),
):
np.linalg.pinv(stack)
def test_histogram():
arr = np.random.normal(size=1000) * cm
counts, bins = np.histogram(arr, bins=10, range=(arr.min(), arr.max()))
assert type(counts) is np.ndarray
assert bins.units == arr.units
def test_histogram2d():
x = np.random.normal(size=100) * cm
y = np.random.normal(loc=10, size=100) * s
counts, xbins, ybins = np.histogram2d(x, y)
assert counts.ndim == 2
assert xbins.units == x.units
assert ybins.units == y.units
def test_histogramdd():
x = np.random.normal(size=100) * cm
y = np.random.normal(size=100) * s
z = np.random.normal(size=100) * g
counts, (xbins, ybins, zbins) = np.histogramdd((x, y, z))
assert counts.ndim == 3
assert xbins.units == x.units
assert ybins.units == y.units
assert zbins.units == z.units
def test_histogram_bin_edges():
arr = np.random.normal(size=1000) * cm
bins = np.histogram_bin_edges(arr)
assert type(bins) is unyt_array
assert bins.units == arr.units
def test_concatenate():
x1 = np.random.normal(size=100) * cm
x2 = np.random.normal(size=100) * cm
res = np.concatenate((x1, x2))
assert res.units == cm
assert res.shape == (200,)
def test_concatenate_different_units():
x1 = np.random.normal(size=100) * cm
x2 = np.random.normal(size=100) * s
with pytest.raises(
UnitInconsistencyError,
match=(
r"Expected all unyt_array arguments to have identical units\. "
r"Received mixed units \(cm, s\)"
),
):
np.concatenate((x1, x2))
def test_cross():
x1 = np.random.random_sample((2, 2)) * cm
x2 = np.eye(2) * s
res = np.cross(x1, x2)
assert res.units == cm * s
def test_intersect1d():
x1 = [1, 2, 3, 4, 5, 6, 7, 8] * cm
x2 = [0, 2, 4, 6, 8] * cm
res = np.intersect1d(x1, x2)
assert res.units == cm
np.testing.assert_array_equal(res, [2, 4, 6, 8])
def test_intersect1d_return_indices():
x1 = [1, 2, 3, 4, 5, 6, 7, 8] * cm
x2 = [0, 2, 4, 6, 8] * cm
ures = np.intersect1d(x1, x2, return_indices=True)
rres = np.intersect1d(x1.d, x2.d, return_indices=True)
np.testing.assert_array_equal(ures, rres)
def test_union1d():
x1 = [-1, 0, 1] * cm
x2 = [-2, -1, -3] * cm
res = np.union1d(x1, x2)
assert res.units == cm
np.testing.assert_array_equal(res, [-3, -2, -1, 0, 1])
def test_linalg_norm():
x = [1, 1, 1] * s
res = np.linalg.norm(x)
assert res.units == s
assert res == pytest.approx(np.sqrt(3))
@pytest.mark.parametrize("func", [np.vstack, np.hstack, np.dstack, np.column_stack])
def test_xstack(func):
x1 = [0, 1, 2] * cm
x2 = [3, 4, 5] * cm
res = func((x1, x2))
assert type(res) is unyt_array
assert res.units == cm
@pytest.mark.parametrize(
"axis, expected", [(0, [[0, 1, 2], [3, 4, 5]]), (1, [[0, 3], [1, 4], [2, 5]])]
)
def test_stack(axis, expected):
x1 = [0, 1, 2] * cm
x2 = [3, 4, 5] * cm
res = np.stack((x1, x2), axis=axis)
assert res.units == cm
np.testing.assert_array_equal(res, expected)
def test_amax():
x1 = [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]] * cm
res = np.amax(x1)
assert type(res) is unyt_quantity
res = np.amax(x1, axis=1)
assert type(res) is unyt_array
def test_amin():
x1 = [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]] * cm
res = np.amin(x1)
assert type(res) is unyt_quantity
res = np.amin(x1, axis=1)
assert type(res) is unyt_array
def test_around():
x1 = [[1, 2, 3], [1, 2, 3], [1, 2, 3.0]] * g
res = np.around(x1, 2)
assert type(res) is unyt_array
assert res.units == g
def test_atleast_nd():
x0 = 1.0 * cm
x1 = np.atleast_1d(x0)
assert type(x1) is unyt_array
assert x1.ndim == 1
assert x1.units == cm
x2 = np.atleast_2d(x0)
assert type(x2) is unyt_array
assert x2.ndim == 2
assert x2.units == cm
x3 = np.atleast_3d(x0)
assert type(x3) is unyt_array
assert x3.ndim == 3
assert x3.units == cm
def test_average():
x1 = [0.0, 1.0, 2.0] * cm
res = np.average(x1)
assert type(res) is unyt_quantity
assert res == 1 * cm
def test_trim_zeros():
x1 = [0, 1, 2, 3, 0] * cm
res = np.trim_zeros(x1)
assert type(res) is unyt_array
def test_any():
assert not np.any([0, 0, 0] * cm)
assert np.any([1, 0, 0] * cm)
x = [1, 2, 3] * cm
assert np.any(x >= 3)
assert np.any(x >= 3 * cm)
assert not np.any(x >= 3 * km)
def test_append():
a = [0, 1, 2, 3] * cm
b = np.append(a, [4, 5, 6] * cm)
assert type(b) is unyt_array
assert b.units == cm
def test_append_inconsistent_units():
a = [0, 1, 2, 3] * cm
with pytest.raises(
UnitInconsistencyError,
match=re.escape(
r"Expected all unyt_array arguments to have identical units. "
r"Received mixed units (cm, dimensionless)"
),
):
np.append(a, [4, 5, 6])
def test_asfarray():
x1 = np.eye(3, dtype="int64") * cm
x2 = np.asfarray(x1)
assert type(x2) is unyt_array
assert x2.units == cm
assert x2.dtype == "float64"
def test_block():
x1 = 1 * np.ones((3, 3)) * cm
x2 = 2 * np.ones((3, 1)) * cm
res = np.block([[x1, x2]])
assert type(res) is unyt_array
assert res.units == cm
def test_block_units_inconsistency():
# check that unit inconsistency is correctly detected
# for nested lists
x1 = 1 * np.ones((3, 3)) * cm
x2 = [3 * cm, 3 * cm, 3 * km]
with pytest.raises(UnitInconsistencyError):
np.block([[x1, x2]])
def test_can_cast():
a = [0, 1, 2] * cm
assert np.can_cast(a, "float64")
assert np.can_cast(a, "int64")
assert not np.can_cast(a, "float16")
def test_isreal_like():
a = [1, 2, 3] * cm
assert np.all(np.isreal(a))
assert np.isrealobj(a)
assert not np.any(np.iscomplex(a))
assert not np.iscomplexobj(a)
b = [1j, 2j, 3j] * cm
assert not np.any(np.isreal(b))
assert not np.isrealobj(b)
assert np.all(np.iscomplex(b))
assert np.iscomplexobj(b)
@pytest.mark.parametrize(
"func",
[
np.fft.fft,
np.fft.hfft,
np.fft.rfft,
np.fft.ifft,
np.fft.ihfft,
np.fft.irfft,
],
)
def test_fft_1D(func):
x1 = [0, 1, 2] * cm
res = func(x1)
assert type(res) is unyt_array
assert res.units == (1 / cm).units
@pytest.mark.parametrize(
"func",
[
np.fft.fft2,
np.fft.fftn,
np.fft.rfft2,
np.fft.rfftn,
np.fft.ifft2,
np.fft.ifftn,
np.fft.irfft2,
np.fft.irfftn,
],
)
def test_fft_ND(func):
x1 = [[0, 1, 2], [0, 1, 2], [0, 1, 2]] * cm
res = func(x1)
assert type(res) is unyt_array
assert res.units == (1 / cm).units
@pytest.mark.parametrize("func", [np.fft.fftshift, np.fft.ifftshift])
def test_fft_shift(func):
x1 = [[0, 1, 2], [0, 1, 2], [0, 1, 2]] * cm
res = func(x1)
assert type(res) is unyt_array
assert res.units == cm
def test_trapz_no_x():
y = [0, 1, 2, 3] * cm
res = np.trapz(y)
assert type(res) is unyt_quantity
assert res.units == cm
def test_trapz_with_raw_x():
y = [0, 1, 2, 3] * cm
x = [0, 1, 2, 3]
res = np.trapz(y, x)
assert type(res) is unyt_quantity
assert res.units == cm
def test_trapz_with_unit_x():
y = [0, 1, 2, 3] * cm
x = [0, 1, 2, 3] * s
res = np.trapz(y, x)
assert type(res) is unyt_quantity
assert res.units == cm * s
def test_trapz_with_raw_dx():
y = [0, 1, 2, 3] * cm
dx = 2.0
res = np.trapz(y, dx=dx)
assert type(res) is unyt_quantity
assert res.units == cm
def test_trapz_with_unit_dx():
y = [0, 1, 2, 3] * cm
dx = 2.0 * s
res = np.trapz(y, dx=dx)
assert type(res) is unyt_quantity
assert res.units == cm * s
@pytest.mark.parametrize(
"op",
["min", "max", "mean", "median", "sum", "nanmin", "nanmax", "nanmean", "nanmedian"],
)
def test_scalar_reduction(op):
x = [0, 1, 2] * cm
res = getattr(np, op)(x)
assert type(res) is unyt_quantity
assert res.units == cm
@pytest.mark.parametrize("op", ["sort", "sort_complex"])
def test_sort(op):
x = [2, 0, 1] * cm
res = getattr(np, op)(x)
assert type(res) is unyt_array
assert res.units == cm
def test_repeat():
x = [2, 0, 1] * cm
res = np.repeat(x, 2)
assert type(res) is unyt_array
assert res.units == cm
def test_tile():
x = [2, 0, 1] * cm
res = np.tile(x, (2, 3))
assert type(res) is unyt_array
assert res.units == cm
def test_shares_memory():
x = [1, 2, 3] * cm
assert np.shares_memory(x, x.view(np.ndarray))
def test_nonzero():
x = [1, 2, 0] * cm
res = np.nonzero(x)
assert len(res) == 1
np.testing.assert_array_equal(res[0], [0, 1])
res2 = np.flatnonzero(x)
np.testing.assert_array_equal(res[0], res2)
def test_isinf():
x = [1, float("inf"), float("-inf")] * cm
res = np.isneginf(x)
np.testing.assert_array_equal(res, [False, False, True])
res = np.isposinf(x)
np.testing.assert_array_equal(res, [False, True, False])
def test_allclose():
x = [1, 2, 3] * cm
y = [1, 2, 3] * km
assert not np.allclose(x, y)
@pytest.mark.parametrize(
"a, b, expected",
[
([1, 2, 3] * cm, [1, 2, 3] * km, [False] * 3),
([1, 2, 3] * cm, [1, 2, 3], [True] * 3),
],
)
def test_isclose(a, b, expected):
res = np.isclose(a, b)
np.testing.assert_array_equal(res, expected)
def test_iclose_error():
x = [1, 2, 3] * cm
y = [1, 2, 3] * g
with pytest.raises(UnitConversionError):
np.isclose(x, y)
@pytest.mark.parametrize(
"func",
[
np.linspace,
np.logspace,
np.geomspace,
],
)
def test_xspace(func):
res = func(1 * cm, 11 * cm, 10)
assert type(res) is unyt_array
assert res.units == cm
def test_full_like():
x = [1, 2, 3] * cm
res = np.full_like(x, 6 * cm)
assert type(res) is unyt_array
assert res.units == cm
@pytest.mark.parametrize(
"func",
[
np.empty_like,
np.zeros_like,
np.ones_like,
],
)
def test_x_like(func):
x = unyt_array([1, 2, 3], cm, dtype="float32")
res = func(x)
assert type(res) is unyt_array
assert res.units == x.units
assert res.shape == x.shape
assert res.dtype == x.dtype
def test_copy():
x = [1, 2, 3] * cm
y = np.copy(x)
# by default, subok=False, so we shouldn't
# expect a unyt_array without switching this arg
assert type(y) is np.ndarray
@pytest.mark.skipif(
NUMPY_VERSION < Version("1.19"), reason="np.copy's subok arg requires numpy 1.19+"
)
def test_copy_subok():
x = [1, 2, 3] * cm
y = np.copy(x, subok=True)
assert type(y) is unyt_array
assert y.units == cm
def test_copyto():
x = [1, 2, 3] * cm
y = np.empty_like(x)
np.copyto(y, x)
assert type(y) is unyt_array
assert y.units == cm
np.testing.assert_array_equal(x, y)
def test_copyto_edge_cases():
x = [1, 2, 3] * cm
y = [1, 2, 3] * g
# copying to an array with a different unit is supported
# to be in line with how we treat the 'out' param in most
# numpy operations
np.copyto(y, x)
assert type(y) is unyt_array
assert y.units == cm
y = np.empty_like(x.view(np.ndarray))
np.copyto(y, x)
assert type(y) is np.ndarray
def test_meshgrid():
x = [1, 2, 3] * cm
y = [1, 2, 3] * s
x2d, y2d = np.meshgrid(x, y)
assert type(x2d) is unyt_array
assert type(y2d) is unyt_array
assert x2d.units == cm
assert y2d.units == s
@pytest.mark.parametrize(
"func, args, kwargs",
[
(np.transpose, (), {}),
(np.reshape, ((9, 2),), {}),
(np.resize, ((3, 6),), {}),
(np.expand_dims, (0,), {}),
(np.squeeze, (), {}),
(np.swapaxes, (0, 1), {}),
(np.moveaxis, (0, 2), {}),
(np.rot90, (), {}),
(np.roll, (3,), {}),
(np.rollaxis, (2,), {}),
(np.flip, (), {}),
(np.fliplr, (), {}),
(np.flipud, (), {}),
(np.broadcast_to, ((1, 1, 2, 3, 3),), {"subok": True}),
(np.delete, (0, 1), {}),
(np.partition, (2,), {}),
],
)
def test_reshaper(func, args, kwargs):
x = [
[
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
]
] * cm
y = func(x, *args, **kwargs)
assert type(y) is unyt_array
assert y.units == cm
def test_broadcast_arrays():
x = [1, 2, 3] * cm
y = [
4,
] * g
res = np.broadcast_arrays(x, y, subok=True)
assert all(type(_) is unyt_array for _ in res)
@pytest.mark.parametrize(
"func, args",
[
(np.split, (3, 2)),
(np.dsplit, (3,)),
(np.hsplit, (2,)),
(np.vsplit, (1,)),
(np.array_split, (3,)),
],
)
def test_xsplit(func, args):
x = [
[
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
]
] * cm
y = func(x, *args)
assert all(type(_) is unyt_array for _ in y)
assert all(_.units == cm for _ in y)
@pytest.mark.parametrize(
"func, expected_units",
[
(np.prod, cm**9),
(np.var, cm**2),
(np.std, cm),
(np.nanprod, cm**9),
(np.nansum, cm),
(np.nanvar, cm**2),
(np.nanstd, cm),
(np.trace, cm),
],