|
4 | 4 |
|
5 | 5 | This example demonstrates a probabilistic inversion of a geological model using Bayesian methods.
|
6 | 6 | """
|
7 |
| - |
| 7 | +import numpy as np |
8 | 8 | import os
|
9 |
| -import time |
10 | 9 |
|
11 | 10 | import arviz as az
|
12 |
| -import numpy as np |
| 11 | +import dotenv |
| 12 | +import gempy_engine |
| 13 | +import gempy_viewer as gpv |
13 | 14 | import pyro
|
14 | 15 | import pyro.distributions as dist
|
15 | 16 | import torch
|
16 |
| -import xarray as xr |
17 |
| -from dotenv import dotenv_values |
| 17 | +from gempy_engine.core.backend_tensor import BackendTensor |
18 | 18 | from matplotlib import pyplot as plt
|
19 | 19 | from pyro.infer import MCMC, NUTS, Predictive
|
20 | 20 |
|
21 | 21 | import gempy as gp
|
22 |
| -import gempy_engine |
23 |
| -import gempy_viewer as gpv |
24 |
| - |
25 |
| -from examples.examples._aux_func_gravity_inversion import calculate_scale_shift, gaussian_kernel, initialize_geo_model, setup_geophysics |
26 |
| -from examples.examples._aux_func_gravity_inversion_II import process_file |
27 |
| - |
28 |
| -from gempy_engine.core.backend_tensor import BackendTensor |
| 22 | +from examples.examples._aux_func_gravity_inversion import setup_geophysics |
| 23 | +from gempy_probability.modules.likelihoods.gravity_likelihoods import calculate_scale_shift, gaussian_kernel |
29 | 24 | from gempy_probability.modules.plot.plot_posterior import default_red, default_blue
|
30 | 25 |
|
| 26 | +dotenv.load_dotenv() |
| 27 | + |
31 | 28 | # %%
|
32 | 29 | # Config
|
33 | 30 | seed = 123456
|
34 | 31 | torch.manual_seed(seed)
|
35 | 32 | pyro.set_rng_seed(seed)
|
36 | 33 |
|
37 |
| -# %% |
38 |
| -# Start the timer for benchmarking purposes |
39 |
| -start_time = time.time() |
40 |
| - |
41 |
| -# %% |
42 |
| -# Load necessary configuration and paths from environment variables |
43 |
| -config = dotenv_values() |
44 |
| -path = config.get("PATH_TO_MODEL_1_Subsurface") |
45 |
| - |
46 |
| -# %% |
47 |
| -# Initialize lists to store structural elements for the geological model |
48 |
| -structural_elements = [] |
49 |
| -global_extent = None |
50 |
| -color_gen = gp.data.ColorsGenerator() |
51 |
| - |
52 |
| -# %% |
53 |
| -# Process each .nc file in the specified directory for model construction |
54 |
| -for filename in os.listdir(path): |
55 |
| - base, ext = os.path.splitext(filename) |
56 |
| - if ext == '.nc': |
57 |
| - structural_element, global_extent = process_file(os.path.join(path, filename), global_extent, color_gen) |
58 |
| - structural_elements.append(structural_element) |
59 |
| - |
60 |
| -# %% |
61 |
| -# Configure GemPy for geological modeling with PyTorch backend |
62 |
| -BackendTensor.change_backend_gempy(engine_backend=gp.data.AvailableBackends.PYTORCH, dtype="float64") |
63 |
| - |
64 |
| -geo_model = initialize_geo_model( |
65 |
| - structural_elements=structural_elements, |
66 |
| - extent=(np.array(global_extent)), |
67 |
| - topography=(xr.open_dataset(os.path.join(path, "Topography.nc"))), |
68 |
| - load_nuggets=False |
69 |
| -) |
70 |
| - |
71 |
| -# TODO: Here is where we are setting the nuggets |
72 |
| -# * ============================================ |
| 34 | +model_path = os.getenv("PATH_TO_NUGGET_TEST_MODEL") |
| 35 | +model_file = os.path.join(model_path, "nugget_effect_optimization.gempy") |
| 36 | +geo_model = gp.API.load_model(model_file) |
73 | 37 |
|
74 | 38 | # %%
|
| 39 | +# Forward Gravity |
| 40 | +# --------------- |
75 | 41 | # Setup geophysics configuration for the model
|
76 | 42 | geophysics_input = setup_geophysics(
|
77 | 43 | env_path="PATH_TO_MODEL_1_BOUGUER",
|
78 |
| - geo_model=geo_model |
| 44 | + geo_model=geo_model, |
| 45 | + densities=[2.61, 2.92, 3.1, 2.92, 2.61, 2.61, 2.61] |
79 | 46 | )
|
80 | 47 |
|
81 | 48 | # %%
|
|
87 | 54 |
|
88 | 55 | # %%
|
89 | 56 | # Compute the geological model
|
90 |
| -sol = gp.compute_model( |
| 57 | +sol: gp.data.Solutions = gp.compute_model( |
91 | 58 | gempy_model=geo_model,
|
92 | 59 | engine_config=gp.data.GemPyEngineConfig(
|
93 | 60 | backend=gp.data.AvailableBackends.PYTORCH,
|
94 | 61 | dtype='float64'
|
95 |
| - ) |
| 62 | + ), |
| 63 | + validate_serialization=False # TODO: [ ] Validate this serialization |
96 | 64 | )
|
97 | 65 |
|
98 | 66 | # %%
|
|
106 | 74 |
|
107 | 75 | # %%
|
108 | 76 | # Calculate and adapt the observed gravity data for model comparison
|
109 |
| -grav = sol.gravity |
110 |
| -s, c = calculate_scale_shift( |
111 |
| - a=geophysics_input["Bouguer_267_complete"].values, |
112 |
| - b=(grav.detach().numpy()) |
| 77 | +scale_factor, shift_term = calculate_scale_shift( |
| 78 | + measured=geophysics_input["Bouguer_267_complete"].values, |
| 79 | + simulated=sol.gravity.detach().numpy() |
113 | 80 | )
|
114 |
| -adapted_observed_grav = s * geophysics_input["Bouguer_267_complete"] + c |
| 81 | +adapted_observed_grav: np.dtype[float] = scale_factor * geophysics_input["Bouguer_267_complete"] + shift_term |
115 | 82 |
|
116 | 83 | # %%
|
117 | 84 | # Plot the 2D gravity data for visualization
|
|
127 | 94 | plt.colorbar(sc, label="mGal")
|
128 | 95 | plt.show()
|
129 | 96 |
|
| 97 | + |
130 | 98 | # %%
|
| 99 | +# Define Probabilistic model |
| 100 | +# -------------------------- |
131 | 101 | # Define hyperparameters for the Bayesian geological model
|
132 | 102 | length_scale_prior = torch.tensor(1_000.0)
|
133 | 103 | variance_prior = torch.tensor(25.0 ** 2)
|
|
141 | 111 | densities=prior_tensor,
|
142 | 112 | )
|
143 | 113 |
|
| 114 | +bar |
144 | 115 |
|
145 | 116 | # %%
|
146 | 117 | # Define the Pyro probabilistic model for inversion
|
|
0 commit comments