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

which(), image() and legend() don't support pathlib.Path inputs #1832

Closed
seisman opened this issue Mar 18, 2022 · 5 comments · Fixed by #1837
Closed

which(), image() and legend() don't support pathlib.Path inputs #1832

seisman opened this issue Mar 18, 2022 · 5 comments · Fixed by #1837
Labels
bug Something isn't working
Milestone

Comments

@seisman
Copy link
Member

seisman commented Mar 18, 2022

I'm trying to fix a pathlib bug of plot and plot3d in #1831 and find that which(), image() and legend also don't support pathlib.Path inputs.

To reproduce the issue, run:

>>> import pygmt
>>> from pathlib import Path

>>> pygmt.which(Path("abc.txt"))

>>> fig = pygmt.Figure()
>>> fig.image(Path("abc.png"))

>>> fig.legend(spec=Path("legend.txt"))

The error message is: TypeError: sequence item 0: expected str instance, PosixPath found.

Other functions work because their input file is wrapped in a context manager, like

with file_context as infile:
arg_str = " ".join([infile, build_arg_string(kwargs)])
lib.call_module("histogram", arg_str)

To fix the bug, the simplest solution is changing input file to a string type, for example, changing fname to str(fname). Do anyone have better and more general solutions?

@seisman seisman added the bug Something isn't working label Mar 18, 2022
@seisman
Copy link
Member Author

seisman commented Mar 19, 2022

In pygmt.which() or other functions, we build the argument string using the following code:

arg_str = " ".join([fname, build_arg_string(kwargs), "->" + tmpfile.name])

Perhaps we should refactor the build_arg_string function to:

build_arg_string(kwargs, infile=None, outfile=None)

and in build_arg_string, we always make sure that infile and outfile are converted to string type.

@seisman
Copy link
Member Author

seisman commented Mar 29, 2022

Does anyone have comments on the proposed refactor in #1837?

@maxrjones
Copy link
Member

Does anyone have comments on the proposed refactor in #1837?

I like the idea in general. I would rather that all modules handle input and output using the same strategy rather than only applying this code structure to these three methods/functions. Would you be open to a larger refactor using the new build_arg_string input and output arguments in other functions/methods for consistency?

A couple other questions:

  1. Should we do any type checking before converting to a string?
  2. The only reference pathlib.Path objects in our docs are the changelog, virtualfile_from_data, and load_dataarray. Should we add this as an accepted input if it is something that we intend to support (as suggested by this issue)? This is something that is explicitly stated in the xarray and pandas docs when available as an input type (e.g., https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html#xarray.open_dataset and https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.read_csv.html)

@seisman
Copy link
Member Author

seisman commented Mar 30, 2022

Does anyone have comments on the proposed refactor in #1837?

I like the idea in general. I would rather that all modules handle input and output using the same strategy rather than only applying this code structure to these three methods/functions. Would you be open to a larger refactor using the new build_arg_string input and output arguments in other functions/methods for consistency?

Yes, the plan is to refactor build_arg_string in all functions.

  1. Should we do any type checking before converting to a string?

Perhaps not necessary now. From what I see, infile/outfile is either a string, a pathlib.Path object or a virtual file (also a string), so converting everything to a string looks OK.

  1. The only reference pathlib.Path objects in our docs are the changelog, virtualfile_from_data, and load_dataarray. Should we add this as an accepted input if it is something that we intend to support (as suggested by this issue)? This is something that is explicitly stated in the xarray and pandas docs when available as an input type (e.g., xarray.pydata.org/en/stable/generated/xarray.open_dataset.html#xarray.open_dataset and pandas.pydata.org/pandas-docs/dev/reference/api/pandas.read_csv.html)

Mention pathlib.Path in docstrings sounds a good idea. BTW, the planned tutorial at #1268 will also mention pathlib.Path support.

@seisman seisman added this to the 0.6.1 milestone Apr 4, 2022
@seisman
Copy link
Member Author

seisman commented Apr 4, 2022

Closed by #1837.

@seisman seisman closed this as completed Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants