def plot_bestdatafits(self): """Plot best data fits from each chain and ever best, ignoring outliers.""" targets = Targets.JointTarget(targets=self.targets) fig, ax = targets.plot_obsdata(mod=False) thebestmodel = np.nan thebestmisfit = 1e15 thebestchain = np.nan modfiles = self.modfiles[1] for i, modfile in enumerate(modfiles): chainidx, _, _ = self._return_c_p_t(modfile) if chainidx in self.outliers: continue models = np.load(modfile) misfits = np.load(modfile.replace('models', 'misfits')).T[-1] bestmodel = models[np.argmin(misfits)] bestmisfit = misfits[np.argmin(misfits)] if bestmisfit < thebestmisfit: thebestmisfit = bestmisfit thebestmodel = bestmodel thebestchain = chainidx vp, vs, h = Model.get_vp_vs_h(bestmodel, self.priors['vpvs']) rho = vp * 0.32 + 0.77 for n, target in enumerate(targets.targets): xmod, ymod = target.moddata.plugin.run_model(h=h, vp=vp, vs=vs, rho=rho) if len(targets.targets) > 1: ax[n].plot(xmod, ymod, color='k', alpha=0.5, lw=0.7) else: ax.plot(xmod, ymod, color='k', alpha=0.5, lw=0.7) if len(targets.targets) > 1: ax[0].set_title('Best data fits from %d chains' % (len(modfiles) - self.outliers.size)) # idx = len(targets.targets) - 1 han, lab = ax[0].get_legend_handles_labels() handles, labels = self._unique_legend(han, lab) ax[0].legend().set_visible(False) else: ax.set_title('Best data fits from %d chains' % (len(modfiles) - self.outliers.size)) han, lab = ax.get_legend_handles_labels() handles, labels = self._unique_legend(han, lab) ax.legend().set_visible(False) fig.legend(handles, labels, loc='center left', bbox_to_anchor=(0.92, 0.5)) return fig
def plot_currentdatafits(self, nchains): """Plot the first nchains chains, no matter of outlier status. """ base = cm.get_cmap(name='rainbow') color_list = base(np.linspace(0, 1, nchains)) targets = Targets.JointTarget(targets=self.targets) fig, ax = targets.plot_obsdata(mod=False) for i, modfile in enumerate(self.modfiles[1][:nchains]): color = color_list[i] chainidx, _, _ = self._return_c_p_t(modfile) models = np.load(modfile) vpvs = np.load(modfile.replace('models', 'vpvs')).T currentvpvs = vpvs[-1] currentmodel = models[-1] vp, vs, h = Model.get_vp_vs_h(currentmodel, currentvpvs, self.mantle) rho = vp * 0.32 + 0.77 jmisfit = 0 for n, target in enumerate(targets.targets): xmod, ymod = target.moddata.plugin.run_model( h=h, vp=vp, vs=vs, rho=rho) yobs = target.obsdata.y misfit = target.valuation.get_rms(yobs, ymod) jmisfit += misfit label = '' if len(targets.targets) > 1: if ((len(targets.targets) - 1) - n) < 1e-2: label = 'c%d / %.3f' % (chainidx, jmisfit) ax[n].plot(xmod, ymod, color=color, alpha=0.7, lw=0.8, label=label) else: label = 'c%d / %.3f' % (chainidx, jmisfit) ax.plot(xmod, ymod, color=color, alpha=0.5, lw=0.7, label=label) if len(targets.targets) > 1: ax[0].set_title('Current data fits') idx = len(targets.targets) - 1 han, lab = ax[idx].get_legend_handles_labels() handles, labels = self._unique_legend(han, lab) ax[0].legend().set_visible(False) else: ax.set_title('Current data fits') han, lab = ax.get_legend_handles_labels() handles, labels = self._unique_legend(han, lab) ax.legend().set_visible(False) fig.legend(handles, labels, loc='center left', bbox_to_anchor=(0.92, 0.5)) return fig
# # ----------------------------------------------------------- DEFINE TARGETS # # Only pass x and y observed data to the Targets object which is matching # the data type. You can chose for SWD any combination of Rayleigh, Love, group # and phase velocity. Default is the fundamendal mode, but this can be updated. # For RF chose P or S. You can also use user defined targets or replace the # forward modeling plugin wih your own module. target1 = Targets.RayleighDispersionPhase(xsw, ysw) target2 = Targets.PReceiverFunction(xrf, yrf) target2.moddata.plugin.set_modelparams(gauss=1., water=0.01, p=6.4) # Join the targets. targets must be a list instance with all targets # you want to use for MCMC Bayesian inversion. targets = Targets.JointTarget(targets=[target1, target2]) # # --------------------------------------------------- Quick parameter update # # "priors" and "initparams" from config.ini are python dictionaries. You could # also simply define the dictionaries directly in the script, if you don't want # to use a config.ini file. Or update the dictionaries as follows, e.g. if you # have station specific values, etc. # See docs/bayhunter.pdf for explanation of parameters priors.update({ 'mohoest': (38, 4), # optional, moho estimate (mean, std) 'rfnoise_corr': 0.9, 'swdnoise_corr': 0. # 'rfnoise_sigma': np.std(yrf_err), # fixed to true value