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

Refactor text to use virtualfile_from_vectors instead of pandas tempfile #559

Merged
merged 4 commits into from
Sep 6, 2020

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Aug 10, 2020

Description of proposed changes

Non user-facing refactor to avoid the use of temporary files in the original text implementation at #321.

Enhance the text implementation by allowing a list/array of angle/font/justify inputs. Something like so: Note: below code will not work yet.

import pygmt

fig = pygmt.Figure()
fig.text(
    x=[5.0, 6.0],
    y=[6.0, 7.0],
    angle=[30, 60],
    justify=['BL', 'TL'],
    font=['16p,2,blue', '32p,3,red'],
    text=["First label", "Second label"]
)
fig.show()

Note that this requires GMT 6.1.1, as mentioned in #520, so it won't work yet. It works now.

One step towards fixing issue at #483

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.

Modified virtualfile_from_vectors to use put_strings on last column instead of put_vectors if it has a string data type. In theory this should work for `text`, but in reality, a segmentation fault happens for GMT < 6.1.1.
@weiji14 weiji14 added the enhancement Improving an existing feature label Aug 10, 2020
@seisman
Copy link
Member

seisman commented Sep 4, 2020

This PR should work after we bump 6.1.1. Please update this branch and see if it works.

@weiji14 weiji14 mentioned this pull request Sep 6, 2020
11 tasks
@seisman
Copy link
Member

seisman commented Sep 6, 2020

This PR is already a good improvement (avoid using temporary files), although it doesn't fix #483.

I think we can remove the Fixes #483 comment in the top post and merge it.

@weiji14
Copy link
Member Author

weiji14 commented Sep 6, 2020

Ok, I'll update the branch.

@weiji14 weiji14 changed the title WIP Refactor text to use virtualfile_from_vectors instead of pandas tempfile Refactor text to use virtualfile_from_vectors instead of pandas tempfile Sep 6, 2020
@weiji14 weiji14 added the skip-changelog Skip adding Pull Request to changelog label Sep 6, 2020
@weiji14 weiji14 marked this pull request as ready for review September 6, 2020 23:04
Comment on lines +987 to +995
with Session() as lib:
file_context = dummy_context(textfiles) if kind == "file" else ""
if kind == "vectors":
if position is not None:
file_context = dummy_context("")
else:
file_context = lib.virtualfile_from_vectors(
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
)
Copy link
Member

Choose a reason for hiding this comment

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

Is it more readable to rewrite the codes to:

if kind == "file":
   ...
elif kind == "vectors":
  ...
else:
  ...

Copy link
Member Author

@weiji14 weiji14 Sep 6, 2020

Choose a reason for hiding this comment

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

Yes it would be more readable. I think the only reason I wrote it that way was because pylint complained about too many if-statements:

R0912: Too many branches (13/12) (too-many-branches)

If readability is more important, I can increase the branch limit on our .pylintrc file, or I can try refactoring more of the code in text to reduce the number of if-statements. OR, we can just merge this in now and worry about it in the next PR that will fix #483.

Copy link
Member

Choose a reason for hiding this comment

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

OR, we can just merge this in now and worry about it in the next PR that will fix #483.

OK to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants