Skip to content
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 13 commits into from
Nov 7, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 5, 2024

This PR adds tests for _to_numpy to ensure it works for the 17 NumPy numeric dtypes, in which 12 are supported by PyGMT.

Related to #3581, #2848, #3513.

@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Nov 5, 2024
@seisman seisman added this to the 0.14.0 milestone Nov 5, 2024
@seisman seisman force-pushed the to_numpy/numpy_numeric branch from 46f8962 to 7aa67f4 Compare November 5, 2024 09:40
@seisman seisman changed the title clib.converison._to_numpy: Add tests for numpy numeric dtypes clib.converison._to_numpy: Add tests for numpy arrays with numpy numeric dtypes Nov 5, 2024
@seisman seisman force-pushed the to_numpy/numpy_numeric branch from 1e1ca8e to c14bcd2 Compare November 5, 2024 10:10
(np.clongdouble, False),
],
)
def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, supported):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name test_to_numpy_ndarray_numpy_dtypes_numeric is in the format of

test_to_numpy_<data-structure-to-test>_<numpy/pandas/pyarrow>_dtypes_<numeric/str/bool/datetime>

(np.clongdouble, False),
],
)
def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, supported):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is almost the same as test_to_numpy_ndarray_numpy_dtypes_numeric and can be combined into one single test, but I feel two separate tests are more readable and maintainable.

@seisman seisman force-pushed the to_numpy/numpy_numeric branch from c14bcd2 to 01abb5e Compare November 5, 2024 10:17
@seisman seisman changed the title clib.converison._to_numpy: Add tests for numpy arrays with numpy numeric dtypes clib.converison._to_numpy: Add tests for numpy arrays of numpy numeric dtypes Nov 5, 2024
@seisman seisman force-pushed the to_numpy/numpy_numeric branch from 8489ebe to 6f966db Compare November 6, 2024 06:16
@pytest.mark.parametrize(
("dtype", "supported"),
[
(np.int8, True),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking if the result ndarray dtype is supported or not, I'm wondering if we should explicitly check the result ndarray dtype. The main reasons is to ensure that dtypes are converted to the expected numpy dtypes (e.g., pd.Int8Dtype is converted to np.int8, not np.int64).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8bc2f56.

pygmt/tests/test_clib_to_numpy.py Outdated Show resolved Hide resolved
pygmt/tests/test_clib_to_numpy.py Outdated Show resolved Hide resolved
Comment on lines 82 to 89
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.
"""
# 1-D array
array = np.array([1, 2, 3], dtype=dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test feels a bit redundant, given that we are converting numpy C-order to numpy C-order which will obviously work. Should we test with order="F" instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point. Instead of testing order="F" which is usually less used, I've used array slice which is not C-contiguous as input (see 933bc62).

seisman and others added 3 commits November 7, 2024 07:08
@seisman seisman force-pushed the to_numpy/numpy_numeric branch from 202c3e6 to 933bc62 Compare November 6, 2024 23:32
pygmt/tests/test_clib_to_numpy.py Outdated Show resolved Hide resolved
("data", "expected_dtype"),
[
pytest.param([1, 2, 3], np.int64, id="int"),
pytest.param([1.0, 2.0, 3.0], np.float64, id="float"),
Copy link
Member

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)

Suggested change
pytest.param([1.0, 2.0, 3.0], np.float64, id="float"),
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",
),

The Python standard library also includes fractions.Fraction and decimal.Decimal, but I don't know if anyone really uses those.

Copy link
Member Author

@seisman seisman Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python standard library also includes fractions.Fraction and decimal.Decimal, but I don't know if anyone really uses those.

They can't be converted to a numpy array, so PyGMT can't support them anyway.

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman seisman removed the needs review This PR has higher priority and needs review. label Nov 7, 2024
@seisman seisman added the final review call This PR requires final review and approval from a second reviewer label Nov 7, 2024
@seisman seisman merged commit dd78693 into main Nov 7, 2024
18 checks passed
@seisman seisman deleted the to_numpy/numpy_numeric branch November 7, 2024 10:38
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Nov 7, 2024
seisman added a commit that referenced this pull request Nov 18, 2024
…c dtypes (#3583)

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
seisman added a commit that referenced this pull request Nov 18, 2024
…c dtypes (#3583)

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
seisman added a commit that referenced this pull request Nov 18, 2024
…c dtypes (#3583)

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
seisman added a commit that referenced this pull request Nov 19, 2024
…c dtypes (#3583)

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants