Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return xarray.DataArray if outgrid is None #1807

Closed
maxrjones opened this issue Mar 12, 2022 · 7 comments · Fixed by #1857
Closed

Return xarray.DataArray if outgrid is None #1807

maxrjones opened this issue Mar 12, 2022 · 7 comments · Fixed by #1857
Labels
bug Something isn't working
Milestone

Comments

@maxrjones
Copy link
Member

Description of the desired feature

For the triangulate draft and grdhisteq, we return an xarray.DataArray if outgrid is True or None, but for other functions we require outgrid to be None to return an xarray.DataArray. Here is an example (requires gridding/triangulate branch):

import pygmt
grid = pygmt.triangulate.regular_grid(
    data="@Table_5_11.txt",
    region=[-0.2,  6.6, -0.2, 6.6],
    spacing=0.2,
    outgrid=True
) # Works
filtered_grid = pygmt.grdfilter(
    grid,
    distance=0,
    filter="c1",
    outgrid=True
) # Fails with "grdfilter [ERROR]: No filename provided"

I would prefer that we return an xarray.DataArray when outgrid is True or None, but otherwise grdhisteq and triangulate should be revised for consistency if we chose to require outgrid to be unset.

@maxrjones maxrjones added the feature request New feature wanted label Mar 12, 2022
@weiji14
Copy link
Member

weiji14 commented Mar 12, 2022

Maybe just stick with None or str to be consistent, I'll modify #731 so that outgrid=True won't work. Edit: done at 39c85b0.

@maxrjones
Copy link
Member Author

Maybe just stick with None or str to be consistent, I'll modify #731 so that outgrid=True won't work.

Actually outgrid=None does not work either, it needs to be unset:

grid = pygmt.datasets.load_earth_relief()
pygmt.grdcut(grid=grid, outgrid=None, region=[0, 2, 0, 2]) # grdcut [ERROR]: Option -G: Must specify output grid file

I think it's unusual to define the behavior based on whether the parameter is set/unset rather than the specific setting.

@weiji14
Copy link
Member

weiji14 commented Mar 12, 2022

Maybe just stick with None or str to be consistent, I'll modify #731 so that outgrid=True won't work.

Actually outgrid=None does not work either, it needs to be unset:

grid = pygmt.datasets.load_earth_relief()
pygmt.grdcut(grid=grid, outgrid=None, region=[0, 2, 0, 2]) # grdcut [ERROR]: Option -G: Must specify output grid file

I think it's unusual to define the behavior based on whether the parameter is set/unset rather than the specific setting.

This explicit setting of outgrid=None problem is similar to what @seisman discovered at #1665 (comment) with frame=None. Something to fix in the @use_alias decorator.

@weiji14 weiji14 added triage Unsure where this issue fits and removed feature request New feature wanted labels Mar 13, 2022
weiji14 added a commit that referenced this issue Mar 14, 2022
Wrapping the triangulate function which does
"Delaunay triangulation or Voronoi partitioning
and gridding of Cartesian data".
Original GMT documentation can be found at
https://docs.generic-mapping-tools.org/6.3/triangulate.html.

Aliased outgrid (G), spacing (I), projection (J),
region (R), verbose (V), registration (r).

* Refactor triangulate to use virtualfile_from_data
* Refactor triangulate implementation to use pygmt.io.load_dataarray
* Alias binary(b), nodata(d), find(e), coltypes(f), header(h), incols(i)

* Rename the parameter 'table' to 'data'

As per #1479.

* Refactor test_triangulate to use Table_5_11_mean.xyz instead of tut_ship
* Refactor test_triangulate_with_outgrid to use xr.testing.assert_allclose
* Refactor test_triangulate_input_xyz to use pd.testing.assert_frame_equal

* Implement regular_grid and delaunay_triples staticmethod for triangulate
* Let list inputs to spacing (I) and incols (i) work

Use  I="sequence" and i="sequence_comma".

* Ensure triangulate.delaunay_triples output_type is valid

Must be either one of numpy, pandas or file

* Autocorrect output_type to 'file' if outfile parameter is set
* Allow only str or None inputs to outgrid parameter

Xref #1807

* Use gmt get GMT_TRIANGULATE to check whether Watson or Shewchuk is used
* State that Shewchuk is the default triangulation algorithm

As per GenericMappingTools/gmt#6438

* Actually document the output_type parameter for delaunay_triples

Co-authored-by: Will Schlitzer <schlitzer90@gmail.com>
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@maxrjones maxrjones changed the title Return xarray.DataArray if outgrid is True or None for functions that return raster data Return xarray.DataArray if outgrid is None Mar 30, 2022
@maxrjones maxrjones added bug Something isn't working and removed triage Unsure where this issue fits labels Mar 30, 2022
@maxrjones maxrjones mentioned this issue Mar 30, 2022
30 tasks
@weiji14
Copy link
Member

weiji14 commented Mar 30, 2022

Should the behaviour of outgrid=False be the same as outgrid=None? I.e. return an output as an xarray.DataArray?

Actually, let me rephrase that, if a user sets outgrid=False, should they be seeing an error like this?

        if status != 0:
>           raise GMTCLibError(
                f"Module '{module}' failed with status code {status}:\n{self._error_message}"
            )
E           pygmt.exceptions.GMTCLibError: Module 'grdgradient' failed with status code 72:
E           grdgradient [ERROR]: Option -G: Must specify output file

pygmt/clib/session.py:502: GMTCLibError

Answer: 1) Yes they deserve to see this error message. 2) No, they should see a nice error message. 3) outgrid=False should just mean return xarray.DataArray.

@maxrjones
Copy link
Member Author

I think (2) because that error message is difficult to understand for non-GMT users and IMO it's not intuitive that outgrid=False would return a xarray.DataArray (e.g., when I first wrapped grdhisteq I added support for outgrid=True to return an xarray.DataArray)

@weiji14
Copy link
Member

weiji14 commented Mar 30, 2022

Ok, I'm leaning towards (1) (current behaviour) because the docstring says:

    ret: xarray.DataArray or None
        Return type depends on whether the ``outgrid`` parameter is set:

        - :class:`xarray.DataArray` if ``outgrid`` is not set
        - None if ``outgrid`` is set (grid output will be stored in file set by
          ``outgrid``)

So a boolean True/False shouldn't be allowed. This keeps the implementation simple (specifically, less if-then checks), and the GMTCLibError could be explained away as a bad case of #256 🙂

But let's get a third opinion on this, @seisman?

@seisman
Copy link
Member

seisman commented Mar 31, 2022

I also agree with (1) to make things simple. As for (2), we may need a more general solution to replace GMT options (e.g., -G) with PyGMT keywords (e.g., outgrid), but it should be done in a separate PR.

@seisman seisman added this to the 0.6.1 milestone Apr 5, 2022
sixy6e pushed a commit to sixy6e/pygmt that referenced this issue Dec 21, 2022
Wrapping the triangulate function which does
"Delaunay triangulation or Voronoi partitioning
and gridding of Cartesian data".
Original GMT documentation can be found at
https://docs.generic-mapping-tools.org/6.3/triangulate.html.

Aliased outgrid (G), spacing (I), projection (J),
region (R), verbose (V), registration (r).

* Refactor triangulate to use virtualfile_from_data
* Refactor triangulate implementation to use pygmt.io.load_dataarray
* Alias binary(b), nodata(d), find(e), coltypes(f), header(h), incols(i)

* Rename the parameter 'table' to 'data'

As per GenericMappingTools#1479.

* Refactor test_triangulate to use Table_5_11_mean.xyz instead of tut_ship
* Refactor test_triangulate_with_outgrid to use xr.testing.assert_allclose
* Refactor test_triangulate_input_xyz to use pd.testing.assert_frame_equal

* Implement regular_grid and delaunay_triples staticmethod for triangulate
* Let list inputs to spacing (I) and incols (i) work

Use  I="sequence" and i="sequence_comma".

* Ensure triangulate.delaunay_triples output_type is valid

Must be either one of numpy, pandas or file

* Autocorrect output_type to 'file' if outfile parameter is set
* Allow only str or None inputs to outgrid parameter

Xref GenericMappingTools#1807

* Use gmt get GMT_TRIANGULATE to check whether Watson or Shewchuk is used
* State that Shewchuk is the default triangulation algorithm

As per GenericMappingTools/gmt#6438

* Actually document the output_type parameter for delaunay_triples

Co-authored-by: Will Schlitzer <schlitzer90@gmail.com>
Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants