def start_routine(filename, P, Me_range, Re_range, n_range, noise_level, R, zp): noise = make_noise(R, noise_level) with open(filename, 'wb') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) header = ['%s = %.2f' % (P[i].name, P[i].value) for i in ['MeD', 'ReD', 'nD']] writer.writerow(header + ['range= [%.1f, %.1f]' % (R[0], R[-1])] + ['noise level = %.1f' % noise_level]) writer.writerow(['MeB_initial', 'ReB_initial', 'nB_initial', 'MeD_final', 'ReD_final', 'nD_final', 'MeB_final', 'ReB_final', 'nB_final',\ 'redchi2_all', 'redchi2_excl', 'KS', 'KS_excl']) for nB in n_range: for ReB in Re_range: for MeB in Me_range: pars = S.copy_params(P, False) pars.add_many(('MeB', float(MeB), True, 1.), ('ReB', float(ReB), True, 0.01), ('nB', float(nB), True, 0.1)) pars['nD'].vary = False test_gal = S.sersic2(pars, R, zp, False) + noise new_pars = S.copy_params(pars, False) fit_data, res_excl = S.fit(new_pars, S.sersic2, R, zp, test_gal, weights=None, fit_range=None, redchi_marker=30.) initials = [pars[i].value for i in ['MeB', 'ReB', 'nB']] if fit_data is None: writer.writerow(['N/A'] * 13) else: finals = [new_pars[i].value for i in ['MeB', 'ReB', 'nB', 'MeD', 'ReD', 'nD']] redchi_excl = np.sum(res_excl) / fit_data.nfree KS, KS_excl = stats.kstest(fit_data.residual, 'norm')[1], stats.kstest(res_excl, 'norm')[1] writer.writerow(initials+finals+['%r' % (res_excl)])
def analysis_array(array, headers, cutoff, zp, noise, x, param_template): names = ['BD_ratio', 'BT_ratio', 'delta_params', 'params_ratio', 'cut_redchi', 'cut_diff'] output = [] for row in array: P_truth, P_guess, P_fit = get_originals(row, param_template) BD = BD_ratio(P_truth, zp) BT = 1. / (1 + ((1./BD))) deltas = {} for i in ['MeB', 'ReB', 'nB', 'MeD', 'ReD']: deltas['delta_'+i] = P_truth[i] - P_fit[i] Me_ratio = P_truth['MeB']/P_truth['MeD'] Re_ratio = P_truth['ReB']/P_truth['ReD'] total_t, bulge_t, disc_t = S.sersic2(P_truth, x, zp, True) total_f, bulge_f, disc_f = S.sersic2(P_fit, x, zp, True) residual_excl = total_t - total_f cutoff_ind = SD.translate_x(x, cutoff) redchi_excl = np.sum(residual_excl[:cutoff_ind]**2.) / (cutoff_ind - 5) cut_points_fit = len(np.where(np.diff(np.sign(bulge_f - disc_f)))[0]) - len(np.where((bulge_f - disc_f) == 0)[0]) cut_points_initial = len(np.where(np.diff(np.sign(bulge_t - disc_t)))[0]) - len(np.where((bulge_t - disc_t) == 0)[0]) d = {'BD_ratio':BD, 'BT_ratio':BT, 'Me_ratio':Me_ratio, 'Re_ratio':Re_ratio, 'redchi2_cut': redchi_excl, 'cross_initial': cut_points_initial, 'cross_final': cut_points_fit} d.update(deltas) output.append(d) return output
def BD_ratio(params, zp): n = params['nB'] bnb = S.get_b_n(n) bnd = S.get_b_n(params['nD']) pre = n * gamma(2*n) * np.exp(bnb) / (bnb ** (2 * n)) h = params['ReD'] / ((bnd) ** params['nD']) Ie = 10 ** ((params['MeD'] - zp) / -2.5) I0 = Ie * np.exp(bnd) return pre * ((params['ReB'] / h)**2.) * (Ie / I0)
def tester(param_template, bulge_params, R, zp, noise, weights, fit_range): """returns fit_data and the initial guesses""" P = copy_params(param_template, False) P['MeB'].value = bulge_params['MeB'] # ready parameter set P['ReB'].value = bulge_params['ReB'] P['nB'].value = bulge_params['nB'] test_gal = S.sersic2(P, R, zp, False) test_gal += noise guesses = guess_params(P, 0.1) return S.fit(copy_params(guesses, True), S.sersic2, R, zp, test_gal, weights, fit_range, redchi_marker=None), guesses, test_gal
def move_wolf(self, sheep: Sheep): vector = (sheep.x - self.x, sheep.y - self.y) tmp_x = vector[0] * Simulate.wolf_move_dist / Simulate.calc_distance( self, sheep) tmp_y = vector[1] * Simulate.wolf_move_dist / Simulate.calc_distance( self, sheep) self.x += tmp_x self.y += tmp_y self.x = round(self.x, 3) self.y = round(self.y, 3)
def rate_fit(data_array_row, param_template, noise, cutoff, x): """returns redchi2 for region before cutoff, truth-model, delta_params""" P_truth, P_guess, P_fit = get_originals(data_array_row, param_template) truth = S.sersic2(P_truth, R, 30., False) fitted = S.sersic2(P_fit, R, 30., False) noisy = truth + noise chi = redchi2(fitted, noisy, x, cutoff) diff = compare_residuals(truth, fitted, cutoff, x) delta_params = 0 for p in P_truth.values(): delta_params += (p.value - P_fit[p.name].value) return chi, diff, delta_params
def main(exp_per, days, qdegree, graph_obj, error_CT, disease_paramters): beta_sy, beta_asy, mu_sy, mu_asy, gamma_sy, gamma_asy, delta = disease_paramters agents = [] for i in range(graph_obj.size): state = 'Susceptible' if random.random() < exp_per: state = 'Exposed' agent = Agent.Agent(state, i) agents.append(agent) #create graph of agents from graph_obj for indx, agent in enumerate(agents): agent.index = indx for j in graph_obj.adj_list[indx]: agent.neighbours.append(agents[j]) individual_types = [ 'Susceptible', 'Exposed', 'Asymptomatic', 'Symptomatic', 'Recovered' ] def p_infection(p1, p2): # probability of infectiong neighbour def p_fn(my_agent, neighbour_agents): p_inf_symp = p1 p_inf_asymp = p2 p_not_inf = 1 for nbr_agent in neighbour_agents: if nbr_agent.state == 'Symptomatic' and not nbr_agent.quarantined and not my_agent.quarantined: p_not_inf *= (1 - p_inf_symp) if nbr_agent.state == 'Asymptomatic' and not nbr_agent.quarantined and not my_agent.quarantined: p_not_inf *= (1 - p_inf_asymp) return 1 - p_not_inf return p_fn def p_standard(p): def p_fn(my_agent, neighbour_agents): return p return p_fn transmission_prob = {} for t in individual_types: transmission_prob[t] = {} for t1 in individual_types: for t2 in individual_types: transmission_prob[t1][t2] = p_standard(0) transmission_prob['Susceptible']['Exposed'] = p_infection( beta_sy, beta_asy) transmission_prob['Exposed']['Symptomatic'] = p_standard(mu_sy) transmission_prob['Exposed']['Asymptomatic'] = p_standard(mu_asy) transmission_prob['Symptomatic']['Recovered'] = p_standard(gamma_sy) transmission_prob['Asymptomatic']['Recovered'] = p_standard(gamma_asy) transmission_prob['Recovered']['Susceptible'] = p_standard(delta) sim_obj = Simulate.Simulate(graph_obj, agents, transmission_prob) sim_obj.simulate_days(days, qdegree, error_CT) return sim_obj.state_history, sim_obj.quarantine_history
def map(style): wtfpath = findwtf(style) if wtfpath is not None: data = Simulate.readwtf(wtfpath) return clockwords.data2json(data) else: abort(404)
def test_simulate_multidomain(self): '''Run HDC policy for domains in 2 multi domain dialogues ''' Settings.init(config_file='./tests/test_configs/simulate_multiDomains_HDC.cfg') ContextLogger.createLoggingHandlers(config=Settings.config) logger = ContextLogger.getLogger('') logger.info("Starting HDC Multidomain Simulation") reload(Ontology.FlatOntologyManager) # since this has a singleton class Ontology.init_global_ontology() simulator = Simulate.SimulationSystem(error_rate=0.1) simulator.run_dialogs(2) # run 2 dialogues
def test_simulate_multidomain(self): '''Run 2 dialogues with the multidomain system and GP policies (NB: config needs to explicitly set each domain to use gp actually...) ''' Settings.init(config_file='./tests/test_configs/simulate_multiDomains_GP.cfg') ContextLogger.createLoggingHandlers(config=Settings.config) logger = ContextLogger.getLogger('') logger.info("Starting GP Multidomain Simulation") reload(Ontology.FlatOntologyManager) # since this has a singleton class Ontology.init_global_ontology() simulator = Simulate.SimulationSystem(error_rate=0.1) simulator.run_dialogs(2) # run 2 dialogues
def simular(): session['numDias'] = int(request.form['numDias']) simu = si.simular(session['numUsuarios'], session['numDias'], session['periodos']) session['simulacion'] = simu write_file('simulacion.json', session['simulacion']) write_file('dias.json', session['numDias']) session['ya_simule'] = 1 return render_template('simulation.html', simulacion=session['simulacion'], dias=session['numDias'], periodos=session['periodos'], aparatos=session['aparatos'], activar=session['ya_simule'])
def loadwtfs(folder): """ load wtfs in folder""" wtfbyname = {} for wtfpath in wtffiles(folder): try: name = wtfname(wtfpath) wtfdata = Simulate.readwtf(wtfpath) wtfbyname[name] = {} wtfbyname[name]['wtfpath'] = wtfpath wtfbyname[name]['wtfdata'] = wtfdata except Exception as exc: print exc return wtfbyname
def preview(fit_data, fitted_profile, x, zp): """shows a graph of fitting""" model_x = np.linspace(x[0], x[-1], 500) model, bulge, disc = S.sersic2(fit_data.params, model_x, zp, True) fig = plt.figure() gs = gridspec.GridSpec(6,1) ax = fig.add_subplot(gs[:4,0]) res = fig.add_subplot(gs[4:,0]) ax.plot(x, S.convert_I(fitted_profile, zp), 'b.') ax.plot(model_x, S.convert_I(model, zp), 'k-') ax.plot(model_x, S.convert_I(bulge, zp), 'g:') ax.plot(model_x, S.convert_I(disc, zp), 'r--') ax.set_ylim([35, 15]) res_model = S.convert_I(S.sersic2(fit_data.params, x, zp, False), zp) res.plot(x, S.convert_I(fitted_profile, zp) - res_model, 'b.') plt.show()
def test_simulate_singledomain_for_all_domains(self): '''Loop over all domains and run 2 dialogues via HDC policy in each ''' Settings.init(config_file='./tests/test_configs/simulate_singledomain_for_all_domains.cfg') ContextLogger.createLoggingHandlers(config=Settings.config) logger = ContextLogger.getLogger('') domains = TestingUtils.get_loop_over_domains() for dstring in domains: Settings.config.set("GENERAL", 'domains', dstring) # no policy settings given --> defaults to HDC logger.info("Starting HDC Single Domain Simulation for "+dstring) reload(Ontology.FlatOntologyManager) # since this has a singleton class Ontology.init_global_ontology() simulator = Simulate.SimulationSystem(error_rate=0.1) simulator.run_dialogs(2) # run 2 dialogues
def Build_Tabs(self): # Build class objects of each tab self.IOFile = IOFile.IOFile(self, self.PyMOL, self.Btn_IOFiles, 'IOFile', IOFile.IOFileVars(), self.Prefs) self.Config1 = Config1.Config1(self, self.PyMOL, self.Btn_Config1, 'Config1', Config1.Config1Vars(), self.Prefs) self.Config2 = Config2.Config2(self, self.PyMOL, self.Btn_Config2, 'Config2', Config2.Config2Vars(), self.Prefs) self.Config3 = Config3.Config3(self, self.PyMOL, self.Btn_Config3, 'Config3', Config3.Config3Vars(), self.Prefs) self.GAParam = GAParam.GAParam(self, self.PyMOL, self.Btn_GAParam, 'GAParam', GAParam.GAParamVars(), self.Prefs) self.Simulate = Simulate.Simulate(self, self.PyMOL, self.Btn_Simulate, 'Simulate', Simulate.SimulateVars(), self.Prefs) self.listTabs = [self.IOFile, self.Config1, self.Config2, self.Config3, self.GAParam, self.Simulate] self.listBtnTabs = [self.Btn_IOFiles, self.Btn_Config1, self.Btn_Config2, self.Btn_Config3, self.Btn_GAParam, self.Btn_Simulate] return
def cells(style): wtfpath = findwtf(style) if wtfpath is not None: fontname = request.args.get('font') try: fontsize = int(request.args.get('fontsize','30')) except ValueError: fontsize = 30 data = Simulate.readwtf(wtfpath) return render_template('cells.html', cells=clockface.build_cells(fontpath=findfontpath(style,fontname), fontsize=fontsize, style=data['letters'], case=lower), style=style, fontname=fontname, fontsize=fontsize) else: abort(404)
def clockfaceimg(style): wtfpath = findwtf(style) if wtfpath is not None: data = Simulate.readwtf(wtfpath) fgcolor = request.args.get('fg', '#303030') fontname = request.args.get('font') try: fontsize = int(request.args.get('fontsize','30')) except ValueError: fontsize = 30 img = clockface.drawclock(fontpath=findfontpath(style,fontname), fontsize=fontsize, fgcolor=fgcolor, bgcolor=clockface.BLACK, style=data['letters'], case=lower, drawLEDs=False) io = StringIO.StringIO() img.save(io, format='JPEG') return Response(io.getvalue(), mimetype='image/jpeg') else: abort(404)
cells.append({'id' :'%s%d'%(chr(97+i),j), 'title' :'%s %d'%(chr(97+i),j), 'left' : x, 'top' : y, 'width' : text_width + text_off_x, 'height' : asc+desc, }) del draw return cells if __name__ == '__main__': data = Simulate.readwtf('langs/Hebrew_v1.wtf') data = Simulate.readwtf('langs/German_v1.wtf') #data = Simulate.readwtf('langs/English_v3.wtf') fontpath = r"./fonts/FreeSans.ttf" #fontpath = r'c:\windows\fonts\tahoma.ttf' #fontpath = r'c:\windows\fonts\mriamc.ttf' fontsize = 40 build_cells(fontpath=fontpath, fontsize=fontsize, style=data['letters'], case=lower) img = drawclock(fontpath=fontpath, fontsize=fontsize, fgcolor=PALEYELLOW,
def wrapper(queue, P, model, x, z, data, weights, fit_range, redchi_marker): out, res_excl = S.fit(P, model, x, z, data, weights, fit_range, redchi_marker) queue.put([out, res_excl]) queue.close()
def co_pandemic_spread(): def p_infection(day,global_state,my_agent,neighbour_agents): # probability of infectiong neighbour p_inf=0.4 p_not_inf=1 for nbr_agent in neighbour_agents: if nbr_agent.individual_type in ['Infected','Asymptomatic'] and not nbr_agent.policy_state['quarantined']: p_not_inf*=(1-p_inf) return 1 - p_not_inf def p_infection_flu(day,global_state,my_agent,neighbour_agents): # probability of infectiong neighbour p_inf=0.2 p_not_inf=1 for nbr_agent in neighbour_agents: if nbr_agent.individual_type in ['Flu'] and not nbr_agent.policy_state['quarantined']: p_not_inf*=(1-p_inf) return 1 - p_not_inf def p_standard(p): def p_fn(day,global_state,a1,nbrs): return p return p_fn individual_types=['Susceptible','Infected','Recovered','Flu','Flu Recovered','grey'] color_list=['white', 'black','red','blue','yellow','grey'] transmission_prob={} for t in individual_types: transmission_prob[t]={} for t1 in individual_types: for t2 in individual_types: transmission_prob[t1][t2]=p_standard(0) transmission_prob['Susceptible']['Infected']= p_infection transmission_prob['Susceptible']['Flu']= p_infection_flu transmission_prob['Flu']['Infected']= p_infection transmission_prob['Flu']['Flu Recovered']= p_standard(0.1) transmission_prob['Flu Recovered']['Infected']= p_infection transmission_prob['Infected']['Recovered']= p_standard(0.2) transmission_prob['Recovered']['Susceptible']= p_standard(0) gridtable =np.zeros((50,50)) for i in range(6): x= random.randint(0,49) y= random.randint(0,49) gridtable[x][y]=1 for i in range(4): x= random.randint(0,49) y= random.randint(0,49) gridtable[x][y]=3 grid=Grid.Grid(gridtable,individual_types) #policy=Policy.Vaccinate_block(grid, individual_types,1,0) policy=None sim_obj= Simulate.Simulate(transmission_prob,individual_types,grid,policy) def reward_fn(days,no_infected): return -days sim_obj.simulate_days(20) #sim_obj.simulate_till_end(reward_fn) sim_obj.grid.animate(False,color_list,0.3) sim_obj.grid.plot_time_series()
def normal_spread(): #Function determining the probability of infecting neighbour def p_infection(day,global_state,my_agent,neighbour_agents): # probability of infectiong neighbour p_inf=0.5 p_not_inf=1 for nbr_agent in neighbour_agents: if nbr_agent.individual_type in ['Infected','Asymptomatic'] and not nbr_agent.policy_state['quarantined']: p_not_inf*=(1-p_inf) #print(1-p_not_inf) return 1 - p_not_inf #Standard fixed probability of changing state def p_standard(p): def p_fn(day,global_state,a1,nbrs): return p return p_fn #Define all possible states and corresponding colors individual_types=['Susceptible','Infected','Recovered','Vaccinated','Asymptomatic','Exposed','Asymptomatic Recovered'] color_list=['white', 'black','red','blue','blue','grey','pink'] transmission_prob={} for t in individual_types: transmission_prob[t]={} for t1 in individual_types: for t2 in individual_types: transmission_prob[t1][t2]=p_standard(0) #define the transmission probabilities between states transmission_prob['Susceptible']['Exposed']= p_infection transmission_prob['Exposed']['Infected']= p_standard(0.5) transmission_prob['Exposed']['Asymptomatic']= p_standard(0.3) transmission_prob['Infected']['Recovered']= p_standard(0.2) transmission_prob['Asymptomatic']['Asymptomatic Recovered']= p_standard(0.2) transmission_prob['Recovered']['Susceptible']= p_standard(0) #Initialise grid and agents with states gridtable =np.zeros((50,50)) gridtable[35][6]=1 #Infected = individual_types[1] at (35,6) gridtable[22][43]=1 gridtable[7][2]=1 gridtable[15][2]=1 grid=Grid.Grid(gridtable,individual_types) #policy=Policy.Quarantine_area(grid, individual_types, 4, 0) policy=None sim_obj= Simulate.Simulate(transmission_prob,individual_types,grid,policy) #define reward function to be maximised for given policy def reward_fn(days,no_infected): return -days #Simulate n days of disease spread without policy intervention n_days=10 sim_obj.simulate_days(n_days) #sim_obj.simulate_till_end(reward_fn) sim_obj.grid.animate(False,color_list,0.3) #animate n_days of disease spread sim_obj.grid.plot_time_series() #plot time series of individual_types(Infected, recovered...)
import Simulate if __name__ == "__main__": simulate = Simulate.Simulate() simulate.init_sheeps() simulate.print_sheeps() round_ = 0 while round_ != simulate.nr_of_rounds and simulate.alive_sheeps() > 0: # ruch owiec simulate.move_sheeps() # ruch wilka simulate.move_wolf() # informacje o turze print("Tura", round_, "/", simulate.nr_of_rounds - 1, " Pozycja wilka: (", str(simulate.wolf.x), ",", str(simulate.wolf.y), ")", " Ilość żywych owiec: ", simulate.alive_sheeps(), "\n") if simulate.wait_flag: input("Press Enter to continue...") simulate.add_to_json_list(round_) simulate.add_to_csv(round_) # licznik tury round_ += 1 # za pętlą simulate.save_json_to_file('pos.json') simulate.save_csv_to_file('alive.csv') simulate.print_sheeps(only_alive=True) Simulate.close_logger()
import numpy import Simulate def rowcol2div(row,col): "returns div id ef '#a0' for row=0, col=0, '#b1' for col=1, row=2 etc.." return '#{}{}'.format(chr(97+col),row) def makeworddivs(data): "return a list of jquery selectors for each word" worddivs=[] for i,w in enumerate(data['words']): row = data['rows'][i] # the row index of w col = data['cols'][i] # the starting column index of w wlen = data['lens'][i] # the length of w (in leds) worddivs.append(','.join([rowcol2div((col+offset),row) for offset in range(wlen)])) return worddivs def makesentences(data): "return a list of word indeces for each sentence" return [(numpy.where(sentence)[0]).tolist() for sentence in data['data']] def data2json(data): worddivs = makeworddivs(data) sentences = makesentences(data) return json.dumps({'words':worddivs,'sentences':sentences}) if __name__ == '__main__': data = Simulate.readwtf('langs/English_v2.wtf') print data2json(data)[:100]