class SampleThetaCheb(Order): def initialize(self, key): super().initialize(key) # for now, just use white noise self.data_mat = self.sigma_mat.copy() self.data_mat_last = self.data_mat.copy() #Set up p0 and the independent sampler fname = Starfish.specfmt.format(self.spectrum_id, self.order) + "phi.json" phi = PhiParam.load(fname) self.p0 = phi.cheb cov = np.diag(Starfish.config["cheb_jump"]**2 * np.ones(len(self.p0))) def lnfunc(p): # turn this into pars self.update_Phi(p) lnp = self.evaluate() self.logger.debug("Evaluated Phi parameters: {} {}".format(p, lnp)) return lnp def rejectfn(): self.logger.debug("Calling Phi revertfn.") self.revert_Phi() self.sampler = StateSampler(lnfunc, self.p0, cov, query_lnprob=self.get_lnprob, rejectfn=rejectfn, debug=True) def update_Phi(self, p): ''' Update the Chebyshev coefficients only. ''' self.chebyshevSpectrum.update(p) def finish(self, *args): super().finish(*args) fname = Starfish.routdir + Starfish.specfmt.format( self.spectrum_id, self.order) + "/mc.hdf5" self.sampler.write(fname=fname)
class SampleThetaCheb(Order): def initialize(self, key): super().initialize(key) # for now, just use white noise self.data_mat = self.sigma_mat.copy() self.data_mat_last = self.data_mat.copy() #Set up p0 and the independent sampler fname = Starfish.specfmt.format(self.spectrum_id, self.order) + "phi.json" phi = PhiParam.load(fname) self.p0 = phi.cheb cov = np.diag(Starfish.config["cheb_jump"]**2 * np.ones(len(self.p0))) def lnfunc(p): # turn this into pars self.update_Phi(p) lnp = self.evaluate() self.logger.debug("Evaluated Phi parameters: {} {}".format(p, lnp)) return lnp def rejectfn(): self.logger.debug("Calling Phi revertfn.") self.revert_Phi() self.sampler = StateSampler(lnfunc, self.p0, cov, query_lnprob=self.get_lnprob, rejectfn=rejectfn, debug=True) def update_Phi(self, p): ''' Update the Chebyshev coefficients only. ''' self.chebyshevSpectrum.update(p) def finish(self, *args): super().finish(*args) fname = Starfish.routdir + Starfish.specfmt.format(self.spectrum_id, self.order) + "/mc.hdf5" self.sampler.write(fname=fname)
class SampleThetaPhiLines(Order): def initialize(self, key): # Run through the standard initialization super().initialize(key) # for now, start with white noise self.data_mat = self.sigma_mat.copy() self.data_mat_last = self.data_mat.copy() #Set up p0 and the independent sampler fname = Starfish.specfmt.format(self.spectrum_id, self.order) + "phi.json" phi = PhiParam.load(fname) # print("Phi.regions", phi.regions) # import sys # sys.exit() # Get the regions matrix region_func = make_k_func_region(phi) max_r = 4.0 * np.max(phi.regions, axis=0)[2] self.region_mat = get_dense_C(self.wl, k_func=region_func, max_r=max_r) print(self.region_mat) # Then set phi to None phi.regions = None #Loading file that was previously output # Convert PhiParam object to an array self.p0 = phi.toarray() jump = Starfish.config["Phi_jump"] cheb_len = (self.npoly - 1) if self.chebyshevSpectrum.fix_c0 else self.npoly cov_arr = np.concatenate((Starfish.config["cheb_jump"]**2 * np.ones((cheb_len,)), np.array([jump["sigAmp"], jump["logAmp"], jump["l"]])**2 )) cov = np.diag(cov_arr) def lnfunc(p): # Convert p array into a PhiParam object ind = self.npoly if self.chebyshevSpectrum.fix_c0: ind -= 1 cheb = p[0:ind] sigAmp = p[ind] ind+=1 logAmp = p[ind] ind+=1 l = p[ind] phi = PhiParam(self.spectrum_id, self.order, self.chebyshevSpectrum.fix_c0, cheb, sigAmp, logAmp, l) self.update_Phi(phi) lnp = self.evaluate() self.logger.debug("Evaluated Phi parameters: {} {}".format(phi, lnp)) return lnp def rejectfn(): self.logger.debug("Calling Phi revertfn.") self.revert_Phi() self.sampler = StateSampler(lnfunc, self.p0, cov, query_lnprob=self.get_lnprob, rejectfn=rejectfn, debug=True) def update_Phi(self, phi): self.logger.debug("Updating nuisance parameters to {}".format(phi)) # Read off the Chebyshev parameters and update self.chebyshevSpectrum.update(phi.cheb) # Check to make sure the global covariance parameters make sense if phi.sigAmp < 0.1: raise C.ModelError("sigAmp shouldn't be lower than 0.1, something is wrong.") max_r = 6.0 * phi.l # [km/s] # Create a partial function which returns the proper element. k_func = make_k_func(phi) # Store the previous data matrix in case we want to revert later self.data_mat_last = self.data_mat self.data_mat = get_dense_C(self.wl, k_func=k_func, max_r=max_r) + phi.sigAmp*self.sigma_mat + self.region_mat def finish(self, *args): super().finish(*args) fname = Starfish.routdir + Starfish.specfmt.format(self.spectrum_id, self.order) + "/mc.hdf5" self.sampler.write(fname=fname)
class SampleThetaPhi(Order): def initialize(self, key): # Run through the standard initialization super().initialize(key) # for now, start with white noise self.data_mat = self.sigma_mat.copy() self.data_mat_last = self.data_mat.copy() #Set up p0 and the independent sampler fname = Starfish.specfmt.format(self.spectrum_id, self.order) + "phi.json" phi = PhiParam.load(fname) # Set the regions to None, since we don't want to include them even if they # are there phi.regions = None #Loading file that was previously output # Convert PhiParam object to an array self.p0 = phi.toarray() jump = Starfish.config["Phi_jump"] cheb_len = (self.npoly - 1) if self.chebyshevSpectrum.fix_c0 else self.npoly cov_arr = np.concatenate((Starfish.config["cheb_jump"]**2 * np.ones((cheb_len,)), np.array([jump["sigAmp"], jump["logAmp"], jump["l"]])**2 )) cov = np.diag(cov_arr) def lnfunc(p): # Convert p array into a PhiParam object ind = self.npoly if self.chebyshevSpectrum.fix_c0: ind -= 1 cheb = p[0:ind] sigAmp = p[ind] ind+=1 logAmp = p[ind] ind+=1 l = p[ind] par = PhiParam(self.spectrum_id, self.order, self.chebyshevSpectrum.fix_c0, cheb, sigAmp, logAmp, l) self.update_Phi(par) # sigAmp must be positive (this is effectively a prior) # See https://github.com/iancze/Starfish/issues/26 if not (0.0 < sigAmp): self.lnprob_last = self.lnprob lnp = -np.inf self.logger.debug("sigAmp was negative, returning -np.inf") self.lnprob = lnp # Same behavior as self.evaluate() else: lnp = self.evaluate() self.logger.debug("Evaluated Phi parameters: {} {}".format(par, lnp)) return lnp def rejectfn(): self.logger.debug("Calling Phi revertfn.") self.revert_Phi() self.sampler = StateSampler(lnfunc, self.p0, cov, query_lnprob=self.get_lnprob, rejectfn=rejectfn, debug=True) def update_Phi(self, p): self.logger.debug("Updating nuisance parameters to {}".format(p)) # Read off the Chebyshev parameters and update self.chebyshevSpectrum.update(p.cheb) # Check to make sure the global covariance parameters make sense #if p.sigAmp < 0.1: # raise C.ModelError("sigAmp shouldn't be lower than 0.1, something is wrong.") max_r = 6.0 * p.l # [km/s] # Create a partial function which returns the proper element. k_func = make_k_func(p) # Store the previous data matrix in case we want to revert later self.data_mat_last = self.data_mat self.data_mat = get_dense_C(self.wl, k_func=k_func, max_r=max_r) + p.sigAmp*self.sigma_mat def finish(self, *args): super().finish(*args) fname = Starfish.routdir + Starfish.specfmt.format(self.spectrum_id, self.order) + "/mc.hdf5" self.sampler.write(fname=fname)
print("No optimal jump matrix found, using diagonal jump matrix.") sampler = StateSampler(lnprob, p0, cov, query_lnprob=query_lnprob, acceptfn=acceptfn, rejectfn=rejectfn, debug=True, outdir=Starfish.routdir) p, lnprob, state = sampler.run_mcmc(p0, N=args.samples, incremental_save=args.incremental_save) print("Final", p) sampler.write() # Kill all of the orders for pconn in pconns.values(): pconn.send(("FINISH", None)) pconn.send(("DIE", None)) # Join on everything and terminate for p in ps.values(): p.join() p.terminate() import sys sys.exit()
class SampleThetaPhiLines(Order): def initialize(self, key): # Run through the standard initialization super().initialize(key) # for now, start with white noise self.data_mat = self.sigma_mat.copy() self.data_mat_last = self.data_mat.copy() #Set up p0 and the independent sampler fname = Starfish.specfmt.format(self.spectrum_id, self.order) + "phi.json" phi = PhiParam.load(fname) # print("Phi.regions", phi.regions) # import sys # sys.exit() # Get the regions matrix region_func = make_k_func_region(phi) max_r = 4.0 * np.max(phi.regions, axis=0)[2] self.region_mat = get_dense_C(self.wl, k_func=region_func, max_r=max_r) print(self.region_mat) # Then set phi to None phi.regions = None #Loading file that was previously output # Convert PhiParam object to an array self.p0 = phi.toarray() jump = Starfish.config["Phi_jump"] cheb_len = (self.npoly - 1) if self.chebyshevSpectrum.fix_c0 else self.npoly cov_arr = np.concatenate( (Starfish.config["cheb_jump"]**2 * np.ones((cheb_len, )), np.array([jump["sigAmp"], jump["logAmp"], jump["l"]])**2)) cov = np.diag(cov_arr) def lnfunc(p): # Convert p array into a PhiParam object ind = self.npoly if self.chebyshevSpectrum.fix_c0: ind -= 1 cheb = p[0:ind] sigAmp = p[ind] ind += 1 logAmp = p[ind] ind += 1 l = p[ind] phi = PhiParam(self.spectrum_id, self.order, self.chebyshevSpectrum.fix_c0, cheb, sigAmp, logAmp, l) self.update_Phi(phi) lnp = self.evaluate() self.logger.debug("Evaluated Phi parameters: {} {}".format( phi, lnp)) return lnp def rejectfn(): self.logger.debug("Calling Phi revertfn.") self.revert_Phi() self.sampler = StateSampler(lnfunc, self.p0, cov, query_lnprob=self.get_lnprob, rejectfn=rejectfn, debug=True) def update_Phi(self, phi): self.logger.debug("Updating nuisance parameters to {}".format(phi)) # Read off the Chebyshev parameters and update self.chebyshevSpectrum.update(phi.cheb) # Check to make sure the global covariance parameters make sense if phi.sigAmp < 0.1: raise C.ModelError( "sigAmp shouldn't be lower than 0.1, something is wrong.") max_r = 6.0 * phi.l # [km/s] # Create a partial function which returns the proper element. k_func = make_k_func(phi) # Store the previous data matrix in case we want to revert later self.data_mat_last = self.data_mat self.data_mat = get_dense_C( self.wl, k_func=k_func, max_r=max_r) + phi.sigAmp * self.sigma_mat + self.region_mat def finish(self, *args): super().finish(*args) fname = Starfish.routdir + Starfish.specfmt.format( self.spectrum_id, self.order) + "/mc.hdf5" self.sampler.write(fname=fname)
print("Calling rejectfn") for ((spectrum_id, order_id), pconn) in pconns.items(): pconn.send(("DECIDE", False)) from Starfish.samplers import StateSampler start = Starfish.config["Theta"] p0 = np.array(start["grid"] + [start["vz"], start["vsini"], start["logOmega"]]) jump = Starfish.config["Theta_jump"] cov = np.diag(np.array(jump["grid"] + [jump["vz"], jump["vsini"], jump["logOmega"]])**2) sampler = StateSampler(lnprob, p0, cov, query_lnprob=query_lnprob, acceptfn=acceptfn, rejectfn=rejectfn, debug=True, outdir=Starfish.routdir) p, lnprob, state = sampler.run_mcmc(p0, N=args.samples) print("Final", p) sampler.write() # Kill all of the orders for pconn in pconns.values(): pconn.send(("FINISH", None)) pconn.send(("DIE", None)) # Join on everything and terminate for p in ps.values(): p.join() p.terminate() import sys;sys.exit()