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

Enhance text with extra functionality and aliases #481

Merged
merged 16 commits into from Jun 21, 2020
Merged

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Jun 19, 2020

Description of proposed changes

Adding extra functionality to text, including aliases that were missing in initial implementation at #321. In support of the text tutorial at #480. Preview documentation at https://pygmt-git-enhance-text.gmt.vercel.app/api/generated/pygmt.Figure.text.html

TODO:

I'd be quite keen to enable text placement without setting an x or y by using position (-F+c in GMT) to get the x/y from the map frame's region. For example, the GMT gallery example at https://docs.generic-mapping-tools.org/latest/gallery/ex40.html does gmt text -Dj0.1i/0.1i -F+cLT+jTL+f18p+t"T = 100 km"to print the text at the top-left (TL) position in the map frame.

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.

@weiji14 weiji14 added the enhancement Improving an existing feature label Jun 19, 2020
So that we can set angle=True or angle="", and users can parse the angle from a textfile (in GMT it will be `-F+a`. Added a test case for this, and ensure that angle/font/justify=True (i.e. setting a boolean type) works.
@seisman
Copy link
Member

seisman commented Jun 19, 2020

These lines convert a list or a tuple into comma-separated strings. They don't make sense here.

angle="sequence_comma",
font="sequence_comma",
justify="sequence_comma",

@weiji14
Copy link
Member Author

weiji14 commented Jun 19, 2020

These lines convert a list or a tuple into comma-separated strings. They don't make sense here.

angle="sequence_comma",
font="sequence_comma",
justify="sequence_comma",

True. I can understand the rationale for font (e.g. doing font=("12p", "Helvetica", "green"), but not sure why I used it for angle and justify. Anyway, if we're going to support passing in an array or list of angle/font/justify, this probably has to go.

pygmt/base_plotting.py Outdated Show resolved Hide resolved
pygmt/base_plotting.py Outdated Show resolved Hide resolved
pygmt/base_plotting.py Outdated Show resolved Hide resolved
pygmt/base_plotting.py Outdated Show resolved Hide resolved
pygmt/base_plotting.py Outdated Show resolved Hide resolved
pygmt/tests/test_text.py Outdated Show resolved Hide resolved
pygmt/tests/test_text.py Outdated Show resolved Hide resolved
pygmt/tests/test_text.py Show resolved Hide resolved
@seisman
Copy link
Member

seisman commented Jun 20, 2020

textfiles : str or list
A text data file name, or a list of filenames containing 1 or more
records with (x, y[, font, angle, justify], text).

The current implementation only allows -F+a+f+j, so the input format should be

x, y[, angle, font, justify], text

Co-Authored-By: Dongdong Tian <seisman.info@gmail.com>
@weiji14
Copy link
Member Author

weiji14 commented Jun 20, 2020

The current implementation only allows -F+a+f+j, so the input format should be

x, y[, angle, font, justify], text

Done. I seem to recall reading somewhere (was it upstream in GMT?) that the reason was because we want all the numerical values first, and then the string values after. Can't seem to track down that issue/comment though :/ Edit: found it at https://docs.generic-mapping-tools.org/dev/text.html#use-from-external-interface.

@vercel vercel bot temporarily deployed to Preview June 20, 2020 11:24 Inactive
So that we don't plot the map frame four times in a loop.
Because it doesn't check for remote @... files. GMT will raise an appropriate GMTClibError if the file isn't found.
Fixes issue with having a blankspace offset in front of TL and BL before. The text can be directly given to the -F+t argument, and we skip using the temporary file. Also made sure that spaces in textstring will work properly.
Comment on lines +970 to +982
pd.DataFrame.from_dict(
{
"x": np.atleast_1d(x),
"y": np.atleast_1d(y),
"text": np.atleast_1d(text),
}
).to_csv(
tmpfile.name,
sep="\t",
header=False,
index=False,
quoting=csv.QUOTE_NONE,
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
pd.DataFrame.from_dict(
{
"x": np.atleast_1d(x),
"y": np.atleast_1d(y),
"text": np.atleast_1d(text),
}
).to_csv(
tmpfile.name,
sep="\t",
header=False,
index=False,
quoting=csv.QUOTE_NONE,
)
file_context = lib.virtualfile_from_vectors(
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
)

@leouieda mentioned before at #321 (comment) to use virtualfiles, and I finally figured out what he meant. Unfortunately, virtualfile_from_vectors doesn't yet support str types. This is the error on _check_dtype_and_dim:

        if array.dtype.name not in DTYPES:
            raise GMTInvalidInput(
>               "Unsupported numpy data type '{}'.".format(array.dtype.name)
            )
E           pygmt.exceptions.GMTInvalidInput: Unsupported numpy data type 'str864'.

From what I can tell, we'll need to use GMT_Put_Strings is that right? We should do this refactor in a separate PR, this one is already getting a bit too long.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, as I understand it, we need to use GMT_Put_Strings to pass trailing strings to GMT.

pygmt/tests/test_text.py Show resolved Hide resolved
pygmt/tests/test_text.py Outdated Show resolved Hide resolved
pygmt/tests/test_text.py Outdated Show resolved Hide resolved
pygmt/tests/test_text.py Outdated Show resolved Hide resolved
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.

See the comment above.

Besides that, I think the PR is good to merge. There are still some missing features about the text() method:

  1. Using virtual files instead of temporary file
  2. Let angle, font, and justify accept list/array as input
  3. more flexible input format, e.g., -F+a+j+f and -F+a+f+j.

I think we can address them in separate PRs in the feature.

Co-Authored-By: Dongdong Tian <seisman.info@gmail.com>
@weiji14
Copy link
Member Author

weiji14 commented Jun 21, 2020

Great, thanks for taking the time to review this so thoroughly @seisman! I'll open a new issue for tackling those future improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants