From f8929bb49c0b001968e8044b9abb2f836b4e7c7b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 6 Dec 2023 21:34:30 +0800 Subject: [PATCH 1/4] Improve the error messages for unsupported numpy dtypes --- pygmt/clib/session.py | 7 +++++-- pygmt/tests/test_clib_put_vector.py | 28 ++++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 6e5a62b69d8..bb8e294351b 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -834,8 +834,11 @@ def _check_dtype_and_dim(self, array, ndim): # Check that the array has a valid/known data type if array.dtype.type not in DTYPES: try: - # Try to convert any unknown numpy data types to np.datetime64 - array = array_to_datetime(array) + if array.dtype.type is np.object_: + # Try to convert unknown object type to np.datetime64 + array = array_to_datetime(array) + else: + raise ValueError except ValueError as e: raise GMTInvalidInput( f"Unsupported numpy data type '{array.dtype.type}'." diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index e9da9e6242a..470882c0efc 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -170,16 +170,24 @@ def test_put_vector_invalid_dtype(): """ Check that it fails with an exception for invalid data types. """ - with clib.Session() as lib: - dataset = lib.create_data( - family="GMT_IS_DATASET|GMT_VIA_VECTOR", - geometry="GMT_IS_POINT", - mode="GMT_CONTAINER_ONLY", - dim=[2, 3, 1, 0], # columns, rows, layers, dtype - ) - data = np.array([37, 12, 556], dtype="object") - with pytest.raises(GMTInvalidInput): - lib.put_vector(dataset, column=1, vector=data) + for dtype in [ + np.float16, + np.object_, + np.bool_, + np.csingle, + np.complex_, + np.clongfloat, + ]: + with clib.Session() as lib: + dataset = lib.create_data( + family="GMT_IS_DATASET|GMT_VIA_VECTOR", + geometry="GMT_IS_POINT", + mode="GMT_CONTAINER_ONLY", + dim=[2, 3, 1, 0], # columns, rows, layers, dtype + ) + data = np.array([37, 12, 556], dtype=dtype) + with pytest.raises(GMTInvalidInput, match="Unsupported numpy data type"): + lib.put_vector(dataset, column=1, vector=data) def test_put_vector_wrong_column(): From 94ed3496a0d50b720d56699f7ebca3b0ed9967a0 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Dec 2023 10:56:01 +0800 Subject: [PATCH 2/4] Order dtypes alphabetically --- pygmt/tests/test_clib_put_vector.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index 470882c0efc..4f3943258e1 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -171,12 +171,14 @@ def test_put_vector_invalid_dtype(): Check that it fails with an exception for invalid data types. """ for dtype in [ + np.bool_, + np.bytes_, + np.complex64, + np.complex128, + np.complex256, np.float16, + np.float128, np.object_, - np.bool_, - np.csingle, - np.complex_, - np.clongfloat, ]: with clib.Session() as lib: dataset = lib.create_data( From c5ca123f3ea5b5c33a93479390a73b5260b717ef Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Dec 2023 11:18:32 +0800 Subject: [PATCH 3/4] np.complex256 is not supported on Windows --- pygmt/tests/test_clib_put_vector.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index 4f3943258e1..4071cdfced0 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -173,9 +173,9 @@ def test_put_vector_invalid_dtype(): for dtype in [ np.bool_, np.bytes_, - np.complex64, - np.complex128, - np.complex256, + np.csingle, + np.complex_, + np.clongfloat, np.float16, np.float128, np.object_, From 578daf1897aa2668703a092077525f9e59dae0c7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Dec 2023 11:36:55 +0800 Subject: [PATCH 4/4] Fix dtype names --- pygmt/tests/test_clib_put_vector.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_clib_put_vector.py b/pygmt/tests/test_clib_put_vector.py index 4071cdfced0..09a4235298b 100644 --- a/pygmt/tests/test_clib_put_vector.py +++ b/pygmt/tests/test_clib_put_vector.py @@ -174,10 +174,10 @@ def test_put_vector_invalid_dtype(): np.bool_, np.bytes_, np.csingle, - np.complex_, + np.cdouble, np.clongfloat, - np.float16, - np.float128, + np.half, + np.longdouble, np.object_, ]: with clib.Session() as lib: