def run_fit_helper(true_comp, starcounts, measurement_error, burnin_step=None, run_name='default', trace_orbit_func=None, ): py_vers = sys.version[0] data_filename = 'temp_data/{}_compfitter_{}.fits'.format(py_vers, run_name) log_filename = 'logs/{}_compfitter_{}.log'.format(py_vers, run_name) plot_dir = 'temp_plots/{}_compfitter_{}'.format(py_vers, run_name) save_dir = 'temp_data/' logging.basicConfig(level=logging.INFO, filename=log_filename, filemode='w') synth_data = SynthData(pars=true_comp.get_pars(), starcounts=starcounts, measurement_error=measurement_error) synth_data.synthesise_everything() tabletool.convert_table_astro2cart(synth_data.table, write_table=True, filename=data_filename) res = gf.fit_comp( data=synth_data.table, plot_it=True, burnin_steps=burnin_step, plot_dir=plot_dir, save_dir=save_dir, trace_orbit_func=trace_orbit_func, ) return res
def run_fit_helper( true_comp, starcounts, measurement_error, burnin_step=None, run_name='default', trace_orbit_func=None, ): py_vers = sys.version[0] data_filename = 'temp_data/{}_compfitter_{}.fits'.format(py_vers, run_name) log_filename = 'logs/{}_compfitter_{}.log'.format(py_vers, run_name) plot_dir = 'temp_plots/{}_compfitter_{}'.format(py_vers, run_name) save_dir = 'temp_data/' logging.basicConfig(level=logging.INFO, filename=log_filename, filemode='w') synth_data = SynthData(pars=true_comp.get_pars(), starcounts=starcounts, measurement_error=measurement_error) synth_data.synthesise_everything() tabletool.convert_table_astro2cart(synth_data.table, write_table=True, filename=data_filename) res = gf.fit_comp( data=synth_data.table, plot_it=True, burnin_steps=burnin_step, plot_dir=plot_dir, save_dir=save_dir, trace_orbit_func=trace_orbit_func, ) return res
star_pars_all =\ torb.generateTracebackFile(star_pars, times, savefile=pdir+xyzuvw_tb_savefile) logging.info("Generated [{}] traceback file".format(prec)) if INIT_WITH_TRUE_ORIGIN: init_pars = origin.getInternalSphericalPars() else: init_pars = None # apply traceforward fitting (with lnprob, corner plots as side # effects) best_fit, chain, lnprob = gf.fit_comp( xyzuvw_dict=star_pars_all, burnin_steps=BURNIN_STEPS, plot_it=True, pool=pool, convergence_tol=C_TOL, plot_dir=pdir, sampling_steps=SAMPLING_STEPS, save_dir=pdir, init_pars=init_pars, traceback=True, ) # store in each directory, for hexplotter # also used as a flag to confirm this prec already fitted for np.save(pdir + group_savefile, origin) if using_mpi: pool.close()
### COMPUTATIONS ################# ################################## #~ print('SHAPES', memb_probs, all_init_pos, all_init_pars) np.savetxt('run1message', [2]) log_message('Fitting comp {}'.format(icomp), symbol='.', surround=True) best_comp, chain, lnprob = compfitter.fit_comp( data=data_dict, memb_probs=memb_probs, init_pos=all_init_pos, init_pars=all_init_pars, burnin_steps=global_pars['burnin'], plot_it=global_pars['plot_it'], pool=pool, convergence_tol=global_pars['convergence_tol'], plot_dir=local_pars['gdir'], save_dir=local_pars['gdir'], Component=component, trace_orbit_func=global_pars['trace_orbit_func'], store_burnin_chains=global_pars['store_burnin_chains'], nthreads=global_pars['nthreads'], ) np.savetxt('run1message_finishedfit_comp', [2]) #~ best_comp, x, lnprob = compfitter.fit_comp_scipy_optimise(data_dict, #~ memb_probs=memb_probs, init_pos=all_init_pos, #~ init_pars=all_init_pars, Component=component, #~ plot_it=global_pars['plot_it'], pool=pool, #~ convergence_tol=0.25, plot_dir=local_pars['gdir'],
hdul[1].data[0]['pmra_error'] hdul[1].data[0]['parralax_error'] hdul[1].data[0]['parallax_error'] np.sqrt(astr_dict['astr_covs'][0]) import chronostar.retired2.converter as cv cv.convertMeasurementsToCartesian(astr_dict=astr_dict, savefile="../data/gaia_dr2_bp_xyzuvw.fits") import chronostar.compfitter as gf star_pars = gf.loadXYZUVW("../data/gaia_dr2_bp_xyzuvw.fits") star_pars['xyzuvw'] import matplotlib.pyplot as plt plt.plot(star_pars['xyzuvw'][:, 0], star_pars['xyzuvw'][:, 1], '.') plt.show() plt.plot(star_pars['xyzuvw'][:, 3], star_pars['xyzuvw'][:, 4], '.') plt.show() gf.fit_comp(xyzuvw_dict=star_pars, plot_it=True, plot_dir='temp_plots') get_ipython().magic(u'save gaia_dr2_bp_data_generation.py 1-62') xyzuvw_dict['xyzuvw'].shape star_pars star_pars['xyzuvw'].shae star_pars['xyzuvw'].shape plt.plot(star_pars['xyzuvw'][0], star_pars['xyzuvw'][3], '.') plt.show() plt.plot(star_pars['xyzuvw'][:, 0], star_pars['xyzuvw'][:, 3], '.') plt.show() np.where(star_pars['xyzuvw'][:, 3] > -10) np.where((star_pars['xyzuvw'][:, 3] > -10) & star_pars['xyzuvw'][:3] < 20) np.where((star_pars['xyzuvw'][:, 3] > -10) & (star_pars['xyzuvw'][:3] < 20)) np.where((star_pars['xyzuvw'][:, 3] > -10) & (star_pars['xyzuvw'][:, 3] < 20)) bp_membs = np.where((star_pars['xyzuvw'][:, 3] > -10) & (star_pars['xyzuvw'][:, 3] < 20))
hdul[1].data[0]['error_pmra'] hdul[1].data[0]['pmra_error'] hdul[1].data[0]['parralax_error'] hdul[1].data[0]['parallax_error'] np.sqrt(astr_dict['astr_covs'][0]) import chronostar.retired2.converter as cv cv.convertMeasurementsToCartesian(astr_dict=astr_dict, savefile="../data/gaia_dr2_bp_xyzuvw.fits") import chronostar.compfitter as gf star_pars = gf.loadXYZUVW("../data/gaia_dr2_bp_xyzuvw.fits") star_pars['xyzuvw'] import matplotlib.pyplot as plt plt.plot(star_pars['xyzuvw'][:,0], star_pars['xyzuvw'][:,1], '.') plt.show() plt.plot(star_pars['xyzuvw'][:,3], star_pars['xyzuvw'][:,4], '.') plt.show() gf.fit_comp(xyzuvw_dict=star_pars, plot_it=True, plot_dir='temp_plots') get_ipython().magic(u'save gaia_dr2_bp_data_generation.py 1-62') xyzuvw_dict['xyzuvw'].shape star_pars star_pars['xyzuvw'].shae star_pars['xyzuvw'].shape plt.plot(star_pars['xyzuvw'][0], star_pars['xyzuvw'][3], '.') plt.show() plt.plot(star_pars['xyzuvw'][:,0], star_pars['xyzuvw'][:,3], '.') plt.show() np.where(star_pars['xyzuvw'][:,3] > -10) np.where((star_pars['xyzuvw'][:,3] > -10) & star_pars['xyzuvw'][:3] < 20) np.where((star_pars['xyzuvw'][:,3] > -10) & (star_pars['xyzuvw'][:3] < 20)) np.where((star_pars['xyzuvw'][:,3] > -10) & (star_pars['xyzuvw'][:,3] < 20)) bp_membs = np.where((star_pars['xyzuvw'][:,3] > -10) & (star_pars['xyzuvw'][:,3] < 20)) bp_membs
def run_fit_helper(true_comp, starcounts, measurement_error, burnin_step=None, run_name='default', trace_orbit_func=None, Component=EllipComponent, init_pars=None): py_vers = sys.version[0] save_dir = 'temp_data/%s_compfitter_%s/' % (py_vers, run_name) data_filename = save_dir + 'synth_data.fits'.format(py_vers, run_name) plot_dir = save_dir print("---------", save_dir) if not os.path.isdir(save_dir): os.mkdir(save_dir) log_filename = save_dir + 'log.log'.format(py_vers, run_name) logging.basicConfig(level=logging.INFO, filename=log_filename, filemode='w') synth_data = SynthData(pars=true_comp.get_pars(), starcounts=starcounts, measurement_error=measurement_error, Components=Component) synth_data.synthesise_everything() tabletool.convert_table_astro2cart(synth_data.table, write_table=True, filename=data_filename) print("newPars ------------------------------ \n", init_pars) if init_pars is None: internal_pars = None else: internal_pars = Component.internalise(init_pars) res = cf.fit_comp(data=synth_data.table, plot_it=True, burnin_steps=burnin_step, store_burnin_chains=True, plot_dir=plot_dir, save_dir=save_dir, trace_orbit_func=trace_orbit_func, optimisation_method='emcee', Component=Component, init_pars=internal_pars) comps_filename = save_dir + 'true_and_best_comp.py' best_comp = res[0] EllipComponent.store_raw_components(comps_filename, [true_comp, best_comp]) star_pars = tabletool.build_data_dict_from_table(synth_data.table) plot_results(true_comp, best_fit_comp=res[0], star_pars=star_pars, plt_dir=save_dir) return res
pool.wait() sys.exit(0) logging.info("Only one thread is master") logging.info("Attempting to load prior memberships") try: z = np.load(init_members_file) logging.info("Prior memberships successfully loaded") except IOError: z = None logging.info("'init_z.npy' not found") # can optionally initialise fit around approximate coords #xyzuvw_dict = gf.loadXYZUVW(xyzuvw_file) #approx_mean = np.mean(xyzuvw_dict['xyzuvw'], axis=0) #approx_dx = np.prod(np.std(xyzuvw_dict['xyzuvw'][:,:3], axis=0))**(1./3.) #approx_dv = np.prod(np.std(xyzuvw_dict['xyzuvw'][:,3:], axis=0))**(1./3.) #init_pars = np.hstack((approx_mean, np.log(approx_dx), np.log(approx_dv), 1.0)) logging.info("applying fit") best_fit, chain, lnprob = gf.fit_comp( xyzuvw_file=xyzuvw_file, burnin_steps=BURNIN_STEPS, plot_it=True, pool=pool, convergence_tol=C_TOL, save_dir=results_dir, #init_pars=init_pars, plot_dir=results_dir, sampling_steps=SAMPLING_STEPS, z=z, ) np.save(best_fit_file, best_fit) if using_mpi: pool.close()
mkpath(pdir) np.save(pdir+group_savefile, origin) # store in each directory, for hexplotter try: best_group = dt.loadGroups(pdir + 'final_groups.npy') logging.info("Precision [{}] already fitted for".format(prec)) except IOError: # convert XYZUVW data into astrometry astro_table = chronostar.synthdata.measureXYZUVW(xyzuvw_now_perf, prec_val[prec], savefile=pdir+astro_savefile) star_pars =\ cv.convertMeasurementsToCartesian(astro_table, savefile=pdir+xyzuvw_conv_savefile) logging.info("Generated [{}] traceback file".format(prec)) # apply traceforward fitting (with lnprob, corner plots as side effects) best_fit, chain, lnprob = gf.fit_comp( xyzuvw_dict=star_pars, burnin_steps=BURNIN_STEPS, plot_it=True, pool=pool, convergence_tol=C_TOL, plot_dir=pdir, save_dir=pdir, sampling_steps=SAMPLING_STEPS ) best_group = chronostar.component.Component(best_fit, form=True, internal=True) np.save(pdir + 'final_best_groups.npy', best_group) med_and_span = dt.calcMedAndSpan(chain) np.save(pdit+'med_and_span.npy', med_and_span) #hp.dataGatherer(save_dir=prec) if using_mpi: pool.close()
# Performing fit for each precision for prec in precs: logging.info("Fitting to prec: {}".format(prec)) mkpath(prec) os.chdir(prec) np.save(group_savefile, origin) # store in each directory, for hexplotter try: res = np.load(result_file) logging.info("Precision [{}] already fitted for".format(prec)) except IOError: # convert XYZUVW data into astrometry astro_table = chronostar.synthdata.measureXYZUVW(xyzuvw_now_perf, prec_val[prec], savefile=astro_savefile) star_pars =\ cv.convertMeasurementsToCartesian(astro_table, savefile=xyzuvw_conv_savefile) logging.info("Generated [{}] traceback file".format(prec)) # apply traceforward fitting (with lnprob, corner plots as side effects) best_fit, chain, lnprob = gf.fit_comp( xyzuvw_dict=star_pars, burnin_steps=BURNIN_STEPS, plot_it=True, pool=pool, convergence_tol=C_TOL ) #hp.dataGatherer(save_dir=prec) finally: os.chdir('..') if using_mpi: pool.close()