Repository navigation
Closed
Labels
enhancementImproving an existing feature
Milestone
Description
One of the project goals is to interface with GMT C API directly using ctypes without system calls. It lets us work on data in memory, which is more efficient than working on files on disk.
However, currently PyGMT still writes tables/grids/CPTs into temporary files. With PRs #2729 and #2398 (not merged yet), we can write data into memory and then working on these data-in-memory directly. Thus, it's possible to get rid of temporary files completely.
This issue report is the central place to track all the functions/methods that need to be refactored.
Modules writting tables
-
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 -
whichpygmt.which: Refactor to get rid of temporary files #3148 -
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
Modules writting grids
Most modules are refactored in #2398, except grdcut.
-
dimfilter -
grdclip -
sphinterpolate -
grdsample -
grdfilter -
grdproject -
grdgradient -
grdfill -
triangulate -
sphdistance -
xyz2grd -
grdcut[**BREAKING** pygmt.grdcut: Refactor to store output in virtualfiles for grids #3115] -
grdhisteq -
nearneighbor -
sph2grd -
grdlandmask -
binstats -
surface
Reacted by Wei Ji
Metadata
Metadata
Assignees
Labels
enhancementImproving an existing feature
Activity
seisman commentedon Dec 19, 2023
Just tried to see if getting rid of temporary files can make PyGMT faster. The result are quit promising. For
grd2xyz, PR #2729 is ~35% faster (130 ms in PR #2729 vs 200 ms in the main branch):For the main branch:
For PR #2729:
weiji14 commentedon Dec 19, 2023
Nice, good to see those benchmarks! I've been wondering if we should use pytest-benchmark to get an idea of speedup over time. There's this
pytest-codspeed/CodSpeedHQ/action tool that would allow us to track these benchmarks over time, but downside is:@pytest.mark.benchmarkThe tests might run slower, because the benchmarks runs a single test multiple times to get the mean/standard deviation values.Edit: Actually no, codspeed only runs each benchmark once, see https://codspeed.io/blog/pinpoint-performance-regressions-with-ci-integrated-differential-profiling.Maybe we can selectively marking some of the tests we want to track from the list above at #2730 (comment)? I can try to set up the CI for this in the coming weekend.
seisman commentedon Dec 20, 2023
Sounds interesting. Manually marking tests sounds OK to me. For your point 2, since performance is not a high-priority issue, maybe we only do the benchmarks in the main branch. Technically, it's possible to override the
@pytest.mark.benchmarkmarker, so that it does nothing if not using CI in the main branch.seisman commentedon Mar 11, 2024
Here are some more completed benchmarks:
Calling the above functions using IPython's magic command
%timeit:Here are the outputs (on macOS):
So, we can conclude that the
vfilemethod is faster than thetmpfilemethod, specially for large data files.weiji14 commentedon Mar 16, 2024
I had a look at refactoring
x2sys_crossto use virtualfiles instead of temporary files, but it's a little tricky because:Input: Cannot pass in virtualfiles as input as mentioned at Wrap x2sys_init and x2sys_cross #546 (comment) and Passing in virtual files into the supplementary x2sys modules gmt#3717, since GMT doesn't support virtualfiles to X2SYS modules
Output: The
virtualfile_to_datasetmethod from clib: Add virtualfile_to_dataset method for converting virtualfile to a dataset #3083 was able to produce apandas.DataFrameoutput, but the column names were missing. The logic for handlingx2sys_cross's output is actually complicated:pygmt/pygmt/src/x2sys_cross.py
Lines 231 to 250 in bcbbcad
Important things to handle are:
datetime64dtypex2sys_crossmay output multi-segment parts (see https://docs.generic-mapping-tools.org/6.5/supplements/x2sys/x2sys_cross.html#remarks) when multiple tracks are passed in and-Qe(external COEs) is selected. Unsure how this is handled in GMT virtualfiles (note that we actually just merge all the multi-segments into one table when pandas.DataFrame output is selected, output to file will preserve the segments though).z_X/z_Morz_1/z_2depending on whether trackvalues/-Zargument is set.It should be possible to handle 1 and 3 somehow, but I'm not so sure about 2 since it will involve checking how GMT outputs virtualfiles in
x2sys_cross. We'll need to do some careful checking to ensure the refactoring doesn't modify the output and makes it incorrect.seisman commentedon Mar 18, 2024
The column names are available as table header in GMT_DATASET. So we can parse the table header and get the column names automatically. See https://github.com/GenericMappingTools/pygmt/pull/3117/files for a proof of concept. We just need to borrow some ideas from the
pd.read_csvmethod and carefully design the API.seisman commentedon Apr 17, 2024
I'm closing the issue since we have refactored most wrappers using virtual files for output. The only exceptions are
info/grdinfo/x2sys_cross/grdcutand will be tracked in their own issue/PRs.