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)
# Wrap the data so that harvester can use it data = [gm.harvester.Gxx(xp, yp, zp, gxx), gm.harvester.Gxy(xp, yp, zp, gxy), gm.harvester.Gxz(xp, yp, zp, gxz), gm.harvester.Gyy(xp, yp, zp, gyy), gm.harvester.Gyz(xp, yp, zp, gyz), gm.harvester.Gzz(xp, yp, zp, gzz)] # Make the seeds seeds = gm.harvester.sow([ [500, 400, 210, {'density':1000}], [500, 550, 510, {'density':1000}]], mesh) # Run the inversioin estimate, predicted = gm.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) mpl.colorbar() mpl.contour(yp, xp, predicted[i], shape, levels, color='k') mpl.xlabel('y (km)') mpl.ylabel('x (km)') mpl.m2km() mpl.figure()
gm.harvester.Gzz(xp, yp, zp, gzz) ] # Make the seeds seeds = gm.harvester.sow([[500, 400, 210, { 'density': 1000 }], [500, 550, 510, { 'density': 1000 }]], mesh) # Run the inversioin estimate, predicted = gm.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) mpl.colorbar() mpl.contour(yp, xp, predicted[i], shape, levels, color='k') mpl.xlabel('y (km)') mpl.ylabel('x (km)') mpl.m2km() mpl.figure()
-------------------- The mesh classes in Fatiando are more efficient ways of representing regular meshes than simples lists of :class:`~fatiando.mesher.Prism` objects. This is how you can create a :class:`~fatiando.mesher.PrismMesh` and assign it a density for each prism. """ from __future__ import print_function from fatiando.mesher import PrismMesh from fatiando.vis import myv mesh = PrismMesh(bounds=(0, 100, 0, 200, 0, 150), shape=(5, 6, 7)) # We'll give each prism a density value corresponding to it's index on the # mesh. Notice that meshes take lists/arrays as their property values mesh.addprop('density', list(range(mesh.size))) # You can iterate over meshes like lists of elements for p in mesh: print(p.props['density'], end=' ') scene = myv.figure(size=(600, 600)) # Because you can iterate over a mesh, you can pass it anywhere a list of # prisms is accepted plot = myv.prisms(mesh, prop='density') # The code below enables and configures the color bar. This will be automated # on a function in the future (write to the mailing list if you'd like to help # out!) plot.module_manager.scalar_lut_manager.show_scalar_bar = True plot.module_manager.scalar_lut_manager.lut_mode = 'Greens' plot.module_manager.scalar_lut_manager.reverse_lut = True
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() mpl.show() # Plot the result myv.figure() myv.polyprisms(model, "density", opacity=0.6, linewidth=5) myv.prisms(vremove(0, "density", mesh), "density")
mpl.ylabel('Horizontal coordinate x (km)') mpl.m2km() mpl.show() # Inversion setup # Create a mesh mesh = PrismMesh(bounds, (25, 25, 25)) # Wrap the data so that harvester can use it data = [gm.harvester.TotalField(xp, yp, zp, tf, inc, dec)] # Make the seed seeds = gm.harvester.sow([[500, 500, 450, {'magnetization':mag}]], mesh) # Run the inversioin estimate, predicted = gm.harvester.harvest(data, seeds, mesh, compactness=1, threshold=0.0001) #Put the estimated magnetization vector in the mesh mesh.addprop('magnetization', estimate['magnetization']) # Plot the adjustment mpl.figure() mpl.title("True: color | Inversion: contour") mpl.axis('scaled') levels = mpl.contourf(yp, xp, tf, 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() residuals = tf - predicted[0] mpl.figure() mpl.title('Residuals: mean=%g stddev=%g' % (residuals.mean(), residuals.std())) mpl.hist(residuals, bins=10)
def test_prism_mesh_copy(): p1 = PrismMesh((0, 1, 0, 2, 0, 3), (1, 2, 2)) p1.addprop('density', 3200 + np.zeros(p1.size)) p2 = p1.copy() assert p1 is not p2 assert np.array_equal(p1.props['density'], p2.props['density'])
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'])