Commit 53392c3d authored by Pablo Iturrieta Rebolledo's avatar Pablo Iturrieta Rebolledo
Browse files

Reordered figures. Modified get_catalogs to not filter inplace. Created...

Reordered figures. Modified get_catalogs to not filter inplace. Created figures with different catalogs as input. Added distance  value into parallelized KRipley func.
parent 64b18876
......@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.8 (venv)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (csep2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (venv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (csep2)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -80,7 +80,7 @@ def plot_results(Results, years, savepath=False):
Line2D([0], [1], color='green', marker='o',lw=0, markersize=4, label='Obs. Likelihood (passes)'),
Line2D([0], [1], color='red', marker='o',lw=0, markersize=4, label='Obs. Likelihood (fails)')]
ax.legend(handles=legend_elements, loc='lower left', fontsize=9)
ax.legend(handles=legend_elements, loc='lower right', fontsize=9)
if savepath:
plt.savefig(os.path.join(savepath), dpi=300)
plt.show()
......@@ -118,7 +118,7 @@ def plot_merged_batchs(batch, savepath):
Line2D([0], [0], color='k', lw=1, label='Sim. 95% conf.'),
Line2D([0], [1], color='green', marker='o',lw=0, markersize=4, label='Obs. Likelihood (passes)'),
Line2D([0], [1], color='red', marker='o',lw=0, markersize=4, label='Obs. Likelihood (fails)')]
ax.legend(handles=legend_elements, loc='lower left', fontsize=9)
ax.legend(handles=legend_elements, loc='lower right', fontsize=9)
if savepath:
plt.savefig(os.path.join(savepath), dpi=300)
......@@ -149,28 +149,35 @@ def get_ref_IG(batch):
return Scores
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'CL'
types = ['']
models = {5: get_models.five_years(),
10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
Results = {}
for yr, type in itertools.product(years, types):
result_path = paths.get_csep_result_path(test, yr, type)
for yr in years:
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, type))
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, catalog_name))
plot_merged_batchs(merge_results_batchs(Results[years[1]], Results[years[0]]),
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), types[0]))
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), catalog_name))
print('Scores 5yr')
get_score(Results[5])
print('Scores 10yr')
......@@ -184,7 +191,9 @@ def run(use_saved=False):
if __name__ == '__main__':
use_saved = True
run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
......
......@@ -125,19 +125,27 @@ def get_ref_IG(batch):
return Scores
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'L'
types = ['']
models = {5: get_models.five_years(),
10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
Results = {}
for yr, type in itertools.product(years, types):
result_path = paths.get_csep_result_path(test, yr, type)
for yr in years:
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else:
......@@ -146,7 +154,7 @@ def run(use_saved=False):
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), types[0]))
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), catalog_name))
print('Scores 5yr')
get_score(Results[5], catalogs[5])
print('Scores 10yr')
......@@ -159,8 +167,9 @@ def run(use_saved=False):
if __name__ == '__main__':
use_saved = False
run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
......@@ -112,29 +112,36 @@ def get_score(batch):
return Scores
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'M'
types = ['']
models = {5 : get_models.five_years(),
10 : get_models.ten_years()}
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
models = {5: get_models.five_years(),
10: get_models.ten_years()}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
Results = {}
for yr, type in itertools.product(years, types):
result_path = paths.get_csep_result_path(test, yr, type)
for yr in years:
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path)
else:
Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path)
Results[yr] = Result
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, type))
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, catalog_name))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), types[0]))
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), catalog_name))
print('Scores 5yr')
get_score(Results[5])
print('Scores 10yr')
......@@ -142,5 +149,7 @@ def run(use_saved=False):
if __name__ == '__main__':
use_saved = False
run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
......@@ -114,39 +114,39 @@ def get_score(batch):
return Scores
def run(use_saved=False):
"""
R
:param use_saved:
:return:
"""
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'N'
types = [''] ## N-test does not have different types
## Get Models and Catalogs as dictionaries
models = {5 : get_models.five_years(),
10 : get_models.ten_years()}
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
models = {5: get_models.five_years(),
10: get_models.ten_years()}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
## Iterate through years and types to get the results
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
## Iterate through yearso get the results
Results = {}
for yr, type in itertools.product(years, types):
for yr in years:
## Get the name of the python object where to save results, or to load them
result_path = paths.get_csep_result_path(test, yr, type)
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path)
else:
Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path)
Results[yr] = Result
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, type))
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, catalog_name))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), types[0]))
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), catalog_name))
print('Scores 5yr')
get_score(Results[5])
print('Scores 10yr')
......@@ -155,6 +155,7 @@ def run(use_saved=False):
if __name__ == '__main__':
use_saved = False
a = run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
......@@ -9,6 +9,18 @@ from codes import get_catalogs, get_models, paths
import itertools
import seaborn as sns
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 14
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['legend.fontsize'] = 14
plt.rcParams['figure.titlesize'] = 18
def pgs_i(Forecasts, ref_forecast, Catalog, bin='spatial_magnitude'):
......@@ -167,7 +179,7 @@ def plot_merged_batchs(Results1, Results2, ref, savepath=False):
fig.savefig(os.path.join(savepath) , dpi=300 )
fig.show()
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'PGS'
......@@ -233,41 +245,43 @@ def plot_all_results(Results, years, range_ = (-10, 10), order=False, savepath=F
plt.savefig(os.path.join(savepath), dpi=300, transparent=True)
plt.show()
def run_cross(yr=10,use_saved=False):
def run_cross(catalog_name='bollettino'):
test = 'PGS'
years = [5, 10]
models = {5 : get_models.five_years(),
10 : get_models.ten_years()}
n_models = len(models[5])
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5 : get_catalogs.filter_5yr_08_2009(cat),
10 : get_catalogs.filter_10yr_01_2010(cat)}
Results = []
for i in range(n_models):
Result_ij = pgs_i(models[yr], i, catalogs[yr], bin='spatial_magnitude')
Results.append(Result_ij)
for yr in years:
for i in range(n_models):
Result_ij = pgs_i(models[yr], i, catalogs[yr], bin='spatial_magnitude')
if i==11:
savepath = paths.get_csep_result_path('pgs', yr, catalog_name)
Results_i = pgs(models[yr], catalogs[yr], ref=i, save_obj=savepath)
Results.append(Result_ij)
# plot_pgs(Result, yr, typ, savepath=paths.get_csep_fig_path(test, yr, 'ref%s' % typ))
plot_all_results(Results, 10, range_=(-10, 10), savepath=paths.get_csep_figpath('pgs', 10, 'all'), order=True)
# plot_pgs(Result, yr, typ, savepath=paths.get_csep_fig_path(test, yr, 'ref%s' % typ))
plot_all_results(Results, yr, range_=(-10, 10), savepath=paths.get_csep_figpath('pgs', yr, catalog_name), order=True)
return Results
# for typ in types:
# plot_merged_batchs(Results[(10, typ)], Results[(5, typ)], typ,
# savepath=paths.get_csep_fig_path(test, '%i-%i' % (5, 10), typ))
if __name__ == '__main__':
a = run_cross(yr=10)
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 14
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['legend.fontsize'] = 14
plt.rcParams['figure.titlesize'] = 18
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
# run(cat)
run_cross(cat)
import matplotlib.ticker as tick
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import csep
from csep.core import poisson_evaluations as poisson
from csep.utils import plots
import numpy as np
......@@ -108,28 +109,36 @@ def get_score(batch):
return Scores
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'S'
types = ['']
models = {5: get_models.five_years(),
10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
Results = {}
for yr, type in itertools.product(years, types):
result_path = paths.get_csep_result_path(test, yr, type)
for yr in years:
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, type))
plot_results(Result, years=yr, savepath=paths.get_csep_figpath(test, yr, catalog_name))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), types[0]))
savepath=paths.get_csep_figpath(test, "%i-%i" % tuple(years), catalog_name))
print('Scores 5yr')
get_score(Results[5])
print('Scores 10yr')
......@@ -138,6 +147,9 @@ def run(use_saved=False):
if __name__ == '__main__':
use_saved = False
run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
import csep
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
......@@ -10,6 +10,18 @@ import os
from codes import get_catalogs, get_models, paths
import itertools
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 14
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['legend.fontsize'] = 14
plt.rcParams['figure.titlesize'] = 18
def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False):
if not load_obj:
......@@ -34,8 +46,6 @@ def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False
raise Exception('Results object not found')
return Results_T, Results_W
def plot_single_results(Results, years, ref_model=0,folder=False):
name_ref = Results[0][ref_model][0].sim_name[1]
......@@ -110,7 +120,7 @@ def plot_all_results(Results, years, p=0.05, order=False, savepath=False):
plt.savefig(os.path.join(savepath), dpi=300, transparent=True)
plt.show()
def run(use_saved=False):
def run(use_saved=False, catalog_name='bollettino'):
years = [5, 10]
test = 'T'
types = [True, False] ## Ordered IG matrix
......@@ -118,11 +128,19 @@ def run(use_saved=False):
models = {5: get_models.five_years(),
10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
if catalog_name == 'bollettino':
cat = get_catalogs.bollettino()
elif catalog_name == 'horus':
cat = get_catalogs.horus()
elif catalog_name == 'emrcmt':
cat = get_catalogs.emrcmt()
catalogs = {5: get_catalogs.filter_5yr_08_2009(cat),
10: get_catalogs.filter_10yr_01_2010(cat)}
Results = {}
for yr in years:
result_path = paths.get_csep_result_path(test, yr)
result_path = paths.get_csep_result_path(test, yr, catalog_name)
if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else:
......@@ -132,25 +150,19 @@ def run(use_saved=False):
tw_figs_path = os.path.join(paths.csep_figs, 'tw_single')
os.makedirs(tw_figs_path, exist_ok=True)
# for yr in years:
# for model_i in range(len(models[yr])):
# plot_single_results(Results[yr], years=yr, ref_model=model_i,
# folder=tw_figs_path)
for yr in years:
for model_i in range(len(models[yr])):
plot_single_results(Results[yr], years=yr, ref_model=model_i,
folder=tw_figs_path)
for yr, typ in itertools.product(years, types):
plot_all_results(Results[yr], years=yr, order=typ,
savepath=paths.get_csep_figpath(test, yr, type=typ * 'order'))
savepath=paths.get_csep_figpath(test, yr, type=catalog_name + typ * 'order'))
if __name__ =='__main__':
use_saved = True
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 14
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['legend.fontsize'] = 14
plt.rcParams['figure.titlesize'] = 18
run(use_saved)
catalogs = ['bollettino', 'emrcmt', 'horus']
for cat in catalogs:
run(use_saved, cat)
......@@ -151,11 +151,13 @@ def filter_5yr_08_2009(cat):
min_mag = 4.95
max_depth = 30
cat.filter([f'origin_time >= {start_date}',
cat = cat.filter([f'origin_time >= {start_date}',
f'origin_time < {end_date}',
f'magnitude >= {min_mag}',
f'depth <= {max_depth}'])
cat.filter_spatial(region=csep.regions.italy_csep_region(magnitudes=get_magnitude_bins()))
f'depth <= {max_depth}'],
in_place=False)
cat = cat.filter_spatial(region=csep.regions.italy_csep_region(magnitudes=get_magnitude_bins()),
in_place=False)
print('n events 5yr: %i' % cat.event_count)
return cat
......@@ -165,11 +167,13 @@ def filter_10yr_01_2010(cat):
min_mag = 4.95
max_depth = 30
cat.filter([f'origin_time >= {start_date}',
cat = cat.filter([f'origin_time >= {start_date}',
f'origin_time < {end_date}',
f'magnitude >= {min_mag}',
f'depth <= {max_depth}'])
cat.filter_spatial(region=csep.regions.italy_csep_region(magnitudes=get_magnitude_bins()))
f'depth <= {max_depth}'],
in_place=False)
cat = cat.filter_spatial(region=csep.regions.italy_csep_region(magnitudes=get_magnitude_bins()),
in_place=False)
print('n events 10yr: %i' % cat.event_count)
return cat
......
......@@ -127,12 +127,12 @@ def plot_legends(Axes, colors, labels):
def plot_results(years, labels, ll_type=None, n_eqk = 1,
ref='avg', p=0.01,l_cl = 'L' , lowcuts=[False, False, False], show=True,
savepath=None):
savepath=None, catalog=False):
### Information Gain
T = np.array([[T_i.observed_statistic for T_i in Ref_model] for
Ref_model in paths.get_csep_result('T', years)[0]]).T
Ref_model in paths.get_csep_result('T', years, catalog)[0]]).T
if ref == 'avg':
t_score = np.sum(T, axis=1) / T.shape[0]
else:
......@@ -142,13 +142,13 @@ def plot_results(years, labels, ll_type=None, n_eqk = 1,
### Parimutuel Gambling score
PGS = paths.get_csep_result('PGS', years, ref)
PGS = paths.get_csep_result('pgs', years, catalog)
pgs =np.array([i.test_distribution[0] for i in PGS]) ## << PGS in all bins
if lowcuts[0]:
pgs[pgs < lowcuts[0]] = lowcuts[0] # << For visualization purposes