Skip to content

Commit ddec7c8

Browse files
weiji14Meghan Jonesseisman
authored
Standardize docstrings for table-like inputs (#1186)
The virtualfile_from_data function wrapped in #961 allows for a standardized way for inputting data to PyGMT modules. This change is for standardizing some of the docstrings, specifically for 'table-like' vector inputs which can be of several different PyData object types. * Reformat pygmt.info docstring to use table-like filler * Reformat plot and plot3d docstring to use table-like filler * Reformat data_kind and virtualfile_from_data docstring to use table-like filler * Reformat rose docstring to use table-like filler * Reformat grdtrack docstring to use table-like filler * Reformat velo docstring to use table-like filler * Reformat wiggle docstring to use table-like filler * Reformat histogram docstring to use table-like filler * Add Python list to histogram's table param description * Use table-classes filler in docstring description * Use fmt_docstring decorator on virtualfile_from_data function Also had to remove curly brackets from doctest so that they are not treated as placeholders to be substituted. * Add a doctest for the table-classes filler text Co-authored-by: Meghan Jones <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 3644860 commit ddec7c8

File tree

11 files changed

+70
-42
lines changed

11 files changed

+70
-42
lines changed

pygmt/clib/session.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
GMTInvalidInput,
2626
GMTVersionError,
2727
)
28-
from pygmt.helpers import data_kind, dummy_context
28+
from pygmt.helpers import data_kind, dummy_context, fmt_docstring
2929

3030
FAMILIES = [
3131
"GMT_IS_DATASET",
@@ -1360,6 +1360,7 @@ def virtualfile_from_grid(self, grid):
13601360
with self.open_virtual_file(*args) as vfile:
13611361
yield vfile
13621362

1363+
@fmt_docstring
13631364
def virtualfile_from_data(
13641365
self, check_kind=None, data=None, x=None, y=None, z=None, extra_arrays=None
13651366
):
@@ -1375,7 +1376,7 @@ def virtualfile_from_data(
13751376
check_kind : str
13761377
Used to validate the type of data that can be passed in. Choose
13771378
from 'raster', 'vector' or None. Default is None (no validation).
1378-
data : str, xarray.DataArray, 2d array, or None
1379+
data : str or xarray.DataArray or {table-like} or None
13791380
Any raster or vector data format. This could be a file name, a
13801381
raster grid, a vector matrix/arrays, or other supported data input.
13811382
x/y/z : 1d arrays or None
@@ -1395,20 +1396,20 @@ def virtualfile_from_data(
13951396
>>> from pygmt.helpers import GMTTempFile
13961397
>>> import xarray as xr
13971398
>>> data = xr.Dataset(
1398-
... coords={"index": [0, 1, 2]},
1399-
... data_vars={
1400-
... "x": ("index", [9, 8, 7]),
1401-
... "y": ("index", [6, 5, 4]),
1402-
... "z": ("index", [3, 2, 1]),
1403-
... },
1399+
... coords=dict(index=[0, 1, 2]),
1400+
... data_vars=dict(
1401+
... x=("index", [9, 8, 7]),
1402+
... y=("index", [6, 5, 4]),
1403+
... z=("index", [3, 2, 1]),
1404+
... ),
14041405
... )
14051406
>>> with Session() as ses:
14061407
... with ses.virtualfile_from_data(
14071408
... check_kind="vector", data=data
14081409
... ) as fin:
14091410
... # Send the output to a file so that we can read it
14101411
... with GMTTempFile() as fout:
1411-
... ses.call_module("info", f"{fin} ->{fout.name}")
1412+
... ses.call_module("info", fin + " ->" + fout.name)
14121413
... print(fout.read().strip())
14131414
...
14141415
<vector memory>: N = 3 <7/9> <4/6> <1/3>

pygmt/helpers/decorators.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def fmt_docstring(module_func):
188188
...
189189
... Parameters
190190
... ----------
191+
... data : str or {table-like}
192+
... Pass in either a file name to an ASCII data table, a 2D
193+
... {table-classes}.
191194
... {R}
192195
... {J}
193196
...
@@ -200,6 +203,11 @@ def fmt_docstring(module_func):
200203
<BLANKLINE>
201204
Parameters
202205
----------
206+
data : str or numpy.ndarray or pandas.DataFrame or xarray.Dataset
207+
Pass in either a file name to an ASCII data table, a 2D
208+
:class:`numpy.ndarray`, a :class:`pandas.DataFrame`, or an
209+
:class:`xarray.Dataset` made up of 1D :class:`xarray.DataArray`
210+
data variables containing the tabular data.
203211
region : str or list
204212
*Required if this is the first plot command*.
205213
*xmin/xmax/ymin/ymax*\ [**+r**][**+u**\ *unit*].
@@ -224,6 +232,20 @@ def fmt_docstring(module_func):
224232
aliases.append("- {} = {}".format(arg, alias))
225233
filler_text["aliases"] = "\n".join(aliases)
226234

235+
filler_text["table-like"] = " or ".join(
236+
[
237+
"numpy.ndarray",
238+
"pandas.DataFrame",
239+
"xarray.Dataset",
240+
# "geopandas.GeoDataFrame",
241+
]
242+
)
243+
filler_text["table-classes"] = (
244+
":class:`numpy.ndarray`, a :class:`pandas.DataFrame`, or an\n"
245+
" :class:`xarray.Dataset` made up of 1D :class:`xarray.DataArray`\n"
246+
" data variables containing the tabular data"
247+
)
248+
227249
for marker, text in COMMON_OPTIONS.items():
228250
# Remove the indentation and the first line break from the multiline
229251
# strings so that it doesn't mess up the original docstring

pygmt/helpers/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ def data_kind(data, x=None, y=None, z=None):
3030
3131
Parameters
3232
----------
33-
data : str, xarray.DataArray, 2d array, or None
34-
Data file name, xarray.DataArray or numpy array.
33+
data : str or xarray.DataArray or {table-like} or None
34+
Pass in either a file name to an ASCII data table, an
35+
:class:`xarray.DataArray`, a 1D/2D
36+
{table-classes}.
3537
x/y : 1d arrays or None
3638
x and y columns as numpy arrays.
3739
z : 1d array or None

pygmt/src/grdtrack.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
3535
3636
Parameters
3737
----------
38-
points : pandas.DataFrame or str
39-
Either a table with (x, y) or (lon, lat) values in the first two
40-
columns, or a filename (e.g. csv, txt format). More columns may be
41-
present.
38+
points : str or {table-like}
39+
Pass in either a file name to an ASCII data table, a 2D
40+
{table-classes}.
4241
4342
grid : xarray.DataArray or str
4443
Gridded array from which to sample values from, or a filename (netcdf

pygmt/src/histogram.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def histogram(self, table, **kwargs):
4444
4545
Parameters
4646
----------
47-
table : str, list, or 1d array
48-
A data file name, list, or 1d numpy array. This is a required argument.
47+
table : str or list or {table-like}
48+
Pass in either a file name to an ASCII data table, a Python list, a 2D
49+
{table-classes}.
4950
{J}
5051
{R}
5152
{B}

pygmt/src/info.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ def info(table, **kwargs):
4646
4747
Parameters
4848
----------
49-
table : str or np.ndarray or pandas.DataFrame or xarray.Dataset
50-
Pass in either a file name to an ASCII data table, a 1D/2D numpy array,
51-
a pandas dataframe, or an xarray dataset made up of 1D xarray.DataArray
52-
data variables.
49+
table : str or {table-like}
50+
Pass in either a file name to an ASCII data table, a 1D/2D
51+
{table-classes}.
5352
per_column : bool
5453
Report the min/max values per column in separate columns.
5554
spacing : str

pygmt/src/plot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
7676
x/y : float or 1d arrays
7777
The x and y coordinates, or arrays of x and y coordinates of the
7878
data points
79-
data : str or 2d array
80-
Either a data file name or a 2d numpy array with the tabular data.
81-
Use parameter ``columns`` to choose which columns are x, y, color,
82-
and size, respectively.
79+
data : str or {table-like}
80+
Pass in either a file name to an ASCII data table, a 2D
81+
{table-classes}.
82+
Use parameter ``columns`` to choose which columns are x, y, color, and
83+
size, respectively.
8384
size : 1d array
8485
The size of the data points in units specified using ``style``.
8586
Only valid if using ``x``/``y``.

pygmt/src/plot3d.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ def plot3d(
7878
x/y/z : float or 1d arrays
7979
The x, y, and z coordinates, or arrays of x, y and z coordinates of
8080
the data points
81-
data : str or 2d array
82-
Either a data file name or a 2d numpy array with the tabular data.
83-
Use parameter ``columns`` to choose which columns are x, y, z,
84-
color, and size, respectively.
81+
data : str or {table-like}
82+
Pass in either a file name to an ASCII data table, a 2D
83+
{table-classes}.
84+
Use parameter ``columns`` to choose which columns are x, y, z, color,
85+
and size, respectively.
8586
size : 1d array
8687
The size of the data points in units specified in ``style``.
8788
Only valid if using ``x``/``y``/``z``.

pygmt/src/rose.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ def rose(self, length=None, azimuth=None, data=None, **kwargs):
5959
Length and azimuth values, or arrays of length and azimuth
6060
values
6161
62-
data : str or 2d array
63-
Either a data file name or a 2d numpy array with the tabular data.
64-
Use option ``columns`` to choose which columns are length and
65-
azimuth, respectively. If a file with only azimuths is given,
66-
use ``columns`` to indicate the single column with azimuths; then
67-
all lengths are set to unity (see ``scale = 'u'`` to set actual
68-
lengths to unity as well).
62+
data : str or {table-like}
63+
Pass in either a file name to an ASCII data table, a 2D
64+
{table-classes}.
65+
Use option ``columns`` to choose which columns are length and azimuth,
66+
respectively. If a file with only azimuths is given, use ``columns`` to
67+
indicate the single column with azimuths; then all lengths are set to
68+
unity (see ``scale = 'u'`` to set actual lengths to unity as well).
6969
7070
orientation : bool
7171
Specifies that the input data are orientation data (i.e., have a

pygmt/src/velo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ def velo(self, data=None, **kwargs):
5353
5454
Parameters
5555
----------
56-
data : str or numpy.ndarray or pandas.DataFrame
57-
Either a file name, a 2D :class:`numpy.ndarray` or a
58-
:class:`pandas.DataFrame` with the tabular data. Note that text columns
59-
are only supported with file or pandas DataFrame inputs.
56+
data : str or {table-like}
57+
Pass in either a file name to an ASCII data table, a 2D
58+
{table-classes}.
59+
Note that text columns are only supported with file or
60+
:class:`pandas.DataFrame` inputs.
6061
6162
spec: str
6263
Selects the meaning of the columns in the data file and the figure to

pygmt/src/wiggle.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def wiggle(self, x=None, y=None, z=None, data=None, **kwargs):
4141
----------
4242
x/y/z : 1d arrays
4343
The arrays of x and y coordinates and z data points.
44-
data : str or 2d array
45-
Either a data file name or a 2d numpy array with the tabular data.
44+
data : str or {table-like}
45+
Pass in either a file name to an ASCII data table, a 2D
46+
{table-classes}.
4647
Use parameter ``columns`` to choose which columns are x, y, z,
4748
respectively.
4849
{J}

0 commit comments

Comments
 (0)