wr_grav = np.sum(simulation_grav.G**2.0, axis=0)**0.5 / (mesh.cell_volumes[actv]) wr_grav = wr_grav / np.max(wr_grav) wr_mag = np.sum(simulation_mag.G**2.0, axis=0)**0.5 / (mesh.cell_volumes[actv]) wr_mag = wr_mag / np.max(wr_mag) # create joint PGI regularization with smoothness reg = utils.make_PGI_regularization( gmmref=gmmref, mesh=mesh, wiresmap=wires, maplist=[idenMap, idenMap], 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)), 1e-2 * 100.0 * np.ones(len(reg.objfcts[2].objfcts)), ]
# petrophysical measurements n = 3 clf = utils.WeightedGaussianMixture( mesh=mesh, n_components=n, covariance_type="full", max_iter=100, n_init=3, reg_covar=5e-4, ) clf.fit(mtrue.reshape(-1, 1)) # Petrophyically constrained regularization reg = utils.make_PGI_regularization( gmmref=clf, mesh=mesh, alpha_s=1.0, alpha_x=1.0, ) # Optimization opt = optimization.ProjectedGNCG(maxIter=10, maxIterCG=50, tolCG=1e-4) opt.remember("xc") # Setup new inverse problem invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) # directives Alphas = directives.AlphasSmoothEstimate_ByEig(alpha0_ratio=10.0, verbose=True) beta = directives.BetaEstimate_ByEig(beta0_ratio=1e-6) betaIt = directives.PGI_BetaAlphaSchedule( verbose=True,
def test_full_covariances(self): print("Test Full covariances: ") print("=======================") # Fit a Gaussian Mixture clf = WeightedGaussianMixture( mesh=self.mesh, n_components=self.n_components, covariance_type="full", max_iter=1000, n_init=10, tol=1e-8, means_init=self.means, warm_start=True, precisions_init=np.linalg.inv(self.sigma), weights_init=self.proportions, ) clf.fit(self.samples) # Define reg 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 dm = self.model - mref 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-4) self.assertTrue(passed_score) print("scores for PGI & Full Cov. are ok.") # check derivatives as an optimization on locally quadratic function deriv = reg.deriv(self.model) reg.objfcts[0].approx_gradient = False reg.objfcts[0].approx_hessian = False deriv_full = reg.deriv(self.model) passed_deriv1 = np.allclose(deriv, deriv_full, rtol=1e-4) self.assertTrue(passed_deriv1) print("1st derivatives for PGI & Full 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-4 ) self.assertTrue(passed_derivative) print("2nd derivatives for PGI & Full Cov. are ok.") if self.PlotIt: print("Plotting", self.PlotIt) import matplotlib.pyplot as plt xmin, xmax = ymin, ymax = self.samples.min(), self.samples.max() x, y = np.mgrid[xmin:xmax:0.5, ymin:ymax:0.5] 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)) figfull, axfull = plt.subplots(1, 1, figsize=(16, 8)) figfull.suptitle("Full Covariances Tests") axfull.contourf(x, y, rvm.reshape(x.shape), alpha=0.25, cmap="brg") axfull.contour(x, y, rv.reshape(x.shape), 20) axfull.scatter( self.samples[:, 0], self.samples[:, 1], color="blue", s=5.0, alpha=0.25 ) axfull.quiver( self.samples[:, 0], self.samples[:, 1], -(self.wires.s0 * deriv), -(self.wires.s1 * deriv), color="red", alpha=0.25, ) axfull.quiver( self.samples[:, 0], self.samples[:, 1], -direction2[:, 0], -direction2[:, 1], color="k", ) axfull.scatter( (self.samples - direction2)[:, 0], (self.samples - direction2)[:, 1], color="k", s=50.0, ) axfull.set_xlabel("Property 1") axfull.set_ylabel("Property 2") axfull.set_title("PGI with W") plt.show()
# 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_PGI_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=50, tolX=1e-6, maxIterCG=100, tolCG=1e-3, lower=-10, upper=10, ) invProb = inverse_problem.BaseInvProblem(dmis, reg_simple_no_map, opt)