Skip to content

Commit 6da7fc1

Browse files
authored
fixes apply_filter and makes CL and L test use the same region as catalog 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
1 parent 9ff2316 commit 6da7fc1

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

csep/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
write_json
1414
)
1515

16+
from csep.core.exceptions import CSEPCatalogException
17+
1618
from csep.utils import datasets
1719
from csep.utils import readers
1820

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

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

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

174178
if apply_filters:
175-
return_val = return_val.filter().filter_spatial()
179+
try:
180+
return_val = return_val.filter().filter_spatial()
181+
except CSEPCatalogException:
182+
return_val = return_val.filter()
176183

177184
return return_val
178185

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

212219
if apply_filters:
213-
comcat = comcat.filter().filter_spatial()
220+
try:
221+
comcat = comcat.filter().filter_spatial()
222+
except CSEPCatalogException:
223+
comcat = comcat.filter()
214224

215225
if verbose:
216226
print("Downloaded catalog from ComCat with following parameters")

csep/core/catalogs.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -744,23 +744,19 @@ def spatial_magnitude_counts(self, mag_bins=None, tol=0.00001, ret_skipped=False
744744
if self.region is None:
745745
raise CSEPCatalogException("Cannot create binned rates without region information.")
746746

747+
if self.region.magnitudes is None and mag_bins is None:
748+
raise CSEPCatalogException("Region must have magnitudes or mag_bins must be defined to "
749+
"compute space magnitude binning.")
750+
747751
# short-circuit if zero-events in catalog... return array of zeros
748752
if self.event_count == 0:
749753
n_poly = self.region.num_nodes
750754
n_mws = self.region.num_mag_bins
751755
output = numpy.zeros((n_poly, n_mws))
752756
skipped = []
753-
754757
else:
755758
if mag_bins is None:
756-
try:
757-
# a forecast is a type of region, but region does not need a magnitude
758-
mag_bins = self.region.magnitudes
759-
except AttributeError:
760-
# use default magnitude bins from csep
761-
mag_bins = CSEP_MW_BINS
762-
self.region.magnitudes = mag_bins
763-
self.region.num_mag_bins = len(mag_bins)
759+
mag_bins = self.region.magnitudes
764760

765761
# compute if not
766762
# todo: this should be routed through self.region to allow for different types of regions

csep/core/poisson_evaluations.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from csep.models import EvaluationResult
88
from csep.utils.stats import poisson_joint_log_likelihood_ndarray
9+
from csep.core.exceptions import CSEPCatalogException
910

1011

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

176177
# grid catalog onto spatial grid
178+
try:
179+
_ = observed_catalog.region.magnitudes
180+
except CSEPCatalogException:
181+
observed_catalog.region = gridded_forecast.region
177182
gridded_catalog_data = observed_catalog.spatial_magnitude_counts()
178183

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

306312
# grid catalog onto spatial grid
313+
# grid catalog onto spatial grid
314+
try:
315+
_ = observed_catalog.region.magnitudes
316+
except CSEPCatalogException:
317+
observed_catalog.region = gridded_forecast.region
307318
gridded_catalog_data = observed_catalog.spatial_magnitude_counts()
308319

309320
# simply call likelihood test on catalog and forecast

0 commit comments

Comments
 (0)