Ejemplo n.º 1
0
# Setup Inversion
inv = inversion.BaseInversion(
    invProb,
    directiveList=[
        alphas, scales, beta, petrodir, targets, betaIt, scaling_schedule
    ],
)

mcluster_map = inv.run(minit)

# Inversion with no nonlinear mapping
reg_simple_no_map = utils.make_SimplePGI_regularization(
    mesh=mesh,
    gmmref=clfnomapping,
    gmm=clfnomapping,
    approx_gradient=True,
    alpha_x=1.0,
    wiresmap=wires,
    cell_weights_list=[wr1, wr2],
)

opt = optimization.ProjectedGNCG(
    maxIter=20,
    tolX=1e-6,
    maxIterCG=100,
    tolCG=1e-3,
    lower=-10,
    upper=10,
)

invProb = inverse_problem.BaseInvProblem(dmis, reg_simple_no_map, opt)
Ejemplo n.º 2
0
wr_grav = np.sum(simulation_grav.G**2.0, axis=0)**0.5
wr_grav = wr_grav / np.max(wr_grav)

wr_mag = np.sum(simulation_mag.G**2.0, axis=0)**0.5
wr_mag = wr_mag / np.max(wr_mag)

# create joint PGI regularization with smoothness
reg = utils.make_SimplePGI_regularization(
    gmmref=gmmref,
    mesh=mesh,
    wiresmap=wires,
    maplist=[idenMap, idenMap],
    mref=m0,
    indActive=actv,
    alpha_s=1.0,
    alpha_x=1.0,
    alpha_y=1.0,
    alpha_z=1.0,
    alpha_xx=0.0,
    alpha_yy=0.0,
    alpha_zz=0.0,
    cell_weights_list=[wr_grav,
                       wr_mag],  # weights each phys. prop. by each sensW
)

# Directives
# Add directives to the inversion
# ratio to use for each phys prop. smoothness in each direction:
# roughly the ratio of range of each phys. prop.
alpha0_ratio = np.r_[np.zeros(len(reg.objfcts[0].objfcts)),
                     1e-2 * np.ones(len(reg.objfcts[1].objfcts)),
Ejemplo n.º 3
0
    def test_spherical_covariances(self):

        print("Test Spherical covariances: ")
        print("============================")
        # Fit a Gaussian Mixture
        clf = WeightedGaussianMixture(
            mesh=self.mesh,
            n_components=self.n_components,
            covariance_type="spherical",
            max_iter=1000,
            n_init=10,
            tol=1e-8,
            means_init=self.means,
            warm_start=True,
            weights_init=self.proportions,
        )
        clf.fit(self.samples)

        # Define reg Simple
        reg_simple = make_SimplePGI_regularization(
            mesh=self.mesh,
            gmmref=clf,
            approx_gradient=True,
            alpha_x=0.0,
            wiresmap=self.wires,
            cell_weights_list=self.cell_weights_list,
        )
        # Define reg with volumes
        reg = make_PGI_regularization(
            mesh=self.mesh,
            gmmref=clf,
            approx_gradient=True,
            alpha_x=0.0,
            wiresmap=self.wires,
            cell_weights_list=self.cell_weights_list,
        )

        mref = mkvc(clf.means_[clf.predict(self.samples)])

        # check score value
        score_approx0 = reg_simple(self.model)
        dm = self.model - mref
        score_approx1 = 0.5 * dm.dot(reg_simple.deriv2(self.model, dm))
        passed_score_approx_simple = np.isclose(score_approx0, score_approx1)
        self.assertTrue(passed_score_approx_simple)
        reg_simple.objfcts[0].approx_eval = False
        score = reg_simple(self.model) - reg_simple(mref)
        passed_score_simple = np.allclose(score_approx0, score, rtol=1e-1)
        self.assertTrue(passed_score_simple)
        print("scores for SimplePGI & spherical Cov. are ok.")

        score_approx0 = reg(self.model)
        score_approx1 = 0.5 * dm.dot(reg.deriv2(self.model, dm))
        passed_score_approx = np.allclose(score_approx0, score_approx1)
        self.assertTrue(passed_score_approx)
        reg.objfcts[0].approx_eval = False
        score = reg(self.model) - reg(mref)
        passed_score = np.allclose(score_approx0, score, rtol=1e-1)
        self.assertTrue(passed_score)
        print("scores for PGI & spherical Cov. are ok.")

        # check derivatives as an optimization on locally quadratic function
        # Simple

        deriv_simple = reg_simple.deriv(self.model)
        reg_simple.objfcts[0].approx_gradient = False
        deriv_simple_full = reg_simple.deriv(self.model)
        passed_deriv1 = np.allclose(deriv_simple, deriv_simple_full, rtol=1e-1)
        self.assertTrue(passed_deriv1)
        print("1st derivatives for SimplePGI & spherical Cov. are ok.")

        Hinv = SolverLU(reg_simple.deriv2(self.model))
        p_simple = Hinv * deriv_simple
        direction2_simple = np.c_[self.wires * p_simple]
        passed_derivative_simple = np.allclose(mkvc(self.samples -
                                                    direction2_simple),
                                               mkvc(mref),
                                               rtol=1e-1)
        self.assertTrue(passed_derivative_simple)
        print("2nd derivatives for SimplePGI & spherical Cov. are ok.")

        # With volumes
        deriv = reg.deriv(self.model)
        reg.objfcts[0].approx_gradient = False
        deriv_full = reg.deriv(self.model)
        passed_deriv1 = np.allclose(deriv, deriv_full, rtol=1e-1)
        self.assertTrue(passed_deriv1)
        print("1st derivatives for PGI & spherical Cov. are ok.")

        Hinv = SolverLU(reg.deriv2(self.model))
        p = Hinv * deriv
        direction2 = np.c_[self.wires * p]
        passed_derivative = np.allclose(mkvc(self.samples - direction2),
                                        mkvc(mref),
                                        rtol=1e-1)
        self.assertTrue(passed_derivative)
        print("2nd derivatives for PGI & spherical Cov. are ok.")

        if self.PlotIt:
            import matplotlib.pyplot as plt

            xmin, xmax = ymin, ymax = self.samples.min(), self.samples.max()
            x, y = np.mgrid[xmin:xmax:0.01, ymin:ymax:0.01]
            pos = np.empty(x.shape + (2, ))
            pos[:, :, 0] = x
            pos[:, :, 1] = y
            rv = clf.score_samples(pos.reshape(-1, 2))
            rvm = clf.predict(pos.reshape(-1, 2))
            figspherical, axspherical = plt.subplots(1, 2, figsize=(16, 8))
            figspherical.suptitle("Spherical Covariances Tests")
            # Simple
            axspherical[0].contourf(x,
                                    y,
                                    rvm.reshape(x.shape),
                                    alpha=0.25,
                                    cmap="brg")
            axspherical[0].contour(x, y, rv.reshape(x.shape), 20)
            axspherical[0].scatter(self.s0[:, 0],
                                   self.s0[:, 1],
                                   color="blue",
                                   s=5.0,
                                   alpha=0.25)
            axspherical[0].scatter(self.s1[:, 0],
                                   self.s1[:, 1],
                                   color="green",
                                   s=5.0,
                                   alpha=0.25)
            axspherical[0].quiver(
                self.samples[:, 0],
                self.samples[:, 1],
                -(self.wires.s0 * deriv_simple),
                -(self.wires.s1 * deriv_simple),
                color="red",
                alpha=0.25,
            )
            axspherical[0].quiver(
                self.samples[:, 0],
                self.samples[:, 1],
                -direction2_simple[:, 0],
                -direction2_simple[:, 1],
                color="k",
            )
            axspherical[0].scatter(
                (self.samples - direction2_simple)[:, 0],
                (self.samples - direction2_simple)[:, 1],
                color="k",
                s=50.0,
            )
            axspherical[0].set_xlabel("Property 1")
            axspherical[0].set_ylabel("Property 2")
            axspherical[0].set_title("SimplePGI")
            # With W
            axspherical[1].contourf(x,
                                    y,
                                    rvm.reshape(x.shape),
                                    alpha=0.25,
                                    cmap="brg")
            axspherical[1].contour(x, y, rv.reshape(x.shape), 20)
            axspherical[1].scatter(self.s0[:, 0],
                                   self.s0[:, 1],
                                   color="blue",
                                   s=5.0,
                                   alpha=0.25)
            axspherical[1].scatter(self.s1[:, 0],
                                   self.s1[:, 1],
                                   color="green",
                                   s=5.0,
                                   alpha=0.25)
            axspherical[1].quiver(
                self.samples[:, 0],
                self.samples[:, 1],
                -(self.wires.s0 * deriv),
                -(self.wires.s1 * deriv),
                color="red",
                alpha=0.25,
            )
            axspherical[1].quiver(
                self.samples[:, 0],
                self.samples[:, 1],
                -direction2[:, 0],
                -direction2[:, 1],
                color="k",
            )
            axspherical[1].scatter(
                (self.samples - direction2)[:, 0],
                (self.samples - direction2)[:, 1],
                color="k",
                s=50.0,
            )
            axspherical[1].set_xlabel("Property 1")
            axspherical[1].set_ylabel("Property 2")
            axspherical[1].set_title("PGI with W")

            plt.show()