def main(args): user_input = read_input(args.input) if args.functions != None: m1 = imp.load_source(args.functions[:-3], args.functions) if isinstance(user_input['distance_func'][0], str): user_input['distance_func'] = getattr( m1, user_input['distance_func'][0]) for l1 in range(user_input['npar']): par = user_input['param_to_fit'][l1] if isinstance(user_input['prior'][par]['func'], str): user_input['prior'][par]['func'] = getattr( m1, user_input['prior_func'][l1]) #initiate ABC construct sampler_ABC = ABC(params=user_input) #build first particle system sys1 = sampler_ABC.BuildFirstPSystem() #update particle system until convergence sampler_ABC.fullABC(nruns=int(user_input['nruns'][0]))
def main(args): user_input = read_input(args.input) if user_input['path_to_obs'] == 'None': raise IOError('It is not possible to continue a process without determining a static data set.') m1 = imp.load_source(args.functions[:-3], args.functions) user_input['simulation_func'] = getattr(m1, user_input['simulation_func'][0]) if isinstance(user_input['distance_func'][0], str): user_input['distance_func'] = getattr(m1, user_input['distance_func'][0]) #check dimension of distance output dtemp = user_input['distance_func'](user_input['dataset1'], user_input) user_input['dist_dim'] = len(dtemp) for l1 in range(user_input['npar']): par = user_input['param_to_fit'][l1] if isinstance(user_input['prior'][par]['func'], str): user_input['prior'][par]['func'] = getattr(m1, user_input['prior_func'][l1]) #initiate ABC construct sampler_ABC = ABC(params=user_input) #define finished particle system index sampler_ABC.T = int(args.PS) #continue from previous run sampler_ABC.ContinueStoppedRun(sampler_ABC.T, nruns=int(user_input['nruns'][0]))
def main( args ): user_input = read_input(args.input) m1 = imp.load_source(args.functions[:-3], args.functions) user_input['simulation_func'] = getattr(m1, user_input['simulation_func'][0]) if isinstance(user_input['distance_func'], list): user_input['distance_func'] = getattr(m1, user_input['distance_func'][0]) for element in user_input['param_to_fit']: if isinstance(user_input['prior'][element]['func'], str): user_input['prior'][element]['func'] = getattr(m1, user_input['prior'][element]['func']) for pvar in user_input[element + '_prior_par_name'][:user_input[element + '_prior_par_name'].index('#')]: indx = user_input[element + '_prior_par_name'].index(pvar) user_input['prior'][element][pvar] = float(user_input[element + '_prior_par_val'][indx]) #check if observed data exist, simulate in case negative if user_input['path_to_obs'] == 'None': user_input['dataset1'] = user_input['simulation_func'](user_input['simulation_input']) #save data to file op1 = open('synthetic_data.dat', 'w') for line in user_input['dataset1']: for item in line: op1.write(str(item) + ' ') op1.write('\n') op1.close() dist_try = user_input['distance_func'](user_input['dataset1'], user_input) if 'dist_dim' not in user_input.keys(): user_input['dist_dim'] = len(dist_try) #initiate ABC construct sampler_ABC = ABC(params=user_input) #build first particle system sys1 = sampler_ABC.BuildFirstPSystem() #update particle system until convergence sampler_ABC.fullABC(nruns=int(user_input['nruns'][0])) #plot results if len(user_input['param_to_fit'] ) == 1 : plot_1p(sampler_ABC.T, 'results.pdf', user_input) elif len(user_input['param_to_fit'] ) == 2 : plot_2p(sampler_ABC.T, 'results.pdf', user_input) elif len(user_input['param_to_fit'] ) == 3 : plot_3p(sampler_ABC.T, 'results.pdf', user_input) else: raise ValueError('Only 1, 2, and 3 dimensional plots are implemented so far!')
def main(args): user_input = read_input(args.input) user_input['simulation_func'] = numcosmo_sim_cluster if user_input['path_to_obs'] == 'None': raise IOError( 'It is not possible to continue a process without determining a static data set.' ) #initiate NumCosmo object necessary for simulation Cosmo = ChooseParamsInput() Cosmo.params = user_input['simulation_input'] Cosmo.params["OL"] = 1. - Cosmo.params['Om'] - Cosmo.params['Ob'] #assign input for simulation user_input['simulation_params'] = Cosmo.params if args.functions != None: m1 = imp.load_source(args.functions[:-3], args.functions) if isinstance(user_input['distance_func'][0], str): user_input['distance_func'] = getattr( m1, user_input['distance_func'][0]) dtemp = user_input['distance_func'](user_input['dataset1'], user_input) user_input['dist_dim'] = len(dtemp) for par in user_input['param_to_fit']: l1 = user_input['param_to_fit'].index(par) if isinstance(user_input['prior'][par]['func'], str): user_input['prior'][par]['func'] = getattr( m1, user_input['prior_func'][l1]) #initiate ABC construct sampler_ABC = ABC(params=user_input) #define finished particle system index sampler_ABC.T = int(args.PS) #continue from previous run sampler_ABC.ContinueStoppedRun(int(args.PS), nruns=int(user_input['nruns'][0]))
def setUp(self): self.mu = 2.5 self.sigma = 1.0 self.n = 1000 self.params = {} self.params['simulation_func'] = ysim self.params['simulation_input'] = {'mu': self.mu, 'sigma':self.sigma, 'n':self.n} self.params['dataset1'] = self.params['simulation_func']( self.params['simulation_input'] ) self.params['param_to_fit']=['mu', 'sigma'] self.params['screen'] = 0 self.params['Mini'] = 200 self.params['M'] = 100 self.params['delta'] =0.1 self.params['qthreshold'] = 0.75 self.params['file_root'] = os.getcwd() + '/test_PS' self.params['distance_func'] = distance_quantiles self.params['quantile_nodes'] = 20 self.params['split_output'] = [1] self.params['prior'] = {} self.params['prior']['sequence'] = self.params['param_to_fit'] self.params['prior']['mu'] = {} self.params['prior']['mu']['func'] = flat_prior self.params['prior']['mu']['pmin'] = 1.0 self.params['prior']['mu']['pmax'] = 4.0 self.params['prior']['mu']['min'] = 1.0 self.params['prior']['mu']['max'] = 4.0 self.params['prior']['sigma'] = {} self.params['prior']['sigma']['func'] = flat_prior self.params['prior']['sigma']['pmin'] = 0.001 self.params['prior']['sigma']['pmax'] = 3.0 self.params['prior']['sigma']['min'] = 0.001 self.params['prior']['sigma']['max'] = 3.0 #initiate ABC sampler self.sampler_ABC = ABC( self.params ) self.W = [1.0/self.params['M'] for i in xrange( self.params['M'] )] self.params = summ_quantiles(self.params['dataset1'], self.params)
def main( args ): user_input = read_input( args.input ) user_input['simulation_func'] = numcosmo_sim_cluster if user_input['path_to_obs'] == 'None': raise IOError('It is not possible to continue a process without determining a static data set.') #initiate NumCosmo object necessary for simulation Cosmo=ChooseParamsInput() Cosmo.params = user_input['simulation_input'] Cosmo.params["OL"] = 1.- Cosmo.params['Om']-Cosmo.params['Ob'] #assign input for simulation user_input['simulation_params'] = Cosmo.params if args.functions != None: m1 = imp.load_source( args.functions[:-3], args.functions ) if isinstance(user_input['distance_func'][0], str): user_input['distance_func'] = getattr(m1, user_input['distance_func'][0]) dtemp = user_input['distance_func'](user_input['dataset1'], user_input) user_input['dist_dim'] = len(dtemp) for par in user_input['param_to_fit']: l1 = user_input['param_to_fit'].index(par) if isinstance( user_input['prior'][par]['func'], str): user_input['prior'][par]['func'] = getattr(m1, user_input['prior_func'][l1]) #initiate ABC construct sampler_ABC = ABC(params = user_input) #define finished particle system index sampler_ABC.T = int(args.PS) #continue from previous run sampler_ABC.ContinueStoppedRun(int(args.PS), nruns=int(user_input['nruns'][0]))
def main( args ): user_input = read_input(args.input) if args.functions != None: m1 = imp.load_source( args.functions[:-3], args.functions ) if isinstance(user_input['distance_func'][0], str): user_input['distance_func'] = getattr(m1, user_input['distance_func'][0]) for l1 in range(user_input['npar']): par = user_input['param_to_fit'][l1] if isinstance(user_input['prior'][par]['func'], str): user_input['prior'][par]['func'] = getattr(m1, user_input['prior_func'][l1]) #initiate ABC construct sampler_ABC = ABC(params=user_input) #build first particle system sys1 = sampler_ABC.BuildFirstPSystem() #update particle system until convergence sampler_ABC.fullABC(nruns=int(user_input['nruns'][0]))
#read user input Parameters = read_input(filename) # update dictionary of user input parameters # with customized functions Parameters['distance_func'] = my_distance Parameters['simulation_func'] = my_simulation Parameters['prior']['mean']['func'] = my_prior # in case you want to generate a pseudo-observed data set Parameters['dataset1'] = my_simulation(Parameters['simulation_input']) #calculate distance between 2 catalogues dtemp = my_distance(Parameters['dataset1'], Parameters) #determine dimension of distance output Parameters['dist_dim'] = len(dtemp) #initiate ABC sampler sampler_ABC = ABC(params=Parameters) #build first particle system sys1 = sampler_ABC.BuildFirstPSystem() #update particle system until convergence sampler_ABC.fullABC() #plot results plot_2p(sampler_ABC.T, 'results.pdf', Parameters)
def main(args): user_input = read_input(args.input) m1 = imp.load_source(args.functions[:-3], args.functions) user_input['simulation_func'] = getattr(m1, user_input['simulation_func'][0]) if isinstance(user_input['distance_func'], list): user_input['distance_func'] = getattr(m1, user_input['distance_func'][0]) for element in user_input['param_to_fit']: if isinstance(user_input['prior'][element]['func'], str): user_input['prior'][element]['func'] = getattr( m1, user_input['prior'][element]['func']) for pvar in user_input[element + '_prior_par_name'][:user_input[ element + '_prior_par_name'].index('#')]: indx = user_input[element + '_prior_par_name'].index(pvar) user_input['prior'][element][pvar] = float( user_input[element + '_prior_par_val'][indx]) #check if observed data exist, simulate in case negative if user_input['path_to_obs'] == 'None': user_input['dataset1'] = user_input['simulation_func']( user_input['simulation_input']) #save data to file op1 = open('synthetic_data.dat', 'w') for line in user_input['dataset1']: for item in line: op1.write(str(item) + ' ') op1.write('\n') op1.close() dist_try = user_input['distance_func'](user_input['dataset1'], user_input) if 'dist_dim' not in user_input.keys(): user_input['dist_dim'] = len(dist_try) #initiate ABC construct sampler_ABC = ABC(params=user_input) #build first particle system sys1 = sampler_ABC.BuildFirstPSystem() #update particle system until convergence sampler_ABC.fullABC(nruns=int(user_input['nruns'][0])) #plot results if len(user_input['param_to_fit']) == 1: plot_1p(sampler_ABC.T, 'results.pdf', user_input) elif len(user_input['param_to_fit']) == 2: plot_2p(sampler_ABC.T, 'results.pdf', user_input) elif len(user_input['param_to_fit']) == 3: plot_3p(sampler_ABC.T, 'results.pdf', user_input) else: raise ValueError( 'Only 1, 2, and 3 dimensional plots are implemented so far!')
class TestABC(unittest.TestCase): def setUp(self): self.mu = 2.5 self.sigma = 1.0 self.n = 1000 self.params = {} self.params['simulation_func'] = ysim self.params['simulation_input'] = {'mu': self.mu, 'sigma':self.sigma, 'n':self.n} self.params['dataset1'] = self.params['simulation_func']( self.params['simulation_input'] ) self.params['param_to_fit']=['mu', 'sigma'] self.params['screen'] = 0 self.params['Mini'] = 200 self.params['M'] = 100 self.params['delta'] =0.1 self.params['qthreshold'] = 0.75 self.params['file_root'] = os.getcwd() + '/test_PS' self.params['distance_func'] = distance_quantiles self.params['quantile_nodes'] = 20 self.params['split_output'] = [1] self.params['prior'] = {} self.params['prior']['sequence'] = self.params['param_to_fit'] self.params['prior']['mu'] = {} self.params['prior']['mu']['func'] = flat_prior self.params['prior']['mu']['pmin'] = 1.0 self.params['prior']['mu']['pmax'] = 4.0 self.params['prior']['mu']['min'] = 1.0 self.params['prior']['mu']['max'] = 4.0 self.params['prior']['sigma'] = {} self.params['prior']['sigma']['func'] = flat_prior self.params['prior']['sigma']['pmin'] = 0.001 self.params['prior']['sigma']['pmax'] = 3.0 self.params['prior']['sigma']['min'] = 0.001 self.params['prior']['sigma']['max'] = 3.0 #initiate ABC sampler self.sampler_ABC = ABC( self.params ) self.W = [1.0/self.params['M'] for i in xrange( self.params['M'] )] self.params = summ_quantiles(self.params['dataset1'], self.params) def test_DrawAllParams( self ): #draw parameters r1 = DrawAllParams(self.params['prior']) res = [] for i1 in xrange(len(r1)): par = self.params['param_to_fit'][i1] if r1[i1] >= self.params['prior'][par]['min'] and r1[i1] <= self.params['prior'][par]['max']: res.append(True) else: res.append(False) self.assertEqual([True for item in r1], res) def test_SetDistanceFromSimulation(self): #set distance r2 = SetDistanceFromSimulation(self.params) self.assertTrue(r2 >= 0) def test_plot(self): self.params['ncores'] = get_cores() self.sampler_ABC.fullABC(build_first_system=True) if len(self.params['param_to_fit']) == 1: plot_1p(self.sampler_ABC.T, 'results.pdf', self.params) elif len(self.params['param_to_fit']) == 2: plot_2p(self.sampler_ABC.T, 'results.pdf', self.params) elif len(self.params['param_to_fit']) == 3: plot_3p(self.sampler_ABC.T, 'results.pdf', self.params) elif len(self.params['param_to_fit']) == 4: plot_4p(self.sampler_ABC.T, 'results.pdf', self.params)