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

pygmt.triangulate: Refactor to use codes consistent with other wrappers #3073

Merged
merged 3 commits into from
Feb 28, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Feb 27, 2024

Description of proposed changes

The triangulate module was first wrapped in PR #731. As mentioned in PR #731:

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

So, in PR #731, a private function _triangulate was implemented and was internally called by triangulate.delaunay_triples/triangulate.regular_grid.

Such an implementation has some limitations:

  • The internal _triangulate function is complicated because it needs to deal with both table and grid outputs.
  • We have to maintain similar docstrings in three functions (although the private _triangulate function doesn't have to have docstrings)
  • In triangulate.delaunay_triples/triangulate.regular_grid, we have to call the private function like:
            return triangulate._triangulate(
                data=data,
                x=x,
                y=y,
                z=z,
                output_type=output_type,
                outgrid=outgrid,
                spacing=spacing,
                projection=projection,
                region=region,
                verbose=verbose,
                binary=binary,
                nodata=nodata,
                find=find,
                coltypes=coltypes,
                header=header,
                incols=incols,
                registration=registration,
                skiprows=skiprows,
                wrap=wrap,
                **kwargs,
            )

This PR refactors the triangulate wrapper by removing the private _triangulate function. The most obvious benefits of the new implementation are that:

  • The triangulate.delaunay_triples method have codes that look like any table-ouput wrappers (e.g., filter1d)
  • The triangulate.regular_grid method has codes that look like any grid-output wrappers (e.g., grd2xyz)

Thus, the new implementation is much easier to maintain and also make it easier to refactor in PR #2729.

Preview:

@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog labels Feb 27, 2024
@seisman seisman added this to the 0.12.0 milestone Feb 27, 2024
@seisman seisman added the needs review This PR has higher priority and needs review. label Feb 27, 2024
@seisman seisman requested a review from weiji14 February 28, 2024 06:12
@seisman seisman changed the title Refactor the triangulate wrapper to make it easier to maintain pygmt.triangulate: Refactor to make it easier to maintain Feb 28, 2024
Copy link
Member

@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.

Looks fine after a quick skim (a little busy this week), and the tests pass, so should be ok.

@seisman seisman changed the title pygmt.triangulate: Refactor to make it easier to maintain pygmt.triangulate: Refactor to use codes consistent with other wrappers Feb 28, 2024
@seisman seisman merged commit 39da1cf into main Feb 28, 2024
18 of 19 checks passed
@seisman seisman deleted the refactor/triangulate branch February 28, 2024 23:06
@seisman seisman removed the needs review This PR has higher priority and needs review. label Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants