def test_harvest_restrict(): def fill(i, case): # Returns density of 10 for center prism and prism given by 'case' cdir = {'above': 4, 'below': 22, 'north': 14, 'south': 12, 'east': 16, 'west': 10} if i == 13: return 10 for key in cdir: if case == key and i == cdir.get(key): return 10 return 0 # The test cases as string list cases = ['above', 'below', 'north', 'south', 'east', 'west'] # Create reference model bounds = (0, 3, 0, 3, 0, 3) shape = (3, 3, 3) shapegz = (10, 10) for testcase in cases: mref = PrismMesh(bounds, shape) mesh = mref.copy() mref.addprop('density', [fill(i, testcase) for i in xrange(mref.size)]) # Calculate reference gravity field xp, yp, zp = gridder.regular(bounds[:4], shapegz, z=-1) gzref = prism.gz(xp, yp, zp, mref) # Initiate harvest hgref = [harvester.Gz(xp, yp, zp, gzref)] loc = [[1.5, 1.5, 1.5, {'density': 10}]] seeds = harvester.sow(loc, mesh) # est0 should be incorrect and thus fail wilst est1 should yield the # same geometry as mref est0, pred0 = harvester.harvest(hgref, seeds, mesh, compactness=0.1, threshold=0.001, restrict=[testcase]) est1, pred1 = harvester.harvest(hgref, seeds, mesh, compactness=0.1, threshold=0.001) res0 = mesh.copy() res0.addprop('density', est0['density']) res1 = mesh.copy() res1.addprop('density', est1['density']) l0 = [] l1 = [] for i, p in enumerate(res0): l0.append(p.props['density'] == mref[i].props['density']) for i, p in enumerate(res1): l1.append(p.props['density'] == mref[i].props['density']) assert not np.all(l0) assert np.all(l1)
mpl.m2km() mpl.show() # Inversion setup # Create a mesh mesh = PrismMesh(bounds, (30, 30, 30)) # Wrap the data so that harvester can use it data = [harvester.Gxx(xp, yp, zp, gxx), harvester.Gxy(xp, yp, zp, gxy), harvester.Gxz(xp, yp, zp, gxz), harvester.Gyy(xp, yp, zp, gyy), harvester.Gyz(xp, yp, zp, gyz), harvester.Gzz(xp, yp, zp, gzz)] # Make the seeds seeds = harvester.sow([ [500, 400, 210, {'density':1000}], [500, 550, 510, {'density':1000}]], mesh) # Run the inversioin estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=0.5, threshold=0.001) # Put the estimated density values in the mesh mesh.addprop('density', estimate['density']) # Plot the adjustment mpl.figure() mpl.suptitle("True: color | Inversion: contour") for i in xrange(len(tensor)): mpl.subplot(2, 3, i + 1) mpl.title(titles[i]) mpl.axis('scaled') levels = mpl.contourf(yp, xp, tensor[i], shape, 12)
data = [harvester.Gz(xp, yp, zp, gz)] # Plot the data and pick the location of the seeds mpl.figure() mpl.suptitle("Pick the seeds (polygon is the true source)") mpl.axis('scaled') levels = mpl.contourf(yp, xp, gz, shape, 12) mpl.colorbar() mpl.polygon(model[0], xy2ne=True) mpl.xlabel('Horizontal coordinate y (km)') mpl.ylabel('Horizontal coordinate x (km)') seedx, seedy = mpl.pick_points(area, mpl.gca(), xy2ne=True).T # Set the right density and depth locations = [[x, y, 1500, {'density': 1000}] for x, y in zip(seedx, seedy)] mpl.show() # Make the seed and set the compactness regularizing parameter mu seeds = harvester.sow(locations, mesh) # Run the inversion estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=0.05, threshold=0.0005) # Put the estimated density values in the mesh mesh.addprop('density', estimate['density']) # Plot the adjustment and the result mpl.figure() mpl.title("True: color | Predicted: contour") mpl.axis('scaled') levels = mpl.contourf(yp, xp, gz, shape, 12) mpl.colorbar() mpl.contour(yp, xp, predicted[0], shape, levels, color='k') mpl.xlabel('Horizontal coordinate y (km)') mpl.ylabel('Horizontal coordinate x (km)') mpl.m2km()
GravMag: Iterate through a 3D gravity inversion by planting anomalous densities """ from fatiando import gridder from fatiando.gravmag import prism, harvester from fatiando.mesher import Prism, PrismMesh from fatiando.vis import myv model = [Prism(200, 800, 400, 600, 200, 400, {'density': 1000})] shape = (20, 20) bounds = [0, 1000, 0, 1000, 0, 1000] area = bounds[0:4] x, y, z = gridder.regular(area, shape, z=-1) gz = prism.gz(x, y, z, model) mesh = PrismMesh(bounds, (10, 10, 10)) data = [harvester.Gz(x, y, z, gz)] seeds = harvester.sow([[500, 500, 250, {'density': 1000}]], mesh) fig = myv.figure(size=(700, 700)) plot = myv.prisms(model, style='wireframe', linewidth=4) plot.actor.mapper.scalar_visibility = False myv.prisms(seeds, 'density') myv.outline(bounds) myv.wall_bottom(bounds) myv.wall_east(bounds) for update in harvester.iharvest(data, seeds, mesh, compactness=0.5, threshold=0.001): best, neighborhood = update[2:4] if best is not None: myv.prisms([mesh[best.i]]) plot = myv.prisms( [mesh[n] for neighbors in neighborhood for n in neighbors],
# Set up the inversion: # Create a prism mesh mesh = PrismMesh(bounds, (15, 50, 50)) # Wrap the data so that harvester can use it data = [harvester.Gyy(x, y, z, gyy), harvester.Gyz(x, y, z, gyz), harvester.Gzz(x, y, z, gzz)] # and the seeds seeds = harvester.sow( [( 800, 3250, 600, {'density':1200}), (1200, 3250, 600, {'density':1200}), (1700, 3250, 600, {'density':1200}), (2100, 3250, 600, {'density':1200}), (2500, 3250, 600, {'density':1200}), (2900, 3250, 600, {'density':1200}), (3300, 3250, 600, {'density':1200}), (3700, 3250, 600, {'density':1200}), (4200, 3250, 600, {'density':1200}), (3300, 2050, 600, {'density':1200}), (3600, 2050, 600, {'density':1200}), (4000, 2050, 600, {'density':1200}), (4300, 2050, 600, {'density':1200})], mesh) # Run the inversion and collect the results estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=1., threshold=0.0001) # Insert the estimated density values into the mesh mesh.addprop('density', estimate['density']) # and get only the prisms corresponding to our estimate density_model = vremove(0, 'density', mesh)
# Generate some synthetic total field anomaly data bounds = [0, 10000, 0, 10000, 0, 5000] props = {'density': 500} props2 = {'density': 1000} model = [ mesher.Prism(4000, 6000, 4000, 6000, 500, 2500, props), mesher.Prism(2000, 2500, 2000, 2500, 500, 1000, props2), mesher.Prism(7500, 8000, 5500, 6500, 500, 1000, props2), mesher.Prism(1500, 2000, 4000, 5000, 500, 1000, props2) ] area = bounds[:4] shape = (50, 50) x, y, z = gridder.regular(area, shape, z=-1) gz = utils.contaminate(prism.gz(x, y, z, model), 0.1) mesh = mesher.PrismMesh(bounds, (20, 40, 40)) seeds = harvester.sow([[5000, 5000, 1000, props]], mesh) # Run the inversion without using weights data = [harvester.Gz(x, y, z, gz)] estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=1.5, threshold=0.001) mesh.addprop('density', estimate['density']) bodies = mesher.vremove(0, 'density', mesh) mpl.figure() mpl.axis('scaled') mpl.title('No weights: Observed (color) vs Predicted (black)') levels = mpl.contourf(y, x, gz, shape, 17) mpl.colorbar()
# Set up the inversion: # Create a prism mesh mesh = PrismMesh(bounds, (15, 50, 50)) # Wrap the data so that harvester can use it data = [harvester.Gyy(x, y, z, gyy), harvester.Gyz(x, y, z, gyz), harvester.Gzz(x, y, z, gzz)] # and the seeds seeds = harvester.sow( [(800, 3250, 600, {'density': 1200}), (1200, 3250, 600, {'density': 1200}), (1700, 3250, 600, {'density': 1200}), (2100, 3250, 600, {'density': 1200}), (2500, 3250, 600, {'density': 1200}), (2900, 3250, 600, {'density': 1200}), (3300, 3250, 600, {'density': 1200}), (3700, 3250, 600, {'density': 1200}), (4200, 3250, 600, {'density': 1200}), (3300, 2050, 600, {'density': 1200}), (3600, 2050, 600, {'density': 1200}), (4000, 2050, 600, {'density': 1200}), (4300, 2050, 600, {'density': 1200})], mesh) # Run the inversion and collect the results estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=1., threshold=0.0001) # Insert the estimated density values into the mesh mesh.addprop('density', estimate['density']) # and get only the prisms corresponding to our estimate density_model = vremove(0, 'density', mesh)
gzz = utils.contaminate(prism.gzz(x, y, z, model), 1) # Wrap the data so that harvester can use it data = [harvester.Gxy(x, y, z, gxy), harvester.Gzz(x, y, z, gzz)] # Create a prism mesh mesh = PrismMesh(bounds, (20, 50, 50)) # and the seeds seeds = harvester.sow( [(901, 701, 750, {'density': 1500}), (901, 1201, 750, {'density': 1500}), (901, 1701, 750, {'density': 1500}), (901, 2201, 750, {'density': 1500}), (901, 2701, 750, {'density': 1500}), (901, 3201, 750, {'density': 1500}), (901, 3701, 750, {'density': 1500}), (3701, 1201, 501, {'density': 1000}), (3201, 1201, 501, {'density': 1000}), (3701, 1701, 501, {'density': 1000}), (3201, 1701, 501, {'density': 1000}), (2951, 3951, 301, {'density': 800}), (2951, 3951, 701, {'density': 800})], mesh) # Run the inversion and collect the results estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=1, threshold=0.0001) # Insert the estimated density values into the mesh mesh.addprop('density', estimate['density']) # and get only the prisms corresponding to our estimate density_model = vremove(0, 'density', mesh)
from fatiando.vis import mpl, myv # Generate some synthetic total field anomaly data bounds = [0, 10000, 0, 10000, 0, 5000] props = {'density':500} props2 = {'density':1000} model = [mesher.Prism(4000, 6000, 4000, 6000, 500, 2500, props), mesher.Prism(2000, 2500, 2000, 2500, 500, 1000, props2), mesher.Prism(7500, 8000, 5500, 6500, 500, 1000, props2), mesher.Prism(1500, 2000, 4000, 5000, 500, 1000, props2)] area = bounds[:4] shape = (50, 50) x, y, z = gridder.regular(area, shape, z=-1) gz = utils.contaminate(prism.gz(x, y, z, model), 0.1) mesh = mesher.PrismMesh(bounds, (20, 40, 40)) seeds = harvester.sow([[5000, 5000, 1000, props]], mesh) # Run the inversion without using weights data = [harvester.Gz(x, y, z, gz)] estimate, predicted = harvester.harvest(data, seeds, mesh, compactness=1.5, threshold=0.001) mesh.addprop('density', estimate['density']) bodies = mesher.vremove(0, 'density', mesh) mpl.figure() mpl.axis('scaled') mpl.title('No weights: Observed (color) vs Predicted (black)') levels = mpl.contourf(y, x, gz, shape, 17) mpl.colorbar() mpl.contour(y, x, predicted[0], shape, levels, color='k') mpl.m2km() mpl.xlabel('East (km)')
cases = ['above', 'below', 'north', 'south', 'east', 'west'] # Create reference model bounds = (0, 3, 0, 3, 0, 3) shape = (3, 3, 3) shapegz = (10, 10) for testcase in cases: mref = PrismMesh(bounds, shape) mesh = mref.copy() mref.addprop('density', [fill(i, testcase) for i in xrange(mref.size)]) # Calculate reference gravity field xp, yp, zp = gridder.regular(bounds[:4], shapegz, z=-1) gzref = prism.gz(xp, yp, zp, mref) # Initiate harvest hgref = [harvester.Gz(xp, yp, zp, gzref)] loc = [[1.5, 1.5, 1.5, {'density': 10}]] seeds = harvester.sow(loc, mesh) # est0 should be incorrect and thus fail wilst est1 should yield the # same geometry as mref est0, pred0 = harvester.harvest(hgref, seeds, mesh, compactness=0.1, threshold=0.001, restrict=[testcase]) est1, pred1 = harvester.harvest(hgref, seeds, mesh, compactness=0.1, threshold=0.001) res0 = mesh.copy() res0.addprop('density', est0['density']) res1 = mesh.copy() res1.addprop('density', est1['density']) l0 = [] l1 = [] for i, p in enumerate(res0): l0.append(p.props['density'] == mref[i].props['density']) for i, p in enumerate(res1):