Skip to content

Failing doctest on pygmt.clib.conversion.dataarray_to_matrix with NumPy 2.0 #2628

@weiji14

Description

@weiji14

Description of the problem

The following doctest started failing on the GMT Dev Tests on 28 Jul 2023, previously passing on 26 Jul 2023. See https://github.com/GenericMappingTools/pygmt/actions/runs/5686923585/job/15414542417#step:16:723.

Doesn't seem to be an issue with upstream GMT, at least I don't see anything obvious in these 7 commits GenericMappingTools/gmt@f497711...c94da14.

The region variable is created here -

# Extract region and inc from the grid
region = []
inc = []
# Reverse the dims because it is rows, columns ordered. In geographic
# grids, this would be North-South, East-West. GMT's region and inc are
# East-West, North-South.
for dim in grid.dims[::-1]:
coord = grid.coords[dim].values
coord_incs = coord[1:] - coord[0:-1]
coord_inc = coord_incs[0]
if not np.allclose(coord_incs, coord_inc):
# calculate the increment if irregular spacing is found
coord_inc = (coord[-1] - coord[0]) / (coord.size - 1)
msg = (
f"Grid may have irregular spacing in the '{dim}' dimension, "
"but GMT only supports regular spacing. Calculated regular spacing "
f"{coord_inc} is assumed in the '{dim}' dimension."
)
warnings.warn(msg, category=RuntimeWarning)
if coord_inc == 0:
raise GMTInvalidInput(
f"Grid has a zero increment in the '{dim}' dimension."
)
region.extend(
[
coord.min() - coord_inc / 2 * grid.gmt.registration,
coord.max() + coord_inc / 2 * grid.gmt.registration,
]
)
inc.append(coord_inc)
Might be related to changes in the dev version of numpy/pandas/xarray?

Minimal Complete Verifiable Example

from pygmt.datasets import load_earth_relief
# Use the global Earth relief grid with 1 degree spacing
grid = load_earth_relief(resolution="01d", registration="pixel")
matrix, region, inc = dataarray_to_matrix(grid)
print(region)

Full error message

=================================== FAILURES ===================================
_____________ [doctest] pygmt.clib.conversion.dataarray_to_matrix ______________
043         If the grid has more than two dimensions or variable grid spacing.
044 
045     Examples
046     --------
047 
048     >>> from pygmt.datasets import load_earth_relief
049     >>> # Use the global Earth relief grid with 1 degree spacing
050     >>> grid = load_earth_relief(resolution="01d", registration="pixel")
051     >>> matrix, region, inc = dataarray_to_matrix(grid)
052     >>> print(region)
Expected:
    [-180.0, 180.0, -90.0, 90.0]
Got:
    [np.float64(-180.0), np.float64(180.0), np.float64(-90.0), np.float64(90.0)]

/home/runner/work/pygmt/pygmt/pygmt/clib/conversion.py:52: DocTestFailure

System information

Failing on:

PyGMT information:
  version: v0.9.1.dev110+gd0ca06a0
System information:
  python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:17) [GCC 12.2.0]
  executable: /home/runner/micromamba/envs/pygmt/bin/python
  machine: Linux-5.15.0-1041-azure-x86_64-with-glibc2.35
Dependency information:
  numpy: 2.0.0.dev0+646.g3dd9dba09
  pandas: 2.1.0.dev0+1303.g577bb7239c
  xarray: 2023.7.1.dev18+g52f5cf1f
  netCDF4: 1.6.4
  packaging: 23.1
  contextily: 1.3.0
  geopandas: 0.13.2
  IPython: 8.15.0.dev
  rioxarray: 0.14.1
  ghostscript: 9.54.0
GMT library information:
  binary version: 6.5.0_c94da14_2023.07.27
  cores: 2
  grid layout: rows
  image layout: 
  library path: /home/runner/work/pygmt/pygmt/gmt-install-dir/lib/libgmt.so
  padding: 2
  plugin dir: /home/runner/work/pygmt/pygmt/gmt-install-dir/lib/gmt/plugins
  share dir: /home/runner/work/pygmt/pygmt/gmt-install-dir/share
  version: 6.5.0

Passing version:

PyGMT information:
  version: v0.9.1.dev110+gd0ca06a0
System information:
  python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:17) [GCC 12.2.0]
  executable: /home/runner/micromamba/envs/pygmt/bin/python
  machine: Linux-5.15.0-1041-azure-x86_64-with-glibc2.35
Dependency information:
  numpy: 2.0.0.dev0+554.g0aaa5d397
  pandas: 2.1.0.dev0+1288.g1a4ac0ecb8
  xarray: 2023.7.1.dev16+gbb501ba5
  netCDF4: 1.6.4
  packaging: 23.1
  contextily: 1.3.0
  geopandas: 0.13.2
  IPython: 8.15.0.dev
  rioxarray: 0.14.1
  ghostscript: 9.54.0
GMT library information:
  binary version: 6.5.0_f497711_2023.07.25
  cores: 2
  grid layout: rows
  image layout: 
  library path: /home/runner/work/pygmt/pygmt/gmt-install-dir/lib/libgmt.so
  padding: 2
  plugin dir: /home/runner/work/pygmt/pygmt/gmt-install-dir/lib/gmt/plugins
  share dir: /home/runner/work/pygmt/pygmt/gmt-install-dir/share
  version: 6.5.0

Diff

Dependency information:
-  numpy: 2.0.0.dev0+554.g0aaa5d397
-  pandas: 2.1.0.dev0+1288.g1a4ac0ecb8
-  xarray: 2023.7.1.dev16+gbb501ba5
+  numpy: 2.0.0.dev0+646.g3dd9dba09
+  pandas: 2.1.0.dev0+1303.g577bb7239c
+  xarray: 2023.7.1.dev18+g52f5cf1f
GMT library information:
-  binary version: 6.5.0_f497711_2023.07.25
+  binary version: 6.5.0_c94da14_2023.07.27

Metadata

Metadata

Assignees

No one assigned

    Labels

    maintenanceBoring but important stuff for the core devsupstreamBug or missing feature of upstream core GMT

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions