Skip to content

Commit a16abde

Browse files
committed
Merge branch 'data_kind/doctest' into refactor/data_kind
2 parents 4074760 + 4cf80a9 commit a16abde

File tree

10 files changed

+86
-32
lines changed

10 files changed

+86
-32
lines changed

pygmt/helpers/utils.py

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def data_kind( # noqa: PLR0911
229229
230230
Examples
231231
--------
232+
<<<<<<< HEAD
232233
>>> import numpy as np
233234
>>> import xarray as xr
234235
>>> import pandas as pd
@@ -247,20 +248,72 @@ def data_kind( # noqa: PLR0911
247248
>>> data_kind(data=["data1.txt", "data2.txt"])
248249
'file'
249250
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3)))
251+
=======
252+
>>> import io
253+
>>> from pathlib import Path
254+
>>> import numpy as np
255+
>>> import pandas as pd
256+
>>> import xarray as xr
257+
258+
The "arg" kind:
259+
260+
>>> [data_kind(data=data, required=False) for data in (2, 2.0, True, False)]
261+
['arg', 'arg', 'arg', 'arg']
262+
>>> data_kind(data=None, required=False)
263+
'arg'
264+
265+
The "file" kind:
266+
267+
>>> [data_kind(data=data) for data in ("file.txt", ("file1.txt", "file2.txt"))]
268+
['file', 'file']
269+
>>> data_kind(data=Path("file.txt"))
270+
'file'
271+
>>> data_kind(data=(Path("file1.txt", "file2.txt")))
272+
'file'
273+
274+
The "grid" kind:
275+
276+
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3))) # 2-D xarray.DataArray
250277
'grid'
251-
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5)))
278+
>>> data_kind(data=xr.DataArray(np.arange(12))) # 1-D xarray.DataArray
279+
'grid'
280+
>>> data_kind(data=xr.DataArray(np.random.rand(2, 3, 4, 5))) # 4-D xarray.DataArray
281+
'grid'
282+
283+
The "image" kind:
284+
285+
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5))) # 3-D xarray.DataArray
252286
'image'
253-
>>> data_kind(data=np.arange(10).reshape((5, 2)))
254-
'matrix'
287+
288+
The "stringio"`` kind:
289+
255290
>>> data_kind(data=io.StringIO("TEXT1\nTEXT23\n"))
256291
'stringio'
257-
>>> data_kind(data=pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}))
258-
'vectors'
259-
>>> data_kind(data={"x": [1, 2], "y": [3, 4]})
260-
'vectors'
261-
>>> data_kind(data=[[1, 2], [3, 4]])
262-
'vectors'
263-
>>> data_kind(data=[1, 2, 3])
292+
293+
The "matrix"`` kind:
294+
295+
>>> data_kind(data=np.arange(10)) # 1-D numpy.ndarray
296+
'matrix'
297+
>>> data_kind(data=np.arange(10).reshape((5, 2))) # 2-D numpy.ndarray
298+
'matrix'
299+
>>> data_kind(data=np.arange(60).reshape((3, 4, 5))) # 3-D numpy.ndarray
300+
'matrix'
301+
>>> data_kind(xr.DataArray(np.arange(12), name="x").to_dataset()) # xarray.Dataset
302+
'matrix'
303+
>>> data_kind(data=[1, 2, 3]) # 1-D sequence
304+
'matrix'
305+
>>> data_kind(data=[[1, 2, 3], [4, 5, 6]]) # sequence of sequences
306+
'matrix'
307+
>>> data_kind(data={"x": [1, 2, 3], "y": [4, 5, 6]}) # dictionary
308+
'matrix'
309+
>>> data_kind(data=pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})) # pd.DataFrame
310+
'matrix'
311+
>>> data_kind(data=pd.Series([1, 2, 3], name="x")) # pd.Series
312+
'matrix'
313+
314+
The "vectors" kind:
315+
316+
>>> data_kind(data=None)
264317
'vectors'
265318
"""
266319
# data is None and is required.

pygmt/tests/test_blockm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pygmt import blockmean, blockmode
1313
from pygmt.datasets import load_sample_data
1414
from pygmt.exceptions import GMTInvalidInput
15-
from pygmt.helpers import GMTTempFile, data_kind
15+
from pygmt.helpers import GMTTempFile
1616

1717

1818
@pytest.fixture(scope="module", name="dataframe")
@@ -68,7 +68,6 @@ def test_blockmean_wrong_kind_of_input_table_grid(dataframe):
6868
Run blockmean using table input that is not a pandas.DataFrame or file but a grid.
6969
"""
7070
invalid_table = dataframe.bathymetry.to_xarray()
71-
assert data_kind(invalid_table) == "grid"
7271
with pytest.raises(GMTInvalidInput):
7372
blockmean(data=invalid_table, spacing="5m", region=[245, 255, 20, 30])
7473

pygmt/tests/test_blockmedian.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pygmt import blockmedian
1111
from pygmt.datasets import load_sample_data
1212
from pygmt.exceptions import GMTInvalidInput
13-
from pygmt.helpers import GMTTempFile, data_kind
13+
from pygmt.helpers import GMTTempFile
1414

1515

1616
@pytest.fixture(scope="module", name="dataframe")
@@ -65,7 +65,6 @@ def test_blockmedian_wrong_kind_of_input_table_grid(dataframe):
6565
Run blockmedian using table input that is not a pandas.DataFrame or file but a grid.
6666
"""
6767
invalid_table = dataframe.bathymetry.to_xarray()
68-
assert data_kind(invalid_table) == "grid"
6968
with pytest.raises(GMTInvalidInput):
7069
blockmedian(data=invalid_table, spacing="5m", region=[245, 255, 20, 30])
7170

pygmt/tests/test_geopandas.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
import pytest
88
from pygmt import Figure, info, makecpt, which
9+
from pygmt.helpers import data_kind
910
from pygmt.helpers.testing import skip_if_no
1011

1112
gpd = pytest.importorskip("geopandas")
@@ -243,3 +244,18 @@ def test_geopandas_plot_int64_as_float(gdf_ridge):
243244
makecpt(cmap="lisbon", series=[10, 60, 10], continuous=True)
244245
fig.colorbar()
245246
return fig
247+
248+
249+
def test_geopandas_data_kind_geopandas(gdf):
250+
"""
251+
Check if geopandas.GeoDataFrame object is recognized as a "geojson" kind.
252+
"""
253+
assert data_kind(data=gdf) == "geojson"
254+
255+
256+
def test_geopandas_data_kind_shapely():
257+
"""
258+
Check if shapely.geometry object is recognized as a "geojson" kind.
259+
"""
260+
polygon = shapely.geometry.Polygon([(20, 10), (23, 10), (23, 14), (20, 14)])
261+
assert data_kind(data=polygon) == "geojson"

pygmt/tests/test_grdtrack.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pytest
1111
from pygmt import grdtrack
1212
from pygmt.exceptions import GMTInvalidInput
13-
from pygmt.helpers import GMTTempFile, data_kind
13+
from pygmt.helpers import GMTTempFile
1414
from pygmt.helpers.testing import load_static_earth_relief
1515

1616
POINTS_DATA = Path(__file__).parent / "data" / "track.txt"
@@ -129,8 +129,6 @@ def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe):
129129
Run grdtrack using points input that is not a pandas.DataFrame or file.
130130
"""
131131
invalid_points = dataframe.longitude.to_xarray()
132-
133-
assert data_kind(invalid_points) == "grid"
134132
with pytest.raises(GMTInvalidInput):
135133
grdtrack(points=invalid_points, grid=dataarray, newcolname="bathymetry")
136134

@@ -140,8 +138,6 @@ def test_grdtrack_wrong_kind_of_grid_input(dataarray, dataframe):
140138
Run grdtrack using grid input that is not as xarray.DataArray (grid) or file.
141139
"""
142140
invalid_grid = dataarray.to_dataset()
143-
144-
assert data_kind(invalid_grid) == "vectors"
145141
with pytest.raises(GMTInvalidInput):
146142
grdtrack(points=dataframe, grid=invalid_grid, newcolname="bathymetry")
147143

pygmt/tests/test_grdview.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from pygmt import Figure, grdcut
77
from pygmt.exceptions import GMTInvalidInput
8-
from pygmt.helpers import GMTTempFile, data_kind
8+
from pygmt.helpers import GMTTempFile
99
from pygmt.helpers.testing import load_static_earth_relief
1010

1111

@@ -58,8 +58,6 @@ def test_grdview_wrong_kind_of_grid(xrgrid):
5858
Run grdview using grid input that is not an xarray.DataArray or file.
5959
"""
6060
dataset = xrgrid.to_dataset() # convert xarray.DataArray to xarray.Dataset
61-
assert data_kind(dataset) == "vectors"
62-
6361
fig = Figure()
6462
with pytest.raises(GMTInvalidInput):
6563
fig.grdview(grid=dataset)
@@ -238,8 +236,6 @@ def test_grdview_wrong_kind_of_drapegrid(xrgrid):
238236
Run grdview using drapegrid input that is not an xarray.DataArray or file.
239237
"""
240238
dataset = xrgrid.to_dataset() # convert xarray.DataArray to xarray.Dataset
241-
assert data_kind(dataset) == "vectors"
242-
243239
fig = Figure()
244240
with pytest.raises(GMTInvalidInput):
245241
fig.grdview(grid=xrgrid, drapegrid=dataset)

pygmt/tests/test_nearneighbor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pygmt import nearneighbor
1212
from pygmt.datasets import load_sample_data
1313
from pygmt.exceptions import GMTInvalidInput
14-
from pygmt.helpers import GMTTempFile, data_kind
14+
from pygmt.helpers import GMTTempFile
1515

1616

1717
@pytest.fixture(scope="module", name="ship_data")
@@ -61,7 +61,6 @@ def test_nearneighbor_wrong_kind_of_input(ship_data):
6161
Run nearneighbor using grid input that is not file/matrix/vectors.
6262
"""
6363
data = ship_data.bathymetry.to_xarray() # convert pandas.Series to xarray.DataArray
64-
assert data_kind(data) == "grid"
6564
with pytest.raises(GMTInvalidInput):
6665
nearneighbor(
6766
data=data, spacing="5m", region=[245, 255, 20, 30], search_radius="10m"

pygmt/tests/test_surface.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import xarray as xr
1010
from pygmt import surface, which
1111
from pygmt.exceptions import GMTInvalidInput
12-
from pygmt.helpers import GMTTempFile, data_kind
12+
from pygmt.helpers import GMTTempFile
1313

1414

1515
@pytest.fixture(scope="module", name="data")
@@ -125,7 +125,6 @@ def test_surface_wrong_kind_of_input(data, region, spacing):
125125
Run surface using grid input that is not file/matrix/vectors.
126126
"""
127127
data = data.z.to_xarray() # convert pandas.Series to xarray.DataArray
128-
assert data_kind(data) == "grid"
129128
with pytest.raises(GMTInvalidInput):
130129
surface(data=data, spacing=spacing, region=region)
131130

pygmt/tests/test_triangulate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import xarray as xr
1111
from pygmt import triangulate, which
1212
from pygmt.exceptions import GMTInvalidInput
13-
from pygmt.helpers import GMTTempFile, data_kind
13+
from pygmt.helpers import GMTTempFile
1414

1515

1616
@pytest.fixture(scope="module", name="dataframe")
@@ -93,7 +93,6 @@ def test_delaunay_triples_wrong_kind_of_input(dataframe):
9393
Run triangulate.delaunay_triples using grid input that is not file/matrix/vectors.
9494
"""
9595
data = dataframe.z.to_xarray() # convert pandas.Series to xarray.DataArray
96-
assert data_kind(data) == "grid"
9796
with pytest.raises(GMTInvalidInput):
9897
triangulate.delaunay_triples(data=data)
9998

pygmt/tests/test_x2sys_cross.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pygmt.clib import __gmt_version__
1717
from pygmt.datasets import load_sample_data
1818
from pygmt.exceptions import GMTInvalidInput
19-
from pygmt.helpers import data_kind
2019

2120

2221
@pytest.fixture(name="mock_x2sys_home")
@@ -241,7 +240,6 @@ def test_x2sys_cross_invalid_tracks_input_type(tracks):
241240
(file) type, which would raise a GMTInvalidInput error.
242241
"""
243242
invalid_tracks = tracks[0].to_xarray().z
244-
assert data_kind(invalid_tracks) == "grid"
245243
with pytest.raises(GMTInvalidInput):
246244
x2sys_cross(tracks=[invalid_tracks])
247245

0 commit comments

Comments
 (0)