Skip to content

Commit

Permalink
Handle setting special parameters without default settings for config (
Browse files Browse the repository at this point in the history
…#411)

There are 8 parameters in gmt.conf that don't have default settings, meaning they can't be reverted back easily when using `pygmt.config`. This can be addressed for 7 of them in a whitelist (FONT_ANNOT, FORMAT_TIME_MAP, MAP_ANNOT_OFFSET, MAP_GRID_CROSS_SIZE, MAP_GRID_PEN, MAP_TICK_LENGTH, MAP_TICK_PEN) by manually setting the relevant `*_PRIMARY` and `*_SECONDARY` parameters. Tests added to check that those special parameters are indeed set and reverted back properly when using `pygmt.config` in a `with` statement.

For the `FONT` config setting, we test to check that `FONT_ANNOT_PRIMARY`, `FONT_ANNOT_SECONDARY`, `FONT_LABEL`, and `FONT_TITLE` are modified when the `FONT` config is set. TODO check `FONT_HEADING` and `FONT_TAG` once `subplot` wrapper is done.
  • Loading branch information
weiji14 authored May 12, 2020
1 parent 74501e0 commit 2c2566d
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pygmt/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,36 @@ class config: # pylint: disable=invalid-name
def __init__(self, **kwargs):
# Save values so that we can revert to their initial values
self.old_defaults = {}
self.special_params = {
"FONT": [
"FONT_ANNOT_PRIMARY",
"FONT_ANNOT_SECONDARY",
"FONT_HEADING",
"FONT_LABEL",
"FONT_TAG",
"FONT_TITLE",
],
"FONT_ANNOT": ["FONT_ANNOT_PRIMARY", "FONT_ANNOT_SECONDARY"],
"FORMAT_TIME_MAP": ["FORMAT_TIME_PRIMARY_MAP", "FORMAT_TIME_SECONDARY_MAP"],
"MAP_ANNOT_OFFSET": [
"MAP_ANNOT_OFFSET_PRIMARY",
"MAP_ANNOT_OFFSET_SECONDARY",
],
"MAP_GRID_CROSS_SIZE": [
"MAP_GRID_CROSS_SIZE_PRIMARY",
"MAP_GRID_CROSS_SIZE_SECONDARY",
],
"MAP_GRID_PEN": ["MAP_GRID_PEN_PRIMARY", "MAP_GRID_PEN_SECONDARY"],
"MAP_TICK_LENGTH": ["MAP_TICK_LENGTH_PRIMARY", "MAP_TICK_LENGTH_SECONDARY"],
"MAP_TICK_PEN": ["MAP_TICK_PEN_PRIMARY", "MAP_TICK_PEN_SECONDARY"],
}
with Session() as lib:
for key in kwargs:
self.old_defaults[key] = lib.get_default(key)
if key in self.special_params:
for k in self.special_params[key]:
self.old_defaults[k] = lib.get_default(k)
else:
self.old_defaults[key] = lib.get_default(key)

# call gmt set to change GMT defaults
arg_str = " ".join(
Expand Down
Binary file added pygmt/tests/baseline/test_config_font_annot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pygmt/tests/baseline/test_config_font_one.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pygmt/tests/baseline/test_config_map_grid_pen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pygmt/tests/baseline/test_config_map_tick_pen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions pygmt/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,135 @@ def test_config():
# Revert to default settings
config(FONT_ANNOT_PRIMARY="black")
return fig


@pytest.mark.mpl_image_compare
def test_config_font_one():
"""
Test that setting `FONT` config changes all `FONT_*` settings except
`FONT_LOGO`. Specifically, this test only checks that `FONT_ANNOT_PRIMARY`,
`FONT_ANNOT_SECONDARY`, `FONT_LABEL`, and `FONT_TITLE` are modified.
"""
fig = Figure()
with config(FONT="8p,red"):
fig.basemap(region=[0, 9, 0, 9], projection="C3/3/9c", T="mjTL+w4c+d4.5+l")
fig.basemap(T="mjBR+w5c+d-4.5+l")
return fig


@pytest.mark.mpl_image_compare
def test_config_font_annot():
"""
Test that setting `FONT_ANNOT` config changes both `FONT_ANNOT_PRIMARY` and
`FONT_ANNOT_SECONDARY`.
"""
fig = Figure()
with config(FONT_ANNOT="6p,red"):
fig.basemap(region=[0, 9, 0, 9], projection="C3/3/9c", T="mjTL+w4c+d4.5")
fig.basemap(T="mjBR+w5c+d-4.5")
return fig


@pytest.mark.mpl_image_compare
def test_config_format_time_map():
"""
Test that setting `FORMAT_TIME_MAP` config changes both
`FORMAT_TIME_PRIMARY_MAP` and `FORMAT_TIME_SECONDARY_MAP`.
"""
fig = Figure()
with config(FORMAT_TIME_MAP="abbreviation"):
fig.basemap(
region=["2020-1-24T", "2020-1-27T", 0, 1],
projection="X6c/1c",
frame=["pa1K", "sa1K", "NWse"],
)
fig.basemap(frame=["pa1K", "sa1K", "nwSE"])
return fig


@pytest.mark.mpl_image_compare
def test_config_map_annot_offset():
"""
Test that setting `MAP_ANNOT_OFFSET` config changes both
`MAP_ANNOT_OFFSET_PRIMARY` and `MAP_ANNOT_OFFSET_SECONDARY`.
"""
fig = Figure()
with config(MAP_ANNOT_OFFSET="15p"):
fig.basemap(
region=["2020-1-24T", "2020-1-27T", 0, 1],
projection="X6c/1c",
frame=["pa1d", "sa1d", "NWse"],
)
fig.basemap(frame=["pa1d", "sa1d", "nwSE"])
return fig


@pytest.mark.mpl_image_compare
def test_config_map_grid_cross_size():
"""
Test that setting `MAP_GRID_CROSS_SIZE` config changes both
`MAP_GRID_CROSS_SIZE_PRIMARY` and `MAP_GRID_CROSS_SIZE_SECONDARY`.
"""
fig = Figure()
config(
MAP_GRID_CROSS_SIZE_PRIMARY="0p", MAP_GRID_CROSS_SIZE_SECONDARY="0p"
) # Remove after https://github.com/GenericMappingTools/gmt/issues/3062 is fixed
with config(MAP_GRID_CROSS_SIZE="3p"):
fig.basemap(
region=["2020-1-24T21:00", "2020-1-25T00:00", 0, 1],
projection="X6c/2c",
frame=["pa1Hg", "sa45mg45m", "NWse"],
)
fig.basemap(frame=["pa1Hg", "sa45mg45m", "nwSE"], Y=-3)
return fig


@pytest.mark.mpl_image_compare
def test_config_map_grid_pen():
"""
Test that setting `MAP_GRID_PEN` config changes both
`MAP_GRID_PEN_PRIMARY` and `MAP_GRID_PEN_SECONDARY`.
"""
fig = Figure()
with config(MAP_GRID_PEN="thick,red"):
fig.basemap(
region=["2020-1-24T21:00", "2020-1-25T00:00", 0, 1],
projection="X6c/2c",
frame=["pa1Hg", "sa45mg45m", "NWse"],
)
fig.basemap(frame=["pa1Hg", "sa45mg45m", "nwSE"], Y=-3)
return fig


@pytest.mark.mpl_image_compare
def test_config_map_tick_length():
"""
Test that setting `MAP_TICK_LENGTH` config changes both
`MAP_TICK_LENGTH_PRIMARY` and `MAP_TICK_LENGTH_SECONDARY`.
"""
fig = Figure()
with config(MAP_TICK_LENGTH="5p"):
fig.basemap(
region=["2020-1-24T21:00", "2020-1-25T00:00", 0, 1],
projection="X6c/2c",
frame=["pa1Hg", "sa45mg45m", "NWse"],
)
fig.basemap(frame=["pa1Hg", "sa45mg45m", "nwSE"], Y=-3)
return fig


@pytest.mark.mpl_image_compare
def test_config_map_tick_pen():
"""
Test that setting `MAP_TICK_PEN` config changes both
`MAP_TICK_PEN_PRIMARY` and `MAP_TICK_PEN_SECONDARY`.
"""
fig = Figure()
with config(MAP_TICK_PEN="thick,red"):
fig.basemap(
region=["2020-1-24T21:00", "2020-1-25T00:00", 0, 1],
projection="X6c/2c",
frame=["pa1Hg", "sa45mg45m", "NWse"],
)
fig.basemap(frame=["pa1Hg", "sa45mg45m", "nwSE"], Y=-3)
return fig

0 comments on commit 2c2566d

Please sign in to comment.