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

Wrap triangulate #731

Merged
merged 38 commits into from
Mar 14, 2022
Merged

Wrap triangulate #731

merged 38 commits into from
Mar 14, 2022

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Dec 14, 2020

Description of proposed changes

Wrapping the triangulate function which does "Delaunay triangulation or Voronoi partitioning and gridding of Cartesian data", implemented under gridding.py. For a GMT example, see https://docs.generic-mapping-tools.org/6.3/gallery/ex16.html

Preview for this Pull Request is at https://pygmt-git-gridding-triangulate-gmt.vercel.app/api/generated/pygmt.triangulate.html

Note that this is a fairly minimal implementation of triangulate for now, and I don't expect to wrap and test all the aliases here as they are super complicated. Just that I've just been using triangulate secretly at https://github.com/weiji14/pyissm/blob/5c9e3be4c4983ccc79a67cc6471421922e8c3af7/plot_figures.py#L68-L83 and thought it should get into pygmt proper 😄

The complicated thing about triangulate is that it generates either text (table) or NetCDF (grid) outputs, and I've implemented it similar to grdhisteq at #1433 as so:

  • triangulate.delaunay_triples:
    • When output_type="numpy", output table as numpy.ndarray
    • When output_type="pandas" (default for table), output table as pandas.DataFrame
    • When output_type="file" and outfile is not None, output table as textfile
  • triangulate.regular_grid:
    • When outgrid=True (default for grid), output grid as 'xarray.DataArray`
    • When outgrid="test.nc", output grid to NetCDF file

Welcome to other suggestions on implementation details.

Fixes #

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Notes

  • You can write /format in the first line of a comment to lint the code automatically

Wrapping the triangulate function which does
"Delaunay triangulation or Voronoi partitioning
and gridding of Cartesian data" under gridding.py.
Original GMT documentation can be found at
https://docs.generic-mapping-tools.org/6.1/triangulate.html.

Aliased outgrid (G), spacing (I), projection (J),
region (R), verbose (V), registration (r).
Also included 8 unit tests modified from `surface`.
@weiji14 weiji14 added the feature Brand new feature label Dec 14, 2020
@seisman
Copy link
Member

seisman commented Dec 15, 2020

When outgrid=None, output grid to NetCDF file

Typo?

When outgrid=None (default), output table as pandas.DataFrame
(Won't be implemented?), output table as textfile

I've never used this module, but how does GMT know the output type (a text file or a netcdf grid)?

@weiji14
Copy link
Member Author

weiji14 commented Dec 15, 2020

When outgrid=None, output grid to NetCDF file

Typo?

Good catch, should be when outgrid="test.nc" I've updated top post.

When outgrid=None (default), output table as pandas.DataFrame
(Won't be implemented?), output table as textfile

I've never used this module, but how does GMT know the output type (a text file or a netcdf grid)?

So in pure GMT, triangulate outputs to a table format (technically stdout) with rows and columns. But using -Gtest.nc would interpolate that table result into a square grid output.

@seisman
Copy link
Member

seisman commented Dec 16, 2020

So in pure GMT, triangulate outputs to a table format (technically stdout) with rows and columns. But using -Gtest.nc would interpolate that table result into a square grid output.

OK. Two solutions for me:

  1. Don't implement the "output to text file" feature. It's easy to save the returned pandas.DataFrame to a text file.
  2. Add a new parameter outfile. Save the output to outfile if it's not None.

@weiji14
Copy link
Member Author

weiji14 commented Dec 16, 2020

  1. Don't implement the "output to text file" feature. It's easy to save the returned pandas.DataFrame to a text file.

Yep, Option 1 is the current state. Saving a pandas.DataFrame to an ASCII text file is definitely easier (compared to saving an xarray.DataArray to a NetCDF).

  1. Add a new parameter outfile. Save the output to outfile if it's not None.

I thought about using outfile as well, and it's definitely implementable, though having 4 possible output types might confuse users. At some point though, this Option 2 might need to be done.

@seisman
Copy link
Member

seisman commented Dec 16, 2020

I think we can adopt option 1 now and add the outfile argument if requested.

Modernizing the code implementation to keep up to date
with PyGMT v0.4.1. Also refreshing the unit tests a bit.
Copy link
Member Author

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an old PR (~9 months), but a good one to put in for PyGMT v0.5.0 I think. Ping @GenericMappingTools/pygmt-maintainers for a review if free. The triangulate function as implemented takes an input table, and outputs either a table or grid.

pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
Co-authored-by: Will Schlitzer <[email protected]>
Co-authored-by: Meghan Jones <[email protected]>
Copy link
Member

@maxrjones maxrjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! These should be my last comments.

Should an exception be raised if output_type == "file" and outfile is None? For example:

pygmt.triangulate.delaunay_triples(
    data="@Table_5_11_mean.xyz",
    output_type="file"
)

The ongoing issues with uninformative error messages affect this PR, but that shouldn't hold back this feature.

pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
@weiji14
Copy link
Member Author

weiji14 commented Mar 13, 2022

Should an exception be raised if output_type == "file" and outfile is None? For example:

pygmt.triangulate.delaunay_triples(
    data="@Table_5_11_mean.xyz",
    output_type="file"
)

Yes an exception should be raised, but this is not specific to triangulate. I think we'll need to handle this across the board in #1318.

@maxrjones
Copy link
Member

Yes an exception should be raised, but this is not specific to triangulate. I think we'll need to handle this across the board in #1318.

Sounds good to deal with this separately (after v0.6.0). Of course I would have liked to fix that issue before this release, but this year has been going by too fast...

Copy link
Member

@maxrjones maxrjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks for all your work on this feature!

@maxrjones maxrjones added the final review call This PR requires final review and approval from a second reviewer label Mar 13, 2022
@maxrjones maxrjones mentioned this pull request Mar 13, 2022
13 tasks
@weiji14 weiji14 mentioned this pull request Mar 14, 2022
6 tasks
Copy link
Member

@seisman seisman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used triangulate, so just two general comments.

pygmt/src/triangulate.py Outdated Show resolved Hide resolved
pygmt/src/triangulate.py Outdated Show resolved Hide resolved
@weiji14 weiji14 removed the final review call This PR requires final review and approval from a second reviewer label Mar 14, 2022
@weiji14 weiji14 merged commit ba51f80 into main Mar 14, 2022
@weiji14 weiji14 deleted the gridding/triangulate branch March 14, 2022 12:15
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request 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 <[email protected]>
Co-authored-by: Meghan Jones <[email protected]>
Co-authored-by: Dongdong Tian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Brand new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants