Repository navigation
Description
For each GMT option, PyGMT usually provides a long-form parameter. For example, GMT CLI option -B is equivalent to frame in PyGMT. This is done by the alias system (the use_alias decorator), which automatically converts long-form PyGMT parameters to short-form GMT options. The alias system works well but has known limitations and issues (e.g., #256, #262).
Sometimes, GMT arguments are still specified using long unreadable strings. For example,
projection="S0/90/12c": related to Create Projection classes instead of strings #356, Implement Projection classes to encapsulate arguments #379frame=["WSen", "xaf+lXLABEL", "yaf+lYLABEL"]: related to Better way to configure axis (instead of B/frame) #249
As these common options are used a lot, we may take some time to find and implement a more Pythonic way to specify projection, frame et al.
However, there are still a lot of other long GMT arguments. Here is a simple example showing how to plot a GMT logo on maps.
import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 2], projection="X6c", frame=["WSen", "xaf", "yaf"])
fig.logo(position="jTR+o0.3c/0.6c+w3c", box="+p1p+glightblue")
fig.show()The above Python script is equivalent to the following GMT CLI script:
gmt begin
gmt basemap -R0/10/0/2 -JX6c -B
gmt logo -DjTR+o0.3c/0.6c+w3c -F+p1p+glightblue
gmt end showThe long argument for position and box are really difficult to remember and write. There are definitely some better and more Pythonic ways to specify these arguments. As GMT has too many options, we must provide a universal way to specify these long arguments.
Perhaps the simplest way is to pass these long arguments as dict, i.e.,
fig.logo(
position={
location: "jTR",
offset: [0.3, 0.6],
width: "3c",
},
box={
pen: "1p",
fill: "lightblue"
}
)
Are there any better ways?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
weiji14 commentedon Apr 6, 2021
This has come up again and again in different forms, so I've started a project at https://github.com/GenericMappingTools/pygmt/projects/6 to track the various issues and PRs tackling this. Let's make this issue the central point to gather all the ideas. Here's my attempt at summarize the various candidates (feel free to edit this comment anyone, or suggest alternatives)
Dictionary
Pass the arguments into the parameter as a Python dictionary. E.g. at #262 (comment), #1082 (comment)
Pros: Appears easy to implement, sort of intuitive for new Python users
Cons: Parameters like
location/offset/widthcannot be tab-completed, every PyGMT module will need to be updated to handle these dict inputsFunctions
Expand the PyGMT module's keyword argument list to specify every possible parameter. E.g. at #1173 (comment)
Pros: Can tab-complete inside PyGMT module, more discoverable by new users
Cons: May result in long list of parameters to scroll through, will need to be added to docstring of every module
Note: if it's a one-off thing though, this functional style might be suitable, e.g. for the text 'paragraph' option at #1078 (comment)
Classes
Set up a dedicated PyGMT Python class to handle the parameter/arguments. E.g. at #356 (comment), #262 (comment).
Pros: Can be tab-completed, only need to implement once and be used throughout all PyGMT modules, can plug into existing workflow
Cons: May not be as discoverable by new users, will be a bit harder to program up front
Yes, my preference is for the 'Classes' option, so I'll elaborate on it a bit more. The main idea is to have a
params.pyfile with lots ofpygmt.param.Abcdeclasses (or some other name). E.g.:pygmt.param.Frame(-B, Better way to configure axis (instead of B/frame) #249)pygmt.param.Position(-D, What's the most Pythonic way for long GMT arguments? #1082)pygmt.proj.Orthographic(-J, Implement Projection classes to encapsulate arguments #379)Implementation (of pygmt.param.ClassName)
It's been mentioned at #249 (comment) and attempted for e.g. at #379, but I'll summarize the idea again. A class like
pygmt.param.Penfor example, will need to have a__str__method to produce the correct-Wargument to be passed intopen:Since PyGMT has adopted NEP29 #1074 and is Python 3.7+ now, we can use Python dataclasses to implement this (see https://realpython.com/python-data-classes), and this means less boilerplate when coding things up (compared to traditional Python classes).
TLDR: Let's start small-ish, e.g. @willschlitzer's issue on pen (-W) at #1173 will be a great start, and we can work out the implementation details along the way.
seisman commentedon Apr 6, 2021
The "Classes" option sounds the best way, but we need to think about a general way to implement classes for all options.
maxrjones commentedon Apr 27, 2021
I agree that the classes option is good. I have a couple further opinions about the implementation of long GMT arguments:
__str__to format into GMT style, such thatprintgives user-readable information rather than the sometimes convoluted GMT syntax.weiji14 commentedon Apr 27, 2021
Yep, we should definitely have standardized aliases across all GMT wrappers. There was a lot of discussion around this in 2019/2020 on GitHub and the GMT forum (e.g. https://forum.generic-mapping-tools.org/t/standardized-human-readable-gmt-parameters-for-pygmt-matlab-julia-pyshtools-etc/77) but the conversation stalled for some reason, so thanks for picking this up again and starting the project to keep track!
This can be arranged. We can have both a
__str__and__repr__method in thepygmt.param.ClassNameclass (see https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr), one which is human-readable (for the user), and one which is machine-readable (for GMT 😛). I'll work on it in the RFC PR at #1239.Edit: Actually, Python 3.7 data classes already implements a
__repr__method (see https://realpython.com/python-data-classes/#basic-data-classes). So the current implementation at #1239 does the following:For the last one, simply putting
penat the end in a Jupyter cell would give the information-rich__repr__representation of the Pen object, same as callingrepr(pen). Callingstr(pen)orprint(pen)would give the GMT style string. It sounds though that you prefer it to be the other way around @meghanrjones?maxrjones commentedon Apr 29, 2021
Cool, this is on the agenda to discuss at the next GMT community meeting. Of course, we could also discuss at the next PyGMT meeting in case anyone is unavailable May 6.
Yes, but I could be convinced elsewise if others feel strongly that
__str__should be used for the GMT readable formatting.leouieda commentedon May 6, 2021
Sorry, jumping in here a bit late here. A quick impression is that something like the dictionary approach would be the easiest way to implement but not necessarily the best way to use the library. Mainly because the dictionary arguments won't be tab-completed. Same thing goes for the classes. It would be nice as a developer but I really wouldn't want to have to create separate instances just to pass them as arguments to a function.
This is something I feel is very easy to forget when making software: good APIs optimize usability. So if we forget about the implementation details: How would you want to use PyGMT? Not as a GMT guru but as a Python user who wants to make some nice maps.
As a user, I feel like the easiest thing to use would be just flat function arguments. Then I can type
fig.grdimagein a notebook and press tab to see what I have to give it very easily from the signature. The current implementation of our argument conversion makes this very difficult since we have to look up alias names etc. It was relatively easy to implement but it's painful to use in practice. Basically, anything that hinders tab-completion is going to hinder usability.So if we take something like this:
As a user, I would want to write something like this instead:
And going back to the logo example:
To me, every time multiple options have the same modifier that is an indication that this function is trying to do too many things. The mapping of "GMT module = PyGMT function/method" is not great for usability. We can always internally combine the options into something GMT wants and call the appropriate module. Decoupling the GMT I/O from the API is something that would make this a lot easier on our side but that is a huge amount of work.
You all have been putting in the time here so of course you get to decide and I'm perfectly happy with whatever the group settles on. You have been an awesome group and I wish I had more time to be interact more with you 🙂
leouieda commentedon May 6, 2021
The
frameargument is another one that I would break into a dozen separate ones. Passing in a list with crazy strings is not something I've enjoyed recently.leouieda commentedon May 7, 2021
Been thinking about this some more. The classes option is not a bad idea but there are some things to watch out for:
Penexample is a good one."1p,black,-"is quite reasonable in terms of complexity and should be somewhat familiar to matplotlib users (like"-k"inplt.plot). Other things like frames and positioning of boxes are the worse in terms of complexity and difficulty to remember. There are many that fall in between and I'm not sure where to draw the line.As a coder, I would definitely prefer implementing the classes since they would make our job easier. But as a user, I would probably end up passing the crazy GMT strings instead since creating all these different instances would be a bit tedious.
5 remaining items
seisman commentedon Nov 22, 2023
Now it's possible to tab-complete dict keys, see #2793. It works in VSCode and other IDEs, but I can't make it work in Jupyter notebooks.
The parameter names are also too long and it means a lot of work for maintainers, as we have to convert
position_location="jTR", position_offset=[0.3, 0.6], position_width="3c"to-DjTR+o0.3/0.6+w3c.I feel we can have both classes and dictionary. We can use classes for common options (e.g.,
-B,-J) and dictionary for non-common options (e.g.,text's-Moption).seisman commentedon Nov 16, 2025
I believe we've reached a consensus to adopt both option 2 (splitting complex options into multiple parameters) and option 3 (implementing classes, e.g., #3995), while option 1 (using a dictionary) will not be supported.
Closing the issue. Feel free to reopen it if anyone disagree.