Repository navigation
Description
Description of the problem
Using the incols parameter does not allow to swap columns (seems like the parameter is ignored at all so far). I believe it's an upstream GMT bug. This was discovered in #1303 (comment).
Full code that generated the error
import pygmt
import numpy as np
# generate sample data
region = [10, 70, -5, 10]
x, y = np.meshgrid(
np.linspace(region[0], region[1]), np.linspace(region[2], region[3])
)
x = x.flatten()
y = y.flatten()
z = (x - 0.5 * (region[0] + region[1])) ** 2 + 4 * y ** 2
z = np.exp(-z / 10 ** 2 * np.log(2))
# generate dataframe
data = np.array([x, y, z]).T
# correct figure
fig1 = pygmt.Figure()
fig1.contour(data=data,
projection="X10c",
frame="a",
pen=True)
fig1.show()
# generate second dataframe, switch x and y from here onwards to simulate
# different column order
data = np.array([y, x, z]).T
# wrong figure
fig2 = pygmt.Figure()
fig2.contour(data=data,
incols=[1, 0, 2], # use incols to assign column order
projection="X10c",
frame="a",
pen=True)
fig2.show()Full output
| fig1: expected | fig2: actual (wrong) |
|---|---|
![]() |
![]() |
System information
Please paste the output of python -c "import pygmt; pygmt.show_versions()":
PyGMT information:
version: v0.2.2.dev80+g132757a
System information:
python: 3.6.12 | packaged by conda-forge | (default, Dec 9 2020, 00:36:02) [GCC 9.3.0]
executable: /home/mgrund/anaconda3/envs/pygmt/bin/python
machine: Linux-4.4.0-19041-Microsoft-x86_64-with-debian-buster-sid
Dependency information:
numpy: 1.19.4
pandas: 1.1.5
xarray: 0.16.2
netCDF4: 1.5.6
packaging: 20.8
ghostscript: 9.53.3
gmt: 6.2.0rc1
GMT library information:
binary dir: /home/mgrund/anaconda3/envs/pygmt/bin
cores: 8
grid layout: rows
library path: /home/mgrund/anaconda3/envs/pygmt/lib/libgmt.so
padding: 2
plugin dir: /home/mgrund/anaconda3/envs/pygmt/lib/gmt/plugins
share dir: /home/mgrund/anaconda3/envs/pygmt/share/gmt
version: 6.2.0rc1
@PaulWessel @meghanrjones could you please check if you can debug it?


Activity
PaulWessel commentedon Jun 1, 2021
Do not think this is upstream bug. Similar test with switching x,y then passing -i works as expected.
seisman commentedon Jun 1, 2021
Yes, I think this is an upstream bug, but it only affects wrappers, that's why the GMT tests pass.
Here is a modified example based on @michaelgrund's example:
As you can see, passing "YXZ" data and using
incols=[1, 0, 2]works well for file input, but not for an array input.PaulWessel commentedon Jun 1, 2021
Debugging the
wrong figurecase now. We call gmt info to determine the region. I see that the list of options passed to contour includes the memoryfile, the projection, the -Ba, the -W and then there is a -incols0 string which of course is not parsed properly. Presumably it is pygmt that generates this string of options?maxrjones commentedon Jun 1, 2021
I think to use
incolsPaul would need to build from this branch (#1303) and that the master branch usescolumnsas the alias name. Rather than checkout out that branch and building, you could just changeincols=tocolumns=ori=.seisman commentedon Jun 1, 2021
Yes, forgot to mention that #1303 is needed. Sorry about that.
PaulWessel commentedon Jun 1, 2021
So I am debugging the C code. I cannot change anything in these strings in the option structure I think. Is that what you are suggesting?
maxrjones commentedon Jun 1, 2021
Just suggesting to use this for the debugging if you build from master branch to avoid the pygmt alias parsing:
PaulWessel commentedon Jun 1, 2021
So I think I found the problem, but concerned that this would certainly affect any matrix being passed in as a dataset with -i options. This is the function that determines which column we should read given input values col = 0, 1, 2, 3 ...:
I have commented out the offending line and replaced it with the one using the member order, like in gmtio_ascii_input for files. I will make a PR for this but please run all tests you have that uses a matrix for dataset input.
michaelgrund commentedon Jun 1, 2021
Thanks for adding this @seisman, only tested it with an array as input.
seisman commentedon Jun 2, 2021
The upstream bug was fixed in GenericMappingTools/gmt#5289 and will be available in GMT 6.2.0.
michaelgrund commentedon Jun 2, 2021
Great! Thanks for your efforts @PaulWessel @meghanrjones @seisman.
maxrjones commentedon Jun 2, 2021
There is still an issue with scalings not being applied on vector inputs. Here is a minimal example (will need to update the file path from
Users/user/to get it to work):9 remaining items
maxrjones commentedon Aug 17, 2021
A second fix was implemented in GenericMappingTools/gmt#5392.
xref #1440 (I will need to check if that is a duplicate issue).
[-]contour does not handle incols parameter correctly[/-][+]The "incols" (-i) parameter doesn't works for vector input correctly[/+]seisman commentedon Aug 29, 2021
The example script in #1313 (comment) works well with the GMT master branch, but #1440 fails. So I think they are not duplicate issues.
As this issue has been fixed, I think we can close it, but we need to add a test before closing the issue. Agree?
maxrjones commentedon Aug 30, 2021
Thanks for checking. Yes, I agree that we can close this issue after adding a test.
seisman commentedon Aug 31, 2021
I'm trying to add a test for this issue, but I found there are still bugs with -i option. With GMT 6.2.0, -i1,0 works for PyGMT, but modifiers are ignored. For example,
-i1+s10.0,0doesn't work:maxrjones commentedon Sep 3, 2021
I suspect the problem is that the fix applied in GenericMappingTools/gmt#5291 wasn't extended to table-reading. @PaulWessel, do you remember if you have any pending work for the
GMT_IS_MATRIXcase? If not, I could look into this more.seisman commentedon Sep 3, 2021
I quickly looked at the source code. I think the
info()function always usesGMT_IS_VECTORnotGMT_IS_MATRIX. So it's still a bug for vectors.Actually,
GMT_IS_MATRIXis rarely used in PyGMT. I searched for thevirtualfile_from_matrixfunction in the source code, and only found thecontour,mecaandsurfacefunctions are using it.If I understand it correctly, using the
GMT_IS_MATIRXkeyword or calling theGMT_Put_Matrixfunction means that the data passed to GMT must be a 2D matrix and all the elements in the 2D matrix must have the same data type (for example, a float 2D matrix). However, forcontour,meca,surfaceor any other functions, data with mixed types should be allowed (for example, an integer x array and a float y array).maxrjones commentedon Sep 7, 2021
I stepped through the code and confirmed that
virtualfile_from_matrixis used for your example failure due to the following lines of code:pygmt/pygmt/clib/session.py
Lines 1457 to 1463 in d01da5a
maxrjones commentedon Nov 5, 2021
I think GenericMappingTools/gmt#5799 fixed the last of the upstream problems reported in this issue. Do we still need more tests for this before closing?
seisman commentedon Nov 6, 2021
I think we still need a test like this one #1313 (comment), because we don't have any tests that use
-iwith modifiers.Figure.contour: Remove parameter "columns", use "incols" instead