Commit 3f7a1fb7 authored by Pablo Iturrieta Rebolledo's avatar Pablo Iturrieta Rebolledo
Browse files

clean pgs code

defined final results figure
modified get_forecast_figures to be adapted to new data structure and new ModelObjectg structure

# Pending: Modify T-test to include t-test significante along with w-test significance
parent 0a011cc4
...@@ -9,7 +9,7 @@ import copy ...@@ -9,7 +9,7 @@ import copy
import scipy.stats import scipy.stats
import pickle import pickle
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False): def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False):
...@@ -128,23 +128,23 @@ def run(use_saved=False): ...@@ -128,23 +128,23 @@ def run(use_saved=False):
test = 'CL' test = 'CL'
types = [''] types = ['']
models = {5: get_models.get_models_5yr(), models = {5: get_models.five_years(),
10: get_models.get_models_10yr()} 10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr, type in itertools.product(years, types): for yr, type in itertools.product(years, types):
result_path = filepaths.get_csep_result_path(test, yr, type) result_path = paths.get_csep_result_path(test, yr, type)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
plot_results(Result, years=yr, savepath=filepaths.get_csep_fig_path(test, yr, type)) plot_results(Result, years=yr, savepath=paths.get_csep_fig_path(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]), plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=filepaths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0])) savepath=paths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0]))
print('Scores 5yr') print('Scores 5yr')
get_score(Results[5]) get_score(Results[5])
print('Scores 10yr') print('Scores 10yr')
......
...@@ -9,7 +9,7 @@ import scipy.stats ...@@ -9,7 +9,7 @@ import scipy.stats
import itertools import itertools
import pickle import pickle
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
...@@ -130,23 +130,23 @@ def run(use_saved=False): ...@@ -130,23 +130,23 @@ def run(use_saved=False):
test = 'L' test = 'L'
types = [''] types = ['']
models = {5: get_models.get_models_5yr(), models = {5: get_models.five_years(),
10: get_models.get_models_10yr()} 10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr, type in itertools.product(years, types): for yr, type in itertools.product(years, types):
result_path = filepaths.get_csep_result_path(test, yr, type) result_path = paths.get_csep_result_path(test, yr, type)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
plot_results(Result, years=yr, savepath=filepaths.get_csep_fig_path(test, yr, type)) plot_results(Result, years=yr, savepath=paths.get_csep_fig_path(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]), plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=filepaths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0])) savepath=paths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0]))
print('Scores 5yr') print('Scores 5yr')
get_score(Results[5], catalogs[5]) get_score(Results[5], catalogs[5])
print('Scores 10yr') print('Scores 10yr')
......
...@@ -9,7 +9,7 @@ import itertools ...@@ -9,7 +9,7 @@ import itertools
import scipy.stats import scipy.stats
import pickle import pickle
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False): def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False):
...@@ -117,24 +117,24 @@ def run(use_saved=False): ...@@ -117,24 +117,24 @@ def run(use_saved=False):
test = 'M' test = 'M'
types = [''] types = ['']
models = {5 : get_models.get_models_5yr(), models = {5 : get_models.five_years(),
10 : get_models.get_models_10yr()} 10 : get_models.ten_years()}
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr, type in itertools.product(years, types): for yr, type in itertools.product(years, types):
result_path = filepaths.get_csep_result_path(test, yr, type) result_path = paths.get_csep_result_path(test, yr, type)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
plot_results(Result, years=yr, savepath=filepaths.get_csep_fig_path(test, yr, type)) plot_results(Result, years=yr, savepath=paths.get_csep_fig_path(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]), plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=filepaths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0])) savepath=paths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0]))
print('Scores 5yr') print('Scores 5yr')
get_score(Results[5]) get_score(Results[5])
print('Scores 10yr') print('Scores 10yr')
......
...@@ -8,7 +8,7 @@ import itertools ...@@ -8,7 +8,7 @@ import itertools
import pickle import pickle
import copy import copy
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
import scipy import scipy
...@@ -127,8 +127,8 @@ def run(use_saved=False): ...@@ -127,8 +127,8 @@ def run(use_saved=False):
types = [''] ## N-test does not have different types types = [''] ## N-test does not have different types
## Get Models and Catalogs as dictionaries ## Get Models and Catalogs as dictionaries
models = {5 : get_models.get_models_5yr(), models = {5 : get_models.five_years(),
10 : get_models.get_models_10yr()} 10 : get_models.ten_years()}
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
...@@ -136,17 +136,17 @@ def run(use_saved=False): ...@@ -136,17 +136,17 @@ def run(use_saved=False):
Results = {} Results = {}
for yr, type in itertools.product(years, types): for yr, type in itertools.product(years, types):
## Get the name of the python object where to save results, or to load them ## Get the name of the python object where to save results, or to load them
result_path = filepaths.get_csep_result_path(test, yr, type) result_path = paths.get_csep_result_path(test, yr, type)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr],catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr],catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
plot_results(Result, years=yr, savepath=filepaths.get_csep_fig_path(test, yr, type)) plot_results(Result, years=yr, savepath=paths.get_csep_fig_path(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]), plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=filepaths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0])) savepath=paths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0]))
print('Scores 5yr') print('Scores 5yr')
get_score(Results[5]) get_score(Results[5])
print('Scores 10yr') print('Scores 10yr')
......
...@@ -5,7 +5,7 @@ import pickle ...@@ -5,7 +5,7 @@ import pickle
import os import os
import numpy as np import numpy as np
import scipy.stats import scipy.stats
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
import itertools import itertools
def pgs(Forecasts, Catalog, ref='avg', bin='spatial_magnitude', save_obj=False, load_obj=False): def pgs(Forecasts, Catalog, ref='avg', bin='spatial_magnitude', save_obj=False, load_obj=False):
...@@ -133,15 +133,15 @@ def run(use_saved=False): ...@@ -133,15 +133,15 @@ def run(use_saved=False):
test = 'PGS' test = 'PGS'
types = ['avg', 11] types = ['avg', 11]
models = {5 : get_models.get_models_5yr(), models = {5 : get_models.five_years(),
10 : get_models.get_models_10yr()} 10 : get_models.ten_years()}
catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5 : get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10 : get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr, typ in itertools.product(years, types): for yr, typ in itertools.product(years, types):
result_path = filepaths.get_csep_result_path(test, yr, typ) result_path = paths.get_csep_result_path(test, yr, typ)
if use_saved: if use_saved:
Result = pgs(models[yr], catalogs[yr], ref=typ, load_obj=result_path) Result = pgs(models[yr], catalogs[yr], ref=typ, load_obj=result_path)
else: else:
...@@ -150,11 +150,11 @@ def run(use_saved=False): ...@@ -150,11 +150,11 @@ def run(use_saved=False):
if typ == int: if typ == int:
typ = models[yr][typ][0].name typ = models[yr][typ][0].name
plot_pgs(Result, yr, typ, savepath=filepaths.get_csep_fig_path(test, yr, 'ref%s'% typ)) plot_pgs(Result, yr, typ, savepath=paths.get_csep_fig_path(test, yr, 'ref%s' % typ))
for typ in types: for typ in types:
plot_merged_batchs(Results[(10, typ)], Results[(5, typ)], typ, plot_merged_batchs(Results[(10, typ)], Results[(5, typ)], typ,
savepath=filepaths.get_csep_fig_path(test, '%i-%i' % (5, 10), typ)) savepath=paths.get_csep_fig_path(test, '%i-%i' % (5, 10), typ))
if __name__ == '__main__': if __name__ == '__main__':
run() run()
...@@ -8,7 +8,7 @@ import copy ...@@ -8,7 +8,7 @@ import copy
import scipy.stats import scipy.stats
import pickle import pickle
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
import itertools import itertools
def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False): def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False):
...@@ -113,23 +113,23 @@ def run(use_saved=False): ...@@ -113,23 +113,23 @@ def run(use_saved=False):
test = 'S' test = 'S'
types = [''] types = ['']
models = {5: get_models.get_models_5yr(), models = {5: get_models.five_years(),
10: get_models.get_models_10yr()} 10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr, type in itertools.product(years, types): for yr, type in itertools.product(years, types):
result_path = filepaths.get_csep_result_path(test, yr, type) result_path = paths.get_csep_result_path(test, yr, type)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
plot_results(Result, years=yr, savepath=filepaths.get_csep_fig_path(test, yr, type)) plot_results(Result, years=yr, savepath=paths.get_csep_fig_path(test, yr, type))
plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]), plot_merged_batchs(merge_results_batchs(Results[years[0]], Results[years[1]]),
savepath=filepaths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0])) savepath=paths.get_csep_fig_path(test, "%i-%i" % tuple(years), types[0]))
print('Scores 5yr') print('Scores 5yr')
get_score(Results[5]) get_score(Results[5])
print('Scores 10yr') print('Scores 10yr')
......
...@@ -7,7 +7,7 @@ from csep.utils import plots ...@@ -7,7 +7,7 @@ from csep.utils import plots
import numpy as np import numpy as np
import pickle import pickle
import os import os
from codes import get_catalogs, get_models, filepaths from codes import get_catalogs, get_models, paths
import itertools import itertools
def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False): def process_forecasts_batch(Model_batch, catalog, save_obj=False, load_obj=False):
...@@ -119,20 +119,20 @@ def run(use_saved=False): ...@@ -119,20 +119,20 @@ def run(use_saved=False):
types = [True, False] ## Ordered IG matrix types = [True, False] ## Ordered IG matrix
models = {5: get_models.get_models_5yr(), models = {5: get_models.five_years(),
10: get_models.get_models_10yr()} 10: get_models.ten_years()}
catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()), catalogs = {5: get_catalogs.filter_5yr_08_2009(get_catalogs.taroni2008()),
10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())} 10: get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino())}
Results = {} Results = {}
for yr in years: for yr in years:
result_path = filepaths.get_csep_result_path(test, yr) result_path = paths.get_csep_result_path(test, yr)
if use_saved: if use_saved:
Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], load_obj=result_path)
else: else:
Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path) Result = process_forecasts_batch(models[yr], catalogs[yr], save_obj=result_path)
Results[yr] = Result Results[yr] = Result
tw_figs_path = os.path.join(filepaths.csep_figs, 'tw_single') tw_figs_path = os.path.join(paths.csep_figs, 'tw_single')
os.makedirs(tw_figs_path, exist_ok=True) os.makedirs(tw_figs_path, exist_ok=True)
# for yr in years: # for yr in years:
...@@ -141,7 +141,7 @@ def run(use_saved=False): ...@@ -141,7 +141,7 @@ def run(use_saved=False):
# folder=tw_figs_path) # folder=tw_figs_path)
for yr, typ in itertools.product(years, types): for yr, typ in itertools.product(years, types):
plot_all_results(Results[yr], years=yr, order=typ, plot_all_results(Results[yr], years=yr, order=typ,
savepath=filepaths.get_csep_fig_path(test, yr, type=typ*'order')) savepath=paths.get_csep_fig_path(test, yr, type=typ * 'order'))
if __name__ =='__main__': if __name__ =='__main__':
use_saved = True use_saved = True
......
...@@ -3,7 +3,7 @@ import datetime ...@@ -3,7 +3,7 @@ import datetime
import csv import csv
import numpy import numpy
import warnings import warnings
from codes import filepaths from codes import paths
from csep.utils import time_utils, readers from csep.utils import time_utils, readers
...@@ -12,10 +12,10 @@ def get_magnitude_bins(): ...@@ -12,10 +12,10 @@ def get_magnitude_bins():
return bins return bins
def horus(): def horus():
return csep.load_catalog(filepaths.horus, type='ingv_horus') return csep.load_catalog(paths.horus, type='ingv_horus')
def emrcmt(): def emrcmt():
return csep.load_catalog(filepaths.emrcmt, type='ingv_emrcmt') return csep.load_catalog(paths.emrcmt, type='ingv_emrcmt')
def bollettino(): def bollettino():
""" """
...@@ -49,7 +49,7 @@ def bollettino(): ...@@ -49,7 +49,7 @@ def bollettino():
out = [] out = []
evcat_id = [] evcat_id = []
n_event = 0 n_event = 0
with open(filepaths.bollettino) as file_: with open(paths.bollettino) as file_:
reader = csv.reader(file_, delimiter='|') reader = csv.reader(file_, delimiter='|')
for n, line in enumerate(reader): for n, line in enumerate(reader):
if is_header_line(line): if is_header_line(line):
...@@ -110,7 +110,7 @@ def taroni2008(): ...@@ -110,7 +110,7 @@ def taroni2008():
'depth': (9, "<f4"), 'depth': (9, "<f4"),
'Mw': (8, "<f4")} 'Mw': (8, "<f4")}
out = [] out = []
data = numpy.genfromtxt(filepaths.taroni2018, skip_header=1, data = numpy.genfromtxt(paths.taroni2018, skip_header=1,
names=ind.keys(), names=ind.keys(),
usecols=[i[0] for i in ind.values()], usecols=[i[0] for i in ind.values()],
dtype=[i[1] for i in ind.values()]) dtype=[i[1] for i in ind.values()])
......
...@@ -2,44 +2,33 @@ ...@@ -2,44 +2,33 @@
import pickle import pickle
import os import os
import cartopy import cartopy
import filepaths import paths
import get_models import get_models
plot_properties = {'grid_labels': True, plot_properties = {'grid_labels': True,
'borders': True, 'borders': True,
'feature_lw': 0.5, 'feature_lw': 0.5,
'basemap': 'google-satellite', 'basemap': 'stock_img',
'cmap': 'rainbow', 'cmap': 'rainbow',
'alpha_exp': 0.8, 'clim' : [-8, 0],
'clim' : [-7, -1],
'projection': cartopy.crs.Mercator()} 'projection': cartopy.crs.Mercator()}
forecasts = get_models.get_models_5yr() # forecasts = get_models.five_years()
figures_dir = filepaths.forecast_figures[5] # figures_dir = paths.forecast_figures[5]
for model_i in forecasts: # for model_i in forecasts:
plot_properties['filename'] = os.path.join(figures_dir, model_i.name) # plot_properties['filename'] = os.path.join(figures_dir, model_i.name)
plot_properties['title'] = model_i.name + ' ' + model_i.author # plot_properties['title'] = model_i.name + ' ' + model_i.author
print(plot_properties['title']) # print(plot_properties['title'])
model_i.plot(plot_args=plot_properties) # model_i.plot(plot_args=plot_properties)
forecasts = get_models.get_models_10yr() forecasts = get_models.ten_years()
figures_dir = filepaths.forecast_figures[10] figures_dir = paths.forecast_figures[10]
for model_i in forecasts: for model_i in forecasts:
plot_properties['filename'] = os.path.join(figures_dir, model_i.name) plot_properties['filename'] = os.path.join(figures_dir, 'fig_bins/%s_8-0-15' % model_i.name)
plot_properties['title'] = model_i.name + ' ' + model_i.author plot_properties['title'] = model_i.name + ' ' + model_i.author
print(plot_properties['title']) print(plot_properties['title'])
model_i.plot(plot_args=plot_properties) model_i.plot(show=True, levels=9, plot_args=plot_properties)
# forecasts_dir = '../data/ten-year-models-10.01/store/'
# figures_dir = '../data/ten-year-models-10.01/figures/'
# for i in os.listdir(forecasts_dir):
# if i.endswith('obj'):
# with open(os.path.join(forecasts_dir , i), 'rb') as file_:
# plot_properties['filename'] = os.path.join(figures_dir, i.replace('.obj', ''))
# Forecast = pickle.load(file_)
# plot_properties['title'] = Forecast[1]['name'] + ' ' + Forecast[1]['author']
# print(plot_properties['title'])
# Forecast[0].plot(plot_args=plot_properties)
...@@ -4,7 +4,7 @@ import pickle ...@@ -4,7 +4,7 @@ import pickle
import xml.etree.ElementTree as et import xml.etree.ElementTree as et
import numpy as np import numpy as np
import ntpath import ntpath
from codes import filepaths from codes import paths
def forecast_xmlreader(filename_): def forecast_xmlreader(filename_):
...@@ -104,27 +104,27 @@ def process_models(): ...@@ -104,27 +104,27 @@ def process_models():
# Reads the Batch of models an pickle.dump into a file # Reads the Batch of models an pickle.dump into a file
Batch_5yr_models = [] Batch_5yr_models = []
Batch_10yr_models = [] Batch_10yr_models = []
for model in filepaths.models_5yr: for model in paths.five_years_src:
Forecast = forecast_xmlreader(model) Forecast = forecast_xmlreader(model)
Batch_5yr_models.append(Forecast) Batch_5yr_models.append(Forecast)
with open(filepaths.models_5yr_obj, 'wb') as obj: with open(paths.models_five_years, 'wb') as obj:
pickle.dump(Batch_5yr_models, obj) pickle.dump(Batch_5yr_models, obj)
for model in filepaths.models_10yr: for model in paths.ten_years_src:
Forecast = forecast_xmlreader(model) Forecast = forecast_xmlreader(model)
Batch_10yr_models.append(Forecast) Batch_10yr_models.append(Forecast)
with open(filepaths.models_10yr_obj, 'wb') as obj: with open(paths.models_ten_years, 'wb') as obj:
pickle.dump(Batch_10yr_models, obj) pickle.dump(Batch_10yr_models, obj)
def get_models_5yr():