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 project #1122

Merged
merged 31 commits into from
Oct 28, 2021
Merged

Wrap project #1122

merged 31 commits into from
Oct 28, 2021

Conversation

claudiodsf
Copy link
Contributor

@claudiodsf claudiodsf commented Mar 25, 2021

Description of proposed changes

⚠️ This is my first try at wrapping a GMT function. ⚠️

Here I propose a wrapper for project.

Still some stuff to do (tests!) and many questions, which I'll ask in a comment below.

Preview docs at https://pygmt-git-fork-claudiodsf-project-gmt.vercel.app/api/generated/pygmt.project.html

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.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@claudiodsf
Copy link
Contributor Author

Let's go with the questions!

  1. I'm confused on whether I should use a pandas dataframe or a NumPy array as input. I saw both in the existing code and I could not understand the rationale between using one or the other (so, for the moment, I'm using pandas).
  2. I'm in a special case where I have a required keyword parameter (center): this prevents me to use the @kwargs_to_strings decorator and I had to do a little hack. Any better idea?
  3. I put points as first parameter for consistency with other wrapped functions. However this parameter is ignored if generate is used. Not sure if the solution I came with is nice...

@seisman seisman added the feature Brand new feature label Mar 25, 2021
@seisman
Copy link
Member

seisman commented Mar 25, 2021

  1. I'm confused on whether I should use a pandas dataframe or a NumPy array as input. I saw both in the existing code and I could not understand the rationale between using one or the other (so, for the moment, I'm using pandas).

Good question. I think we should always use NumPy if possible, but some modules (e.g., blockmean) may only support pandas dataframe. I think we should fix it.

  1. I'm in a special case where I have a required keyword parameter (center): this prevents me to use the @kwargs_to_strings decorator and I had to do a little hack. Any better idea?

If you add C="center" to @use_alias and C="sequence" to @kwargs_to_strings, it might work.

  1. I put points as first parameter for consistency with other wrapped functions. However this parameter is ignored if generate is used. Not sure if the solution I came with is nice...

Perhaps you can set points=None and check if points is given when generate is not used?

@claudiodsf
Copy link
Contributor Author

Thanks @seisman for your comments.

Let me reply to each of them:

Good question. I think we should always use NumPy if possible, but some modules (e.g., blockmean) may only support pandas dataframe. I think we should fix it.

What about accepting both NumPy arrays and pandas dataframes? The function return value could be adjusted accordingly. For the specific case of project, dataframes are quite handy to keep track of column names.

If you add C="center" to @use_alias and C="sequence" to @kwargs_to_strings, it might work.

It doesn't work because center is a required argument and has to be positional, not keyword.
My understanding is that the decorator @kwargs_to_strings only deals with keyword arguments.

Maybe a solution would be using required keyword-only arguments:

def project(points, *, center, outfile=None, **kwargs)

but it doesn't seem to be supported by @kwargs_to_strings.

Perhaps you can set points=None and check if points is given when generate is not used?

This will conflict with the second argument (center) which is required. A solution would be moving points to second argument, but this would be inconsistent with other functions. Let me know what you prefer.

@seisman
Copy link
Member

seisman commented Mar 27, 2021

Good question. I think we should always use NumPy if possible, but some modules (e.g., blockmean) may only support pandas dataframe. I think we should fix it.

What about accepting both NumPy arrays and pandas dataframes? The function return value could be adjusted accordingly. For the specific case of project, dataframes are quite handy to keep track of column names.

I meant the input should be general enough to support string (filenames), 2D list, 2D numpy array and pandas.DataFrame. For output, pandas.DataFrame sounds good. For people who want numpy, perhaps they should just convert the pandas.DataFrame to numpy using pandas.DataFrame.to_numpy()?

@willschlitzer
Copy link
Contributor

Ping @claudiodsf to check in and see if you need any help getting this pull request to completion

@claudiodsf
Copy link
Contributor Author

Ping @claudiodsf to check in and see if you need any help getting this pull request to completion

Hi @willschlitzer and thanks for your help.

There are two pending questions for this PR:

  1. Should points be a NumPy array, a Pandas data frame, or should we support every kind of input. This seems to vary from function to function in PyGMT, but maybe you have now defined a strategy?
  2. Should we keep points as first argument (for consistency with other PyGMT functions) or move it after center, given that center is a required argument and points is not?

Please note that I'm leaving on summer break next Wednesday and will have little time to work on this PR till then. I'll be back on the second half of August.

@willschlitzer
Copy link
Contributor

  1. Should points be a NumPy array, a Pandas data frame, or should we support every kind of input. This seems to vary from function to function in PyGMT, but maybe you have now defined a strategy?

I would give it the option to accept multiple types of table-like inputs, such as a list, numpy array, or DataFrame. Have a look at grdtrack on how to accept multiple types of table-like inputs.

  1. Should we keep points as first argument (for consistency with other PyGMT functions) or move it after center, given that center is a required argument and points is not?

Since neither points or center are in kwargs, I don't think the order matters too much. That being said, to update the discussion from your comments earlier about having C="center" in the kwargs, I would still recommend including it in one of the aliases, and then have some sort of check to ensure that C is in kwargs; having required arguments in the kwargs is common for PyGMT modules. Something like:

if "C" not in kwargs.keys():
    raise GMTInvalidInput("'center' must be specified.")

Please note that I'm leaving on summer break next Wednesday and will have little time to work on this PR till then. I'll be back on the second half of August.

No worries! Just wanted to check in to make sure this PR isn't forgotten about. Enjoy your summer break; PyGMT will still need work to be done when you get back!

@weiji14 weiji14 added this to the 0.5.0 milestone Aug 8, 2021
@maxrjones maxrjones linked an issue Sep 14, 2021 that may be closed by this pull request
@maxrjones maxrjones mentioned this pull request Sep 14, 2021
@weiji14
Copy link
Member

weiji14 commented Oct 3, 2021

Hi @claudiodsf, we're planning to release PyGMT v0.5.0 in about two weeks (~15 October 2021). Let us know if you have time to finish this Pull Request, otherwise we can get one of the maintainers to finalize things and get this feature in 😄

@maxrjones
Copy link
Member

Hi @claudiodsf, we're planning to release PyGMT v0.5.0 in about two weeks (~15 October 2021). Let us know if you have time to finish this Pull Request, otherwise we can get one of the maintainers to finalize things and get this feature in 😄

I am available to help with finalizing this pull request. @claudiodsf, would you mind if I push changes to this branch in order to include your new feature in v0.5.0?

@claudiodsf
Copy link
Contributor Author

Hi @meghanrjones,

sure! Feel free to push your changes.

Sorry for slow replying, but it's a tough working period.

@maxrjones
Copy link
Member

Hi @meghanrjones,

sure! Feel free to push your changes.

Sorry for slow replying, but it's a tough working period.

Great, thanks. And no problem, completely understand!

@maxrjones maxrjones marked this pull request as draft October 7, 2021 17:18
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/src/project.py Outdated Show resolved Hide resolved
pygmt/tests/test_project.py Show resolved Hide resolved
This was referenced Oct 23, 2021
@willschlitzer
Copy link
Contributor

@GenericMappingTools/pygmt-maintainers Could someone take a look at the suggestions I made for the project documentation and give it a yes/no? I want to get this pull request done for v0.5.0, and as @meghanrjones is currently on vacation (:tada:), I don't want to be the one approving my own suggestions.

maxrjones and others added 2 commits October 27, 2021 14:50
Co-authored-by: Will Schlitzer <schlitzer90@gmail.com>
@maxrjones
Copy link
Member

/format

pygmt/src/project.py Outdated Show resolved Hide resolved
Copy link
Contributor

@willschlitzer willschlitzer left a comment

Choose a reason for hiding this comment

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

Approved with one change recommendation

Co-authored-by: Will Schlitzer <schlitzer90@gmail.com>
@maxrjones maxrjones merged commit 91dbece into GenericMappingTools:main Oct 28, 2021
@maxrjones
Copy link
Member

Thanks again for this feature @claudiodsf! Excited to have this out in the v0.5.0 release 🎉! As a reminder, you're welcome to submit a PR adding yourself to AUTHORS.md.

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Mar 18, 2022
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
* Wrap project

Co-authored-by: Meghan Jones <meghanj@alum.mit.edu>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com>
Co-authored-by: Will Schlitzer <schlitzer90@gmail.com>
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.

Wrapper for project
6 participants