示例#1
0
def makedatacoder(s96, which=Datacoder_log):
    if os.path.exists(s96):
        s = surf96reader(s96)
    else:
        s = surf96reader_from_surf96string(s96)

    waves, types, modes, freqs, values, dvalues = s.wtmfvd()
    return which(waves, types, modes, freqs, values, dvalues)
示例#2
0
    def __init__(self, fig=None, targetfile=None):
        if fig is None:
            self.fig = plt.figure(figsize=(12, 6))#figsize=(18, 10))
            self.fig.subplots_adjust(wspace=0.05)
        else:
            fig.clf()
            self.fig = fig

        self.axdepth = {}
        self.axdepth['VP'] = self.fig.add_subplot(1, 5, 1, title="$V_P\,(km/s)$", ylabel="depth (km)")
        self.axdepth['VS'] = self.fig.add_subplot(1, 5, 2, title="$V_S\,(km/s)$",sharey=self.axdepth['VP'])
        self.axdepth['PR'] = self.fig.add_subplot(1, 5, 3, title=r"$V_P/V_S$", sharey=self.axdepth['VP'])
        self.axdepth['RH'] = self.fig.add_subplot(1, 5, 4, title=r"$\rho\,(g/cm^3)$", sharey=self.axdepth['VP'])

        if targetfile is None:
            self.axdisp = {}
            self.axdisp['RU0'] = self.fig.add_subplot(4, 5, 5, title="RU0", ylabel="grpvel (km/s)")
            self.axdisp['RU1'] = self.fig.add_subplot(4, 5, 10, title="RU1", ylabel="grpvel (km/s)", sharex=self.axdisp['RU0'], sharey=self.axdisp['RU0'])
            self.axdisp['RC0'] = self.fig.add_subplot(4, 5, 15, title="RC0", ylabel="phsvel (km/s)", sharex=self.axdisp['RU0'], sharey=self.axdisp['RU0'])
            self.axdisp['RC1'] = self.fig.add_subplot(4, 5, 20, title="RC1", ylabel="phsvel (km/s)", sharex=self.axdisp['RU0'], sharey=self.axdisp['RU0'])
            for key, ax in self.axdisp.items():
                ax.loglog([1.],[1.]) #f**k matplotlib v2
                ax.yaxis.set_label_position("right")
                ax.yaxis.tick_right()
        else:
            #adjust the suplots according to the target data
            s = surf96reader(targetfile)
            Ndisp = max([4, len(list(s.wtm()))])

            self.axdisp = {}
            share = None
            for n, (w, t, m)  in enumerate(s.wtm()):
                ax = self.fig.add_subplot(Ndisp, 5, (n+1)*5,
                                          title="%s%s%d" % (w, t, m),
                                          sharex=share, sharey=share,
                                          #ylabel="velocity (km/s)")
                                          ylabel="%s (km/s)" % (["grpvel", "phsvel"][int(t=="C")]))
                ax.yaxis.set_label_position("right")
                ax.yaxis.tick_right()

                self.axdisp["%s%s%d" % (w.upper(), t.upper(), m)] = share = ax
                plt.setp(ax.get_xticklabels(), visible = False)
            plt.setp(ax.get_xticklabels(), visible=True)
            ax.set_xlabel('$period\,(s)$')

        pos = ax.get_position()
        self.cax = self.fig.add_axes((pos.x0, np.max([0.12, pos.y0 - 0.12]), pos.width, 0.01))

        plt.setp(self.axdepth['VS'].get_yticklabels(), visible=False)
        plt.setp(self.axdepth['PR'].get_yticklabels(), visible=False)
        plt.setp(self.axdepth['RH'].get_yticklabels(), visible=False)
        self.axconv = None #Not needed here

        if not self.axdepth['VP'].yaxis_inverted():
            self.axdepth['VP'].invert_yaxis()

        # initiate collections data
        self.clear_collections()
示例#3
0
    def __init__(self, fig=None, targetfile=None):
        if fig is None:
            self.fig = plt.figure(figsize=(12, 6))#figsize=(18, 10))
            self.fig.subplots_adjust(wspace=0.05)
        else:
            self.fig = fig

        self.axvp = self.fig.add_subplot(1, 5, 1, title="$V_P\,(km/s)$", ylabel="depth (km)")
        self.axvs = self.fig.add_subplot(1, 5, 2, title="$V_S\,(km/s)$", sharey=self.axvp)  # , sharex = self.axvp)
        self.axpr = self.fig.add_subplot(1, 5, 3, title=r"$V_P/V_S$", sharey=self.axvp)
        self.axrh = self.fig.add_subplot(1, 5, 4, title=r"$\rho\,(g/cm^3)$", sharey=self.axvp)

        if targetfile is None:
            self.axru0 = self.fig.add_subplot(4, 5, 5,  title="RU0", ylabel="grpvel (km/s)")
            self.axru1 = self.fig.add_subplot(4, 5, 10, title="RU1", ylabel="grpvel (km/s)", sharex=self.axru0, sharey=self.axru0)
            self.axrc0 = self.fig.add_subplot(4, 5, 15, title="RC0", ylabel="phsvel (km/s)", sharex=self.axru0, sharey=self.axru0)
            self.axrc1 = self.fig.add_subplot(4, 5, 20, title="RC1", ylabel="phsvel (km/s)", sharex=self.axru0, sharey=self.axru0)
            self.axdisp = [self.axru0, self.axru1, self.axrc0, self.axrc1]
            for ax in self.axdisp:
                ax.loglog([1.],[1.]) #f**k matplotlib v2
                ax.yaxis.set_label_position("right")
                ax.yaxis.tick_right()
        else:
            #adjust the suplots according to the target data
            s = surf96reader(targetfile)
            Ndisp = max([4, len(list(s.wtm()))])

            self.axdisp = []
            share = None
            for n, (w, t, m)  in enumerate(s.wtm()):
                ax = self.fig.add_subplot(Ndisp, 5, (n+1)*5,
                                          title="%s%s%d" % (w, t, m),
                                          sharex=share, sharey=share,
                                          #ylabel="velocity (km/s)")
                                          ylabel="%s (km/s)" % (["grpvel", "phsvel"][int(t=="C")]))
                ax.yaxis.set_label_position("right")
                ax.yaxis.tick_right()
                share=ax
                self.__setattr__("ax%s%s%d" % (w.lower(), t.lower(), m), ax)
                self.axdisp.append(eval("self.ax%s%s%d" % (w.lower(), t.lower(), m)))
                plt.setp(ax.get_xticklabels(), visible = False)
            plt.setp(ax.get_xticklabels(), visible=True)
            ax.set_xlabel('$period\,(s)$')

        pos = self.axdisp[-1].get_position()
        #self.cax = self.fig.add_axes((pos.x0, 0.12, pos.width, 0.01))
        self.cax = self.fig.add_axes((pos.x0, pos.y0 - 0.12, pos.width, 0.01))

        plt.setp(self.axvs.get_yticklabels(), visible=False)
        plt.setp(self.axpr.get_yticklabels(), visible=False)
        plt.setp(self.axrh.get_yticklabels(), visible=False)
        self.axconv = None #Not needed here

        self.axdepth = [self.axvp, self.axvs, self.axpr, self.axrh]
        if not self.axvp.yaxis_inverted():
            self.axvp.invert_yaxis()
示例#4
0
 def plots96(self, s96, **kwargs):
     s = surf96reader(s96)
     waves, types, modes, freqs, values, dvalues = s.wtmfvd()
     self.plotdisp(waves,
                   types,
                   modes,
                   freqs,
                   values,
                   dvalues=dvalues,
                   **kwargs)
示例#5
0
def makedatacoder(surf96filename, which=Datacoder_log):
    """
    :param surf96filename:
    :type surf96filename: str
    :param which: which datacoder to use
    :type which: type
    :return datacoder: a datacoder object initialized from the data found in surf96filename
    :rtype datacoder: DataCoder
    """
    if not isinstance(which, type):
        raise TypeError("which must be the class of the datacoder to use")

    if os.path.exists(surf96filename):
        s = surf96reader(surf96filename)
    else:
        s = surf96reader_from_surf96string(surf96filename)

    waves, types, modes, freqs, values, dvalues = s.wtmfvd()
    return which(waves, types, modes, freqs, values, dvalues)
示例#6
0
def target(argv, verbose):

    for k in argv.keys():
        if k in ['main', "_keyorder"]:
            continue  # private keys

        if k not in authorized_keys:
            raise Exception('option %s is not recognized' % k)

    # determine root names from target filess
    rootnames = []
    for s96 in argv['main']:
        rootname = "_HerrMet_" + s96.split('/')[-1].split('.s96')[0].split(
            '.surf96')[0]
        rootnames.append(rootname)
    assert len(np.unique(rootnames)) == len(
        rootnames)  # make sure all rootnames are distinct

    # handle already existing files
    if "-ot" not in argv.keys():
        for rootname in rootnames:
            if os.path.exists('%s/_HerrMet.target' % rootname):
                raise Exception(
                    'file %s/_HerrMet.target exists already, use -ot to overwrite'
                    % rootname)

    # loop over targets
    for rootname, s96 in zip(rootnames, argv['main']):

        # create directory
        if not os.path.isdir(rootname):
            os.mkdir(rootname)
            os.system('cp %s %s/%s.copy' % (s96, rootname, s96.split('/')[-1]))

        s = surf96reader(s96)
        # -------------------
        if "-resamp" in argv.keys():
            news = s.copy()
            news.clear()  # clear all entries
            newf = freqspace(freqmin=float(argv["-resamp"][0]),
                             freqmax=float(argv["-resamp"][1]),
                             nfreq=int(argv["-resamp"][2]),
                             scale=argv["-resamp"][3])
            for law in s.get_all():
                law.set_extrapolationmode(1)
                stdlaw = Claw(freq=law.freq,
                              value=law.dvalue,
                              extrapolationmode=0)

                newvalues = law(newf)
                newdvalues = stdlaw(newf)

                I = ~np.isnan(newvalues)
                if I.any():
                    N = I.sum()
                    news.data['WAVE'] = np.concatenate(
                        (news.data['WAVE'], np.array([law.wave]).repeat(N)))
                    news.data['TYPE'] = np.concatenate(
                        (news.data['TYPE'], np.array([law.type]).repeat(N)))
                    news.data['FLAG'] = np.concatenate(
                        (news.data['FLAG'], np.array([law.flag]).repeat(N)))
                    news.data['MODE'] = np.concatenate(
                        (news.data['MODE'], np.array([law.mode]).repeat(N)))
                    news.data['PERIOD'] = np.concatenate(
                        (news.data['PERIOD'], 1. / newf[I]))
                    news.data['VALUE'] = np.concatenate(
                        (news.data['VALUE'], newvalues[I]))
                    news.data['DVALUE'] = np.concatenate(
                        (news.data['DVALUE'], newdvalues[I]))

            s = news
            # print news
        # -------------------
        if "-lunc" in argv.keys():
            # set uncertainties to constant in log domain
            lunc = float(argv["-lunc"][0])
            s.data['DVALUE'] = s.data['VALUE'] * lunc
        elif "-unc" in argv.keys():
            # set uncertainties to constant in lin domain
            unc = float(argv["-unc"][0])
            s.data['DVALUE'] = np.ones(len(s.data['VALUE'])) * unc
        # -------------------
        if verbose:
            print "writing %s/_HerrMet.target" % rootname
        s.write96('%s/_HerrMet.target' % rootname)

    # -------------------
    print "please keep only datapoints to invert in */_HerrMet.target"
    print "use option --display to see the target data"
示例#7
0
    argv = readargv()
    # -----------------------------------
    if "inline" in argv.keys():
        showme = plt.show

    # -----------------------------------
    if "help" in argv.keys() or "h" in argv.keys():
        print help
        sys.exit()

    # -----------------------------------
    elif "show" in argv.keys():
        pmin, pmax = np.inf, -np.inf
        for f in argv['show']:
            s = surf96reader(f)
            print f
            for law in s.get_all():
                print "    %s" % str(law)
                law.show(gca(),
                         period=not "freq" in argv.keys(),
                         label="%s%s%d" % (law.wave, law.type, law.mode)
                         )  #alpha = 0.5, color = "r" if law.mode else "k")
                pmin = min([pmin, 1. / law.freq.max()])
                pmax = max([pmax, 1. / law.freq.min()])

        gca().set_xscale('log')
        gca().set_yscale('log')
        logtick(gca(), "xy")
        if not "freq" in argv.keys(): gca().set_xlim(pmin, pmax)
        else: gca().set_xlim(1. / pmax, 1. / pmin)
示例#8
0
def optimize(argv, verbose, mapkwargs):

    if '-h' in argv.keys() or "-help" in argv.keys():
        print(long_help)
        return

    check_keys(argv)

    # ===============================
    if "-temp" in argv.keys():
        template = OPTIMIZEPARAMTXT.format(
            nx=1,
            ny=1,
            newnz=1,
            dx=1.,
            dy=1.0,
            newdz=1.0,
            targetfiles=
            "./path/to/inversion/_HerrMet_node_iy{iy:03d}_ix{ix:03d}/_HerrMet.target",
            extractfiles=
            "./path/to/inversion/_HerrMet_node_iy{iy:03d}_ix{ix:03d}/_HerrMet.best_1000_0_1.p0.50.mod96",
            ndecim=1,
            Lh=1.0,
            Lv=0.5,
            vsunc=0.1,
            damping=1.0)

        if verbose:
            print(template)

        if os.path.isfile(HERRMETOPTIMIZEPARAMFILE):
            raise IOError(HERRMETOPTIMIZEPARAMFILE, ' exists already')

        with open(HERRMETOPTIMIZEPARAMFILE, 'w') as fid:
            fid.write(template)

        print('please customize {} and rerun this plugin'.format(
            HERRMETOPTIMIZEPARAMFILE))
        sys.exit(0)

    # ===============================
    if "-mprior" in argv.keys():

        if not os.path.isfile(HERRMETOPTIMIZEPARAMFILE):
            raise IOError(HERRMETOPTIMIZEPARAMFILE)

        # ==================== read the parameter file
        nx, ny, newnz, \
            dx, dy, newdz, ndecim, \
            Lh, Lv, vsunc, damping, \
            extractfiles, datafiles = \
            load_optimize_parameters()

        x = (np.arange(nx) * dx)[::ndecim]
        y = (np.arange(ny) * dy)[::ndecim]
        ixs = np.arange(nx)[::ndecim]  # indexs used to name the files
        iys = np.arange(ny)[::ndecim]
        ztop = np.arange(newnz) * newdz
        # WARNING : I update nx, ny
        del dx, dy  # to avoid confusion
        nz, ny, nx = len(ztop), len(y), len(x)

        # =============== load the results of the point wise inversion
        # config = z, y, x
        zmid = np.hstack((ztop[:-1] + 0.5 * (ztop[1:] - ztop[:-1]),
                          ztop[-1] + ztop[1] - ztop[0]))
        vs_cube = np.zeros((len(ztop), len(y), len(x)), float)
        vsunc_cube = np.zeros_like(vs_cube)

        for i, iy in enumerate(iys):
            for j, ix in enumerate(ixs):
                try:
                    m96file = extractfiles.format(iy=iy, ix=ix)
                    print('loading {}'.format(m96file))
                    dm = depthmodel_from_mod96(m96file)
                except Exception as e:
                    raise e

                vs_cube[:, i, j] = dm.vs.interp(z=zmid)
                vsunc_cube[:, i, j] = vsunc

        # ===============
        iz, jy, kx = np.mgrid[:nz, :ny, :nx]

        ixsflat = ixs[kx].flat[:]
        iysflat = iys[jy].flat[:]
        xflat = x[kx].flat[:]
        yflat = y[jy].flat[:]
        ztopflat = ztop[iz].flat[:]
        zmidflat = zmid[iz].flat[:]
        Mprior = vs_cube  # .flat[:]
        Munc = vsunc_cube  # .flat[:]

        xedges = np.hstack(
            (x - 0.5 * (x[1] - x[0]), x[-1] + 0.5 * (x[1] - x[0])))
        yedges = np.hstack(
            (y - 0.5 * (y[1] - y[0]), y[-1] + 0.5 * (y[1] - y[0])))
        zedges = np.hstack((ztop, ztop[-1] + 0.5 * (ztop[1] - ztop[0])))

        # ================== compute the parameterizers
        """
        construct parameterizers needed to define the theory function in each node
        """

        # write the header of the parameterizer for each node
        parameter_string_header = """
        #met NLAYER = {}
        #met TYPE = 'mZVSVPvsRHvp'
        #met VPvs = 'lambda VS: 0.9409+2.0947*VS-0.8206*VS**2+0.2683*VS**3-0.0251*VS**4'
        #met RHvp = 'lambda VP: 1.6612*VP-0.4721*VP**2+0.0671*VP**3-0.0043*VP**4+0.000106*VP**5'
        #fld KEY     VINF          VSUP
        #unt []      []            []
        #fmt %s      %f            %f
        """.format(len(ztop)).replace('    #', '#')

        for i in range(1, len(ztop)):
            # force VINF=VSUP => means lock the depth of the interfaces in the theory operator
            parameter_string_header += "-Z{} {} {}\n".format(
                i, -ztop[i], -ztop[i])  # add locked depth interfaces

        def job_generator():
            for iy in range(ny):
                for jx in range(nx):  # order matters!!!!
                    vs = Mprior[:, iy, jx]
                    yield Job(iy, jx, ztop, vs)

        def job_handler(iy, jx, ztop, vs):
            parameterizer_string = parameter_string_header

            for i in range(len(ztop)):
                # SET VINF < VS extracted from pointwise inv < VSUP
                # such as parameterizer.MMEAN corresponds to the extracted vs
                parameterizer_string += "VS{} {} {}\n".format(
                    i, vs[i] - 0.01, vs[i] + 0.01)
            # parameterizer = load_paramfile(parameter_string, verbose=False)[0]
            return iy, jx, parameterizer_string

        wb = None
        if verbose:
            wb = waitbarpipe('parameterizers')

        parameterizer_strings = []
        with MapSync(job_handler, job_generator(),
                     **mapkwargs) as ma:  # order matters!!!!
            for jobid, (iy, jx, parameter_string), _, _ in ma:
                parameterizer_strings.append(parameter_string)

                if verbose:
                    wb.refresh(jobid / float(nx * ny))

        if verbose:
            wb.close()

        parameterizer_strings = np.asarray(parameterizer_strings, str)

        np.savez(HERRMETOPTIMIZEPRIORFILE,
                 nz=nz,
                 ny=ny,
                 nx=nx,
                 x=x,
                 xedges=xedges,
                 xflat=xflat,
                 ixs=ixs,
                 ixsflat=ixsflat,
                 y=y,
                 yedges=yedges,
                 yflat=yflat,
                 iys=iys,
                 iysflat=iysflat,
                 ztop=ztop,
                 zmid=zmid,
                 zedges=zedges,
                 ztopflat=ztopflat,
                 zmidflat=zmidflat,
                 Mprior=Mprior,
                 Munc=Munc,
                 damping=damping,
                 Lv=Lv,
                 Lh=Lh,
                 parameterizer_strings=parameterizer_strings)

    # ===============================
    if "-dobs" in argv.keys():

        # ==================== read the parameter file
        nx, ny, newnz, \
            dx, dy, newdz, ndecim, \
            Lh, Lv, vsunc, damping, \
            extractfiles, datafiles = \
            load_optimize_parameters()

        # x = (np.arange(nx) * dx)[::ndecim]
        # y = (np.arange(ny) * dy)[::ndecim]
        ixs = np.arange(nx)[::ndecim]  # indexs used to name the files
        iys = np.arange(ny)[::ndecim]

        datacoder_strings = []
        for i, iy in enumerate(iys):
            for j, ix in enumerate(ixs):  # order matters!!!!

                try:
                    datafile = datafiles.format(iy=iy, ix=ix)
                    if verbose:
                        print('loading ', datafile)
                    s96 = surf96reader(filename=datafile)

                except Exception as e:
                    raise e

                datacoder_string = str(s96)
                datacoder_strings.append(datacoder_string)

        datacoder_strings = np.asarray(datacoder_strings, str)
        datacoders = [
            makedatacoder(datacoder_string, which=Datacoder_log)
            for datacoder_string in datacoder_strings
        ]

        Nobs = []
        Waves = []
        Types = []
        Modes = []
        Freqs = []
        Dobs = []
        Dunc = []
        for datacoder in datacoders:
            _dobs, _CDinv = datacoder.target()
            Nobs.append(len(datacoder.waves))
            Waves.append(datacoder.waves)
            Types.append(datacoder.types)
            Modes.append(datacoder.modes)
            Freqs.append(datacoder.freqs)
            Dobs.append(_dobs)
            Dunc.append(_CDinv**-0.5)

        Nobs = np.array(Nobs)
        Waves = np.hstack(Waves)
        Types = np.hstack(Types)
        Modes = np.hstack(Modes)
        Freqs = np.hstack(Freqs)
        Dobs = np.hstack(Dobs)
        Dunc = np.hstack(Dunc)

        np.savez(HERRMETOPTIMIZEDOBSFILE,
                 datacoder_strings=datacoder_strings,
                 Nobs=Nobs,
                 Waves=Waves,
                 Types=Types,
                 Modes=Modes,
                 Freqs=Freqs,
                 Dobs=Dobs,
                 Dunc=Dunc)

    # ===============================
    if "-inv" in argv.keys():

        # ========== Load inputs
        with np.load(HERRMETOPTIMIZEPRIORFILE) as loader:
            x = loader['x']
            y = loader['y']
            # xedges = loader['xedges']
            # yedges = loader['yedges']
            # zedges = loader['zedges']
            # ixs = loader['ixs']
            # iys = loader['iys']
            zmid = loader['zmid']
            xflat = loader['xflat']
            yflat = loader['yflat']
            zflat = zmidflat = loader['zmidflat']
            Lh = loader['Lh']
            Lv = loader['Lv']
            Mprior = loader['Mprior'].flat[:]
            Munc = loader['Munc'].flat[:]
            damping = loader['damping']
            parameterizer_strings = loader['parameterizer_strings']

        with np.load(HERRMETOPTIMIZEDOBSFILE) as loader:
            Nobs = loader['Nobs']
            # Waves = loader['Waves']
            # Types = loader['Types']
            # Modes = loader['Modes']
            # Freqs = loader['Freqs']
            Dobs = loader['Dobs']
            Dunc = loader['Dunc']
            datacoder_strings = loader['datacoder_strings']

        nx, ny, nz = len(x), len(y), len(zmid)
        nobs = len(Dobs)

        # save the grid
        # add_to_npz(HERRMETOPTIMIZEINVFILE,
        #            # 3D grid needed for display
        #            nx=nx, ny=ny, nz=nz,
        #            xedges=xedges, yedges=yedges, zedges=zedges,  # edge grid points (for pcolormesh)
        #            x=x, y=y, zmid=zmid,  # mid grid points
        #            ixs=ixs, iys=iys,  # indexs of the point wise inversion used
        #            Nobs=Nobs)   # number of disp point per node (array) flatten in the horizontal plane

        Munc *= np.sqrt(float(len(Mprior)) / float(
            len(Dobs))) / damping  # equilibrate the costs, apply damping here

        CD = sp.diags(Dunc**2., format="csc", shape=(len(Dobs), len(Dobs)))
        CDinvdiag = Dunc**-2.

        smoother = ModelSmoother(x=x,
                                 y=y,
                                 zmid=zmid,
                                 xflat=xflat,
                                 yflat=yflat,
                                 zmidflat=zflat,
                                 Lh=Lh,
                                 Lv=Lv)

        CM = ModelCovarianceMatrix(Munc=Munc,
                                   x=x,
                                   y=y,
                                   zmid=zmid,
                                   xflat=xflat,
                                   yflat=yflat,
                                   zmidflat=zflat,
                                   Lh=Lh,
                                   Lv=Lv)

        g = ForwardOperator(parameterizer_strings=parameterizer_strings,
                            datacoder_strings=datacoder_strings,
                            nx=nx,
                            ny=ny,
                            nz=nz,
                            Nobs=Nobs,
                            **mapkwargs)

        Dprior = g(Mprior)
        add_to_npz(HERRMETOPTIMIZEINVFILE, Dprior=Dprior)

        M0 = smoother.dot(Mprior, trunc=4.)

        D0 = g(M0)
        add_to_npz(HERRMETOPTIMIZEINVFILE, M0=M0.reshape((nz, ny, nx)), D0=D0)

        MI = M0.copy()
        DI = D0.copy()

        with Timer('CM_sparse'):
            CM_sparse = CM.sparse(trunc=4.)
        with Timer('CM_lu'):
            CM_sparse_lu = splinalg.splu(CM_sparse)

        G = None
        data_costs = [0.5 * (CDinvdiag * (Dobs - DI)**2.0).sum()]
        model_costs = [
            0.
        ]  #0.5 * (CM_sparse_lu.solve(MI - M0) * (MI - M0)).sum()
        for i in range(20):
            if G is None or True:
                G = g.frechet_derivatives(MI)
                with Timer('G * CM * GT + CD'):
                    Ksparse = G * CM_sparse * G.T + CD

                with Timer('splu(G * CM * GT + CD)'):
                    Klu = splinalg.splu(Ksparse)

            XI = Dobs - DI + G * (MI - M0)
            DM = CM.dot(G.T * Klu.solve(XI))
            DM = (M0 - MI + DM)

            if np.abs(DM).max() > 0.25:
                warnings.warn(str(np.abs(DM).max()))
                DM *= 0.25 / np.abs(DM).max()

            print("max correction:", np.abs(DM).max())
            MI = MI + DM
            DI = g(MI)

            # ============== costs
            chi2_data = 0.5 * (CDinvdiag * (Dobs - DI)**2.0).sum()
            chi2_model = 0.5 * (
                CM_sparse_lu.solve(MI - M0) *
                (MI - M0)).sum()  # fails if I use Mprior instead of M0
            if chi2_model < 0 or chi2_data < 0:
                raise ValueError(
                    chi2_model,
                    chi2_data)  # may happen after numerical instability

            print('data cost: ', chi2_data)
            print('model cost: ', chi2_model)
            print('total cost: ', chi2_data + chi2_model)

            data_costs.append(chi2_data)
            model_costs.append(chi2_model)

            add_to_npz(HERRMETOPTIMIZEINVFILE,
                       data_costs=np.asarray(data_costs),
                       model_costs=np.asarray(model_costs),
                       total_costs=np.asarray(model_costs) +
                       np.asarray(data_costs),
                       Msol=MI.reshape((nz, ny, nx)),
                       Dsol=DI)

            if np.abs(DM).max() <= 0.01:
                print('convergence achieved')
                break

    # ===============================
    if "-show" in argv.keys():
        import matplotlib.pyplot as plt

        with np.load(HERRMETOPTIMIZEPRIORFILE) as loader:
            x = loader['x']
            y = loader['y']
            zmid = loader['zmid']
            xedges = loader['xedges']
            yedges = loader['yedges']
            zedges = loader['zedges']
            vs_prior = loader['Mprior']

        with np.load(HERRMETOPTIMIZEINVFILE) as loader:
            vs_0 = loader['M0']
            vs_sol = loader['Msol']
            data_costs = loader['data_costs']
            model_costs = loader['model_costs']
            total_costs = loader['total_costs']

        plt.figure()

        plt.plot(data_costs, 'bs-', label="$ \chi_D^2 $"
                 )  #0.5 * (CDinvdiag * (Dobs - DI) ** 2.0).sum()
        plt.semilogy(
            model_costs, 'gs-', label="$ \chi_M^2 $"
        )  ##0.5 * (CM_sparse_lu.solve(MI - M0) * (MI - M0)).sum()  # fails if I use Mprior instead of M0
        plt.plot(total_costs, 'ro-', label="$ \chi_D^2 + chi_M^2 $")
        plt.gca().set_xlabel('iteration number')
        plt.gca().grid(True, linestyle=":")
        plt.gca().set_ylabel(r'$ \chi $')
        plt.gca().legend()

        if argv['-show'][0] == "z":
            zslice = argv['-show'][1]
            iz = np.argmin(abs(zmid - zslice))

            fig = plt.figure(figsize=(12, 4))
            ax1 = plt.subplot(131,
                              title="$ Vs^{prior} $",
                              aspect=1.0,
                              xlabel="x(km)",
                              ylabel="y(km)")
            ax2 = plt.subplot(132,
                              title="$ Vs^{0} $",
                              aspect=1.0,
                              xlabel="x(km)",
                              ylabel="y(km)",
                              sharex=ax1,
                              sharey=ax1)
            ax3 = plt.subplot(133,
                              title="$ Vs^{sol} $",
                              aspect=1.0,
                              xlabel="x(km)",
                              ylabel="y(km)",
                              sharex=ax1,
                              sharey=ax1)
            fig.suptitle('depth : {:.2f}km'.format(zmid[iz]))
            cax = fig.add_axes((0.95, 0.3, 0.012, 0.3))

            vsmin = min([vs[iz, ...].min() for vs in [vs_prior, vs_0, vs_sol]])
            vsmax = min([vs[iz, ...].max() for vs in [vs_prior, vs_0, vs_sol]])
            for ax, vs in zip([ax1, ax2, ax3], [vs_prior, vs_0, vs_sol]):
                coll = ax.pcolormesh(xedges,
                                     yedges,
                                     vs[iz, ...],
                                     vmin=vsmin,
                                     vmax=vsmax,
                                     cmap=plt.get_cmap('jet_r'))

            plt.colorbar(coll, cax=cax)
            plt.ion()
            plt.show()
            input('pause')

        elif argv['-show'][0] == "x":
            xslice = argv['-show'][1]
            ix = np.argmin(abs(x - xslice))

            fig = plt.figure(figsize=(12, 4))
            ax1 = plt.subplot(131,
                              title="$ Vs^{prior} $",
                              xlabel="y(km)",
                              ylabel="z(km)")
            ax2 = plt.subplot(132,
                              title="$ Vs^{0} $",
                              xlabel="y(km)",
                              ylabel="z(km)",
                              sharex=ax1,
                              sharey=ax1)
            ax3 = plt.subplot(133,
                              title="$ Vs^{sol} $",
                              xlabel="y(km)",
                              ylabel="z(km)",
                              sharex=ax1,
                              sharey=ax1)
            for ax in ax1, ax2, ax3:
                ax.invert_yaxis()
            fig.suptitle('x = {:.2f}km'.format(x[ix]))
            cax = fig.add_axes((0.95, 0.3, 0.012, 0.3))

            vsmin = min([vs[..., ix].min() for vs in [vs_prior, vs_0, vs_sol]])
            vsmax = min([vs[..., ix].max() for vs in [vs_prior, vs_0, vs_sol]])
            for ax, vs in zip([ax1, ax2, ax3], [vs_prior, vs_0, vs_sol]):
                coll = ax.pcolormesh(yedges,
                                     zedges,
                                     vs[..., ix],
                                     vmin=vsmin,
                                     vmax=vsmax,
                                     cmap=plt.get_cmap('jet_r'))

            plt.colorbar(coll, cax=cax)
            plt.ion()
            plt.show()
            input('pause')

        elif argv['-show'][0] == "y":
            yslice = argv['-show'][1]
            iy = np.argmin(abs(y - yslice))

            fig = plt.figure(figsize=(12, 4))
            ax1 = plt.subplot(131,
                              title="$ Vs^{prior} $",
                              xlabel="y(km)",
                              ylabel="z(km)")
            ax2 = plt.subplot(132,
                              title="$ Vs^{0} $",
                              xlabel="y(km)",
                              ylabel="z(km)",
                              sharex=ax1,
                              sharey=ax1)
            ax3 = plt.subplot(133,
                              title="$ Vs^{sol} $",
                              xlabel="y(km)",
                              ylabel="z(km)",
                              sharex=ax1,
                              sharey=ax1)
            for ax in ax1, ax2, ax3:
                ax.invert_yaxis()
            fig.suptitle('y = {:.2f}km'.format(y[iy]))
            cax = fig.add_axes((0.95, 0.3, 0.012, 0.3))

            vsmin = min(
                [vs[:, iy, :].min() for vs in [vs_prior, vs_0, vs_sol]])
            vsmax = min(
                [vs[:, iy, :].max() for vs in [vs_prior, vs_0, vs_sol]])
            for ax, vs in zip([ax1, ax2, ax3], [vs_prior, vs_0, vs_sol]):
                coll = ax.pcolormesh(xedges,
                                     zedges,
                                     vs[:, iy, :],
                                     vmin=vsmin,
                                     vmax=vsmax,
                                     cmap=plt.get_cmap('jet_r'))

            plt.colorbar(coll, cax=cax)
            plt.ion()
            plt.show()
            input('pause')
示例#9
0
def target(argv, verbose):

    if '-h' in argv.keys() or "-help" in argv.keys():
        print(long_help)
        return

    for k in argv.keys():
        if k in ['main', "_keyorder"]:
            continue  # private keys

        if k not in authorized_keys:
            raise Exception('option %s is not recognized' % k)

    # determine root names from target filess
    rootnames = []
    for s96 in argv['main']:
        rootname = ROOTNAME.format(node=surf96filename_to_nodename(s96))
        rootnames.append(rootname)
    assert len(np.unique(rootnames)) == len(
        rootnames)  # make sure all rootnames are distinct

    # handle already existing files
    if "-ot" not in argv.keys():
        for rootname in rootnames:
            target_file = HERRMETTARGETFILE.format(rootname=rootname)
            if os.path.exists(target_file):
                raise Exception(
                    'file {} exists already, use -ot to overwrite' %
                    target_file)

    # loop over targets
    for rootname, s96 in zip(rootnames, argv['main']):
        target_file = HERRMETTARGETFILE.format(rootname=rootname)
        target_dir = os.path.dirname(target_file)

        # create directory
        if not os.path.isdir(target_dir):
            if verbose:
                print("creating " + target_dir)
            os.makedirs(target_dir)
            s96copy = os.path.join(target_dir, os.path.basename(s96)) + ".copy"
            cpcmd = 'cp {} {}'.format(s96, s96copy)
            if verbose:
                print(cpcmd)
            os.system(cpcmd)

        s = surf96reader(s96)

        if "-resamp" in argv.keys():
            news = s.copy()
            news.clear()  # clear all entries
            newf = freqspace(freqmin=float(argv["-resamp"][0]),
                             freqmax=float(argv["-resamp"][1]),
                             nfreq=int(argv["-resamp"][2]),
                             scale=argv["-resamp"][3])
            for law in s.get_all():
                law.set_extrapolationmode(1)
                stdlaw = Claw(freq=law.freq,
                              value=law.dvalue,
                              extrapolationmode=0)

                newvalues = law(newf)
                newdvalues = stdlaw(newf)

                I = ~np.isnan(newvalues)
                if I.any():
                    N = I.sum()
                    news.data['WAVE'] = np.concatenate(
                        (news.data['WAVE'], np.array([law.wave]).repeat(N)))
                    news.data['TYPE'] = np.concatenate(
                        (news.data['TYPE'], np.array([law.type]).repeat(N)))
                    news.data['FLAG'] = np.concatenate(
                        (news.data['FLAG'], np.array([law.flag]).repeat(N)))
                    news.data['MODE'] = np.concatenate(
                        (news.data['MODE'], np.array([law.mode]).repeat(N)))
                    news.data['PERIOD'] = np.concatenate(
                        (news.data['PERIOD'], 1. / newf[I]))
                    news.data['VALUE'] = np.concatenate(
                        (news.data['VALUE'], newvalues[I]))
                    news.data['DVALUE'] = np.concatenate(
                        (news.data['DVALUE'], newdvalues[I]))

            s = news
            # print news

        if "-lunc" in argv.keys():
            # set uncertainties to constant in log domain
            lunc = float(argv["-lunc"][0])
            s.data['DVALUE'] = s.data['VALUE'] * lunc

        elif "-unc" in argv.keys():
            # set uncertainties to constant in lin domain
            unc = float(argv["-unc"][0])
            s.data['DVALUE'] = np.ones(len(s.data['VALUE'])) * unc

        if verbose:
            print("writing " + target_file)
        s.write96(target_file)

    # -------------------
    print("please keep only datapoints to invert in " +
          HERRMETTARGETFILE.format(rootname="*"))
    print("use option --display to see the target data")