GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add the Session.virtualfile_from_stringio method to allow StringIO input for certain functions/methods by seisman · Pull Request #3326 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Jul 11, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Description of proposed changes

This PR adds support for StringIO inputs, which was initially requested in #571 (4 yeas ago!).

GMT doesn't natively support reading StringIO objects. To pass a StringIO object into GMT, we need to create a GMT_DATASET container and store the contents of the StringIO object in the GMT_DATASET container.

Ideally, any PyGMT methods/functions should accept StringIO input, but it's technically difficult to implement. The main reason is that, when creating a GMT_DATASET container, we need to specify the number of tables, segments, columns, and rows, which means we must try very hard to parse the contents of the StringIO object. The number of columns actually is the most difficult to determine. For example, the content below should be 2 numeric columns and one trailing text column, but we may incorrectly parse it to have 0 numeric columns:

30.0W 20.0S TEXT1 TEXT4
35.0W 20.5S TEXT2

and the following content that we want to pass to Figure.text's paragraph mode (#1078), should have zero numeric columns and one trailing text column, but we may incorrectly think it has two numeric columns:

gmt text -R0/3/0/5 -JX3i -h1 -M -N -F+f12,Times-Roman+jLT -pdf figure << EOF
This is an unmarked header record not starting with #
> 0 -0.5 13p 3i j
20240101 010101 Event 1
20240101 010101 Event 2
EOF

Thus, to keep things as simple as possible, I think we should only allow StringIO input for a few modules (e.g., makes much sense for Figure.legend, but makes little sense to Figure.plot). In this case, we just need to store the StringIO contents into the trailing text of the GMT_DATASET container.

This PR implements the virtualfile_from_string method to allow StringIO objects. There are two related PRs:

  • Support StringIO object in Figure.legend (PR Figure.legend: Support passing a StringIO object as the legend specification #3438 addressing Allow Figure.legend to read from StringIO #571)
  • Let Figure.text support paragraph mode (PR #XX addressing Figure.text(): Support paragraph mode #1078)

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 wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

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

  • /format: automatically format and lint the code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14 reacted with laugh emoji
All reactions
  • 😄 1 reaction
seisman added 5 commits July 12, 2024 00:31
@seisman
Add the Session.virtualfile_from_stringio function to support StringI…
5e22d55
…O object as inputs
@seisman
Let virtualfile_in support the stringio kind
f75844e
@seisman
Make data_kind support stringio
50467b5
@seisman
Refactor Figure.legend to support stringio
14a14e9
@seisman
Add a legend test for StringIO input
37c3c2f
@weiji14 weiji14 added the feature Brand new feature label Jul 11, 2024
@seisman

seisman commented Jul 12, 2024

Copy link
Copy Markdown
Member Author

Ideally, any PyGMT methods/functions should accept StringIO input, but it's technically difficult to implement. The main reason is that, when creating a GMT_DATASET container, we need to specify the number of tables, segments, columns, and rows, which means we must try very hard to parse the contents of the StringIO object. The number of columns actually is the most difficult to determine.

Actually, when calling a GMT module, we usually know the expected number of numeric columns (for example, 0 for legend, 2 or more for plot), so we could add an extra parameter n_columns to the virtualfile_from_stringio function after refactoring the virtualfile_in method in #2744. But I feel that passing a StringIO object to modules like plot makes little sense and has very low priority. Then the question is, how can we add StringIO support to specific functions only (e.g., Figure.legend)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seisman seisman added the needs review This PR has higher priority and needs review. label Jul 12, 2024
seisman added 6 commits July 17, 2024 14:05
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
4e2e545
@seisman
Some updates
8e1a609
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
55d6e81
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
b3b2cb9
@seisman
Fix styling issue
89757ec
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
0489783
@seisman seisman removed the needs review This PR has higher priority and needs review. label Jul 23, 2024
seisman added 7 commits July 24, 2024 17:03
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
791e4f6
@seisman
Updates
5f4d21f
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
1a2d336
@seisman
Improve Figure.legend
3682098
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
21b2496
@seisman
Fix legend
146e430
@seisman
Updates
c589a40
@seisman seisman mentioned this pull request Sep 11, 2024
Figure.legend: Refactor to simplify the logic of checking legend specification #3437
Merged
@seisman
Figure.legend: Refactor to simplify the logic of checking legend spec…
ff90b2e
…ification
@seisman
seisman changed the base branch from main to refactor/legend September 12, 2024 00:41
seisman added 3 commits September 12, 2024 08:45
@seisman
Merge remote-tracking branch 'origin/refactor/legend' into api/virtua…
90455c1
…lfile-from-stringio
@seisman
FIx
4e4bd2d
@seisman
Revert changes in legend
deb917d
@seisman seisman mentioned this pull request Sep 12, 2024
Figure.legend: Support passing a StringIO object as the legend specification #3438
Merged
Base automatically changed from refactor/legend to main September 12, 2024 16:00
weiji14
weiji14 reviewed Sep 12, 2024
View reviewed changes
Comment thread pygmt/clib/session.py Outdated
Comment on lines +1660 to +1664
if line.startswith(">"): # Segment header
if header is not None: # Only one segment is allowed now.
raise GMTInvalidInput("Only one segment is allowed.")
header = line
continue

@weiji14 weiji14 Sep 12, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Need a test to check that multi-segment inputs fail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 13, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It's likely we have to allow multi-segments in StringIO.

Here is a CLI version for typesetting a paragraph of text. It seems the first line must be a line not starting with # and is ignored. Not sure why it was designed like this. I'll open another POC PR for Figure.text and see how it works.

gmt text -R0/3/0/5 -JX3i -h1 -M -N -F+f12,Times-Roman+jLT -pdf figure << EOF
This is an unmarked header record not starting with #
> 0 -0.5 13p 3i j
@%5%Figure 1.@%% This illustration shows nothing useful, but it still needs
a figure caption. Highlighted in @;255/0/0;red@;; you can see the locations
of cities where it is @\_impossible@\_ to get any good Thai food; these are to be avoided.
EOF

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 13, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Correction: I didn't see the -h1 option in the CLI version, which skips the first line. Without -h1, the CLI version still works:

gmt text -R0/3/0/5 -JX3i -M -N -F+f12,Times-Roman+jLT -pdf figure << EOF
> 0 -0.5 13p 3i j
@%5%Figure 1.@%% This illustration shows nothing useful, but it still needs
a figure caption. Highlighted in @;255/0/0;red@;; you can see the locations
of cities where it is @\_impossible@\_ to get any good Thai food; these are to be avoided.
EOF

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 13, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Here is a POC example to show how to support paragraph mode via StringIO:

In [1]: import io

In [2]: stringio = io.StringIO(
   ...:     "> 0 -0.5 13p 3i j\n"
   ...:     "@%5%Figure 1.@%% This illustration shows nothing useful, but it still needs\n"
   ...:     "a figure caption. Highlighted in @;255/0/0;red@;; you can see the locations\n"
   ...:     "of cities where it is @_impossible@_ to get any good Thai food; these are to be avoided.\n"
   ...: )

In [3]: import pygmt

In [4]: fig = pygmt.Figure()

In [5]: from pygmt.clib import Session

In [6]: with Session() as lib:
   ...:     with lib.virtualfile_in(data=stringio) as vintbl:
   ...:         lib.call_module(module="text", args=f"{vintbl} -R0/3/0/5 -JX3i -M -N -F+f12,Times-Roman+jLT")
   ...:

In [7]: fig.show()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 13, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

StringIO objects containing multi-segments are supported in the latest version.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14 reacted with hooray emoji
All reactions
  • 🎉 1 reaction
Comment thread pygmt/clib/session.py

def virtualfile_in( # noqa: PLR0912
self,
check_kind=None,

@weiji14 weiji14 Sep 12, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Then the question is, how can we add StringIO support to specific functions only (e.g., Figure.legend)?

Maybe legend should have a special check_kind? E.g. if check_kind("legend"): valid_kinds += ("stringio", ...)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 13, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

See #3438 for the POC PR for Figure.legend. Since we already checked the data kind before entering the session, we can have check_kind=False in Figure.legend.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
seisman added 2 commits September 13, 2024 08:01
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
97bbe05
@seisman
Improve docstrings
dece315
@seisman seisman changed the title Add the Session.virtualfile_from_stringio function to allow StringIO input Add the Session.virtualfile_from_stringio method to allow StringIO input Sep 13, 2024
seisman added 9 commits September 13, 2024 14:23
@seisman
Support mutli-segment stringio input
486fce7
@seisman
Revert "Support mutli-segment stringio input"
021a97a
This reverts commit 486fce7.
@seisman
Remove the leading '>' from header
f6da405
@seisman
Also need to set the header pointer to None
824d861
@seisman
Support mutli-segment stringio input
640e9a9
@seisman
Update docstrings
3161963
@seisman
Fix a bug in n_rows
026f6e4
@seisman
Add some tests
850337e
@seisman
Fix static type checking
ed20118
@seisman seisman changed the title Add the Session.virtualfile_from_stringio method to allow StringIO input WIP: Add the Session.virtualfile_from_stringio method to allow StringIO input Sep 13, 2024
@seisman
Improve the tests
f1b5f08
@seisman
seisman marked this pull request as ready for review September 13, 2024 10:52
@seisman seisman changed the title WIP: Add the Session.virtualfile_from_stringio method to allow StringIO input Add the Session.virtualfile_from_stringio method to allow StringIO input Sep 13, 2024
@seisman seisman added the needs review This PR has higher priority and needs review. label Sep 13, 2024
@seisman seisman added this to the 0.14.0 milestone Sep 13, 2024
@seisman seisman changed the title Add the Session.virtualfile_from_stringio method to allow StringIO input Add the Session.virtualfile_from_stringio method to allow StringIO input for certain functions/methods Sep 13, 2024
@seisman seisman mentioned this pull request Sep 13, 2024
RFC Allow for io.StringIO inputs to certain modules #576
Closed
5 tasks
seisman
seisman commented Sep 13, 2024
View reviewed changes
Comment thread pygmt/clib/session.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

seisman added 3 commits September 14, 2024 07:30
@seisman
Simplify the checking of segment header
e10cef6
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
22d4d55
@seisman
Merge branch 'main' into api/virtualfile-from-stringio
5ef08e3
@seisman
seisman requested a review from a team September 18, 2024 02:12
weiji14
weiji14 approved these changes Sep 18, 2024
View reviewed changes

@weiji14 weiji14 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks @seisman for adding multi-segment support! I just have one concern/thought about non-ASCII character support, but that could be handled later (if needed).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pygmt/clib/session.py
"GMT_IS_PLP", # items could be any one of POINT, LINE, or POLY
"GMT_IS_SURFACE", # items are 2-D grid
"GMT_IS_VOLUME", # items are 3-D grid
"GMT_IS_TEXT", # Text strings which triggers ASCII text reading

@weiji14 weiji14 Sep 18, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is only ASCII supported for now? What about other encodings?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman Sep 18, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

GMT only accepts ASCII text strings. Any non-ASCII characters should be written in the form of octal codes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@seisman
seisman merged commit 890626d into main Sep 18, 2024
@seisman
seisman deleted the api/virtualfile-from-stringio branch September 18, 2024 23:42
@seisman seisman removed the needs review This PR has higher priority and needs review. label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@weiji14 weiji14 weiji14 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

feature Brand new feature

Projects

None yet

Milestone

0.14.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@seisman @weiji14
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.