Skip to content

Enhance text with extra functionality and aliases - #481

Merged
weiji14 merged 16 commits into
masterfrom
enhance-text
Jun 21, 2020
Merged

Enhance text with extra functionality and aliases#481
weiji14 merged 16 commits into
masterfrom
enhance-text

Conversation

@weiji14

@weiji14weiji14 commented Jun 19, 2020

Copy link
Copy Markdown
Member

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.

@weiji14weiji14 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
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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.

Comment threadpygmt/base_plotting.py Outdated
Comment threadpygmt/base_plotting.py Outdated
Comment threadpygmt/base_plotting.py Outdated
Comment threadpygmt/base_plotting.py Outdated
Comment threadpygmt/base_plotting.py Outdated
Comment threadpygmt/tests/test_text.py Outdated
Comment threadpygmt/tests/test_text.py Outdated
Comment threadpygmt/tests/test_text.py
@seisman

seisman commented Jun 20, 2020

Copy link
Copy Markdown
Member

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

weiji14 commented Jun 20, 2020

Copy link
Copy Markdown
MemberAuthor

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
vercelBot 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
Copy Markdown
MemberAuthor

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 notinDTYPES:
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
Copy Markdown
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.

Comment threadpygmt/tests/test_text.py
Comment threadpygmt/tests/test_text.py Outdated
Comment threadpygmt/tests/test_text.py Outdated
Comment threadpygmt/tests/test_text.py Outdated

@seismanseisman left a comment

Copy link
Copy Markdown
Member

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>
@weiji14weiji14 modified the milestones: 0.1.x, 0.2.xJun 21, 2020
@weiji14

Copy link
Copy Markdown
MemberAuthor

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

@weiji14
weiji14 merged commit 86c46f0 into masterJun 21, 2020
@weiji14
weiji14 deleted the enhance-text branch June 21, 2020 02:51
@weiji14weiji14 modified the milestones: 0.2.x, 0.1.xJun 29, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementImproving an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@weiji14@seisman@EJFielding