You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pygmt.config: pen settings are not reverted correctly when set in a context manager #3252
Currently, pygmt.config is implemented (in PR #293) like a context manager. When entering the context manager, we first get the current values of GMT configurations, store them in a dictionary, and then call gmt set to set the GMT configurations to new values. When exiting the context manager, we then restore the old values of GMT configurations by using gmt set again.
THe current values of GMT configurations are obtained by calling the GMT API function GMT_Get_Default (or the Session.get_default wrapper), but this function doesn't work as we expected.
The following test explains what's happening:
In [1]: frompygmt.clibimportSession# Get the current valueIn [2]: withSession() aslib:
...: print(lib.get_default("MAP_FRAME_PEN"))
...:
black# Set to a new valueIn [3]: withSession() aslib:
...: lib.call_module("set", "MAP_FRAME_PEN=4p,blue")
...:
# Get the current valueIn [4]: withSession() aslib:
...: print(lib.get_default("MAP_FRAME_PEN"))
...:
4p,blue# Restore to the old valueIn [5]: withSession() aslib:
...: lib.call_module("set", "MAP_FRAME_PEN=black")
...:
# Check the current valueIn [6]: withSession() aslib:
...: print(lib.get_default("MAP_FRAME_PEN"))
...:
4p,black
Here is the output from gmt defaults in modern mode. It seems that, all PEN related configurations have the value black, rather than something like default,black. So, I expect all other PEN-related configurations have the same issue.
>>>frompygmt.clibimportSession>>>withSession() aslib:
... lib.call_module("defaults", "")
## GMT 6.5.0 Defaults file## COLOR Parameters#COLOR_BACKGROUND=blackCOLOR_FOREGROUND=whiteCOLOR_CPT=turboCOLOR_NAN=128COLOR_MODEL=noneCOLOR_HSV_MIN_S=1COLOR_HSV_MAX_S=0.1COLOR_HSV_MIN_V=0.3COLOR_HSV_MAX_V=1COLOR_SET=#0072BD,#D95319,#EDB120,#7E2F8E,#77AC30,#4DBEEE,#A2142F## DIR Parameters#DIR_CACHE=/home/seisman/.gmt/cacheDIR_DATA=DIR_DCW=/home/seisman/opt/miniforge/envs/pygmt/share/dcw-gmtDIR_GSHHG=/home/seisman/opt/miniforge/envs/pygmt/share/gshhg-gmt## FONT Parameters#FONT_ANNOT_PRIMARY=auto,Helvetica,blackFONT_ANNOT_SECONDARY=auto,Helvetica,blackFONT_HEADING=auto,Helvetica-Bold,blackFONT_LABEL=auto,Helvetica,blackFONT_LOGO=8p,Helvetica,blackFONT_SUBTITLE=auto,Helvetica-Bold,blackFONT_TAG=auto,Helvetica,blackFONT_TITLE=auto,Helvetica-Bold,black## FORMAT Parameters#FORMAT_CLOCK_IN=hh:mm:ssFORMAT_CLOCK_OUT=hh:mm:ssFORMAT_CLOCK_MAP=hh:mm:ssFORMAT_DATE_IN=yyyy-mm-ddFORMAT_DATE_OUT=yyyy-mm-ddFORMAT_DATE_MAP=yyyy-mm-ddFORMAT_GEO_OUT=DFORMAT_GEO_MAP=ddd:mm:ssFFORMAT_FLOAT_OUT=%.12gFORMAT_FLOAT_MAP=%.12gFORMAT_TIME_PRIMARY_MAP=fullFORMAT_TIME_SECONDARY_MAP=fullFORMAT_TIME_STAMP=%Y%b%d%H:%M:%S## GMT Miscellaneous Parameters#GMT_DATA_SERVER=oceaniaGMT_DATA_SERVER_LIMIT=unlimitedGMT_DATA_UPDATE_INTERVAL=1dGMT_COMPATIBILITY=6GMT_CUSTOM_LIBS=GMT_EXPORT_TYPE=doubleGMT_EXTRAPOLATE_VAL=NaNGMT_FFT=autoGMT_GRAPHICS_DPU=300iGMT_GRAPHICS_FORMAT=pdfGMT_HISTORY=trueGMT_INTERPOLANT=akimaGMT_LANGUAGE=usGMT_MAX_CORES=0GMT_THEME=modernGMT_TRIANGULATE=ShewchukGMT_VERBOSE=warning## I/O Parameters#IO_COL_SEPARATOR=tabIO_FIRST_HEADER=maybeIO_GRIDFILE_FORMAT=nfIO_GRIDFILE_SHORTHAND=falseIO_HEADER=falseIO_HEADER_MARKER=#%!;"',#IO_N_HEADER_RECS=0IO_NAN_RECORDS=passIO_NC4_CHUNK_SIZE=autoIO_NC4_DEFLATION_LEVEL=3IO_LONLAT_TOGGLE=falseIO_SEGMENT_BINARY=2IO_SEGMENT_MARKER=>## MAP Parameters#MAP_ANNOT_MIN_ANGLE=20MAP_ANNOT_MIN_SPACING=autoMAP_ANNOT_OBLIQUE=separate,lon_horizontal,lat_horizontal,tick_extendMAP_ANNOT_OFFSET_PRIMARY=autoMAP_ANNOT_OFFSET_SECONDARY=autoMAP_ANNOT_ORTHO=weMAP_DEFAULT_PEN=default,blackMAP_DEGREE_SYMBOL=degreeMAP_EMBELLISHMENT_MODE=autoMAP_FRAME_AXES=autoMAP_FRAME_PEN=blackMAP_FRAME_PERCENT=100MAP_FRAME_TYPE=fancyMAP_FRAME_WIDTH=autoMAP_GRID_CROSS_SIZE_PRIMARY=0pMAP_GRID_CROSS_SIZE_SECONDARY=0pMAP_GRID_PEN_PRIMARY=blackMAP_GRID_PEN_SECONDARY=blackMAP_HEADING_OFFSET=autoMAP_LABEL_MODE=annotMAP_LABEL_OFFSET=autoMAP_LINE_STEP=0.75pMAP_LOGO=falseMAP_LOGO_POS=BL/-54p/-54pMAP_ORIGIN_X=72pMAP_ORIGIN_Y=72pMAP_POLAR_CAP=autoMAP_SCALE_HEIGHT=5pMAP_SYMBOL_PEN_SCALE=15%MAP_TICK_LENGTH_PRIMARY=autoMAP_TICK_LENGTH_SECONDARY=autoMAP_TICK_PEN_PRIMARY=blackMAP_TICK_PEN_SECONDARY=blackMAP_TITLE_OFFSET=autoMAP_VECTOR_SHAPE=0.5## Projection Parameters#PROJ_AUX_LATITUDE=authalicPROJ_DATUM=PROJ_ELLIPSOID=WGS-84PROJ_GEODESIC=VincentyPROJ_LENGTH_UNIT=cmPROJ_MEAN_RADIUS=authalicPROJ_SCALE_FACTOR=default## PostScript Parameters#PS_CHAR_ENCODING=ISOLatin1+PS_COLOR_MODEL=rgbPS_COMMENTS=falsePS_CONVERT=APS_IMAGE_COMPRESS=deflate,5PS_LINE_CAP=buttPS_LINE_JOIN=miterPS_MITER_LIMIT=35PS_MEDIA=32767x32767PS_PAGE_COLOR=whitePS_PAGE_ORIENTATION=portraitPS_SCALE_X=1PS_SCALE_Y=1PS_TRANSPARENCY=Normal## Calendar/Time Parameters#TIME_EPOCH=1970-01-01T00:00:00TIME_IS_INTERVAL=offTIME_INTERVAL_FRACTION=0.5TIME_LEAP_SECONDS=falseTIME_REPORT=noneTIME_UNIT=sTIME_WEEK_START=MondayTIME_Y2K_OFFSET_YEAR=1950
Calling gmt get directly also doesn't work:
>>> with Session() as lib:
... lib.call_module("get", "MAP_FRAME_PEN")
black
changed the title [-]pygmt.config: Some settings are not reverted correctly even pygmt.config is used as a context manager[/-][+]pygmt.config: pen settings are not reverted correctly when set in a context manager[/+]on May 19, 2024
Here is the output from gmt defaults in modern mode. It seems that, all PEN related configurations have the value black, rather than something like default,black. So, I expect all other PEN-related configurations have the same issue.
Yes, the same issue occurs with MAP_GRID_PEN and MAP_TICK_PEN. Also, reverting the line style does not work.
In GMT CLI, temporarily changing a GMT configuration is done by adding --key=value to the CLI argument. However, in PyGMT, it's implemented by calling gmt set to set the configuration when entering the context manager and calling gmt set again to change the configuration value to its original value. As explained above, this implementation has flaws and I don't think we have a simple workaround for this bug. So, we probably have to implement it like the GMT CLI way:
When entering a context manager, store the key-value pairs in a dictionary
In Sesssion.call_module, convert the dictionary (if not empty) to --key=value format and append them to the argument list
When exiting a context manager, delete the key-value pairs from the dictionary
We can't pass the dictionary as an argument to Session.call_module because when calling Session.call_module, we have no idea if we're in a context manager or not. So we need to have a global variable that keeps track of the current PyGMT state as proposed in #2388.
Edit: Not as easy as I initially thought, since we need to execute different codes depending on whether it's used in a context manager or not.
I reproduced the width and dash-style changes discussed above. Explicitly restoring the saved width first and then the color and style fixes the frame, grid, and tick pen cases on GMT 6.5 through 6.8. This works with the existing gmt set context implementation. I’ll open a PR with the regression tests and the rendering comparisons.
Activity
seisman commentedon May 15, 2024
Currently,
pygmt.configis implemented (in PR #293) like a context manager. When entering the context manager, we first get the current values of GMT configurations, store them in a dictionary, and then callgmt setto set the GMT configurations to new values. When exiting the context manager, we then restore the old values of GMT configurations by usinggmt setagain.THe current values of GMT configurations are obtained by calling the GMT API function
GMT_Get_Default(or theSession.get_defaultwrapper), but this function doesn't work as we expected.The following test explains what's happening:
The main issue is that
Session.get_default("MAP_FRAME_PEN")returnsblack, but we expect a string likethicker,black. The string is returned from thegmt_putpenfunction (https://github.com/GenericMappingTools/gmt/blob/master/src/gmt_support.c#L7555). I think this is the desired behavior of the function, and it's not an upstream bug.seisman commentedon May 19, 2024
Here is the output from
gmt defaultsin modern mode. It seems that, all PEN related configurations have the valueblack, rather than something likedefault,black. So, I expect all other PEN-related configurations have the same issue.Calling
gmt getdirectly also doesn't work:[-]pygmt.config: Some settings are not reverted correctly even pygmt.config is used as a context manager[/-][+]pygmt.config: pen settings are not reverted correctly when set in a context manager[/+]yvonnefroehlich commentedon May 21, 2024
Yes, the same issue occurs with
MAP_GRID_PENandMAP_TICK_PEN. Also, reverting the line style does not work.seisman commentedon Aug 13, 2024
In GMT CLI, temporarily changing a GMT configuration is done by adding
--key=valueto the CLI argument. However, in PyGMT, it's implemented by callinggmt setto set the configuration when entering the context manager and callinggmt setagain to change the configuration value to its original value. As explained above, this implementation has flaws and I don't think we have a simple workaround for this bug. So, we probably have to implement it like the GMT CLI way:Sesssion.call_module, convert the dictionary (if not empty) to--key=valueformat and append them to the argument listWe can't pass the dictionary as an argument to
Session.call_modulebecause when callingSession.call_module, we have no idea if we're in a context manager or not. So we need to have a global variable that keeps track of the current PyGMT state as proposed in #2388.Edit: Not as easy as I initially thought, since we need to execute different codes depending on whether it's used in a context manager or not.
yousizaitianqiong commentedon Nov 21, 2026
I reproduced the width and dash-style changes discussed above. Explicitly restoring the saved width first and then the color and style fixes the frame, grid, and tick pen cases on GMT 6.5 through 6.8. This works with the existing
gmt setcontext implementation. I’ll open a PR with the regression tests and the rendering comparisons.