Skip to content

Commit

Permalink
fixes apply_filter and makes CL and L test use the same region as cat…
Browse files Browse the repository at this point in the history
…alog if not defined (#101)

* fixes #83 and #94

- spatial_magnitude_counts() now throws error if magnitude bins are not defined.
- remove default bins in spatial_magnitude_counts()
- CL test and L test now share region with the catalog if not defined
- load_catalog() function now properly handles the apply_filter case where spatial region is not define
- spatial_magnitude_counts() now throws error if magnitude bins are not defined.
- remove default bins in spatial_magnitude_counts()
- CL test and L test now share region with the catalog if not defined
- load_catalog() function now properly handles the apply_filter case where spatial region is not defined
- adds same check to the query_comcat function
  • Loading branch information
wsavran authored Mar 24, 2021
1 parent 9ff2316 commit 6da7fc1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
16 changes: 13 additions & 3 deletions csep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
write_json
)

from csep.core.exceptions import CSEPCatalogException

from csep.utils import datasets
from csep.utils import readers

Expand Down Expand Up @@ -112,11 +114,13 @@ def load_catalog(filename, type='csep-csv', format='native', loader=None, apply_
format (str): ('native', 'csep') determines whether the catalog should be converted into the csep
formatted catalog or kept as native.
apply_filters (bool): if true, will apply filters and spatial filter to catalog. time-varying magnitude completeness
will still need to be applied.
will still need to be applied. filters kwarg should be included. see catalog
documentation for more details.
Returns (:class:`~csep.core.catalogs.AbstractBaseCatalog`)
"""


if type not in ('ucerf3', 'csep-csv', 'zmap', 'jma-csv', 'ingv_horus', 'ingv_emrcmt', 'ndk') and loader is None:
raise ValueError("type must be one of the following: ('ucerf3', 'csep-csv', 'zmap', 'jma-csv', 'ndk', 'ingv_horus', 'ingv_emrcmt').")

Expand Down Expand Up @@ -172,7 +176,10 @@ def load_catalog(filename, type='csep-csv', format='native', loader=None, apply_
raise ValueError('format must be either "native" or "csep"')

if apply_filters:
return_val = return_val.filter().filter_spatial()
try:
return_val = return_val.filter().filter_spatial()
except CSEPCatalogException:
return_val = return_val.filter()

return return_val

Expand Down Expand Up @@ -210,7 +217,10 @@ def query_comcat(start_time, end_time, min_magnitude=2.50,
print("Fetched ComCat catalog in {} seconds.\n".format(t1 - t0))

if apply_filters:
comcat = comcat.filter().filter_spatial()
try:
comcat = comcat.filter().filter_spatial()
except CSEPCatalogException:
comcat = comcat.filter()

if verbose:
print("Downloaded catalog from ComCat with following parameters")
Expand Down
14 changes: 5 additions & 9 deletions csep/core/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,23 +744,19 @@ def spatial_magnitude_counts(self, mag_bins=None, tol=0.00001, ret_skipped=False
if self.region is None:
raise CSEPCatalogException("Cannot create binned rates without region information.")

if self.region.magnitudes is None and mag_bins is None:
raise CSEPCatalogException("Region must have magnitudes or mag_bins must be defined to "
"compute space magnitude binning.")

# short-circuit if zero-events in catalog... return array of zeros
if self.event_count == 0:
n_poly = self.region.num_nodes
n_mws = self.region.num_mag_bins
output = numpy.zeros((n_poly, n_mws))
skipped = []

else:
if mag_bins is None:
try:
# a forecast is a type of region, but region does not need a magnitude
mag_bins = self.region.magnitudes
except AttributeError:
# use default magnitude bins from csep
mag_bins = CSEP_MW_BINS
self.region.magnitudes = mag_bins
self.region.num_mag_bins = len(mag_bins)
mag_bins = self.region.magnitudes

# compute if not
# todo: this should be routed through self.region to allow for different types of regions
Expand Down
11 changes: 11 additions & 0 deletions csep/core/poisson_evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from csep.models import EvaluationResult
from csep.utils.stats import poisson_joint_log_likelihood_ndarray
from csep.core.exceptions import CSEPCatalogException


def paired_t_test(forecast, benchmark_forecast, observed_catalog, alpha=0.05, scale=False):
Expand Down Expand Up @@ -174,8 +175,13 @@ def conditional_likelihood_test(gridded_forecast, observed_catalog, num_simulati
"""

# grid catalog onto spatial grid
try:
_ = observed_catalog.region.magnitudes
except CSEPCatalogException:
observed_catalog.region = gridded_forecast.region
gridded_catalog_data = observed_catalog.spatial_magnitude_counts()


# simply call likelihood test on catalog data and forecast
qs, obs_ll, simulated_ll = _poisson_likelihood_test(gridded_forecast.data, gridded_catalog_data,
num_simulations=num_simulations, seed=seed, random_numbers=random_numbers,
Expand Down Expand Up @@ -304,6 +310,11 @@ def likelihood_test(gridded_forecast, observed_catalog, num_simulations=1000, se
"""

# grid catalog onto spatial grid
# grid catalog onto spatial grid
try:
_ = observed_catalog.region.magnitudes
except CSEPCatalogException:
observed_catalog.region = gridded_forecast.region
gridded_catalog_data = observed_catalog.spatial_magnitude_counts()

# simply call likelihood test on catalog and forecast
Expand Down

0 comments on commit 6da7fc1

Please sign in to comment.