Open
Labels
bugSomething isn't working
Description
Description of the problem
Under specific circumstances, Figure.plot does not work if a pandas.DataFrame is passed to the data parameter and a column order is selected via incols. The issue does not occur in case the pd.DataFrame contains only integers. If the desired columns are passed directly to the x and y parameters, the code works well. For me, this issue occurs under Windows but not under Linux.
For context, see PR #2515 up on comment #2515 (comment)
Maybe related to the issues in
- Add gallery example for Figure.ternary method #2138 up on comment Add gallery example for Figure.ternary method #2138 (comment) with fix in Figure.ternary: Fix the crash for pd.DataFrame input with GMT 6.3-6.4 #2274
- The "incols" (-i) parameter doesn't works for vector input correctly #1313 and The incols parameter does not always work for pandas.DataFrame #1440
Minimal Complete Verifiable Example
import pandas as pd
import pygmt
size = 5
# Set up random test data
test_dict_int = {
'a': [ 2, 2, 2, 2],
'z': [ 8, 6, 7, 3],
'x': [-3, -1, 1, 3],
'y': [ 2, 2, 2, 2],
}
test_df_int = pd.DataFrame(data=test_dict_int)
fig = pygmt.Figure()
fig.basemap(
region=[-size, size, -size, size],
projection="X" + str(size*2),
frame=True,
)
fig.plot(
# data=test_df_int, # integers -> WORKs
data=test_df_int.astype(float), # floats -> FAILs
incols=[2, 3],
# verbose="d",
)
# fig.show()
# fig.savefig(fname="bug_MWE.png")Output of verbose="d"
plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt
plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt/cache
plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt/server
plot [DEBUG]: Got regular w/e/s/n for region (-5/5/-5/5)
plot [INFORMATION]: Processing input table data
plot [DEBUG]: Operation will require 2 input columns [n_cols_start = 2]
plot [DEBUG]: Reset MAP_ANNOT_OBLIQUE to anywhere
plot [DEBUG]: Projected values in meters: -5 5 -5 5
plot [DEBUG]: Computed automatic parameters using dimension scaling: 0.9
plot [INFORMATION]: Map scale is 0.001 km per cm or 1:100.
plot [DEBUG]: Running in PS mode modern
plot [DEBUG]: Use PS filename C:/Users/Admin/.gmt/sessions/gmt_session.20196/gmt_1.ps-
plot [DEBUG]: Append to hidden PS file C:/Users/Admin/.gmt/sessions/gmt_session.20196/gmt_1.ps-
plot [DEBUG]: Got session name as pygmt-session and default graphics formats as pdf
plot [DEBUG]: Basemap order: Frame = above Grid = below Tick/Annot = below
plot [DEBUG]: gmtapi_init_import: Passed family = Data Table and geometry = Line
plot [DEBUG]: gmtapi_init_import: Added 1 new sources
plot [DEBUG]: GMT_Init_IO: Returned first Input object ID = 0
plot [DEBUG]: gmtapi_begin_io: Input resource access is now enabled [container]
plot [DEBUG]: gmtapi_import_dataset: Passed ID = -1 and mode = 0
plot [INFORMATION]: Referencing data table from user 4 column arrays of length 4
plot [DEBUG]: Object ID 1 : Registered Data Table Memory Reference 1e0f157bfc0 as an Input resource with geometry Point [n_objects = 2]
plot [DEBUG]: gmtapi_import_dataset processed 1 resources
plot [DEBUG]: GMT_End_IO: Input resource access is now disabled
plot [INFORMATION]: Plotting segment 0
plot [DEBUG]: GMT memory: Initialize 2 temporary column double arrays, each of length : 0
Full error message
Windows fatal exception: code 0xc0000374
Main thread:
Current thread 0x00003654 (most recent call first):
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\clib\session.py", line 624 in call_module
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\src\plot.py", line 267 in plot
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 738 in new_module
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 598 in new_module
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 818 in new_module
File "c:\users\admin\c2\eigenedokumente\studium\promotion\e_gmt\00_testing\001_gmt_pygmt\pr_tracksampling\bug_mwe_red.py", line 35 in <module>
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file
File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile
File "C:\Users\Admin\AppData\Local\Temp\ipykernel_1076\1879108342.py", line 1 in <module>
Restarting kernel...System information
PyGMT information:
version: v0.9.1.dev125
System information:
python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 17:59:51) [MSC v.1935 64 bit (AMD64)]
executable: C:\ProgramData\Anaconda3\envs\pygmt_env_dev\python.exe
machine: Windows-10-10.0.19045-SP0
Dependency information:
numpy: 1.24.3
pandas: 2.0.2
xarray: 2023.1.1.dev17
netCDF4: 1.6.2
packaging: 23.1
contextily: 1.3.0
geopandas: 0.13.2
IPython: 8.14.0
rioxarray: 0.14.1
ghostscript: 9.54.0
GMT library information:
binary version: 6.4.0
cores: 4
grid layout: rows
image layout:
library path: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt.dll
padding: 2
plugin dir: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt_plugins
share dir: C:/Program Files (x86)/gmt6/share
version: 6.4.0Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
seisman commentedon Nov 23, 2023
I can reproduce the bug on Linux.
If
pd.DataFramecontain integer-only columns, the debug messages are:If
pd.DataFramecontain float-type columns, the debug messages are:Here is the diff:
So, for the integer-type case, data is duplicated, but for the float-type case, data is used by reference.
@PaulWessel Need your help.
PaulWessel commentedon Nov 23, 2023
How is the DataFrame passed to GMT? Via matrix? Also, this looks like a bad sign
gmtapi_import_dataset: Passed ID = -1 and mode = 0since ID = -1 means "not set", so that can't be good.
seisman commentedon Nov 23, 2023
It's passed via
GMT_Put_Vectors.seisman commentedon Nov 23, 2023
Here are the values used in
GMT_Open_VirtualfilePaulWessel commentedon Nov 23, 2023
Not clear. Might you share a minimal example that (1) loads the data frame, (2) passes it to some simple module like gmtconvert (assuming that also crashes)? Think I need to debug.
seisman commentedon Nov 24, 2023
Tried to pass the same dataset to
gmtconvert, but it doesn't crash.The verbose messages are:
ID = -1so it's not the real problem.seisman commentedon Nov 24, 2023
For the example in #2637 (comment), if I add
style="c0.2c"(i.e.,-Sc0.2c), the script works. So, it's likely it only crashes when plotting lines.PaulWessel commentedon Nov 24, 2023
If I try this:
and run
python bug.pyI get no errors and this plot
What am I missing?
seisman commentedon Nov 24, 2023
@yvonnefroehlich said "For me, this issue occurs under Windows but not under Linux.". Now I can reproduce the issue under Linux, but you can't reproduce it under macOS.
Need to find out why the behavior is system-dependent.
PaulWessel commentedon Nov 24, 2023
Need a Linux or Win (@joa-quim ) person to run in debug and determine WTF is going on. I cannot.
joa-quim commentedon Nov 24, 2023
I could try to start my python learning through a debug session but for that I would need that PyGMT was able to find my
gmt.dll(which, ofc, has to be a debug build) as I don't want to mess with Conda and environments stuff.seisman commentedon Nov 24, 2023
Just set the
GMT_LIBRARY_PATHenvironment variable to the path to thegmt.dll(something likeC:\Users\USERNAME\Mambaforge\envs\pygmt\Library\bin\).joa-quim commentedon Nov 24, 2023
OK, but when I said
gmt.dllI was being generic. The true name isgmt_w64.dlland if only the path is set viaGMT_LIBRARY_PATHthen the right dll wont be found.seisman commentedon Nov 24, 2023
PyGMT will try to find gmt.dll, gmt_w32.dll and gmt_w64.dll
joa-quim commentedon Nov 24, 2023
Good, thanks.
seisman commentedon Dec 3, 2023
Tried to debug this issue. It seems
plotcrashes when trying to free theGMT_DATASETobjecthttps://github.com/GenericMappingTools/gmt/blob/7825ff4632c85ef6569acf19192068b977127e07/src/psxy.c#L3008
Actually it crashes in the
gmt_free_segmentfunction (https://github.com/GenericMappingTools/gmt/blob/7825ff4632c85ef6569acf19192068b977127e07/src/gmt_io.c#L8875):SH->alloc_mode[col]areGMT_ALLOC_EXTERNALLYfor all the four columns, sosegment->data[col]are not freed, but it crashes when freeingsegment->data.seisman commentedon Dec 4, 2023
Ping @PaulWessel Does the above debugging help?
PaulWessel commentedon Dec 4, 2023
Yes, I got the same. Will debug again to see exactly that data is internally allocated. Not sure why that would crash, but it does.
PaulWessel commentedon Dec 4, 2023
Since I cannot reproduce it (works for macOS) and I cannot see why this would depend on the OS I cannot really help. Someone would need to debug in Linux but not sure what to look fore. segment->data is allocated in GMT so fair game to free as long as we dont free the read-only vectors.