-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clib.converison._to_numpy: Add tests for numpy numeric dtypes
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
Tests for the _to_numpy function in the clib.conversion module. | ||
""" | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
import pytest | ||
from pygmt.clib.conversion import _to_numpy | ||
from pygmt.clib.session import DTYPES | ||
|
||
|
||
def _check_result(result, supported): | ||
""" | ||
Check the result of the _to_numpy function. | ||
""" | ||
# Check that the result is a NumPy array and is C-contiguous. | ||
assert isinstance(result, np.ndarray) | ||
assert result.flags.c_contiguous | ||
# Check that the dtype is supported by PyGMT (or the GMT C API). | ||
assert (result.dtype.type in DTYPES) == supported | ||
|
||
|
||
######################################################################################## | ||
# Test the _to_numpy function with NumPy dtypes. | ||
# | ||
# There are 24 fundamental dtypes in NumPy. Not all of them are supported by PyGMT. | ||
# Reference: https://numpy.org/doc/2.1/reference/arrays.scalars.html | ||
# | ||
# - Numeric dtypes: | ||
# - int8, int16, int32, int64, longlong | ||
# - uint8, uint16, uint32, uint64, ulonglong | ||
# - float16, float32, float64, longdouble | ||
# - complex64, complex128, clongdouble | ||
# - bool | ||
# - datetime64, timedelta64 | ||
# - str_ | ||
# - bytes_ | ||
# - object_ | ||
# - void | ||
######################################################################################## | ||
@pytest.mark.parametrize( | ||
("dtype", "supported"), | ||
[ | ||
(np.int8, True), | ||
(np.int16, True), | ||
(np.int32, True), | ||
(np.int64, True), | ||
(np.longlong, True), | ||
(np.uint8, True), | ||
(np.uint16, True), | ||
(np.uint32, True), | ||
(np.uint64, True), | ||
(np.ulonglong, True), | ||
(np.float16, False), | ||
(np.float32, True), | ||
(np.float64, True), | ||
(np.longdouble, False), | ||
(np.complex64, False), | ||
(np.complex128, False), | ||
(np.clongdouble, False), | ||
], | ||
) | ||
def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, supported): | ||
""" | ||
Test the _to_numpy function with NumPy arrays of NumPy numeric dtypes. | ||
"dtype" is the NumPy dtype to be tested and "supported" is a boolean value | ||
indicating whether the dtype is supported by PyGMT (or the GMT C API). | ||
""" | ||
# 1-D array | ||
array = np.array([1, 2, 3], dtype=dtype) | ||
assert array.dtype == dtype | ||
result = _to_numpy(array) | ||
_check_result(result, supported) | ||
npt.assert_array_equal(result, array) | ||
|
||
# 2-D array | ||
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=dtype) | ||
assert array.dtype == dtype | ||
result = _to_numpy(array) | ||
_check_result(result, supported) | ||
npt.assert_array_equal(result, array) |