Skip to content

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

Closed

Description

@seisman

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?

Activity

  1. added
    bug
    on Mar 18, 2022
  2. seisman commented on Mar 19, 2022

    @seisman
    MemberAuthor

    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.

  3. seisman commented on Mar 29, 2022

    @seisman
    MemberAuthor

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

  4. maxrjones commented on Mar 29, 2022

    @maxrjones
    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)
  5. seisman commented on Mar 30, 2022

    @seisman
    MemberAuthor

    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.

  6. added this to the 0.6.1 milestone on Apr 4, 2022
  7. seisman commented on Apr 4, 2022

    @seisman
    MemberAuthor

    Closed by #1837.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions