11# Third-Party Imports
2+ import time
3+
24import numpy
35import scipy .stats
46
1517from csep .utils .stats import get_quantiles , cumulative_square_diff
1618
1719
18- def number_test (forecast , observed_catalog ):
20+ def number_test (forecast , observed_catalog , verbose = True ):
1921 """ Performs the number test on a catalog-based forecast.
2022
2123 The number test builds an empirical distribution of the event counts for each data. By default, this
@@ -30,7 +32,14 @@ def number_test(forecast, observed_catalog):
3032 evaluation result (:class:`csep.models.EvaluationResult`): evaluation result
3133 """
3234 event_counts = []
33- for catalog in forecast :
35+ t0 = time .time ()
36+ for i , catalog in enumerate (forecast ):
37+ # output status
38+ if verbose :
39+ tens_exp = numpy .floor (numpy .log10 (i + 1 ))
40+ if (i + 1 ) % 10 ** tens_exp == 0 :
41+ t1 = time .time ()
42+ print (f'Processed { i + 1 } catalogs in { t1 - t0 } seconds' , flush = True )
3443 event_counts .append (catalog .event_count )
3544 obs_count = observed_catalog .event_count
3645 delta_1 , delta_2 = get_quantiles (event_counts , obs_count )
@@ -46,7 +55,7 @@ def number_test(forecast, observed_catalog):
4655 obs_name = observed_catalog .name )
4756 return result
4857
49- def spatial_test (forecast , observed_catalog ):
58+ def spatial_test (forecast , observed_catalog , verbose = True ):
5059 """ Performs spatial test for catalog-based forecasts.
5160
5261
@@ -70,7 +79,7 @@ def spatial_test(forecast, observed_catalog):
7079
7180 # compute expected rates for forecast if needed
7281 if forecast .expected_rates is None :
73- forecast .get_expected_rates ()
82+ forecast .get_expected_rates (verbose = verbose )
7483
7584 expected_cond_count = forecast .expected_rates .sum ()
7685 forecast_mean_spatial_rates = forecast .expected_rates .spatial_counts ()
@@ -81,10 +90,17 @@ def spatial_test(forecast, observed_catalog):
8190 n_obs = numpy .sum (gridded_obs )
8291
8392 # iterate through catalogs in forecast and compute likelihood
84- for catalog in forecast :
93+ t0 = time .time ()
94+ for i , catalog in enumerate (forecast ):
8595 gridded_cat = catalog .spatial_counts ()
8696 _ , lh_norm = _compute_likelihood (gridded_cat , forecast_mean_spatial_rates , expected_cond_count , n_obs )
8797 test_distribution .append (lh_norm )
98+ # output status
99+ if verbose :
100+ tens_exp = numpy .floor (numpy .log10 (i + 1 ))
101+ if (i + 1 ) % 10 ** tens_exp == 0 :
102+ t1 = time .time ()
103+ print (f'Processed { i + 1 } catalogs in { t1 - t0 } seconds' , flush = True )
88104
89105 _ , obs_lh_norm = _compute_likelihood (gridded_obs , forecast_mean_spatial_rates , expected_cond_count , n_obs )
90106 # if obs_lh is -numpy.inf, recompute but only for indexes where obs and simulated are non-zero
@@ -124,7 +140,7 @@ def spatial_test(forecast, observed_catalog):
124140
125141 return result
126142
127- def magnitude_test (forecast , observed_catalog ):
143+ def magnitude_test (forecast , observed_catalog , verbose = True ):
128144 """ Performs magnitude test for catalog-based forecasts """
129145 test_distribution = []
130146
@@ -149,7 +165,7 @@ def magnitude_test(forecast, observed_catalog):
149165
150166 # compute expected rates for forecast if needed
151167 if forecast .expected_rates is None :
152- forecast .get_expected_rates ()
168+ forecast .get_expected_rates (verbose = verbose )
153169
154170 # returns the average events in the magnitude bins
155171 union_histogram = forecast .expected_rates .magnitude_counts ()
@@ -160,7 +176,8 @@ def magnitude_test(forecast, observed_catalog):
160176 scaled_union_histogram = union_histogram * union_scale
161177
162178 # compute the test statistic for each catalog
163- for catalog in forecast :
179+ t0 = time .time ()
180+ for i , catalog in enumerate (forecast ):
164181 mag_counts = catalog .magnitude_counts ()
165182 n_events = numpy .sum (mag_counts )
166183 if n_events == 0 :
@@ -172,6 +189,12 @@ def magnitude_test(forecast, observed_catalog):
172189 test_distribution .append (
173190 cumulative_square_diff (numpy .log10 (catalog_histogram + 1 ), numpy .log10 (scaled_union_histogram + 1 ))
174191 )
192+ # output status
193+ if verbose :
194+ tens_exp = numpy .floor (numpy .log10 (i + 1 ))
195+ if (i + 1 ) % 10 ** tens_exp == 0 :
196+ t1 = time .time ()
197+ print (f'Processed { i + 1 } catalogs in { t1 - t0 } seconds' , flush = True )
175198
176199 # compute observed statistic
177200 obs_d_statistic = cumulative_square_diff (numpy .log10 (obs_histogram + 1 ), numpy .log10 (scaled_union_histogram + 1 ))
@@ -192,7 +215,7 @@ def magnitude_test(forecast, observed_catalog):
192215
193216 return result
194217
195- def pseudolikelihood_test (forecast , observed_catalog ):
218+ def pseudolikelihood_test (forecast , observed_catalog , verbose = True ):
196219 """ Performs the spatial pseudolikelihood test for catalog forecasts.
197220
198221 Performs the spatial pseudolikelihood test as described by Savran et al., 2020. The tests uses a pseudolikelihood
@@ -216,7 +239,7 @@ def pseudolikelihood_test(forecast, observed_catalog):
216239
217240 # compute expected rates for forecast if needed
218241 if forecast .expected_rates is None :
219- _ = forecast .get_expected_rates ()
242+ _ = forecast .get_expected_rates (verbose = verbose )
220243
221244 expected_cond_count = forecast .expected_rates .sum ()
222245 forecast_mean_spatial_rates = forecast .expected_rates .spatial_counts ()
@@ -226,10 +249,17 @@ def pseudolikelihood_test(forecast, observed_catalog):
226249 gridded_obs = observed_catalog .spatial_counts ()
227250 n_obs = numpy .sum (gridded_obs )
228251
229- for catalog in forecast :
252+ t0 = time .time ()
253+ for i , catalog in enumerate (forecast ):
230254 gridded_cat = catalog .spatial_counts ()
231255 plh , _ = _compute_likelihood (gridded_cat , forecast_mean_spatial_rates , expected_cond_count , n_obs )
232256 test_distribution .append (plh )
257+ # output status
258+ if verbose :
259+ tens_exp = numpy .floor (numpy .log10 (i + 1 ))
260+ if (i + 1 ) % 10 ** tens_exp == 0 :
261+ t1 = time .time ()
262+ print (f'Processed { i + 1 } catalogs in { t1 - t0 } seconds' , flush = True )
233263
234264 obs_plh , _ = _compute_likelihood (gridded_obs , forecast_mean_spatial_rates , expected_cond_count , n_obs )
235265 # if obs_lh is -numpy.inf, recompute but only for indexes where obs and simulated are non-zero
0 commit comments