Commit aa82a35e authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

minor fixes and printing time of each task in step method

parent a13e8f7f
......@@ -39,7 +39,7 @@ class Scenario:
cult_to_decrease = surfDelta[surfDelta<0].sort_values(ascending=True).keys().tolist()
cult_to_increase = surfDelta[surfDelta>0].sort_values(ascending=False).keys().tolist()
# Sampling the patches to reallocate
if len(self.patches['cultgeopat'].isin(cult_to_decrease)) >= nbPatches:
if len(self.patches[self.patches['cultgeopat'].isin(cult_to_decrease)]) >= nbPatches:
samples = self.patches[self.patches['cultgeopat'].isin(cult_to_decrease)].sample(n=nbPatches, random_state=rng)#.reset_index(drop=True)
else:
# All cultural types have decreased to objective
......@@ -114,7 +114,7 @@ class MCMC:
sys.exit(1)
self.mcmc_config = yaml.load(open(mcmc_config_filename,'r'))
self.multiprocessing = self.mcmc_config.get('multiprocessing', False)
self.write_shp = self.mcmc_config.get('write_shp', False)
self.write_data = self.mcmc_config.get('write_data', False)
self.patches_md5sum = md5sum(self.mcmc_config['patches'])
if 'rng_seed' in self.mcmc_config:
self.rng = np.random.RandomState(self.mcmc_config['rng_seed'])
......@@ -132,9 +132,7 @@ class MCMC:
shutil.copy(mcmc_config_filename, self.outputdir)
for f in [ self.mcmc_config['target'],
self.mcmc_config['indicators_config']['resilience'],
self.mcmc_config['indicators_config']['biodiversity']['matrixfilename'],
self.mcmc_config['indicators_config']['social']['cost_matrix_filename'],
self.mcmc_config['indicators_config']['social']['patches_costs_filename']
self.mcmc_config['indicators_config']['social']['cost_matrix_filename']
]:
shutil.copy(f, self.outputdir)
with open(self.outputdir+'/seed.txt', 'w') as outfile:
......@@ -185,7 +183,7 @@ class MCMC:
def scenario_scores(patches, indicators):
return indicators.compute_indicators(patches)
def step(self, iter_nb, nb_particles, scenarios, scores=None, write_shp=False):
def step(self, iter_nb, nb_particles, scenarios, scores=None, write_data=False):
'''
Sample new scenarios, evaluate their scores and retain only pareto front.
......@@ -209,18 +207,28 @@ class MCMC:
if self.multiprocessing:
new_scores = list(tqdm(self.pool.imap(partial(MCMC.scenario_scores, indicators=self.indicators), new_scenarios, chunksize=50), total=len(new_scenarios)))
# merging with precedent data
start_time = time.time()
new_scores = pd.DataFrame(new_scores, index=new_scenarios_id, columns=self.indicators.indicators_names)
new_scores['iteration'] = iter_nb
if scores is None:
scores = new_scores
scenarios = pd.DataFrame(new_scenarios, index=new_scenarios_id)
scenarios = pd.DataFrame(new_scenarios, columns=['patches'], index=new_scenarios_id)
scenarios['iteration'] = iter_nb
else:
scores = scores.append(new_scores, sort=False)
scenarios = scenarios.append(pd.DataFrame(new_scenarios, index=new_scenarios_id), sort=False)
new_scenarios = pd.DataFrame(new_scenarios, columns=['patches'], index=new_scenarios_id)
new_scenarios['iteration'] = iter_nb
scenarios = scenarios.append(new_scenarios, sort=False)
elapsed_time = time.time() - start_time
print('Data merged in {} - '.format(time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
# Computing pareto front
start_time = time.time()
pareto_mask = MCMC.is_pareto_efficient(scores[['Resilience','Proximity','Productivity','Biodiversity','Social','TargetDelta']].values)
scores['pareto'] = pareto_mask
elapsed_time = time.time() - start_time
print('Pareto front computed in {} - '.format(time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
# Writing output data
start_time = time.time()
scores.to_csv(self.outputdir+'/mcmc_iter_{0:03d}.csv'.format(iter_nb), index=True)
try:
pairplot = sns.pairplot(scores, vars=['Resilience','Proximity','Productivity','Biodiversity','Social','TargetDelta'],
......@@ -232,15 +240,22 @@ class MCMC:
# when data doesn't vary enough on a dimension, it is impossible to generate the pareto density
# and an error is triggered. Here, we ignore this error.
pass
elapsed_time = time.time() - start_time
print('Scores written in {} - '.format(time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
# Retaining only optimal particules
scenarios = scenarios[pareto_mask]
scores = scores[pareto_mask]
# Writing shapefiles
if write_shp:
if write_data:
# Writing shapefiles
start_time = time.time()
shp_dir = self.outputdir + '/patches_iter_{0:03d}'.format(iter_nb)
os.makedirs(shp_dir)
for index,scenario in scenarios.iterrows():
scenario = scenario[0]
scenario[scenario['cultmodified']].drop('cultmodified', axis=1).to_file(shp_dir+'/{0:03d}_{}'.format(iter_nb,index), encoding='utf-8')
elapsed_time = time.time() - start_time
print(' - Shape files written in {}'.format(time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
print()
return [scenarios, scores]
def run(self, nb_processes=mp.cpu_count()):
......@@ -254,7 +269,7 @@ class MCMC:
for i in range(self.mcmc_config['max_iterations']):
print('Iteration #{}'.format(i+1))
# Write SHP only for the last iteration
scenarios, scores = self.step(i+1, nb_particles, scenarios, scores, self.write_shp and i == self.mcmc_config['max_iterations']-1)
scenarios, scores = self.step(i+1, nb_particles, scenarios, scores, self.write_data and i == self.mcmc_config['max_iterations']-1)
# TODO
# Storing variation of indicators
init_var = scores.std()
......
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