|
| 1 | +""" |
| 2 | +Tests for the _to_numpy function in the clib.conversion module. |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import numpy.testing as npt |
| 7 | +import pytest |
| 8 | +from pygmt.clib.conversion import _to_numpy |
| 9 | +from pygmt.clib.session import DTYPES |
| 10 | + |
| 11 | + |
| 12 | +def _check_result(result, supported): |
| 13 | + """ |
| 14 | + Check the result of the _to_numpy function. |
| 15 | + """ |
| 16 | + # Check that the result is a NumPy array and is C-contiguous. |
| 17 | + assert isinstance(result, np.ndarray) |
| 18 | + assert result.flags.c_contiguous |
| 19 | + # Check that the dtype is supported by PyGMT (or the GMT C API). |
| 20 | + assert (result.dtype.type in DTYPES) == supported |
| 21 | + |
| 22 | + |
| 23 | +######################################################################################## |
| 24 | +# Test the _to_numpy function with NumPy dtypes. |
| 25 | +# |
| 26 | +# There are 24 fundamental dtypes in NumPy. Not all of them are supported by PyGMT. |
| 27 | +# Reference: https://numpy.org/doc/2.1/reference/arrays.scalars.html |
| 28 | +# |
| 29 | +# - Numeric dtypes: |
| 30 | +# - int8, int16, int32, int64, longlong |
| 31 | +# - uint8, uint16, uint32, uint64, ulonglong |
| 32 | +# - float16, float32, float64, longdouble |
| 33 | +# - complex64, complex128, clongdouble |
| 34 | +# - bool |
| 35 | +# - datetime64, timedelta64 |
| 36 | +# - str_ |
| 37 | +# - bytes_ |
| 38 | +# - object_ |
| 39 | +# - void |
| 40 | +######################################################################################## |
| 41 | +@pytest.mark.parametrize( |
| 42 | + ("dtype", "supported"), |
| 43 | + [ |
| 44 | + (np.int8, True), |
| 45 | + (np.int16, True), |
| 46 | + (np.int32, True), |
| 47 | + (np.int64, True), |
| 48 | + (np.longlong, True), |
| 49 | + (np.uint8, True), |
| 50 | + (np.uint16, True), |
| 51 | + (np.uint32, True), |
| 52 | + (np.uint64, True), |
| 53 | + (np.ulonglong, True), |
| 54 | + (np.float16, False), |
| 55 | + (np.float32, True), |
| 56 | + (np.float64, True), |
| 57 | + (np.longdouble, False), |
| 58 | + (np.complex64, False), |
| 59 | + (np.complex128, False), |
| 60 | + (np.clongdouble, False), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, supported): |
| 64 | + """ |
| 65 | + Test the _to_numpy function with NumPy arrays of NumPy numeric dtypes. |
| 66 | +
|
| 67 | + "dtype" is the NumPy dtype to be tested and "supported" is a boolean value |
| 68 | + indicating whether the dtype is supported by PyGMT (or the GMT C API). |
| 69 | + """ |
| 70 | + # 1-D array |
| 71 | + array = np.array([1, 2, 3], dtype=dtype) |
| 72 | + assert array.dtype == dtype |
| 73 | + result = _to_numpy(array) |
| 74 | + _check_result(result, supported) |
| 75 | + npt.assert_array_equal(result, array) |
| 76 | + |
| 77 | + # 2-D array |
| 78 | + array = np.array([[1, 2, 3], [4, 5, 6]], dtype=dtype) |
| 79 | + assert array.dtype == dtype |
| 80 | + result = _to_numpy(array) |
| 81 | + _check_result(result, supported) |
| 82 | + npt.assert_array_equal(result, array) |
0 commit comments