Skip to content

Commit 9f9b7f6

Browse files
seismanweiji14
andauthored
Support passing data in numpy int8, int16, uint8 and uint16 dtypes to GMT (#1963)
Co-authored-by: Wei Ji <[email protected]>
1 parent 1067fa3 commit 9f9b7f6

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

pygmt/clib/session.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@
5353
REGISTRATIONS = ["GMT_GRID_PIXEL_REG", "GMT_GRID_NODE_REG"]
5454

5555
DTYPES = {
56-
np.float64: "GMT_DOUBLE",
57-
np.float32: "GMT_FLOAT",
58-
np.int64: "GMT_LONG",
56+
np.int8: "GMT_CHAR",
57+
np.int16: "GMT_SHORT",
5958
np.int32: "GMT_INT",
60-
np.uint64: "GMT_ULONG",
59+
np.int64: "GMT_LONG",
60+
np.uint8: "GMT_UCHAR",
61+
np.uint16: "GMT_USHORT",
6162
np.uint32: "GMT_UINT",
62-
np.datetime64: "GMT_DATETIME",
63+
np.uint64: "GMT_ULONG",
64+
np.float32: "GMT_FLOAT",
65+
np.float64: "GMT_DOUBLE",
6366
np.str_: "GMT_TEXT",
67+
np.datetime64: "GMT_DATETIME",
6468
}
6569

6670

@@ -732,8 +736,8 @@ def put_vector(self, dataset, column, vector):
732736
The dataset must be created by :meth:`pygmt.clib.Session.create_data`
733737
first. Use ``family='GMT_IS_DATASET|GMT_VIA_VECTOR'``.
734738
735-
Not at all numpy dtypes are supported, only: float64, float32, int64,
736-
int32, uint64, uint32, datetime64 and str\_.
739+
Not all numpy dtypes are supported, only: int8, int16, int32, int64,
740+
uint8, uint16, uint32, uint64, float32, float64, str\_ and datetime64.
737741
738742
.. warning::
739743
The numpy array must be C contiguous in memory. If it comes from a
@@ -856,8 +860,8 @@ def put_matrix(self, dataset, matrix, pad=0):
856860
The dataset must be created by :meth:`pygmt.clib.Session.create_data`
857861
first. Use ``|GMT_VIA_MATRIX'`` in the family.
858862
859-
Not at all numpy dtypes are supported, only: float64, float32, int64,
860-
int32, uint64, and uint32.
863+
Not all numpy dtypes are supported, only: int8, int16, int32, int64,
864+
uint8, uint16, uint32, uint64, float32 and float64.
861865
862866
.. warning::
863867
The numpy array must be C contiguous in memory. Use

pygmt/tests/test_clib.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def data():
3636
return np.loadtxt(POINTS_DATA)
3737

3838

39+
@pytest.fixture(scope="module", name="dtypes")
40+
def fixture_dtypes():
41+
"""
42+
List of supported numpy dtypes.
43+
"""
44+
return "int8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64".split()
45+
46+
3947
@contextmanager
4048
def mock(session, func, returns=None, mock_func=None):
4149
"""
@@ -339,11 +347,10 @@ def test_create_data_fails():
339347
)
340348

341349

342-
def test_virtual_file():
350+
def test_virtual_file(dtypes):
343351
"""
344352
Test passing in data via a virtual file with a Dataset.
345353
"""
346-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
347354
shape = (5, 3)
348355
for dtype in dtypes:
349356
with clib.Session() as lib:
@@ -497,11 +504,10 @@ def test_virtualfile_from_data_fail_non_valid_data(data):
497504
)
498505

499506

500-
def test_virtualfile_from_vectors():
507+
def test_virtualfile_from_vectors(dtypes):
501508
"""
502509
Test the automation for transforming vectors to virtual file dataset.
503510
"""
504-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
505511
size = 10
506512
for dtype in dtypes:
507513
x = np.arange(size, dtype=dtype)
@@ -588,11 +594,10 @@ def test_virtualfile_from_vectors_diff_size():
588594
print("This should have failed")
589595

590596

591-
def test_virtualfile_from_matrix():
597+
def test_virtualfile_from_matrix(dtypes):
592598
"""
593599
Test transforming a matrix to virtual file dataset.
594600
"""
595-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
596601
shape = (7, 5)
597602
for dtype in dtypes:
598603
data = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape)
@@ -606,11 +611,10 @@ def test_virtualfile_from_matrix():
606611
assert output == expected
607612

608613

609-
def test_virtualfile_from_matrix_slice():
614+
def test_virtualfile_from_matrix_slice(dtypes):
610615
"""
611616
Test transforming a slice of a larger array to virtual file dataset.
612617
"""
613-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
614618
shape = (10, 6)
615619
for dtype in dtypes:
616620
full_data = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape)
@@ -627,11 +631,10 @@ def test_virtualfile_from_matrix_slice():
627631
assert output == expected
628632

629633

630-
def test_virtualfile_from_vectors_pandas():
634+
def test_virtualfile_from_vectors_pandas(dtypes):
631635
"""
632636
Pass vectors to a dataset using pandas Series.
633637
"""
634-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
635638
size = 13
636639
for dtype in dtypes:
637640
data = pd.DataFrame(

pygmt/tests/test_clib_put_matrix.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111
from pygmt.tests.test_clib import mock
1212

1313

14-
def test_put_matrix():
14+
@pytest.fixture(scope="module", name="dtypes")
15+
def fixture_dtypes():
16+
"""
17+
List of supported numpy dtypes.
18+
"""
19+
return "int8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64".split()
20+
21+
22+
def test_put_matrix(dtypes):
1523
"""
1624
Check that assigning a numpy 2d array to a dataset works.
1725
"""
18-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
1926
shape = (3, 4)
2027
for dtype in dtypes:
2128
with clib.Session() as lib:
@@ -57,11 +64,10 @@ def test_put_matrix_fails():
5764
lib.put_matrix(dataset=None, matrix=np.empty((10, 2)), pad=0)
5865

5966

60-
def test_put_matrix_grid():
67+
def test_put_matrix_grid(dtypes):
6168
"""
6269
Check that assigning a numpy 2d array to an ASCII and NetCDF grid works.
6370
"""
64-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
6571
wesn = [10, 15, 30, 40, 0, 0]
6672
inc = [1, 1]
6773
shape = ((wesn[3] - wesn[2]) // inc[1] + 1, (wesn[1] - wesn[0]) // inc[0] + 1)

pygmt/tests/test_clib_put_vector.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
from pygmt.helpers import GMTTempFile
1313

1414

15-
def test_put_vector():
15+
@pytest.fixture(scope="module", name="dtypes")
16+
def fixture_dtypes():
17+
"""
18+
List of supported numpy dtypes.
19+
"""
20+
return "int8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64".split()
21+
22+
23+
def test_put_vector(dtypes):
1624
"""
1725
Check that assigning a numpy array to a dataset works.
1826
"""
19-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
2027
for dtype in dtypes:
2128
with clib.Session() as lib:
2229
dataset = lib.create_data(
@@ -50,13 +57,12 @@ def test_put_vector():
5057
npt.assert_allclose(newz, z)
5158

5259

53-
def test_put_vector_mixed_dtypes():
60+
def test_put_vector_mixed_dtypes(dtypes):
5461
"""
5562
Passing a numpy array of mixed dtypes to a dataset.
5663
5764
See https://github.com/GenericMappingTools/pygmt/issues/255
5865
"""
59-
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
6066
for dtypex, dtypey in itertools.permutations(dtypes, r=2):
6167
with clib.Session() as lib:
6268
dataset = lib.create_data(

0 commit comments

Comments
 (0)