-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clib.converison._to_numpy: Add tests for numpy arrays of numpy numeric dtypes #3583
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
127d8ca
clib.converison._to_numpy: Add tests for numpy numeric dtypes
seisman 6ae1ddb
Add tests for pandas.Series with NumPy numeric dtypes
seisman a9635b5
Improve docstrings for tests
seisman 6f966db
Add tests for Python built-in types
seisman 9fd655b
Merge branch 'main' into to_numpy/numpy_numeric
seisman f9bf19c
Refactor a few tests
seisman 8bc2f56
Check the expected dtype
seisman 42a0951
Define params list
seisman 0d102a2
Merge branch 'main' into to_numpy/numpy_numeric
seisman 933bc62
Input array now is not C-contiguous
seisman 4edfef0
Add test for Python built-in complex dtype
seisman b8f6d12
Merge branch 'main' into to_numpy/numpy_numeric
seisman fddb53a
Windows default integer dtype is np.int32 for NumPy 1.x
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,154 @@ | ||
""" | ||
Tests for the _to_numpy function in the clib.conversion module. | ||
""" | ||
|
||
import sys | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
import pandas as pd | ||
import pytest | ||
from packaging.version import Version | ||
from pygmt.clib.conversion import _to_numpy | ||
|
||
|
||
def _check_result(result, expected_dtype): | ||
""" | ||
A helper function to check if the result of the _to_numpy function is a C-contiguous | ||
NumPy array with the expected dtype. | ||
""" | ||
assert isinstance(result, np.ndarray) | ||
assert result.flags.c_contiguous | ||
assert result.dtype.type == expected_dtype | ||
|
||
|
||
######################################################################################## | ||
# Test the _to_numpy function with Python built-in types. | ||
######################################################################################## | ||
@pytest.mark.parametrize( | ||
("data", "expected_dtype"), | ||
[ | ||
pytest.param( | ||
[1, 2, 3], | ||
np.int32 | ||
if sys.platform == "win32" and Version(np.__version__) < Version("2.0") | ||
else np.int64, | ||
id="int", | ||
), | ||
pytest.param([1.0, 2.0, 3.0], np.float64, id="float"), | ||
pytest.param( | ||
[complex(+1), complex(-2j), complex("-Infinity+NaNj")], | ||
np.complex128, | ||
id="complex", | ||
), | ||
], | ||
) | ||
def test_to_numpy_python_types_numeric(data, expected_dtype): | ||
""" | ||
Test the _to_numpy function with Python built-in numeric types. | ||
""" | ||
result = _to_numpy(data) | ||
_check_result(result, expected_dtype) | ||
npt.assert_array_equal(result, data) | ||
|
||
|
||
######################################################################################## | ||
# Test the _to_numpy function with NumPy arrays. | ||
# | ||
# There are 24 fundamental dtypes in NumPy. Not all of them are supported by PyGMT. | ||
# | ||
# - 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 | ||
# | ||
# Reference: https://numpy.org/doc/2.1/reference/arrays.scalars.html | ||
######################################################################################## | ||
np_dtype_params = [ | ||
pytest.param(np.int8, np.int8, id="int8"), | ||
pytest.param(np.int16, np.int16, id="int16"), | ||
pytest.param(np.int32, np.int32, id="int32"), | ||
pytest.param(np.int64, np.int64, id="int64"), | ||
pytest.param(np.longlong, np.longlong, id="longlong"), | ||
pytest.param(np.uint8, np.uint8, id="uint8"), | ||
pytest.param(np.uint16, np.uint16, id="uint16"), | ||
pytest.param(np.uint32, np.uint32, id="uint32"), | ||
pytest.param(np.uint64, np.uint64, id="uint64"), | ||
pytest.param(np.ulonglong, np.ulonglong, id="ulonglong"), | ||
pytest.param(np.float16, np.float16, id="float16"), | ||
pytest.param(np.float32, np.float32, id="float32"), | ||
pytest.param(np.float64, np.float64, id="float64"), | ||
pytest.param(np.longdouble, np.longdouble, id="longdouble"), | ||
pytest.param(np.complex64, np.complex64, id="complex64"), | ||
pytest.param(np.complex128, np.complex128, id="complex128"), | ||
pytest.param(np.clongdouble, np.clongdouble, id="clongdouble"), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize(("dtype", "expected_dtype"), np_dtype_params) | ||
def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, expected_dtype): | ||
""" | ||
Test the _to_numpy function with NumPy arrays of NumPy numeric dtypes. | ||
|
||
Test both 1-D and 2-D arrays which are not C-contiguous. | ||
""" | ||
# 1-D array that is not C-contiguous | ||
array = np.array([1, 2, 3, 4, 5, 6], dtype=dtype)[::2] | ||
assert array.flags.c_contiguous is False | ||
result = _to_numpy(array) | ||
_check_result(result, expected_dtype) | ||
npt.assert_array_equal(result, array, strict=True) | ||
|
||
# 2-D array that is not C-contiguous | ||
array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=dtype)[::2, ::2] | ||
assert array.flags.c_contiguous is False | ||
result = _to_numpy(array) | ||
_check_result(result, expected_dtype) | ||
npt.assert_array_equal(result, array, strict=True) | ||
|
||
|
||
######################################################################################## | ||
# Test the _to_numpy function with pandas.Series. | ||
# | ||
# In pandas, dtype can be specified by | ||
# | ||
# 1. NumPy dtypes (see above) | ||
# 2. pandas dtypes | ||
# 3. PyArrow dtypes | ||
# | ||
# pandas provides following dtypes: | ||
# | ||
# - Numeric dtypes: | ||
# - Int8, Int16, Int32, Int64 | ||
# - UInt8, UInt16, UInt32, UInt64 | ||
# - Float32, Float64 | ||
# - DatetimeTZDtype | ||
# - PeriodDtype | ||
# - IntervalDtype | ||
# - StringDtype | ||
# - CategoricalDtype | ||
# - SparseDtype | ||
# - BooleanDtype | ||
# - ArrowDtype: a special dtype used to store data in the PyArrow format. | ||
# | ||
# References: | ||
# 1. https://pandas.pydata.org/docs/reference/arrays.html | ||
# 2. https://pandas.pydata.org/docs/user_guide/basics.html#basics-dtypes | ||
# 3. https://pandas.pydata.org/docs/user_guide/pyarrow.html | ||
######################################################################################## | ||
@pytest.mark.parametrize(("dtype", "expected_dtype"), np_dtype_params) | ||
def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, expected_dtype): | ||
""" | ||
Test the _to_numpy function with pandas.Series of NumPy numeric dtypes. | ||
""" | ||
series = pd.Series([1, 2, 3, 4, 5, 6], dtype=dtype)[::2] # Not C-contiguous | ||
result = _to_numpy(series) | ||
_check_result(result, expected_dtype) | ||
npt.assert_array_equal(result, series) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's necessary, but Python does have built-in complex number types (https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)
The Python standard library also includes fractions.Fraction and decimal.Decimal, but I don't know if anyone really uses those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can't be converted to a numpy array, so PyGMT can't support them anyway.