This 3D histogram class allows efficient updating of histograms, plotting and saving as HDF5. """ #%% import geobipy from geobipy import StatArray from geobipy import Histogram import matplotlib.pyplot as plt from geobipy import RectilinearMesh3D import numpy as np #%% # Create some histogram bins in x and y x = StatArray(np.linspace(-4.0, 4.0, 11), 'Variable 1') y = StatArray(np.linspace(-4.0, 4.0, 21), 'Variable 2') z = StatArray(np.linspace(-4.0, 4.0, 31), 'Variable 3') mesh = RectilinearMesh3D(xEdges=x, yEdges=y, zEdges=z) ################################################################################ # Instantiate H = Histogram(mesh=mesh) ################################################################################ # Generate some random numbers a = np.random.randn(100000) b = np.random.randn(100000) c = np.random.randn(100000) x = np.asarray([a, b, c])
------------------- This 3D rectilinear mesh defines a grid with straight cell boundaries. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh3D import matplotlib.pyplot as plt import numpy as np import h5py #%% # Specify some cell centres in x and y x = StatArray(np.arange(10.0), 'Easting', 'm') y = StatArray(np.arange(11.0), 'Northing', 'm') z = StatArray(np.arange(12.0), 'Depth', 'm') rm = RectilinearMesh3D(xEdges=x, yEdges=y, zEdges=z) rm1 = rm[:5, :5, :5] rm2 = rm[:, :, 5] rm3 = rm[:, 5, :] rm4 = rm[5, :, :] plt.figure() plt.subplot(231) rm2.plotGrid() plt.subplot(232) rm3.plotGrid()
# Send and Recv Left myMPI.IsendToLeft(x, world) z = myMPI.IrecvFromRight(world) tst = [[1, 1], [2, 2, 2], [3, 3, 3, 3], [0]] assert np.all(z == tst[rank]), Exception("d) Could not Isend/Irecv Right to Left. Rank {}".format(rank)) myMPI.IsendToRight(x, world) z = myMPI.IrecvFromLeft(world) tst = [[3, 3, 3, 3], 0, [1, 1], [2, 2, 2]] assert np.all(z == tst[rank]), Exception("e) Could not Isend/Irecv Left to Right. Rank {}".format(rank)) ## Test the StatArray routines x = StatArray((rank+1) * np.arange(N, dtype=np.float64), "name", "units") # Bcast y = x.Bcast(world) assert np.all(y == [0.0, 1.0, 2.0, 3.0, 4.0]), Exception("Could not use StatArray.Bcast. Rank {}".format(rank)) # Scatterv y = x.Scatterv(starts, chunks, world) tst = [[0.0, 1.0], 2.0, 3.0, 4.0 ] assert np.all(y == tst[rank]), Exception("Could not use StatArray.Scatterv. Rank {}".format(rank)) # Send and Recv z = None if master: x.Isend(1, world)
Topo Rectilinear Mesh 2D ------------------------ The Topo version of the rectilinear mesh has the same functionality as the :ref:`Rectilinear Mesh 2D` but the top surface of the mesh can undulate. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh2D import matplotlib.pyplot as plt import numpy as np #%% # Specify some cell centres in x and y x = StatArray(np.arange(11.0), 'Easting', 'm') y = StatArray(np.arange(11.0), 'Depth', 'm') # Create a height profile for the mesh height = StatArray( np.asarray([5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5]) * 3.0, 'Height', 'm') # Instantiate the mesh rm = RectilinearMesh2D(xEdges=x, yEdges=y, heightEdges=height) ################################################################################ # Plot only the grid lines of the mesh plt.figure() _ = rm.plotGrid(linewidth=0.5) ################################################################################ # Create some cell values values = StatArray(np.random.random(rm.shape), 'Name', 'Units')
""" #%% import h5py from geobipy import hdfRead from geobipy import StatArray from geobipy import Histogram1D import numpy as np import matplotlib.pyplot as plt #%% # Histogram with regular bins # +++++++++++++++++++++++++++ # Create regularly spaced bins bins = StatArray(np.linspace(-3.0, 3.0, 101), 'Regular bins') ################################################################################ # Set the histogram using the bins, and update H = Histogram1D(bins=bins) ################################################################################ # We can update the histogram with some new values x = np.random.randn(1000) H.update(x, clip=True, trim=True) # Plot the histogram plt.figure() _ = H.plot()
""" #%% from geobipy import StatArray from geobipy import Model1D from geobipy import Distribution import matplotlib.pyplot as plt import numpy as np import h5py from geobipy import hdfRead ################################################################################ # Make a test model with 10 layers, and increasing parameter values par = StatArray(np.linspace(0.001, 0.02, 10), "Conductivity", "$\\frac{S}{m}$") thk = StatArray(np.arange(1, 11)) mod = Model1D(parameters=par, thickness=thk, hasHalfspace=False) ################################################################################ # Randomness and Model Perturbations # ++++++++++++++++++++++++++++++++++ # We can set the priors on the 1D model by assigning minimum and maximum layer # depths and a maximum number of layers. These are used to create priors on # the number of cells in the model, a new depth interface, new parameter values # and the vertical gradient of those parameters. # The halfSpaceValue is used as a reference value for the parameter prior. prng = np.random.RandomState() # Set the priors mod.setPriors(halfSpaceValue=0.01, minDepth=1.0,
A common bottleneck with large parallel algorithms is the input output of information to disk. We use HDF5 to read and write data in order to leverage the parallel capabililties of the HDF5 API. Each object within GeoBIPy has a create_hdf, write_hdf, and read_hdf routine. """ import numpy as np import h5py from geobipy import StatArray ################################################################################ # StatArray # +++++++++ # Instantiate a StatArray x = StatArray(np.arange(10.0), name = 'an Array', units = 'some units') # Write the StatArray to a HDF file. with h5py.File("x.h5", 'w') as f: x.toHdf(f, "x") # Read the StatArray back in. with h5py.File("x.h5", 'r') as f: y = StatArray.fromHdf(f, 'x') print('x', x) print('y', y) ################################################################################ # There are actually steps within the "toHdf" function. # First, space is created within the HDF file and second, the data is written to that space
This 2D histogram class allows efficient updating of histograms, plotting and saving as HDF5. """ #%% import geobipy from geobipy import StatArray from geobipy import Histogram2D import matplotlib.pyplot as plt import numpy as np #%% # Create some histogram bins in x and y x = StatArray(np.linspace(-4.0, 4.0, 101), 'Variable 1') y = StatArray(np.linspace(-4.0, 4.0, 101), 'Variable 2') ################################################################################ # Instantiate H = Histogram2D(xBins=x, yBins=y) ################################################################################ # Generate some random numbers a = np.random.randn(1000000) b = np.random.randn(1000000) x = np.asarray([a, b]) ################################################################################ # Update the histogram counts H.update(x)
This 2D rectilinear model defines a grid with straight cell boundaries. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh2D from geobipy import Model import h5py import matplotlib.pyplot as plt import numpy as np #%% # Specify some cell centres in x and y x = StatArray(np.arange(11.0), 'Easting', 'm') y = StatArray(np.arange(11.0), 'Northing', 'm') mesh = RectilinearMesh2D(xEdges=x, yEdges=y) xx, yy = np.meshgrid(mesh.x.centres, mesh.y.centres) values = StatArray(np.sin(np.sqrt(xx ** 2.0 + yy ** 2.0)), "Values") mod = Model(mesh=mesh, values = values) plt.figure() mod.pcolor() mod2 = mod.resample(0.5, 0.5) mod3 = mod.resample(1.5, 1.5) plt.figure() plt.subplot(121)
""" 1D Rectilinear Mesh ------------------- """ #%% from geobipy import StatArray from geobipy import RectilinearMesh1D import matplotlib.pyplot as plt import numpy as np #%% # Instantiate a new 1D rectilinear mesh by specifying cell centres or edges. # Here we use edges x = StatArray(np.cumsum(np.arange(0.0, 10.0)), 'Depth', 'm') ################################################################################ rm = RectilinearMesh1D(cellEdges=x) ################################################################################ print(rm.cellCentres) ################################################################################ print(rm.cellEdges) ################################################################################ print(rm.internalCellEdges) ################################################################################ print(rm.cellWidths) ################################################################################
------------------------ The Topo version of the rectilinear mesh has the same functionality as the :ref:`Rectilinear Mesh 2D` but the top surface of the mesh can undulate. """ #%% from geobipy import StatArray from geobipy import TopoRectilinearMesh2D import matplotlib.pyplot as plt import numpy as np #%% # Specify some cell centres in x and y x = StatArray(np.arange(10.0), 'Easting', 'm') y = StatArray(np.arange(10.0), 'Height', 'm') # Create a height profile for the mesh height = StatArray(np.asarray([5,4,3,2,1,1,2,3,4,5])*3.0, 'Height', 'm') # Instantiate the mesh rm = TopoRectilinearMesh2D(xCentres=x, yCentres=y, heightCentres=height) ################################################################################ # Plot only the grid lines of the mesh plt.figure() _ = rm.plotGrid(linewidth=0.5) ################################################################################ # Create some cell values values = StatArray(np.random.random(rm.shape), 'Name', 'Units')
The second embeds the 2D mesh in 3D by providing the cell centres or edges in three dimensions. The first two dimensions specify the mesh coordinates in the horiztontal cartesian plane while the third discretizes in depth. This allows us to characterize a mesh whose horizontal coordinates do not follow a line that is parallel to either the "x" or "y" axis. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh2D import matplotlib.pyplot as plt import numpy as np #%% # Specify some cell centres in x and y x = StatArray(np.arange(10.0), 'Easting', 'm') y = StatArray(np.arange(10.0), 'Northing', 'm') rm = RectilinearMesh2D(xCentres=x, yCentres=y) ################################################################################ # We can plot the grid lines of the mesh. plt.figure() _ = rm.plotGrid(linewidth=0.5) ################################################################################ # 2D Mesh embedded in 3D # ++++++++++++++++++++++ z = StatArray(np.cumsum(np.arange(15.0)), 'Depth', 'm') rm = RectilinearMesh2D(xCentres=x, yCentres=y, zCentres=z) ################################################################################
# D = FdemData.read_csv(dataFile,systemFile) # ################################################################################ # # Get a data point from the dataset # fdp = D.datapoint(0) # plt.figure() # _ = fdp.plot() ############################################################################### # Using a datapoint # +++++++++++++++++ ################################################################################ # We can define a 1D layered earth model, and use it to predict some data nCells = 19 par = StatArray(np.linspace(0.01, 0.1, nCells), "Conductivity", "$\frac{S}{m}$") depth = StatArray(np.arange(nCells + 1) * 10.0, "Depth", 'm') depth[-1] = np.inf mod = Model(mesh=RectilinearMesh1D(edges=depth), values=par) ################################################################################ # Forward model the data fdp.forward(mod) ############################################################################### plt.figure() plt.subplot(121) _ = mod.pcolor(transpose=True) plt.subplot(122) _ = fdp.plotPredicted() plt.tight_layout()
------------------- """ #%% from copy import deepcopy from geobipy import StatArray from geobipy import RectilinearMesh1D, RectilinearMesh2D, RectilinearMesh2D_stitched import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import numpy as np import h5py #%% # The basics # ++++++++++ # Instantiate a new 1D rectilinear mesh by specifying cell centres, edges, or widths. x = StatArray(np.cumsum(np.arange(0.0, 10.0)), 'Depth', 'm') ################################################################################ # Cell edges rm = RectilinearMesh1D(edges=x, centres=None, widths=None) ################################################################################ # We can plot the grid of the mesh # Or Pcolor the mesh showing. An array of cell values is used as the colour. arr = StatArray(np.random.randn(*rm.shape), "Name", "Units") p = 0 plt.figure(p) plt.subplot(121) _ = rm.plotGrid(transpose=True, flip=True) plt.subplot(122) _ = rm.pcolor(arr, grid=True, transpose=True, flip=True)
""" 1D Rectilinear Mesh ------------------- """ #%% from geobipy import StatArray from geobipy import RectilinearMesh1D import matplotlib.pyplot as plt import numpy as np #%% # The basics # ++++++++++ # Instantiate a new 1D rectilinear mesh by specifying cell centres, edges, or widths. x = StatArray(np.cumsum(np.arange(0.0, 10.0)), 'Depth', 'm') ################################################################################ # Cell widths rm = RectilinearMesh1D(widths=np.full(10, fill_value=50.0)) ################################################################################ # Cell centres rm = RectilinearMesh1D(centres=x) ################################################################################ # Cell edges rm = RectilinearMesh1D(edges=x) ################################################################################ # Cell centres print(rm.centres)
The second embeds the 2D mesh in 3D by providing the cell centres or edges in three dimensions. The first two dimensions specify the mesh coordinates in the horiztontal cartesian plane while the third discretizes in depth. This allows us to characterize a mesh whose horizontal coordinates do not follow a line that is parallel to either the "x" or "y" axis. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh2D import matplotlib.pyplot as plt import numpy as np #%% # Specify some cell centres in x and y x = StatArray(np.arange(10.0), 'Easting', 'm') y = StatArray(np.arange(10.0), 'Northing', 'm') rm = RectilinearMesh2D(xCentres=x, yCentres=y) ################################################################################ # We can plot the grid lines of the mesh. plt.figure() _ = rm.plotGrid(linewidth=0.5) ############################################################################### # Intersecting lines with a mesh arr = np.zeros(rm.shape) ################################################################################ # Intersecting multisegment lines with a mesh # arr = np.zeros(rm.shape)
-------------------- This 3D rectilinear model defines a grid with straight cell boundaries. """ #%% from geobipy import StatArray from geobipy import RectilinearMesh3D from geobipy import Model import matplotlib.pyplot as plt import numpy as np import h5py #%% # Specify some cell centres in x and y x = StatArray(np.arange(10.0), 'Easting', 'm') y = StatArray(np.arange(11.0), 'Northing', 'm') z = StatArray(np.arange(12.0), 'Depth', 'm') xx, yy = np.meshgrid(x.internalEdges(), y.internalEdges()) height = StatArray(np.sin(np.sqrt(xx**2.0 + yy**2.0)), "Height") rm = RectilinearMesh3D(xEdges=x, yEdges=y, zEdges=z, height=height) values = StatArray(np.repeat(height[None, :, :], rm.z.nCells, 0), "Values") mod = Model(mesh=rm, values=values) ################################################################################ # We can plot the mesh in 3D! pv = mod.pyvista_mesh()
# Get a datapoint from the file. tdp = Dataset._read_record(0) print(tdp.summary) plt.figure() tdp.plot() ################################################################################ # Using a time domain datapoint # +++++++++++++++++++++++++++++ ################################################################################ # We can define a 1D layered earth model, and use it to predict some data par = StatArray( np.r_[1 / 50.0, 1 / 100.0, 1 / 1000.0, 1 / 5.0, 1 / 1000.0, 1 / 800.0], "Conductivity", "$\frac{S}{m}$") mod = Model(mesh=RectilinearMesh1D(edges=np.r_[0, 20.0, 50.0, 100.0, 150.0, 250.0, np.inf]), values=par) ################################################################################ # Forward model the data tdp.forward(mod) ################################################################################ plt.figure() plt.subplot(121) _ = mod.pcolor(transpose=True) plt.subplot(122) _ = tdp.plot()
from geobipy import StatArray from geobipy import Model1D from geobipy import Distribution from geobipy import FdemData import matplotlib.pyplot as plt import numpy as np import h5py from geobipy import hdfRead # %% # Instantiate the 1D Model with a Half Space # ++++++++++++++++++++++++++++++++++++++++++ # Make a test model with 10 layers, and increasing parameter values nLayers = 2 par = StatArray(np.linspace(0.001, 0.02, nLayers), "Conductivity", "$\\frac{S}{m}$") thk = StatArray(np.full(nLayers, fill_value=10.0)) thk[-1] = np.inf mod = Model1D(parameters=par, widths=thk) # plt.figure() # mod.plotGrid(transpose=True, flip=True) ################################################################################ # Randomness and Model Perturbations # ++++++++++++++++++++++++++++++++++ # We can set the priors on the 1D model by assigning minimum and maximum layer # depths and a maximum number of layers. These are used to create priors on # the number of cells in the model, a new depth interface, new parameter values # and the vertical gradient of those parameters. # The halfSpaceValue is used as a reference value for the parameter prior.
# Prepare the dataset so that we can read a point at a time. Dataset = TdemData._initialize_sequential_reading(dataFile, systemFile) # Get a datapoint from the file. tdp = Dataset._read_record() Dataset._file.close() # plt.figure() # tdp.plot() ################################################################################ # Using a time domain datapoint # +++++++++++++++++++++++++++++ ################################################################################ # We can define a 1D layered earth model, and use it to predict some data par = StatArray(np.r_[500.0, 20.0], "Conductivity", "$\frac{S}{m}$") mod = Model1D(edges=np.r_[0, 75.0, np.inf], parameters=par) ################################################################################ # Forward model the data tdp.forward(mod) ################################################################################ # plt.figure() # plt.subplot(121) # _ = mod.pcolor() # plt.subplot(122) # _ = tdp.plot() # _ = tdp.plotPredicted() # plt.tight_layout()
#%% from geobipy import StatArray from geobipy import Histogram1D import numpy as np import matplotlib.pyplot as plt import h5py from geobipy import hdfRead #%% # Instantiating a new StatArray class # +++++++++++++++++++++++++++++++++++ # # The StatArray can take any numpy function that returns an array as an input. # The name and units of the variable can be assigned to the StatArray. Density = StatArray(np.random.randn(1), name="Density", units="$\frac{g}{cc}$") Density.summary() #%% # Attaching Prior and Proposal Distributions to a StatArray # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # # The StatArray class has been built so that we may easily # attach not only names and units, but statistical distributions too. # We won't go into too much detail about the different distribution # classes here so check out the :ref:`Distribution Class` for a better description. # # Two types of distributions can be attached to the StatArray. # # * Prior Distribution # The prior represents how the user believes the variable should
D = FdemData.read_csv(dataFile, systemFile) ################################################################################ # Get a data point from the dataset fdp = D.datapoint(0) plt.figure() _ = fdp.plot() ################################################################################ # Using a datapoint # +++++++++++++++++ ################################################################################ # We can define a 1D layered earth model, and use it to predict some data nCells = 19 par = StatArray(np.linspace(0.01, 0.1, nCells), "Conductivity", "$\frac{S}{m}$") thk = StatArray(np.ones(nCells) * 10.0) thk[-1] = np.inf mod = Model1D(nCells=nCells, parameters=par, widths=thk) ################################################################################ # Forward model the data fdp.forward(mod) ############################################################################### plt.figure() plt.subplot(121) _ = mod.pcolor() plt.subplot(122) _ = fdp.plotPredicted() plt.tight_layout()
#%% from geobipy.src.classes.mesh.RectilinearMesh1D import RectilinearMesh1D import h5py from geobipy import hdfRead from geobipy import StatArray from geobipy import Histogram import numpy as np import matplotlib.pyplot as plt #%% # Histogram with regular bins # +++++++++++++++++++++++++++ # Create regularly spaced bins mesh = RectilinearMesh1D( edges=StatArray(np.linspace(-3.0, 3.0, 101), 'bins', 'm')) ################################################################################ # Set the histogram using the bins, and update H = Histogram(mesh=mesh) ################################################################################ # We can update the histogram with some new values H.update(np.random.randn(1000), trim=True) # Plot the histogram plt.figure() plt.subplot(221) _ = H.plot() plt.subplot(222) _ = H.pdf.bar()