Ejemplo n.º 1
0
    def return_swddata(h, vs, vpvs=1.73, pars=dict(), x=None):
        """Return dictionary of forward modeled data based on Surf96."""
        if x is None:
            x = np.linspace(1, 40, 20)

        h = np.array(h)
        vs = np.array(vs)

        mode = pars.get('mode', 1)  # fundamental mode

        target1 = Targets.RayleighDispersionPhase(x=x, y=None)
        target1.moddata.plugin.set_modelparams(mode=mode)
        target2 = Targets.RayleighDispersionGroup(x=x, y=None)
        target2.moddata.plugin.set_modelparams(mode=mode)
        target3 = Targets.LoveDispersionPhase(x=x, y=None)
        target3.moddata.plugin.set_modelparams(mode=mode)
        target4 = Targets.LoveDispersionGroup(x=x, y=None)
        target4.moddata.plugin.set_modelparams(mode=mode)
        vp = vs * vpvs
        rho = vp * 0.32 + 0.77

        targets = [target1, target2, target3, target4]

        data = {}
        for i, target in enumerate(targets):
            xmod, ymod = target.moddata.plugin.run_model(
                h=h, vp=vp, vs=vs, rho=rho)
            data[target.ref] = np.array([xmod, ymod])
        logger.info('Compute SWD for %d periods, with model vp/vs %.2f.'
                    % (x.size, vpvs))
        return data
Ejemplo n.º 2
0
    def return_rfdata(h, vs, vpvs=1.73, pars=dict(), x=None):
        """Return dictionary of forward modeled data based on RFMini.
        - x must be linspace to provide an equal sampling rate
        - pars is a dictionary of additional parameters used for RF
        computation (uses defaults if empty), such as:
            - gauss: Gaussian factor (low pass filter),
            - water: water level,
            - p: slowness in s/deg
            - nsv: near surface velocity (km/s) for RF rotation angle.
        """
        if x is None:
            x = np.linspace(-5, 35, 201)

        h = np.array(h)
        vs = np.array(vs)

        gauss = pars.get('gauss', 1.0)
        water = pars.get('water', 0.001)
        p = pars.get('p', 6.4)
        nsv = pars.get('nsv', None)

        target5 = Targets.PReceiverFunction(x=x, y=None)
        target5.moddata.plugin.set_modelparams(gauss=gauss,
                                               water=water,
                                               p=p,
                                               nsv=nsv)
        target6 = Targets.SReceiverFunction(x=x, y=None)
        target6.moddata.plugin.set_modelparams(gauss=gauss,
                                               water=water,
                                               p=p,
                                               nsv=nsv)

        vp = vs * vpvs
        rho = vp * 0.32 + 0.77

        targets = [target5, target6]

        data = {}
        for i, target in enumerate(targets):
            xmod, ymod = target.moddata.plugin.run_model(h=h,
                                                         vp=vp,
                                                         vs=vs,
                                                         rho=rho)
            data[target.ref] = np.array([xmod, ymod])

        logger.info('Compute RF with gauss: %.2f, waterlevel: ' % gauss +
                    '%.4f, slowness: %.2f' % (water, p))
        return data
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    def save_model(h, vs, vpvs=1.73, outfile=None):
        """Save input model as ASCII file."""
        h = np.array(h)
        vs = np.array(vs)

        vp = vs * vpvs
        rho = vp * 0.32 + 0.77

        if outfile is None:
            outfile = 'syn_mod.dat'

        x = np.arange(10)
        target = Targets.PReceiverFunction(x=x, y=None)
        target.moddata.plugin.write_startmodel(h, vp, vs, rho, outfile)
        logger.info('Model file saved: %s' % outfile)
Ejemplo n.º 6
0
    'nlays': 3,
    'noise': truenoise,
    'explike': explike,
}

print truenoise, explike

#
#  -----------------------------------------------------------  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