Exemplo n.º 1
0
def loop(a, level, parass, results, results_global, vers=version):
    """
    New in 2.6: Makes an arbitrary number of loops
    a: dictionnary, with keys as parameters to loop on and values as the values on which to loop.
    level: list of parameters on which to loop. The first one is the most outer loop, the last one is the most inner loop.
    """
    if level==[]:
        #rep, name= build_path_average(paras, vers=vers)
        # print parass['par']
        # print parass['AC_dict']
        
        rep, name= build_path_average(parass, vers=vers)
        with open(rep + name, 'r') as f:
            results=pickle.load(f)
        # print 'results'
        # print results
        # print
        #results_global={met:{k:sum([results[met][p][k]*paras['AC_dict'][p]/float(paras['ACtot']) \
        #        for p in results[met].keys()]) for k in ['avg', 'std']} for met in ['satisfaction', 'regulated_FPs', 'regulated_F']}
        results_global={met:{k:sum([results[met][p][k]*parass['AC_dict'][p]/float(parass['ACtot']) \
                for p in results[met].keys() if parass['AC_dict'].has_key(p)]) for k in ['avg', 'std']} for met in ['satisfaction', 'regulated_FPs', 'regulated_F']}
        return results,results_global
    else:
        assert level[0] in a.keys()
        for i in a[level[0]]:
            # print level[0], '=', i
            parass.update(level[0],i)
            # print parass['AC_dict']
            results[i], results_global[i]=loop(a, level[1:], parass, {}, {}, vers=vers)
            
    return results, results_global
Exemplo n.º 2
0
def average_sim(paras=None, G=None, save=1, suffix='', stat_distance='common_area'):
	"""
	New in 2.6: makes a certain number of iterations (given in paras) and extract the averaged mettrics.
	Change in 2.7: parallelized.
	Changed in 2.9.1: added force 
	"""
	def do((paras, G,i)):
		rlts={} 
		sim=Simulation(paras, G=G.copy(), verbose=False)
		sim.make_simu(storymode=False)
		sim.queue=post_process_queue(sim.queue)

		rlts = compare_networks(sim, stat_distance = stat_distance)

		del sim
		return rlts

	rep, name = build_path_average(paras, Gname=paras['G'].name)

	name = name[:-4] + suffix + '.pic' #_comparison_distributions.pic'

	if paras['force'] or not os.path.exists(rep + name):
		inputs = [(paras, G, i) for i in range(paras['n_iter'])]
		start_time=time()
		if paras['parallel']:
			print 'Doing iterations',
			results_list = parmap(do, inputs)
		else:
			results_list=[]
			for i,a in enumerate(inputs):
				sys.stdout.write('\r' + 'Doing simulations...' + str(int(100*(i+1)/float(paras['n_iter']))) + '%')
				sys.stdout.flush() 
				results_list.append(do(a))

		print '... done in', time()-start_time, 's'
		results={}
		for met in results_list[0].keys():
			#print met, results_list[0][met], type(results_list[0][met]), type(np.float64(1.0))
			if type(results_list[0][met])==type(np.float64(1.0)) or type(results_list[0][met])==type(1.0):
				results[met]={'avg':np.mean([v[met] for v in results_list]), 'std':np.std([v[met] for v in results_list])}
			elif type(results_list[0][met])==type({}):
				results[met]={tuple(p):[] for p in results_list[0][met].keys()}
				for company in results_list[0][met].keys():
					results[met][company]={'avg':np.mean([v[met][company] for v in results_list]), 'std':np.std([v[met][company] for v in results_list])}

		if save>0:
			#rep, name=build_path_average(paras, Gname=G.name)
			os.system('mkdir -p ' + rep)
			with open(rep + name,'w') as f:
				pickle.dump(results, f)
				#print 'Saving in', rep + name
	else:
		print 'Skipped this value because the file already exists and parameter force is deactivated.'
		print 'I load it from disk.'
		with open(rep + name,'r') as f:
			results = pickle.load(f)

	return results
Exemplo n.º 3
0
def build_path(paras, vers='2.9', **kwargs):
    rep, name = build_path_average(paras, vers = vers, **kwargs)
    return rep, name + '_shocks'