Commit ba725e2f authored by Pablo Iturrieta Rebolledo's avatar Pablo Iturrieta Rebolledo
Browse files

finalized parallelizing

parent aaab76c5
import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import fiona import fiona
from codes import get_models from codes import get_models
from codes import paths from codes import paths
...@@ -16,6 +20,8 @@ import cartopy ...@@ -16,6 +20,8 @@ import cartopy
import seaborn as sns import seaborn as sns
import time import time
def get_simulations(model, catalog, lons, lats): def get_simulations(model, catalog, lons, lats):
forecast_data = model.spatial_counts() forecast_data = model.spatial_counts()
...@@ -115,6 +121,7 @@ def K_r_parallel(data): ...@@ -115,6 +121,7 @@ def K_r_parallel(data):
# Get the model rates # Get the model rates
rates = model.spatial_counts(cartesian=True) rates = model.spatial_counts(cartesian=True)
rates[rates==0] = np.nan
rates_array = robjects.FloatVector(rates.T.ravel()) rates_array = robjects.FloatVector(rates.T.ravel())
rates_mat = robjects.r['matrix'](rates_array, nrow=rates.shape[0]) rates_mat = robjects.r['matrix'](rates_array, nrow=rates.shape[0])
image = spat_geom.im(rates_mat, Xregion_array, Yregion_array) image = spat_geom.im(rates_mat, Xregion_array, Yregion_array)
...@@ -295,67 +302,74 @@ def plot_results(Results, alpha=0.05): ...@@ -295,67 +302,74 @@ def plot_results(Results, alpha=0.05):
def run_parallel(): def run_parallel():
models = get_models.ten_years() models = get_models.ten_years()
catalog = get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino()) catalogs = [get_catalogs.filter_10yr_01_2010(get_catalogs.bollettino()),
get_catalogs.filter_10yr_01_2010(get_catalogs.emrcmt()),
get_catalogs.filter_10yr_01_2010(get_catalogs.horus())]
cat_names = ['bollettino',
'emrcmt',
'horus']
Results = dict.fromkeys([i.name for i in models]) Results = dict.fromkeys([i.name for i in models])
for model in models:
print(model.name)
points = np.stack((catalog.get_longitudes(), catalog.get_latitudes())).T
r = np.linspace(0,800,100)
K = K_r_spatstat(points, model, r=r)
rk_cat = K[0]
K_cat = K[2]
PCF = pcf_spatstat(K)
rpcf_cat = PCF[0]
pcf_cat = PCF[2]
lons = model.get_longitudes() for catalog, cat_name in zip(catalogs, cat_names):
lats = model.get_latitudes() for model in models:
nsim = 16 print(model.name)
sim_catalogs = iter([(get_simulations(model, catalog, lons, lats)[0], model) for i in range(nsim)])
Rk_sims = []
K_sims = []
Rpcf_sims = []
PCF_sims = []
start = time.process_time()
p = Pool(8)
A = p.map(K_r_parallel, sim_catalogs)
p.close()
for K in A:
rk_i = list(K[0])
K_i = list(K[2])
points = np.stack((catalog.get_longitudes(), catalog.get_latitudes())).T
r = np.linspace(0,800,100)
K = K_r_spatstat(points, model, r=r)
rk_cat = K[0]
K_cat = K[2]
PCF = pcf_spatstat(K) PCF = pcf_spatstat(K)
rpcf_i = list(PCF[0]) rpcf_cat = PCF[0]
pcf_i = list(PCF[2]) pcf_cat = PCF[2]
Rk_sims.append(rk_i)
K_sims.append(K_i) lons = model.get_longitudes()
Rpcf_sims.append(rpcf_i) lats = model.get_latitudes()
PCF_sims.append(pcf_i) nsim = 240
print(time.process_time() - start) sim_catalogs = iter([(get_simulations(model, catalog, lons, lats)[0], model) for i in range(nsim)])
assert numpy.allclose(np.mean(Rk_sims, axis=0), np.array(Rk_sims[0]))
assert numpy.allclose(np.mean(Rpcf_sims, axis=0), np.array(Rpcf_sims[0])) Rk_sims = []
K_sims = []
L = np.divide(np.sqrt(K_sims),np.sqrt(np.pi)) Rpcf_sims = []
L_cat = np.sqrt(np.array(K_cat) / np.pi) PCF_sims = []
start = time.process_time()
Results[model.name] = {'K_sims': K, p = Pool(16)
'Rk_sims': Rk_sims, A = p.map(K_r_parallel, sim_catalogs)
'K_cat': K_cat, p.close()
'rk_cat': rk_cat, for K in A:
'L_sims': L, rk_i = list(K[0])
'L_cat':L_cat, K_i = list(K[2])
'PCF_sims': PCF_sims,
'Rpcf_sims':Rpcf_sims, PCF = pcf_spatstat(K)
'pcf_cat': pcf_cat, rpcf_i = list(PCF[0])
'rpcf_cat': rpcf_cat} pcf_i = list(PCF[2])
with open(paths.get_kripley_result_path('KRipley_bolletino', 10), 'wb') as file_: Rk_sims.append(rk_i)
pickle.dump(Results, file_) K_sims.append(K_i)
Rpcf_sims.append(rpcf_i)
PCF_sims.append(pcf_i)
print(time.process_time() - start)
assert numpy.allclose(np.mean(Rk_sims, axis=0), np.array(Rk_sims[0]))
assert numpy.allclose(np.mean(Rpcf_sims, axis=0), np.array(Rpcf_sims[0]))
L = np.divide(np.sqrt(K_sims),np.sqrt(np.pi))
L_cat = np.sqrt(np.array(K_cat) / np.pi)
Results[model.name] = {'K_sims': K,
'Rk_sims': Rk_sims,
'K_cat': K_cat,
'rk_cat': rk_cat,
'L_sims': L,
'L_cat':L_cat,
'PCF_sims': PCF_sims,
'Rpcf_sims':Rpcf_sims,
'pcf_cat': pcf_cat,
'rpcf_cat': rpcf_cat}
with open(paths.get_kripley_result_path('KRipley_%s' % cat_name, 10), 'wb') as file_:
pickle.dump(Results, file_)
return Results return Results
...@@ -381,7 +395,7 @@ def run(): ...@@ -381,7 +395,7 @@ def run():
lons = model.get_longitudes() lons = model.get_longitudes()
lats = model.get_latitudes() lats = model.get_latitudes()
nsim = 16 nsim = 200
sim_catalogs = iter([get_simulations(model, catalog, lons, lats) for i in range(nsim)]) sim_catalogs = iter([get_simulations(model, catalog, lons, lats) for i in range(nsim)])
Rk_sims = [] Rk_sims = []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment