percent=True, return_stddev=True) # Make the solver and run the inversion using damping regularization # (assumes known thicknesses of the layers) solver = (LayeredStraight(tts, zp, thickness) + 0.1 * Damping(len(thickness))).fit() velocity_ = solver.estimate_ # Plot the results mpl.figure(figsize=(12, 5)) mpl.subplot(1, 2, 1) mpl.grid() mpl.title("Vertical seismic profile") mpl.plot(tts, zp, 'ok', label='Observed') mpl.plot(solver[0].predicted(), zp, '-r', linewidth=3, label='Predicted') mpl.legend(loc='upper right', numpoints=1) mpl.xlabel("Travel-time (s)") mpl.ylabel("Z (m)") mpl.ylim(sum(thickness), 0) mpl.subplot(1, 2, 2) mpl.grid() mpl.title("Velocity profile") mpl.layers(thickness, velocity_, 'o-k', linewidth=2, label='Estimated') mpl.layers(thickness, velocity, '--b', linewidth=2, label='True') mpl.ylim(zmax, zmin) mpl.xlim(vmin, vmax) leg = mpl.legend(loc='upper right', numpoints=1) leg.get_frame().set_alpha(0.5) mpl.xlabel("Velocity (m/s)") mpl.ylabel("Z (m)") mpl.show()
# 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.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() cb.set_label('Velocity') mpl.points(src_loc, '*y', label="Sources") mpl.points(rec_loc, '^r', label="Receivers") mpl.legend(loc='lower left', shadow=True, numpoints=1, prop={'size': 10}) mpl.m2km() mpl.subplot(1, 2, 2) mpl.axis('scaled') mpl.title('Tomography result') mpl.squaremesh(mesh, prop='vp', vmin=4000, vmax=10000, cmap=mpl.cm.seismic) cb = mpl.colorbar() cb.set_label('Velocity') mpl.m2km() mpl.figure() mpl.grid() mpl.title('Residuals (data with %.4f s error)' % (error)) mpl.hist(residuals, color='gray', bins=10) mpl.xlabel("seconds") mpl.show()
zp = numpy.arange(0, 100, 1) temp, error = utils.contaminate(climsig.abrupt(amp, age, zp), 0.02, percent=True, return_stddev=True) # Preparing for the inversion p, residuals = climsig.iabrupt(temp, zp) est_amp, est_age = p mpl.figure(figsize=(12, 5)) mpl.subplot(1, 2, 1) mpl.title("Climate signal (abrupt)") mpl.plot(temp, zp, 'ok', label='Observed') mpl.plot(temp - residuals, zp, '--r', linewidth=3, label='Predicted') mpl.legend(loc='lower right', numpoints=1) mpl.xlabel("Temperature (C)") mpl.ylabel("Z") mpl.ylim(100, 0) ax = mpl.subplot(1, 2, 2) ax2 = mpl.twinx() mpl.title("Age and amplitude") width = 0.3 ax.bar([1 - width], [age], width, color='b', label="True") ax.bar([1], [est_age], width, color='r', label="Estimate") ax2.bar([2 - width], [amp], width, color='b') ax2.bar([2], [est_amp], width, color='r') ax.legend(loc='upper center', numpoints=1) ax.set_ylabel("Age (years)") ax2.set_ylabel("Amplitude (C)") ax.set_xticks([1, 2])
relief.addprop('density', (2670 for i in xrange(relief.size))) gridarea = (-80, 80, -220, 220) gridshape = (100, 100) xp, yp, zp = gridder.regular(gridarea, gridshape, z=-200) gz = prism.gz(xp, yp, zp, relief) mpl.figure(figsize=(10, 7)) mpl.subplot(1, 2, 1) mpl.title("Synthetic topography") mpl.axis('scaled') mpl.pcolor(x, y, height, shape) cb = mpl.colorbar() cb.set_label("meters") mpl.square(gridarea, label='Computation grid') mpl.legend() mpl.subplot(1, 2, 2) mpl.title("Topographic effect") mpl.axis('scaled') mpl.pcolor(xp, yp, gz, gridshape) cb = mpl.colorbar() cb.set_label("mGal") mpl.show() myv.figure() myv.prisms(relief, prop='density') axes = myv.axes(myv.outline()) myv.wall_bottom(axes.axes.bounds, opacity=0.2) myv.wall_north(axes.axes.bounds) myv.show()
mpl.points(src, '*y') initial = mpl.pick_points(area, mpl.gca(), marker='*', color='b') if len(initial) > 1: print "Don't be greedy! Pick only one point" sys.exit() estimate = solver.config('levmarq', initial=initial[0]).fit().estimate_ mpl.figure(figsize=(10, 4)) mpl.subplot(1, 2, 1) mpl.title('Epicenter + %d recording stations' % (len(recs))) mpl.axis('scaled') mpl.points(src, '*y', label="True") mpl.points(recs, '^r', label="Stations") mpl.points(initial, '*b', label="Initial") mpl.points([estimate], '*g', label="Estimate") mpl.set_area(area) mpl.legend(loc='lower right', shadow=True, numpoints=1, prop={'size': 12}) mpl.xlabel("X") mpl.ylabel("Y") ax = mpl.subplot(1, 2, 2) mpl.title('Travel-time residuals + error bars') s = numpy.arange(len(traveltime)) + 1 width = 0.3 mpl.bar(s - width, traveltime, width, color='g', label="Observed", yerr=error) mpl.bar(s, solver.predicted(), width, color='r', label="Predicted") ax.set_xticks(s) mpl.legend(loc='upper right', shadow=True, prop={'size': 12}) mpl.xlabel("Station number") mpl.ylabel("Travel-time residual") mpl.show()
log.info("Generating synthetic data") verts = [(10000, 1.), (90000, 1.), (90000, 7000), (10000, 3330)] model = mesher.Polygon(verts, {'density':-100}) xp = numpy.arange(0., 100000., 1000.) zp = numpy.zeros_like(xp) gz = utils.contaminate(gravmag.talwani.gz(xp, zp, [model]), 0.5) log.info("Preparing for the inversion") solver = inversion.gradient.levmarq(initial=(9000, 500)) estimate, residuals = gravmag.basin2d.trapezoidal(xp, zp, gz, verts[0:2], -100, solver) mpl.figure() mpl.subplot(2, 1, 1) mpl.title("Gravity anomaly") mpl.plot(xp, gz, 'ok', label='Observed') mpl.plot(xp, gz - residuals, '-r', linewidth=2, label='Predicted') mpl.legend(loc='lower left', numpoints=1) mpl.ylabel("mGal") mpl.xlim(0, 100000) mpl.subplot(2, 1, 2) mpl.polygon(estimate, 'o-r', linewidth=2, fill='r', alpha=0.3, label='Estimated') mpl.polygon(model, '--k', linewidth=2, label='True') mpl.legend(loc='lower left', numpoints=1) mpl.xlabel("X") mpl.ylabel("Z") mpl.set_area((0, 100000, 10000, -500)) mpl.show()
0.02, percent=True, return_stddev=True) # Assume that the thicknesses are unknown. In this case, use a mesh of many # thin layers and invert for each slowness thick = 10. mesh = [thick]*int(sum(thickness)/thick) solver = (LayeredStraight(tts, zp, mesh) + 5*Smoothness1D(len(mesh))).fit() velocity_ = solver.estimate_ mpl.figure(figsize=(12,5)) mpl.subplot(1, 2, 1) mpl.grid() mpl.title("Vertical seismic profile") mpl.plot(tts, zp, 'ok', label='Observed') mpl.plot(solver.predicted(), zp, '-r', linewidth=3, label='Predicted') mpl.legend(loc='upper right', numpoints=1) mpl.xlabel("Travel-time (s)") mpl.ylabel("Z (m)") mpl.ylim(sum(mesh), 0) mpl.subplot(1, 2, 2) mpl.grid() mpl.title("True velocity + smooth estimate") mpl.layers(mesh, velocity_, '.-k', linewidth=2, label='Estimated') mpl.layers(thickness, velocity, '--b', linewidth=2, label='True') mpl.ylim(sum(mesh), 0) mpl.xlim(0, 10000) mpl.legend(loc='upper right', numpoints=1) mpl.xlabel("Velocity (m/s)") mpl.ylabel("Z (m)") mpl.show()
initial = mpl.pick_points(area, mpl.gca(), marker='*', color='b') if len(initial) > 1: print "Don't be greedy! Pick only one point" sys.exit() estimate = solver.config('levmarq', initial=initial[0]).fit().estimate_ mpl.figure(figsize=(10,4)) mpl.subplot(1, 2, 1) mpl.title('Epicenter + %d recording stations' % (len(recs))) mpl.axis('scaled') mpl.points(src, '*y', label="True") mpl.points(recs, '^r', label="Stations") mpl.points(initial, '*b', label="Initial") mpl.points([estimate], '*g', label="Estimate") mpl.set_area(area) mpl.legend(loc='lower right', shadow=True, numpoints=1, prop={'size':12}) mpl.xlabel("X") mpl.ylabel("Y") ax = mpl.subplot(1, 2, 2) mpl.title('Travel-time residuals + error bars') s = numpy.arange(len(traveltime)) + 1 width = 0.3 mpl.bar(s - width, traveltime, width, color='g', label="Observed", yerr=error) mpl.bar(s, solver.predicted(), width, color='r', label="Predicted") ax.set_xticks(s) mpl.legend(loc='upper right', shadow=True, prop={'size':12}) mpl.xlabel("Station number") mpl.ylabel("Travel-time residual") mpl.show()
mpl.pcolor(layer.y, layer.x, layer.props['density'], layer.shape) mpl.colorbar().set_label(r'Density $kg.m^{-3}$') mpl.m2km() mpl.subplot(1, 3, 2) mpl.axis('scaled') mpl.title('Fit gz (mGal)') levels = mpl.contour(y1, x1, gz, shape, 15, color='k', interp=True) mpl.contour(y1, x1, solver.predicted()[0], shape, levels, color='r', interp=True) mpl.plot(y1, x1, 'xk', label='Data points') mpl.legend() mpl.m2km() mpl.subplot(1, 3, 3) mpl.axis('scaled') mpl.title('Fit gzz (Eotvos)') levels = mpl.contour(y2, x2, gzz, shape, 10, color='k', interp=True) mpl.contour(y2, x2, solver.predicted()[1], shape, levels, color='r', interp=True) mpl.plot(y2, x2, 'xk', label='Data points') mpl.legend() mpl.m2km()
z = data(x, y) shape = (100, 100) # First, we need to know the real data at the grid points grdx, grdy = gridder.regular(area, shape) grdz = data(grdx, grdy) mpl.figure() mpl.subplot(2, 2, 1) mpl.axis("scaled") mpl.title("True grid data") mpl.plot(x, y, ".k", label="Data points") mpl.contourf(grdx, grdy, grdz, shape, 50) mpl.colorbar() mpl.legend(loc="lower right", numpoints=1) # Use the default interpolation (cubic) grdx, grdy, grdz = gridder.interp(x, y, z, shape) mpl.subplot(2, 2, 2) mpl.axis("scaled") mpl.title("Interpolated using cubic minimum-curvature") mpl.plot(x, y, ".k", label="Data points") mpl.contourf(grdx, grdy, grdz, shape, 50) mpl.colorbar() mpl.legend(loc="lower right", numpoints=1) # Use the nearest neighbors interpolation grdx, grdy, grdz = gridder.interp(x, y, z, shape, algorithm="nearest") mpl.subplot(2, 2, 3) mpl.axis("scaled")
""" Gridding: Cut a section from a grid """ from fatiando import gridder, utils from fatiando.vis import mpl # Generate some synthetic data on a regular grid x, y = gridder.regular((-10, 10, -10, 10), (100,100)) # Using a 2D Gaussian z = utils.gaussian2d(x, y, 1, 1) subarea = [-2, 2, -3, 3] subx, suby, subscalar = gridder.cut(x, y, [z], subarea) mpl.figure(figsize=(12, 5)) mpl.subplot(1, 2, 1) mpl.title("Whole grid") mpl.axis('scaled') mpl.pcolor(x, y, z, (100,100)) mpl.square(subarea, 'k', linewidth=2, label='Cut this region') mpl.legend(loc='lower left') mpl.subplot(1, 2, 2) mpl.title("Cut grid") mpl.axis('scaled') mpl.pcolor(subx, suby, subscalar[0], (40,60), interp=True) mpl.show()
x, y = gridder.scatter((-2, 2, -2, 2), n=300, seed=1) # And calculate 2D Gaussians on these points as sample data def data(x, y): return (utils.gaussian2d(x, y, -0.6, -1) - utils.gaussian2d(x, y, 1.5, 1.5)) d = data(x, y) # Extract a profile along the diagonal p1, p2 = [-1.5, 0], [1.5, 1.5] xp, yp, distance, dp = gridder.profile(x, y, d, p1, p2, 100) dp_true = data(xp, yp) mpl.figure() mpl.subplot(2, 1, 2) mpl.title("Irregular grid") mpl.plot(xp, yp, '-k', label='Profile', linewidth=2) mpl.contourf(x, y, d, (100, 100), 50, interp=True) mpl.colorbar(orientation='horizontal') mpl.legend(loc='lower right') mpl.subplot(2, 1, 1) mpl.title('Profile') mpl.plot(distance, dp, '.b', label='Extracted') mpl.plot(distance, dp_true, '-k', label='True') mpl.xlim(distance.min(), distance.max()) mpl.legend(loc='lower right') mpl.show()
""" Gridding: Cut a section from a grid """ from fatiando import gridder, utils from fatiando.vis import mpl # Generate some synthetic data on a regular grid x, y = gridder.regular((-10, 10, -10, 10), (100, 100)) # Using a 2D Gaussian z = utils.gaussian2d(x, y, 1, 1) subarea = [-2, 2, -3, 3] subx, suby, subscalar = gridder.cut(x, y, [z], subarea) mpl.figure(figsize=(12, 5)) mpl.subplot(1, 2, 1) mpl.title("Whole grid") mpl.axis('scaled') mpl.pcolor(x, y, z, (100, 100)) mpl.square(subarea, 'k', linewidth=2, label='Cut this region') mpl.legend(loc='lower left') mpl.subplot(1, 2, 2) mpl.title("Cut grid") mpl.axis('scaled') mpl.pcolor(subx, suby, subscalar[0], (40, 60), interp=True) mpl.show()
from fatiando import utils from fatiando.geothermal.climsig import linear, SingleChange from fatiando.vis import mpl # Generating synthetic data amp = 5.43 age = 78.2 # along a well at these depths zp = numpy.arange(0, 100, 1) temp, error = utils.contaminate(linear(amp, age, zp), 0.02, percent=True, return_stddev=True) # Preparing for the inversion data = SingleChange(temp, zp, mode='linear').config('levmarq', initial=[1, 1]) amp_, age_ = data.fit().estimate_ print "Linear change in temperature" print " true: amp=%.3f age=%.3f" % (amp, age) print " estimated: amp=%.3f age=%.3f" % (amp_, age_) mpl.figure(figsize=(4, 5)) mpl.title("Residual well temperature") mpl.plot(temp, zp, 'ok', label='Observed') mpl.plot(data.predicted(), zp, '--r', linewidth=3, label='Predicted') mpl.legend(loc='lower right', numpoints=1) mpl.xlabel("Temperature (C)") mpl.ylabel("Z (m)") mpl.ylim(100, 0) mpl.show()
verts = [(10000, 1.), (90000, 1.), (80000, 5000)] model = mesher.Polygon(verts, {'density': -100}) xp = numpy.arange(0., 100000., 1000.) zp = numpy.zeros_like(xp) gz = utils.contaminate(gravmag.talwani.gz(xp, zp, [model]), 1) solver = inversion.gradient.levmarq(initial=(10000, 1000)) estimate, residuals = gravmag.basin2d.triangular(xp, zp, gz, verts[0:2], -100, solver) mpl.figure() mpl.subplot(2, 1, 1) mpl.title("Gravity anomaly") mpl.plot(xp, gz, 'ok', label='Observed') mpl.plot(xp, gz - residuals, '-r', linewidth=2, label='Predicted') mpl.legend(loc='lower left') mpl.ylabel("mGal") mpl.xlim(0, 100000) mpl.subplot(2, 1, 2) mpl.polygon(estimate, 'o-r', linewidth=2, fill='r', alpha=0.3, label='Estimated') mpl.polygon(model, '--k', linewidth=2, label='True') mpl.legend(loc='lower left', numpoints=1) mpl.xlabel("X") mpl.ylabel("Z") mpl.set_area((0, 100000, 10000, -500)) mpl.show()
# Print the estimated and true dipole monents, inclinations and declinations print 'Estimated magnetization (intensity, inclination, declination)' for e in solver.estimate_: print e # Plot the fit and the normalized histogram of the residuals mpl.figure(figsize=(14, 5)) mpl.subplot(1, 2, 1) mpl.title("Total Field Anomaly (nT)", fontsize=14) mpl.axis('scaled') nlevels = mpl.contour(y, x, tf, (50, 50), 15, interp=True, color='r', label='Observed', linewidth=2.0) mpl.contour(y, x, solver.predicted(), (50, 50), nlevels, interp=True, color='b', label='Predicted', style='dashed', linewidth=2.0) mpl.legend(loc='upper left', shadow=True, prop={'size': 13}) mpl.xlabel('East y (m)', fontsize=14) mpl.ylabel('North x (m)', fontsize=14) mpl.subplot(1, 2, 2) residuals_mean = numpy.mean(solver.residuals()) residuals_std = numpy.std(solver.residuals()) # Each residual is subtracted from the mean and the resulting # difference is divided by the standard deviation s = (solver.residuals() - residuals_mean) / residuals_std mpl.hist(s, bins=21, range=None, normed=True, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None) mpl.xlim(-4, 4) mpl.title("mean = %.3f std = %.3f" % (residuals_mean, residuals_std), fontsize=14)
# Generate random points x, y = gridder.scatter((-2, 2, -2, 2), n=300, seed=1) # And calculate 2D Gaussians on these points as sample data def data(x, y): return (utils.gaussian2d(x, y, -0.6, -1) - utils.gaussian2d(x, y, 1.5, 1.5)) d = data(x, y) # Extract a profile along the diagonal p1, p2 = [-1.5, 0], [1.5, 1.5] xp, yp, distance, dp = gridder.profile(x, y, d, p1, p2, 100) dp_true = data(xp, yp) mpl.figure() mpl.subplot(2, 1, 2) mpl.title("Irregular grid") mpl.plot(xp, yp, '-k', label='Profile', linewidth=2) mpl.contourf(x, y, d, (100, 100), 50, interp=True) mpl.colorbar(orientation='horizontal') mpl.legend(loc='lower right') mpl.subplot(2, 1, 1) mpl.title('Profile') mpl.plot(distance, dp, '.b', label='Extracted') mpl.plot(distance, dp_true, '-k', label='True') mpl.xlim(distance.min(), distance.max()) mpl.legend(loc='lower right') mpl.show()
tf, (50, 50), 15, interp=True, color='r', label='Observed', linewidth=2.0) mpl.contour(y, x, solver.predicted(), (50, 50), nlevels, interp=True, color='b', label='Predicted', style='dashed', linewidth=2.0) mpl.legend(loc='upper left', shadow=True, prop={'size': 13}) mpl.xlabel('East y (m)', fontsize=14) mpl.ylabel('North x (m)', fontsize=14) mpl.subplot(1, 2, 2) residuals_mean = numpy.mean(solver.residuals()) residuals_std = numpy.std(solver.residuals()) # Each residual is subtracted from the mean and the resulting # difference is divided by the standard deviation s = (solver.residuals() - residuals_mean) / residuals_std mpl.hist(s, bins=21, range=None, normed=True, weights=None, cumulative=False, bottom=None,