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

Sensible array outputs for pygmt info #575

Merged
merged 7 commits into from
Sep 9, 2020
Merged

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Sep 2, 2020

Description of proposed changes

Output the result of pygmt.info as a numpy.ndarray when either of per_column (C), spacing (I), or nearest_multiple (T) are used. This would be more usable that a raw string that is meant for the command line world.

This implements @seisman's original suggestion at #147 (comment), with both 1D and 2D array outputs supported!

Examples:

import pygmt

table = pygmt.datasets.load_ocean_ridge_points()

info(table=table, per_column=True)  # for basemap region -R
# array([-179.9401,  179.935 ,  -65.6182,   86.8   ])

info(table=table, nearest_multiple=0.1)  # for makecpt series -T
# array([-1.8e+02,  1.8e+02,  1.0e-01])

info(table=table, spacing=0.1)  # for basemap region -R
# array([-180. ,  180. ,  -65.7,   86.8])

info(table=table, spacing="b")  # for plot -Ap
# array([[-179.9401,  -65.6182],
#        [ 179.935 ,  -65.6182],
#        [ 179.935 ,   86.8   ],
#        [-179.9401,   86.8   ],
#        [-179.9401,  -65.6182]])

The parsing uses a bit of regex to remove the "-R", "-I" from the raw string, and split on "/" or " " delimiters.

pygmt/pygmt/modules.py

Lines 125 to 130 in 05a3f23

if any(arg in kwargs for arg in ["C", "I", "T"]):
# Converts certain output types into a numpy array
# instead of a raw string that is less useful.
result = np.loadtxt(
re.sub(pattern="-R|-T|/", repl=" ", string=result).splitlines()
)

Note: Merge after #574!!! ✔️

Fixes #147

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.

Also renamed 'fname' argument to 'table'
since `info` supports both file name inputs
and pandas.DataFrame tables now.
When either of per_column (C), spacing (I), or
nearest_multiple (T) are used in `pygmt.info`,
output the result as a numpy.ndarray which
would be more usable that a raw string that is
meant for the command line world. Also improve
the docstring of `pygmt.info` to mention that
numpy.ndarray outputs are being reported.
@weiji14 weiji14 added the enhancement Improving an existing feature label Sep 2, 2020
@weiji14 weiji14 changed the title WIP Sensible array outputs for pygmt info Sensible array outputs for pygmt info Sep 7, 2020
@vercel vercel bot temporarily deployed to Preview September 7, 2020 05:26 Inactive
@weiji14 weiji14 marked this pull request as ready for review September 7, 2020 05:27
pygmt/modules.py Outdated Show resolved Hide resolved
@weiji14
Copy link
Member Author

weiji14 commented Sep 9, 2020

Cool, thanks for reviewing, this will be a great addition for v0.2.0! I'll temporarily disable the required Windows Py3.8 test (random unrelated failure we should investigate at some point) to merge this (because I don't want to waste another 15min of CI resources).

@weiji14 weiji14 merged commit c6b5621 into master Sep 9, 2020
@weiji14 weiji14 deleted the sensible_info_outputs branch September 9, 2020 04:32
weiji14 added a commit to weiji14/deepicedrain that referenced this pull request Sep 13, 2020
Bumps [pygmt](https://github.com/GenericMappingTools/pygmt) from 0.1.2-36-g4939ee2a to 0.2.0.
  - [Release notes](https://github.com/GenericMappingTools/pygmt/releases)
  - [Changelog](https://github.com/GenericMappingTools/pygmt/blob/master/doc/changes.rst)
  - [Commits](GenericMappingTools/pygmt@v0.1.2-36-g4939ee2a...v0.2.0)

This includes several enhancements such as 'Sensible array outputs for pygmt info' (GenericMappingTools/pygmt#575) and 'Allow passing in pandas dataframes to x2sys_cross' (GenericMappingTools/pygmt#591) that will make our crossover analysis work and figure generation easier! Also edited Github Actions workflow to only run Docker build on Pull Requests when ready to review or when review is requested (i.e. not when PR is in draft mode).
weiji14 added a commit to weiji14/deepicedrain that referenced this pull request Sep 15, 2020
Bumps [pygmt](https://github.com/GenericMappingTools/pygmt) from 0.1.2-36-g4939ee2a to 0.2.0.
  - [Release notes](https://github.com/GenericMappingTools/pygmt/releases)
  - [Changelog](https://github.com/GenericMappingTools/pygmt/blob/master/doc/changes.rst)
  - [Commits](GenericMappingTools/pygmt@v0.1.2-36-g4939ee2a...v0.2.0)

This includes several enhancements such as 'Sensible array outputs for pygmt info' (GenericMappingTools/pygmt#575) and 'Allow passing in pandas dataframes to x2sys_cross' (GenericMappingTools/pygmt#591) that will make our crossover analysis work and figure generation easier! Also edited Github Actions workflow to only run Docker build on Pull Requests when ready to review or when review is requested (i.e. not when PR is in draft mode).
@weiji14 weiji14 mentioned this pull request Oct 27, 2020
5 tasks
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.

Return values of gmt.info
2 participants