Skip to content

clib.converison._to_numpy: Add tests for numpy arrays of numpy numeric dtypes - #3583

Merged
seisman merged 13 commits into
mainfrom
to_numpy/numpy_numeric
Nov 7, 2024
Merged

clib.converison._to_numpy: Add tests for numpy arrays of numpy numeric dtypes#3583
seisman merged 13 commits into
mainfrom
to_numpy/numpy_numeric

Conversation

@seisman

@seismanseisman commented Nov 5, 2024

Copy link
Copy Markdown
Member

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.

@seismanseisman 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
@seismanseisman added this to the 0.14.0 milestone Nov 5, 2024
@seisman
seismanforce-pushed the to_numpy/numpy_numeric branch from 46f8962 to 7aa67f4CompareNovember 5, 2024 09:40
@seismanseisman changed the title clib.converison._to_numpy: Add tests for numpy numeric dtypesclib.converison._to_numpy: Add tests for numpy arrays with numpy numeric dtypesNov 5, 2024
@seisman
seismanforce-pushed the to_numpy/numpy_numeric branch from 1e1ca8e to c14bcd2CompareNovember 5, 2024 10:10
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
(np.clongdouble, False),
],
)
def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, supported):

Copy link
Copy Markdown
MemberAuthor

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>

Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
(np.clongdouble, False),
],
)
def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, supported):

Copy link
Copy Markdown
MemberAuthor

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
seismanforce-pushed the to_numpy/numpy_numeric branch from c14bcd2 to 01abb5eCompareNovember 5, 2024 10:17
@seismanseisman changed the title clib.converison._to_numpy: Add tests for numpy arrays with numpy numeric dtypesclib.converison._to_numpy: Add tests for numpy arrays of numpy numeric dtypesNov 5, 2024
@seisman
seismanforce-pushed the to_numpy/numpy_numeric branch from 8489ebe to 6f966dbCompareNovember 6, 2024 06:16
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
@pytest.mark.parametrize(
("dtype", "supported"),
[
(np.int8, True),

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Done in 8bc2f56.

Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
seismanforce-pushed the to_numpy/numpy_numeric branch from 202c3e6 to 933bc62CompareNovember 6, 2024 23:32
Comment threadpygmt/tests/test_clib_to_numpy.py Outdated
("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
Copy Markdown
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.

@seismanseismanNov 6, 2024

Copy link
Copy Markdown
MemberAuthor

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>
@seismanseisman removed the needs review This PR has higher priority and needs review. label Nov 7, 2024
@seismanseisman 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 mainNov 7, 2024
@seisman
seisman deleted the to_numpy/numpy_numeric branch November 7, 2024 10:38
@seismanseisman 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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenanceBoring but important stuff for the core devsskip-changelogSkip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@seisman@weiji14