Skip to content

Commit

Permalink
refac: initial refactoring of the plotting module.
Browse files Browse the repository at this point in the history
 - Harmonized the parameters for all plotting functions. Removed the `plot_args` argument from all functions. Now, default parameters, such as `title`, `figsize` and many other repeating parameters are now found in a global dictionary `DEFAULT_PLOT_ARGS`. All plotting functions now default to these parameters and update a copy of it with **kwargs, thus having control to customize plots. Relevant parameters to the function are left in the function signature.
 - Added type hints for all signatures.
 - Removed functions `plot_cumulative_events_versus_time_dev`, `plot_ecdf`, `plot_magnitude_histogram_dev`.
 - Refactored catalog evaluation plot functions `plot_number_test`, `plot_magnitude_test`, `plot_spatial_test`, `plot_likelihood_test` into a generic `plot_distribution_test` (Should it be renamed plot_consistency_test?). Automatic annotations for each type test are done if required, or when called directly from EvaluationResult subclasses.
 - The function `plot_poisson_consistency_test` was collapsed onto `plot_consistency_test`, which also included the negative binomial model.
 - Reworked `plot_magnitude_vs_time`: removed some old functionality and give the possibility to scale the size of each event by its magnitude. Added datetime formatter/locator
 - Reworked`plot_cumulative_events_versus_time`. Now it has the option to plot with the real datetimes, or hours/days after a given mainshock.
 - Now plot_magnitude_histogram plots in log scale.
 - plot_basemap now allows cacheing of downloaded data
 - Created or refactored from existing functions multiple helper functions to ease unit testing and code readability: `_get_basemap`,`_autosize_scatter`, `_size_map`, `_autoscale_histogram`, `_annotate_distribution_plot`, `_calculate_spatial_extent`, `_create_geo_axes`, `_calculate_marker_size`, `_add_gridlines`, `_define_colormap_and_alpha`, `_add_colorbar`, _process_stat_distribution`. These still needs some review, type hinting and further refactoring if necessary.
 tests: Completed test coverage of almost all plots.py functions. Added test artifacts for catalog based evaluations (catalogs, forecasts, test results).
  • Loading branch information
pabloitu committed Aug 20, 2024
1 parent 5f84ea9 commit 9648e76
Show file tree
Hide file tree
Showing 14 changed files with 41,014 additions and 2,451 deletions.
8 changes: 6 additions & 2 deletions csep/core/catalog_evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def number_test(forecast, observed_catalog, verbose=True):
obs_name=observed_catalog.name)
return result


def spatial_test(forecast, observed_catalog, verbose=True):
""" Performs spatial test for catalog-based forecasts.
Expand Down Expand Up @@ -129,7 +130,7 @@ def spatial_test(forecast, observed_catalog, verbose=True):
delta_1, delta_2 = get_quantiles(test_distribution_spatial_1d, obs_lh_norm)

result = CatalogSpatialTestResult(test_distribution=test_distribution_spatial_1d,
name='S-Test',
name='Catalog S-Test',
observed_statistic=obs_lh_norm,
quantile=(delta_1, delta_2),
status=message,
Expand All @@ -140,6 +141,7 @@ def spatial_test(forecast, observed_catalog, verbose=True):

return result


def magnitude_test(forecast, observed_catalog, verbose=True):
""" Performs magnitude test for catalog-based forecasts """
test_distribution = []
Expand All @@ -152,7 +154,7 @@ def magnitude_test(forecast, observed_catalog, verbose=True):
print("Cannot perform magnitude test when observed event count is zero.")
# prepare result
result = CatalogMagnitudeTestResult(test_distribution=test_distribution,
name='M-Test',
name='Catalog M-Test',
observed_statistic=None,
quantile=(None, None),
status='not-valid',
Expand Down Expand Up @@ -215,6 +217,7 @@ def magnitude_test(forecast, observed_catalog, verbose=True):

return result


def pseudolikelihood_test(forecast, observed_catalog, verbose=True):
""" Performs the spatial pseudolikelihood test for catalog forecasts.
Expand Down Expand Up @@ -310,6 +313,7 @@ def pseudolikelihood_test(forecast, observed_catalog, verbose=True):

return result


def calibration_test(evaluation_results, delta_1=False):
""" Perform the calibration test by computing a Kilmogorov-Smirnov test of the observed quantiles against a uniform
distribution.
Expand Down
12 changes: 6 additions & 6 deletions csep/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def plot(self, show=False, plot_args=None):
'bins': bins}
# looks funny, but will update the defaults with the user defined arguments
plot_args_defaults.update(plot_args)
ax = plots.plot_number_test(self, show=show, plot_args=plot_args)
ax = plots.plot_distribution_test(self, show=show)
return ax


Expand All @@ -160,7 +160,7 @@ def plot(self, show=False, plot_args=None):
'bins': 'auto'}
# looks funny, but will update the defaults with the user defined arguments
plot_args_defaults.update(plot_args)
ax = plots.plot_likelihood_test(self, show=show, plot_args=plot_args)
ax = plots.plot_distribution_test(self, show=show)
return ax


Expand All @@ -175,7 +175,7 @@ def plot(self, show=False, plot_args=None):
'title': 'Magnitude Test',
'bins': 'auto'}
plot_args_defaults.update(plot_args)
ax = plots.plot_magnitude_test(self, show=show, plot_args=plot_args)
ax = plots.plot_distribution_test(self, show=show)
return ax


Expand All @@ -194,7 +194,7 @@ def plot(self, show=False, plot_args=None):
}
# looks funny, but will update the defaults with the user defined arguments
plot_args_defaults.update(plot_args)
ax = plots.plot_spatial_test(self, show=show, plot_args=plot_args)
ax = plots.plot_distribution_test(self, show=show)
return ax


Expand All @@ -211,7 +211,7 @@ def plot(self, show=False, axes=None, plot_args=None):
'title': self.name
}
plot_args_defaults.update(plot_args)
ax = plots.plot_calibration_test(self, show=show, axes=axes, plot_args=plot_args)
ax = plots.plot_calibration_test(self, show=show, ax=axes, plot_args=plot_args)
return ax


Expand Down Expand Up @@ -361,4 +361,4 @@ def from_great_circle_radius(cls, centroid, radius, num_points=10):
# get new lons and lats
endlon, endlat, backaz = geod.fwd(center_lons, center_lats, azim, radius)
# class method
return cls(np.column_stack([endlon, endlat]))
return cls(np.column_stack([endlon, endlat]))
Loading

0 comments on commit 9648e76

Please sign in to comment.