def write_halfchains(self, burn, name1="halfchain1.txt", name2="halfchain2.txt"): post = tb.Column(self.post, name="post") samp = copy.deepcopy(self.samples) samp.add_column(post) n = len(self.samples) np.savetxt(name1, np.array(samp[:n/2]).T, header=self.header, comments="") np.savetxt(name2, np.array(samp[n/2:]).T, header=self.header, comments="") print "Saving half chains to %s %s"%(name1, name2) del(samp)
def write_halfchains(self, name1="halfchain1.txt", name2="halfchain2.txt"): post = tb.Column(self.post, name="post") samp = copy.deepcopy(self.samples) samp.add_column(post) n = len(self.samples) np.savetxt(name1, np.array(samp[:n/2]).T, header=self.header, comments="") np.savetxt(name2, np.array(samp[n/2:]).T, header=self.header, comments="") print("Saving half chains to %s %s"%(name1, name2)) del(samp)
def write_columns(self, filename=None, overwrite=False, bounds=True, impweight=False, apply_weights=False, halfchains=False): if not filename and overwrite: filename = self.filename print("Overwriting file %s"%self.filename) if bounds: sel = np.array(self.mask).astype(bool) else: sel = np.ones_like(self.post).astype(bool) samp = copy.deepcopy(self.samples) nsamp=len(self.samples) if impweight: print("Using importance sampled posterior ",np.array(self.wt)) nsamp, i, samp = self.do_importance_resampling() print(i) post=np.array(self.post)[i] sel=np.ones_like(post).astype(bool) elif apply_weights: print("Applying weights to posterior ",self.wt) post=self.post*self.wt else: post=self.post post = tb.Column(post, name="post") samp.add_column(post) if self.has_wt: weight = tb.Column(self.weight, name="weight") samp.add_column(weight) np.savetxt(filename, np.array(samp[sel]).T, header=self.header, comments="") if halfchains: import os base=os.path.basename(filename) dirname=os.path.dirname(filename) h1=os.path.join(dirname,"h1_"+base) h2=os.path.join(dirname,"h2_"+base) self.write_halfchains(name1=h1,name2=h2) del(samp) return nsamp
def write_columns(self, filename=None, overwrite=False, bounds=True, impweight=False, apply_weights=False, halfchains=False): if not filename and overwrite: filename = self.filename print "Overwriting file %s"%self.filename if bounds: sel = np.array(self.mask).astype(bool) else: sel = np.ones_like(self.post).astype(bool) samp = copy.deepcopy(self.samples) nsamp=len(self.samples) if impweight: print "Using importance sampled posterior ",np.array(self.wt) nsamp, i, samp = self.do_importance_resampling() print i post=np.array(self.post)[i] sel=np.ones_like(post).astype(bool) elif apply_weights: print "Applying weights to posterior ",self.wt post=self.post*self.wt else: post=self.post post = tb.Column(post, name="post") samp.add_column(post) np.savetxt(filename, np.array(samp[sel]).T, header=self.header, comments="") if halfchains: import os base=os.path.basename(filename) dirname=os.path.dirname(filename) h1=os.path.join(dirname,"h1_"+base) h2=os.path.join(dirname,"h2_"+base) self.write_halfchains(0,name1=h1,name2=h2) del(samp) return nsamp