-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support N-d sequences in
TypeTracer.asarray
(#2361)
- Loading branch information
Showing
2 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from awkward._nplikes.typetracer import TypeTracer | ||
|
||
typetracer = TypeTracer.instance() | ||
|
||
|
||
def test_nd(): | ||
data = [ | ||
[ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
], | ||
[ | ||
[1, 2, 3], | ||
[4, 5, 8], | ||
], | ||
] | ||
result = typetracer.asarray(data, dtype=np.uint8) | ||
assert result.dtype == np.dtype(np.uint8) | ||
assert result.shape == (2, 2, 3) | ||
|
||
# Check default size of array | ||
array = np.array([1, 2, 3]) | ||
default_int_dtype = array.dtype | ||
|
||
result = typetracer.asarray(data) | ||
assert result.dtype == default_int_dtype | ||
assert result.shape == (2, 2, 3) | ||
|
||
|
||
def test_nd_ragged(): | ||
data = [ | ||
[ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
], | ||
[ | ||
[1, 2, 3], | ||
[4, 5], | ||
], | ||
] | ||
with pytest.raises(ValueError, match=r"sequence at dimension .* does not match"): | ||
typetracer.asarray(data) | ||
|
||
|
||
def test_unknown_scalar(): | ||
unknown_array = typetracer.asarray([0, 1, 2.0], dtype=np.dtype(np.float64)) | ||
|
||
data = [ | ||
[ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
], | ||
[ | ||
[1, 2, 3], | ||
[4, 5, unknown_array[0]], | ||
], | ||
] | ||
result = typetracer.asarray(data) | ||
assert result.dtype == np.dtype(np.float64) | ||
assert result.shape == (2, 2, 3) |