maxit = int(duration/dt) stations = [[75*ds, 125*ds]] # x, z coordinate of the seismometer snapshots = 3 # every 3 iterations plots one simulation = wavefd.scalar(velocity, area, dt, maxit, sources, stations, snapshots) # This part makes an animation using matplotlibs animation API background = (velocity-4000)*10**-1 fig = mpl.figure(figsize=(8, 6)) mpl.subplots_adjust(right=0.98, left=0.11, hspace=0.5, top=0.93) mpl.subplot2grid((4, 3), (0,0), colspan=3,rowspan=3) wavefield = mpl.imshow(np.zeros_like(velocity), extent=area, cmap=mpl.cm.gray_r, vmin=-1000, vmax=1000) mpl.points(stations, '^b', size=8) mpl.ylim(area[2:][::-1]) mpl.xlabel('x (km)') mpl.ylabel('z (km)') mpl.m2km() mpl.subplot2grid((4,3), (3,0), colspan=3) seismogram1, = mpl.plot([],[],'-k') mpl.xlim(0, duration) mpl.ylim(-200, 200) mpl.ylabel('Amplitude') times = np.linspace(0, dt*maxit, maxit) # This function updates the plot every few timesteps def animate(i): t, u, seismogram = simulation.next() seismogram1.set_data(times[:t+1], seismogram[0][:t+1]) wavefield.set_array(background[::-1]+u[::-1]) return wavefield, seismogram1 anim = animation.FuncAnimation(fig, animate, frames=maxit/snapshots, interval=1) mpl.show()
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) mpl.ylabel("P(z)", fontsize=14) mpl.xlabel("z", fontsize=14) mpl.show()
from fatiando import utils, mesher from fatiando.gravmag import talwani from fatiando.vis import mpl # Notice that the last two number are switched. # This way, the z axis in the plots points down. area = (-5000, 5000, 5000, 0) axes = mpl.figure().gca() mpl.xlabel("X") mpl.ylabel("Z") mpl.axis('scaled') polygons = [mesher.Polygon(mpl.draw_polygon(area, axes), {'density':500})] xp = numpy.arange(-4500, 4500, 100) zp = numpy.zeros_like(xp) gz = talwani.gz(xp, zp, polygons) mpl.figure() mpl.axis('scaled') mpl.subplot(2,1,1) mpl.title(r"Gravity anomaly produced by the model") mpl.plot(xp, gz, '-k', linewidth=2) mpl.ylabel("mGal") mpl.xlim(-5000, 5000) mpl.subplot(2,1,2) mpl.polygon(polygons[0], 'o-k', linewidth=2, fill='k', alpha=0.5) mpl.xlabel("X") mpl.ylabel("Z") mpl.set_area(area) 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()
duration = 20 maxit = int(duration / dt) stations = [[50000, 0]] # x, z coordinate of the seismometer snapshot = int(0.5 / dt) # Plot a snapshot of the simulation every 0.5 seconds simulation = wavefd.elastic_sh(mu, density, area, dt, maxit, sources, stations, snapshot, padding=50, taper=0.01) # This part makes an animation using matplotlibs animation API fig = mpl.figure(figsize=(14, 5)) ax = mpl.subplot(1, 2, 2) mpl.title('Wavefield') # Start with everything zero and grab the plot so that it can be updated later wavefield_plt = mpl.imshow(np.zeros(shape), extent=area, vmin=-10 ** (-5), vmax=10 ** (-5), cmap=mpl.cm.gray_r) mpl.points(stations, '^b') mpl.xlim(area[:2]) mpl.ylim(area[2:][::-1]) mpl.xlabel('x (km)') mpl.ylabel('z (km)') mpl.subplot(1, 2, 1) seismogram_plt, = mpl.plot([], [], '-k') mpl.xlim(0, duration) mpl.ylim(-10 ** (-4), 10 ** (-4)) mpl.xlabel('time (s)') mpl.ylabel('Amplitude') times = np.linspace(0, duration, maxit) # Update the plot everytime the simulation yields def animate(i): """
# 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()
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()
0.02, 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()
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) 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()
""" import numpy from fatiando import utils, mesher, gravmag, inversion from fatiando.vis import mpl # Notice that the last two number are switched. # This way, the z axis in the plots points down. area = (-5000, 5000, 5000, 0) axes = mpl.figure().gca() mpl.xlabel("X") mpl.ylabel("Z") mpl.axis('scaled') polygons = [mesher.Polygon(mpl.draw_polygon(area, axes), {'density': 500})] xp = numpy.arange(-4500, 4500, 100) zp = numpy.zeros_like(xp) gz = gravmag.talwani.gz(xp, zp, polygons) mpl.figure() mpl.axis('scaled') mpl.subplot(2, 1, 1) mpl.title(r"Gravity anomaly produced by the model") mpl.plot(xp, gz, '-k', linewidth=2) mpl.ylabel("mGal") mpl.xlim(-5000, 5000) mpl.subplot(2, 1, 2) mpl.polygon(polygons[0], 'o-k', linewidth=2, fill='k', alpha=0.5) mpl.xlabel("X") mpl.ylabel("Z") mpl.set_area(area) mpl.show()
dt = wavefd.maxdt(area, shape, svel.max()) duration = 250 maxit = int(duration / dt) stations = [[100000, 0], [700000, 0]] snapshots = int(1. / dt) simulation = wavefd.elastic_sh(mu, density, area, dt, maxit, sources, stations, snapshots, padding=70, taper=0.005) # This part makes an animation using matplotlibs animation API background = svel * 5 * 10 ** -7 fig = mpl.figure(figsize=(10, 8)) mpl.subplots_adjust(right=0.98, left=0.11, hspace=0.3, top=0.93) mpl.subplot(3, 1, 1) mpl.title('Seismogram 1') seismogram1, = mpl.plot([], [], '-k') mpl.xlim(0, duration) mpl.ylim(-0.1, 0.1) mpl.ylabel('Amplitude') mpl.subplot(3, 1, 2) mpl.title('Seismogram 2') seismogram2, = mpl.plot([], [], '-k') mpl.xlim(0, duration) mpl.ylim(-0.1, 0.1) mpl.ylabel('Amplitude') ax = mpl.subplot(3, 1, 3) mpl.title('time: 0.0 s') wavefield = mpl.imshow(background, extent=area, cmap=mpl.cm.gray_r, vmin=-0.005, vmax=0.005) mpl.points(stations, '^b', size=8) mpl.text(750000, 20000, 'Crust') mpl.text(740000, 100000, 'Mantle')
maxit, sources, stations, snapshots, padding=70, taper=0.005, xz2ps=True) # This part makes an animation using matplotlibs animation API background = 10**-5 * ((density - density.min()) / density.max()) fig = mpl.figure(figsize=(10, 8)) mpl.subplots_adjust(right=0.98, left=0.11, hspace=0.3, top=0.93) mpl.subplot(3, 1, 1) mpl.title('x seismogram') xseismogram, = mpl.plot([], [], '-k') mpl.xlim(0, duration) mpl.ylim(-0.05, 0.05) mpl.ylabel('Amplitude') mpl.subplot(3, 1, 2) mpl.title('z seismogram') zseismogram, = mpl.plot([], [], '-k') mpl.xlim(0, duration) mpl.ylim(-0.05, 0.05) mpl.ylabel('Amplitude') ax = mpl.subplot(3, 1, 3) mpl.title('time: 0.0 s') wavefield = mpl.imshow(background, extent=area, cmap=mpl.cm.gray_r, vmin=-0.00001, vmax=0.00001)
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) mpl.ylabel("P(z)", fontsize=14) mpl.xlabel("z", fontsize=14) mpl.show()