Skip to content

Commit e693c93

Browse files
committed
API changes: Added Deprecation warnings for all functions when plot_args is passed as arguments. Backtracked on plot_spatial_dataset, which now fallbacks to legacy call + deprecwarning.
1 parent f5c254e commit e693c93

File tree

2 files changed

+254
-25
lines changed

2 files changed

+254
-25
lines changed

csep/utils/plots.py

+133-14
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,17 @@ def plot_magnitude_versus_time(
145145
matplotlib.axes.Axes: The Matplotlib axes object with the plotted data.
146146
"""
147147
# Initialize plot
148-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
148+
if "plot_args" in kwargs:
149+
warnings.warn(
150+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
151+
"Fine-tuning of plot appearance may not behave as expected'.\n"
152+
"Please use explicit arguments instead (e.g., color='red').\n"
153+
"Refer to the function's documentation for supported keyword arguments:\n"
154+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_magnitude_versus_time.html",
155+
DeprecationWarning,
156+
stacklevel=2
157+
)
158+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
149159
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
150160

151161
# Plot data
@@ -247,7 +257,17 @@ def _plot_cumulative_events_versus_time(
247257
matplotlib.axes.Axes: The Matplotlib axes object with the plotted data.
248258
"""
249259
# Initialize plot
250-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
260+
if "plot_args" in kwargs:
261+
warnings.warn(
262+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
263+
"Fine-tuning of plot appearance may not behave as expected'.\n"
264+
"Please use explicit arguments instead (e.g., color='red').\n"
265+
"Refer to the function's documentation for supported keyword arguments:\n"
266+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_cumulative_events_versus_time.html",
267+
DeprecationWarning,
268+
stacklevel=2
269+
)
270+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
251271
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
252272

253273
# Get information from stochastic event set
@@ -419,7 +439,17 @@ def plot_magnitude_histogram(
419439
"""
420440

421441
# Initialize plot
422-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
442+
if "plot_args" in kwargs:
443+
warnings.warn(
444+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
445+
"Fine-tuning of plot appearance may not behave as expected'.\n"
446+
"Please use explicit arguments instead (e.g., color='red').\n"
447+
"Refer to the function's documentation for supported keyword arguments:\n"
448+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_magnitude_histogram.html",
449+
DeprecationWarning,
450+
stacklevel=2
451+
)
452+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
423453
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
424454

425455
# Get magnitudes from observations and (lazily) from forecast
@@ -691,7 +721,17 @@ def plot_catalog(
691721
matplotlib.axes.Axes: The Matplotlib axes object with the plotted data.
692722
"""
693723

694-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
724+
if "plot_args" in kwargs:
725+
warnings.warn(
726+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
727+
"Fine-tuning of plot appearance may not behave as expected'.\n"
728+
"Please use explicit arguments instead (e.g., color='red').\n"
729+
"Refer to the function's documentation for supported keyword arguments:\n"
730+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_catalog.html",
731+
DeprecationWarning,
732+
stacklevel=2
733+
)
734+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
695735
# Get spatial information for plotting
696736
extent = extent or _calculate_spatial_extent(catalog, set_global, plot_region)
697737
# Instantiate GeoAxes object
@@ -844,8 +884,17 @@ def plot_gridded_dataset(
844884
matplotlib.axes.Axes: Matplotlib axes object.
845885
"""
846886

847-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
848-
887+
if "plot_args" in kwargs:
888+
warnings.warn(
889+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
890+
"Fine-tuning of plot appearance may not behave as expected'.\n"
891+
"Please use explicit arguments instead (e.g., color='red').\n"
892+
"Refer to the function's documentation for supported keyword arguments:\n"
893+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_gridded_dataset.html",
894+
DeprecationWarning,
895+
stacklevel=2
896+
)
897+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
849898
# Get spatial information for plotting
850899
extent = extent or _calculate_spatial_extent(region, set_global, plot_region)
851900
# Instantiate GeoAxes object
@@ -955,8 +1004,18 @@ def plot_test_distribution(
9551004
matplotlib.axes.Axes: Matplotlib axes handle.
9561005
"""
9571006

958-
# Initialize plot
959-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1007+
# Initialize plot]
1008+
if "plot_args" in kwargs:
1009+
warnings.warn(
1010+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1011+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1012+
"Please use explicit arguments instead (e.g., color='red').\n"
1013+
"Refer to the function's documentation for supported keyword arguments:\n"
1014+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_test_distribution.html",
1015+
DeprecationWarning,
1016+
stacklevel=2
1017+
)
1018+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
9601019
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
9611020

9621021
# Get distributions
@@ -1075,7 +1134,17 @@ def plot_calibration_test(
10751134
matplotlib.axes.Axes: The Matplotlib axes object containing the plot.
10761135
"""
10771136
# Initialize plot
1078-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1137+
if "plot_args" in kwargs:
1138+
warnings.warn(
1139+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1140+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1141+
"Please use explicit arguments instead (e.g., color='red').\n"
1142+
"Refer to the function's documentation for supported keyword arguments:\n"
1143+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_calibration_test.html",
1144+
DeprecationWarning,
1145+
stacklevel=2
1146+
)
1147+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
10791148
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
10801149

10811150
# Set up QQ plots and KS test
@@ -1179,7 +1248,17 @@ def _plot_comparison_test(
11791248
"""
11801249

11811250
# Initialize plot
1182-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1251+
if "plot_args" in kwargs:
1252+
warnings.warn(
1253+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1254+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1255+
"Please use explicit arguments instead (e.g., color='red').\n"
1256+
"Refer to the function's documentation for supported keyword arguments:\n"
1257+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_comparison_test.html",
1258+
DeprecationWarning,
1259+
stacklevel=2
1260+
)
1261+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
11831262
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
11841263

11851264
# Iterate through T-test results, or jointly through t- and w- test results
@@ -1347,7 +1426,17 @@ def _plot_consistency_test(
13471426
"""
13481427

13491428
# Initialize plot
1350-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1429+
if "plot_args" in kwargs:
1430+
warnings.warn(
1431+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1432+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1433+
"Please use explicit arguments instead (e.g., color='red').\n"
1434+
"Refer to the function's documentation for supported keyword arguments:\n"
1435+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_consistency_test.html",
1436+
DeprecationWarning,
1437+
stacklevel=2
1438+
)
1439+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
13511440
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
13521441

13531442
# Ensure eval_results is a list
@@ -1478,7 +1567,17 @@ def _plot_concentration_ROC_diagram(
14781567
"""
14791568

14801569
# Initialize plot
1481-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1570+
if "plot_args" in kwargs:
1571+
warnings.warn(
1572+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1573+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1574+
"Please use explicit arguments instead (e.g., color='red').\n"
1575+
"Refer to the function's documentation for supported keyword arguments:\n"
1576+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_concentration_ROC_diagram.html",
1577+
DeprecationWarning,
1578+
stacklevel=2
1579+
)
1580+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
14821581
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
14831582

14841583
if not catalog.region == forecast.region:
@@ -1591,7 +1690,17 @@ def _plot_ROC_diagram(
15911690
"""
15921691

15931692
# Initialize plot
1594-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1693+
if "plot_args" in kwargs:
1694+
warnings.warn(
1695+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1696+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1697+
"Please use explicit arguments instead (e.g., color='red').\n"
1698+
"Refer to the function's documentation for supported keyword arguments:\n"
1699+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_ROC_diagram.html",
1700+
DeprecationWarning,
1701+
stacklevel=2
1702+
)
1703+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
15951704
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
15961705

15971706
if not catalog.region == forecast.region:
@@ -1738,7 +1847,17 @@ def _plot_Molchan_diagram(
17381847
RuntimeError: If the catalog and forecast do not have the same region.
17391848
"""
17401849

1741-
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs}
1850+
if "plot_args" in kwargs:
1851+
warnings.warn(
1852+
"'plot_args' usage is deprecated and will be removed in version 1.0.\n"
1853+
"Fine-tuning of plot appearance may not behave as expected'.\n"
1854+
"Please use explicit arguments instead (e.g., color='red').\n"
1855+
"Refer to the function's documentation for supported keyword arguments:\n"
1856+
" https://docs.cseptesting.org/reference/generated/csep.utils.plots.plot_Molchan_diagram.html",
1857+
DeprecationWarning,
1858+
stacklevel=2
1859+
)
1860+
plot_args = {**DEFAULT_PLOT_ARGS, **kwargs.get("plot_args", {}), **kwargs}
17421861
fig, ax = pyplot.subplots(figsize=plot_args["figsize"]) if ax is None else (ax.figure, ax)
17431862

17441863
if not catalog.region == forecast.region:

csep/utils/plots_legacy.py

+121-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import matplotlib.lines
1111
from matplotlib.collections import PatchCollection
1212
import matplotlib.pyplot as pyplot
13+
import cartopy.crs as ccrs
1314
from cartopy.io import img_tiles
15+
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
1416

1517
# PyCSEP imports
1618
from csep.utils.calc import bin1d_vec
@@ -566,7 +568,6 @@ def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None,
566568
"""
567569
Legacy-compatible wrapper for plot_gridded_dataset.
568570
"""
569-
from .plots import plot_gridded_dataset
570571

571572
plot_args = plot_args or {}
572573
warnings.warn(
@@ -577,16 +578,125 @@ def plot_spatial_dataset(gridded, region, ax=None, show=False, extent=None,
577578
stacklevel=2
578579
)
579580

580-
return plot_gridded_dataset(
581-
gridded=gridded,
582-
region=region,
583-
ax=ax,
584-
show=show,
585-
extent=extent,
586-
set_global=set_global,
587-
**plot_args,
588-
**kwargs
589-
)
581+
# Get spatial information for plotting
582+
bbox = region.get_bbox()
583+
if extent is None and not set_global:
584+
extent = [bbox[0], bbox[1], bbox[2], bbox[3]]
585+
586+
# Retrieve plot arguments
587+
plot_args = plot_args or {}
588+
# figure and axes properties
589+
figsize = plot_args.get('figsize', None)
590+
title = plot_args.get('title', None)
591+
title_size = plot_args.get('title_size', None)
592+
filename = plot_args.get('filename', None)
593+
# cartopy properties
594+
projection = plot_args.get('projection',
595+
ccrs.PlateCarree(central_longitude=0.0))
596+
grid = plot_args.get('grid', True)
597+
grid_labels = plot_args.get('grid_labels', False)
598+
grid_fontsize = plot_args.get('grid_fontsize', False)
599+
basemap = plot_args.get('basemap', None)
600+
coastline = plot_args.get('coastline', True)
601+
borders = plot_args.get('borders', False)
602+
tile_scaling = plot_args.get('tile_scaling', 'auto')
603+
linewidth = plot_args.get('linewidth', True)
604+
linecolor = plot_args.get('linecolor', 'black')
605+
region_border = plot_args.get('region_border', True)
606+
# color bar properties
607+
include_cbar = plot_args.get('include_cbar', True)
608+
cmap = plot_args.get('cmap', None)
609+
clim = plot_args.get('clim', None)
610+
clabel = plot_args.get('clabel', None)
611+
clabel_fontsize = plot_args.get('clabel_fontsize', None)
612+
cticks_fontsize = plot_args.get('cticks_fontsize', None)
613+
alpha = plot_args.get('alpha', 1)
614+
alpha_exp = plot_args.get('alpha_exp', 0)
615+
616+
apprx = False
617+
central_latitude = 0.0
618+
if projection == 'fast':
619+
projection = ccrs.PlateCarree()
620+
apprx = True
621+
n_lats = len(region.ys) // 2
622+
central_latitude = region.ys[n_lats]
623+
624+
# Instantiate GeoAxes object
625+
if ax is None:
626+
fig = pyplot.figure(figsize=figsize)
627+
ax = fig.add_subplot(111, projection=projection)
628+
else:
629+
fig = ax.get_figure()
630+
631+
if set_global:
632+
ax.set_global()
633+
region_border = False
634+
else:
635+
ax.set_extent(extents=extent,
636+
crs=ccrs.PlateCarree()) # Defined extent always in lat/lon
637+
638+
# Basemap plotting
639+
ax = plot_basemap(basemap, extent, ax=ax, coastline=coastline,
640+
borders=borders,
641+
linecolor=linecolor, linewidth=linewidth,
642+
projection=projection, apprx=apprx,
643+
central_latitude=central_latitude,
644+
tile_scaling=tile_scaling, set_global=set_global)
645+
646+
## Define colormap and transparency function
647+
if isinstance(cmap, str) or not cmap:
648+
cmap = pyplot.get_cmap(cmap)
649+
cmap_tup = cmap(numpy.arange(cmap.N))
650+
if isinstance(alpha_exp, (float, int)):
651+
if alpha_exp != 0:
652+
cmap_tup[:, -1] = numpy.linspace(0, 1, cmap.N) ** alpha_exp
653+
alpha = None
654+
cmap = matplotlib.colors.ListedColormap(cmap_tup)
655+
656+
## Plot spatial dataset
657+
lons, lats = numpy.meshgrid(numpy.append(region.xs, bbox[1]),
658+
numpy.append(region.ys, bbox[3]))
659+
im = ax.pcolor(lons, lats, gridded, cmap=cmap, alpha=alpha, snap=True,
660+
transform=ccrs.PlateCarree())
661+
im.set_clim(clim)
662+
663+
664+
# Colorbar options
665+
# create an axes on the right side of ax. The width of cax will be 5%
666+
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
667+
if include_cbar:
668+
cax = fig.add_axes(
669+
[ax.get_position().x1 + 0.01, ax.get_position().y0, 0.025,
670+
ax.get_position().height],
671+
label='Colorbar')
672+
cbar = fig.colorbar(im, ax=ax, cax=cax)
673+
cbar.set_label(clabel, fontsize=clabel_fontsize)
674+
cbar.ax.tick_params(labelsize=cticks_fontsize)
675+
676+
# Gridline options
677+
if grid:
678+
gl = ax.gridlines(draw_labels=grid_labels, alpha=0.5)
679+
gl.right_labels = False
680+
gl.top_labels = False
681+
gl.xlabel_style['fontsize'] = grid_fontsize
682+
gl.ylabel_style['fontsize'] = grid_fontsize
683+
gl.xformatter = LONGITUDE_FORMATTER
684+
gl.yformatter = LATITUDE_FORMATTER
685+
686+
if region_border:
687+
pts = region.tight_bbox()
688+
ax.plot(pts[:, 0], pts[:, 1], lw=1, color='black',
689+
transform=ccrs.PlateCarree())
690+
691+
# matplotlib figure options
692+
ax.set_title(title, y=1.06, fontsize=title_size)
693+
if filename is not None:
694+
ax.get_figure().savefig(filename + '.pdf')
695+
ax.get_figure().savefig(filename + '.png', dpi=300)
696+
if show:
697+
pyplot.show()
698+
699+
return ax
590700

591701

592702
def plot_number_test(evaluation_result, axes=None, show=True, plot_args=None):

0 commit comments

Comments
 (0)