def test_general(): """ General test of the class SRTomo, as it is in the docs of this class. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) assert_array_almost_equal(tomo.fit().estimate_, np.array([2., 5.]), 9)
def test_jacobian(): """ srtomo.SRTomo.jacobian return the jacobian of the model provided. In this simple model, the jacobian can be easily calculated. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) assert_array_almost_equal(tomo.jacobian().todense(), np.array([[5., 0.], [5., 5.]]), 9)
def test_predicted(): """ Test to verify srtomo.SRTomo.predicted function. Given the correct parameters, this function must return the result of the forward data. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) # The parameter used inside the class is slowness, so 1/vp. tomo.p_ = np.array([1. / 2., 1. / 5.]) assert_array_almost_equal(tomo.predicted(), ttimes, 9)
# Make some travel time data and add noise seed = 0 # Set the random seed so that points are the same every time src_loc = utils.random_points(area, 80, seed=seed) rec_loc = utils.circular_points(area, 30, random=True, seed=seed) srcs, recs = utils.connect_points(src_loc, rec_loc) tts = ttime2d.straight(model, 'vp', srcs, recs) tts, error = utils.contaminate(tts, 0.02, percent=True, return_stddev=True, seed=seed) # Make the mesh mesh = SquareMesh(area, shape) # and run the inversion misfit = srtomo.SRTomo(tts, srcs, recs, mesh) regularization = Smoothness2D(mesh.shape) # Will use the l-curve criterion to find the best regularization parameter tomo = LCurve(misfit, regularization, [10**i for i in np.arange(0, 10, 1)], jobs=8).fit() mesh.addprop('vp', tomo.estimate_) # Plot the L-curve annd print the regularization parameter estimated mpl.figure() mpl.title('L-curve: triangle marks the best solution') tomo.plot_lcurve() print "Estimated regularization parameter: %g" % (tomo.regul_param_) # Calculate and print the standard deviation of the residuals # Should be close to the data error if the inversion was able to fit the data
30, random=True, seed=seed) rec_loc = np.transpose([rec_loc_x, rec_loc_y]) srcs = [src for src in src_loc for _ in rec_loc] recs = [rec for _ in src_loc for rec in rec_loc] tts = ttime2d.straight(model, 'vp', srcs, recs) tts, error = utils.contaminate(tts, 0.02, percent=True, return_stddev=True, seed=seed) # Make the mesh mesh = SquareMesh(area, shape) # and run the inversion tomo = (srtomo.SRTomo(tts, srcs, recs, mesh) + 30 * TotalVariation2D(1e-10, mesh.shape)) # Since Total Variation is a non-linear function, then the tomography becomes # non-linear. So we need to configure fit to use the Levemberg-Marquardt # algorithm, a gradient descent method, that requires an initial estimate tomo.config('levmarq', initial=0.00001 * np.ones(mesh.size)).fit() mesh.addprop('vp', tomo.estimate_) # Calculate and print the standard deviation of the residuals # Should be close to the data error if the inversion was able to fit the data residuals = tomo[0].residuals() mpl.figure(figsize=(14, 5)) mpl.subplot(1, 2, 1) mpl.axis('scaled') mpl.title('Vp model')
30, random=True, seed=seed) rec_loc = np.transpose([rec_loc_x, rec_loc_y]) srcs = [src for src in src_loc for _ in rec_loc] recs = [rec for _ in src_loc for rec in rec_loc] tts = ttime2d.straight(model, 'vp', srcs, recs) tts, error = utils.contaminate(tts, 0.02, percent=True, return_stddev=True, seed=seed) # Make the mesh mesh = SquareMesh(area, shape) # and run the inversion tomo = (srtomo.SRTomo(tts, srcs, recs, mesh) + 1e8 * Smoothness2D(mesh.shape)) tomo.fit() mesh.addprop('vp', tomo.estimate_) # Calculate and print the standard deviation of the residuals # Should be close to the data error if the inversion was able to fit the data residuals = tomo[0].residuals() print "Assumed error: %g" % (error) print "Standard deviation of residuals: %g" % (np.std(residuals)) mpl.figure(figsize=(14, 5)) mpl.subplot(1, 2, 1) mpl.axis('scaled') mpl.title('Vp model') mpl.squaremesh(model, prop='vp', cmap=mpl.cm.seismic) cb = mpl.colorbar()
seed=seed) rec_loc = np.transpose([rec_loc_x, rec_loc_y]) srcs = [src for src in src_loc for _ in rec_loc] recs = [rec for _ in src_loc for rec in rec_loc] tts = ttime2d.straight(model, 'vp', srcs, recs) # Use 2% random noise to corrupt the data tts = utils.contaminate(tts, 0.02, percent=True, seed=seed) # Make a mesh for the inversion. The inversion will estimate the velocity in # each square of the mesh. To make things simpler, we'll use a mesh that is the # same as our original model. mesh = SquareMesh(area, shape) # Create solvers for each type of regularization and fit the synthetic data to # obtain an estimated velocity model solver = srtomo.SRTomo(tts, srcs, recs, mesh) smooth = solver + 1e8 * Smoothness2D(mesh.shape) smooth.fit() damped = solver + 1e8 * Damping(mesh.size) damped.fit() sharp = solver + 30 * TotalVariation2D(1e-10, mesh.shape) # Since Total Variation is a non-linear regularizing function, then the # tomography becomes non-linear as well. We need to configure the inversion to # use the Levemberg-Marquardt algorithm, a gradient descent method, that # requires an initial estimate sharp.config('levmarq', initial=0.00001 * np.ones(mesh.size)).fit() # Plot the original model and the 3 estimates using the same color bar
30, random=True, seed=seed) rec_loc = np.transpose([rec_loc_x, rec_loc_y]) srcs = [src for src in src_loc for _ in rec_loc] recs = [rec for _ in src_loc for rec in rec_loc] tts = ttime2d.straight(model, 'vp', srcs, recs) tts, error = utils.contaminate(tts, 0.01, percent=True, return_stddev=True, seed=seed) # Make the mesh mesh = SquareMesh(area, shape) # and run the inversion tomo = (srtomo.SRTomo(tts, srcs, recs, mesh) + 1e8 * Damping(mesh.size)) tomo.fit() mesh.addprop('vp', tomo.estimate_) # Calculate and print the standard deviation of the residuals # Should be close to the data error if the inversion was able to fit the data residuals = tomo[0].residuals() print "Assumed error: %g" % (error) print "Standard deviation of residuals: %g" % (np.std(residuals)) mpl.figure(figsize=(14, 5)) mpl.subplot(1, 2, 1) mpl.axis('scaled') mpl.title('Vp model') mpl.squaremesh(model, prop='vp', cmap=mpl.cm.seismic) cb = mpl.colorbar()