.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/working_with_catalog_forecasts.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_working_with_catalog_forecasts.py: .. _tutorial-working-catalog-forecasts: Working with Catalog-based Forecasts ==================================== This example shows some basic interactions with data-based forecasts. We will load in a forecast stored in the CSEP data format, and compute the expected rates on a 0.1° x 0.1° grid covering the state of California. We will plot the expected rates in the spatial cells. Overview: 1. Define forecast properties (time horizon, spatial region, etc). 2. Compute the expected rates in space and magnitude bins 3. Plot expected rates in the spatial cells .. GENERATED FROM PYTHON SOURCE LINES 19-24 Load required libraries ----------------------- Most of the core functionality can be imported from the top-level :mod:`csep` package. Utilities are available from the :mod:`csep.utils` subpackage. .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: Python import numpy import csep from csep.core import regions from csep.utils import datasets .. GENERATED FROM PYTHON SOURCE LINES 31-36 Load data forecast --------------------- PyCSEP contains some basic forecasts that can be used to test of the functionality of the package. This forecast has already been filtered to the California RELM region. .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python forecast = csep.load_catalog_forecast(datasets.ucerf3_ascii_format_landers_fname) .. GENERATED FROM PYTHON SOURCE LINES 40-47 Define spatial and magnitude regions ------------------------------------ Before we can compute the bin-wise rates we need to define a spatial region and a set of magnitude bin edges. The magnitude bin edges # are the lower bound (inclusive) except for the last bin, which is treated as extending to infinity. We can bind these # to the forecast object. This can also be done by passing them as keyword arguments into :func:`csep.load_catalog_forecast`. .. GENERATED FROM PYTHON SOURCE LINES 47-60 .. code-block:: Python # Magnitude bins properties min_mw = 4.95 max_mw = 8.95 dmw = 0.1 # Create space and magnitude regions magnitudes = regions.magnitude_bins(min_mw, max_mw, dmw) region = regions.california_relm_region() # Bind region information to the forecast (this will be used for binning of the catalogs) forecast.region = regions.create_space_magnitude_region(region, magnitudes) .. GENERATED FROM PYTHON SOURCE LINES 61-66 Compute spatial event counts ---------------------------- The :class:`csep.core.forecasts.CatalogForecast` provides a method to compute the expected number of events in spatial cells. This requires a region with magnitude information. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python _ = forecast.get_expected_rates(verbose=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Processed 1 catalogs in 0.001 seconds Processed 2 catalogs in 0.002 seconds Processed 3 catalogs in 0.003 seconds Processed 4 catalogs in 0.004 seconds Processed 5 catalogs in 0.005 seconds Processed 6 catalogs in 0.006 seconds Processed 7 catalogs in 0.007 seconds Processed 8 catalogs in 0.007 seconds Processed 9 catalogs in 0.009 seconds Processed 10 catalogs in 0.010 seconds Processed 20 catalogs in 0.018 seconds Processed 30 catalogs in 0.026 seconds Processed 40 catalogs in 0.035 seconds Processed 50 catalogs in 0.044 seconds Processed 60 catalogs in 0.052 seconds Processed 70 catalogs in 0.061 seconds Processed 80 catalogs in 0.069 seconds Processed 90 catalogs in 0.078 seconds Processed 100 catalogs in 0.086 seconds Processed 200 catalogs in 0.169 seconds Processed 300 catalogs in 0.254 seconds Processed 400 catalogs in 0.340 seconds Processed 500 catalogs in 0.452 seconds Processed 600 catalogs in 0.536 seconds Processed 700 catalogs in 0.621 seconds Processed 800 catalogs in 0.737 seconds Processed 900 catalogs in 0.821 seconds Processed 1000 catalogs in 0.907 seconds Processed 2000 catalogs in 1.891 seconds Processed 3000 catalogs in 2.833 seconds Processed 4000 catalogs in 3.798 seconds Processed 5000 catalogs in 4.742 seconds Processed 6000 catalogs in 5.681 seconds Processed 7000 catalogs in 6.651 seconds Processed 8000 catalogs in 7.574 seconds Processed 9000 catalogs in 8.595 seconds Processed 10000 catalogs in 9.553 seconds .. GENERATED FROM PYTHON SOURCE LINES 71-77 .. _tutorial-working-catalog-forecasts-plot: Plot expected event counts -------------------------- We can plot the expected event counts the same way that we plot a :class:`csep.core.forecasts.GriddedForecast` .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python ax = forecast.expected_rates.plot(clim=[-3.5, 0], show=True) .. image-sg:: /tutorials/images/sphx_glr_working_with_catalog_forecasts_001.png :alt: ucerf3-landers :srcset: /tutorials/images/sphx_glr_working_with_catalog_forecasts_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-83 The images holes in the image are due to under-sampling from the forecast. .. GENERATED FROM PYTHON SOURCE LINES 85-91 Quick sanity check ------------------ The forecasts were filtered to the spatial region so all events should be binned. We loop through each data in the forecast and count the number of events and compare that with the expected rates. The expected rate is an average in each space-magnitude bin, so we have to multiply this value by the number of catalogs in the forecast. .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: Python total_events = 0 for catalog in forecast: total_events += catalog.event_count numpy.testing.assert_allclose(total_events, forecast.expected_rates.sum() * forecast.n_cat) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.425 seconds) .. _sphx_glr_download_tutorials_working_with_catalog_forecasts.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: working_with_catalog_forecasts.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: working_with_catalog_forecasts.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: working_with_catalog_forecasts.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_