Repository navigation
Description
Originally posted by @weiji14 in #1284 (comment):
Currently, the blockm*/grdtrack modules simply outputs pandas.DataFrame/filenames (see #1099) automatically depending on the input type (2 options). The implementation @willschlitzer made at bf58dc2 and 68fab7c returns either numpy/pandas/str depending on the user input (3 options). Best to be consistent across PyGMT on the types of outputs users can expect.
I am opening this issue to find out what the output format/options should be with the additional of x/y/z in blockmean, blockmedian, and grdtrack, as well as for new methods such as grd2xyz.
Edit by @seisman on Oct 9, 2023:
The PyGMT team has reach an agreement that PyGMT functions/methods should have consistent behavior for table-like outputs, i.e., the output depends on the output_type parameter.
Valid output_type values are:
file: output to a file directly (usually need to setoutfile)numpy: return a NumPy array (may not always possible because data must be in the same dtype in a 2-D numpy array)pandas: return a pandas.DataFrame (sometimes need to setnewcolname)
Here are a list of functions/methods that have table-like outputs:
-
blockm*pygmt.blockm*: Add 'output_type' parameter for output in pandas/numpy/file formats #3103 -
filter1dpygmt.filter1d: Improve performance by storing output in virtual files #3085 -
grdinfo[Will be tracked in Better return values for grdinfo #593] -
grdtrackpygmt.grdtrack: Add 'output_type' parameter for output in pandas/numpy/file formats #3106 -
triangulatepygmt.triangulate.delaunay_triples: Improve performance by storing output in virtual files #3107 -
grdvolumepygmt.grdvolume: Refactor to store output in virtual files instead of temporary files #3102 -
selectpygmt.select: Improve performance by storing output in virtual files #3108 -
which[Will be tracked in pygmt.which: Find full path to specified files #3003] -
grdhisteqpygmt.grdhisteq.compute_bins: Refactor to store output in virtual files instead of temporary files #3109 -
x2sys_cross[Will be tracked in x2sys_cross: Refactor to get rid of temporary files and have consistent table-like output behavior #3160] -
info[Will be tracked in Better return value for pygmt.info #3159] -
projectpygmt.project: Add 'output_type' parameter for output in pandas/numpy/file formats #3110 -
grd2xyzpygmt.grd2xyz: Improve performance by storing output in virtual files #3097
We need to make sure they behave consistently.
Activity
[-]What should the default table-like output be for PyGMT methods?[/-][+]What should the default table-like output be for PyGMT functions/methods?[/+]weiji14 commentedon Jun 6, 2021
Just wanted to say, this is a similar issue to that in the RAPIDS AI
cumllibrary. See https://docs.rapids.ai/api/cuml/0.19/api.html#output-data-type-configuration. Their solution is to:numpyin/numpyout, orpandas.DataFramein/pandas.DataFrameout.using_output_typecontext manager. E.g. setusing_output_type=numpyto returnnumpyregardless of input type.As you can see, this involves more levels of I/O complexity going from phase 1 to phase 2. I would advocate to move to phase '1' for PyGMT v0.5.0 or v0.6.0, and keep PyGMT's default output type for table-like outputs as
pandas.DataFrame(or None if outfile is set) for PyGMT v0.4.0 as per #1099 (i.e phase '0' if you will). Longer term, as PyGMT matures and integrates more with the PyData stack, we may want to consider mimickingcuml's configurable I/O behaviour.That said, I'm open to other suggestions, if someone has a good idea on how to deal with I/O in PyGMT.
maxrjones commentedon Jun 8, 2021
Thanks for the helpful reference, @weiji14! I like the mirroring behavior and agree that it would be a good milestone for v0.5.0+.
In my opinion, a parameter to control the output type seems a bit simpler for users than a context manager (examples below). The options for
pygmt.using_output_typemay also depend on the specific function/method, which could become difficult to document. The context manager would be useful if users want to easily specify the output type for a block of code. Are there other benefits to implementing the configurable I/O behavior with a context manager rather than with function/method parameters?Context manager example:
Parameter example:
weiji14 commentedon Jun 8, 2021
You're right that controlling the output type via a parameter (similar to what @willschlitzer did in bf58dc2) would be simpler than a context manager. I don't have a preference for either style, but after doing a bit of digging, it seems like
cumlhad anoutput_typeparameter at one point, but they deprecated it in favour of the context manager option in NVIDIA/cuml#3040 for some reason (though it still seems to be mentioned in places like at https://github.com/rapidsai/cuml/blob/branch-21.08/wiki/python/ESTIMATOR_GUIDE.md#specifying-the-array-output-type).Anyways, implementation-wise, I'm wondering if we could have a reusable decorator or helper function that can convert any table-like output to the desired format (be it
pandas.DataFrameor otherwise) instead of having to have a big block of code for each PyGMT method that returns table-like outputs. I.e. reuse the if-then block from bf58dc2, but have it in a single standardized place.maxrjones commentedon Jun 10, 2021
Yes, I agree that a helper function is a good idea for the implementation of an output format choice.
willschlitzer commentedon Jun 15, 2021
I think the helper function would be our best bet (although I'm not too strong with writing decorators). Borrowing/stealing entirely from my commits mentioned above, I think something like this would could be helpful:
I know it's a little redundant accepting the
resultargument only to immediately return it ifdata_formatis set as a string, but it avoids the reuse of if statements. Additionally, I put an f-string into theGMTInvalidInputerror in the case of different format parameter names in different functions (as is the case between #1284 and #1299). I can submit it in a PR if that's the way we agree that this should be handled.weiji14 commentedon Jun 15, 2021
The return_table function looks to be on the right track. We can discuss this in the meeting tomorrow (https://forum.generic-mapping-tools.org/t/pygmt-community-zoom-meeting-tuesday-15-june-2021-utc-1700/1762) if there's time.
In terms of your PRs #1284 and #1299 though @willschlitzer, I'd suggest sticking to a
strorpandas.DataFrameonly output for PyGMT v0.4.0 if you want to get those PRs in on time (and not wait until v0.5.0). I.e. follow the code used bygrdtrackandblockm*:pygmt/pygmt/src/grdtrack.py
Lines 273 to 281 in a10af5d
You're welcome to start a proof of concept PR, but realistically speaking, I don't expect it to be ready for PyGMT v0.4.0. See this blog post by the RAPIDS AI team (https://medium.com/rapids-ai/making-rapids-cudf-io-readers-and-writers-more-reliable-with-fuzz-testing-5d595aa97389) which talks about the complexity of testing different I/O format combinations. Just want to manage expectations a bit 🙂
willschlitzer commentedon Jun 16, 2021
@weiji14 @meghanrjones I know this is different than what we discussed at the PyGMT meeting last night, but after giving it some more thought, I would prefer not getting the table output options for
grd2xyzandgrdvolumein line withgrdtrackfor v0.4.0 if the plan is to move towards their current implementation for v0.5.0 (along with the use of a helper function). I don't thinkgrdvolumeandgrd2xyzare particularly in-demand modules, so I don't think holding off on them for v0.5.0 is especially problematic. My understanding is that the issue with the numpy array is that it cannot accept different data types, while a pandas DataFrame and list can, but it seems like all of the table outputs should be able to be converted to floating numbers, and be used in a numpy array. If I'm wrong on this one, please let me know and I'll make the changes, but I would prefer not to add these modules with the intention of making them backwards incompatible in the next release. I'll make a proof of concept pull request as discussed above.maxrjones commentedon Jun 17, 2021
Thanks for the detailed explanation and for submitting the proof-of-concept PR. I understand your points. I'll add some comments on #1336, but I agree with you that it's not likely we will reach consensus and update the other methods/functions with table output in the next few days. So, it seems #1318, #1336, #1299, and #1284 will be on track for v0.5.0.
[-]What should the default table-like output be for PyGMT functions/methods?[/-][+]Consistent table-like output for PyGMT functions/methods[/+]maxrjones commentedon Aug 10, 2021
I support Will wanting to merge in #1299 and #1284 before addressing this issue to keep things moving. At the same time, I would prefer that the argument format for the
output_typeparameter is final (i.e., will be used for all applicable functions) before merging these PRs to avoid deprecations. Here's the current section:When testing out the PR, I found it hard to remember 'a', 'd', and 's' (I kept instinctively trying 'p' for pandas.DataFrame). I would prefer longer names like 'numpy', 'pandas', and 'str'.
Here's my question - how do we decide on the implementation when there's differences in the preferred method between developers? Can we have a vote?
willschlitzer commentedon Aug 10, 2021
@meghanrjones I'm fine with a vote, but as the developer with the least experience I'm happy to defer to your opinion on this one!
26 remaining items
seisman commentedon Oct 9, 2023
I think we have reached an agreement (at least partially) about the expected behavior of table-like output. I've revised the top post to better track the progress.
weiji14 commentedon Oct 10, 2023
Just noting that Pandas 2.0 has not just a NumPy-backed arrays, but also PyArrow(C++)-backed arrays (see https://pandas.pydata.org/docs/whatsnew/v2.0.0.html#argument-dtype-backend-to-return-pyarrow-backed-or-numpy-backed-nullable-dtypes). Also according to PDEP10, Pandas 3.0 will use
ArrowDtypeinstead ofobjectdtype for string columns.Haven't had time yet to work out what this would mean for our implementation of
GMT_DATASETat #2729, but we might need some matrix tests to check that both NumPy and PyArrow-backedpandas.DataFrames work.seisman commentedon Apr 8, 2024
Now most modules already have consistent behaviors for table-like output. There are still four exceptions:
x2sys_crossis complicated and need more work; forwhich/info/grdinfo,file/numpy/pandasoutput types may make no sense, so they need more discussions and will be tracked in separate issues.Closing the issue. Great to have consistent table-like output behavior across the whole project!
yvonnefroehlich commentedon Apr 9, 2024
Sounds great! Thanks @seisman for all your efforts on this!