gempy icon indicating copy to clipboard operation
gempy copied to clipboard

Uncertainty quantification takes a lot of time with Gempy 2024

Open Anagabrielamantilla opened this issue 11 months ago • 2 comments

Hello, I am using gempy version 2024.2.0.2. I am doing the uncertainty quantification using Monte Carlo method of my gempy model named "loaded_model". The numbers "205134058, 363156420, 128925917, 36792972" are the ID of each layer. However, when I start to run the code it takes a long time it has been running for an hour and does not finish or show anything. Is this normal? How much time does it take? How can I make it faster? I appreciate any comments that help me improve my code.

` indices_bool_1 = loaded_model.surface_points_copy.df['id'].isin([205134058, 363156420, 128925917, 36792972])

indices_1 = loaded_model.surface_points_copy.df.index[indices_bool_1]

Z_init_1 = loaded_model.surface_points_copy.df.loc[(indices_1, ['Z', 'id'])].copy()

df = pd.DataFrame(Z_init_1)

capa1 = df[df['id'] == 205134058]['Z'] capa2 = df[df['id'] == 363156420]['Z'] capa3 = df[df['id'] == 128925917]['Z'] capa4 = df[df['id'] == 36792972]['Z']

def sample(silent=True): Z_var = np.random.normal(0, 30, size=4) Z_loc = np.hstack([capa1 + Z_var[0], capa2 + Z_var[1], capa3 + Z_var[2], capa4 + Z_var[3]])

if silent:
    #loaded_model.modify_surface_points(indices_1, Z=Z_loc)
    loaded_model.surface_points_copy.df.loc[indices_1, 'Z'] = Z_loc
    gp.compute_model(loaded_model)
else:
    # Create an object
    plot_object = gp.plot_2d(loaded_model)
    
    # Pass the object as argument of modify_surface_points
    loaded_model.surface_points_copy.df.loc[indices_1, 'Z'] = Z_loc

    plot_object.show() 

return loaded_model.solutions.lith_block

lith_blocks = np.array([]) n_iterations = 10 # Number of iterations

for i in range(n_iterations): print('iteration number:', i) lith_blocks = np.append(lith_blocks, sample()) `

I have tried reducing the number of iterations but does not work either.

Anagabrielamantilla avatar Dec 26 '24 16:12 Anagabrielamantilla

Hi @Anagabrielamantilla,

thanks for the questiom. I am not an expert in this field - but my first question would be how long a single computation of your model takes. This mainly depends on the resolution you set and the amount of input data. Have you already checked that?

Cheers, Jan

javoha avatar Dec 27 '24 09:12 javoha

Quick addition @Anagabrielamantilla : as speed for each model matters in your case, you could try running the model computation with the PyTorch Backend.

Steps:

  • Install PyTorch (https://pytorch.org/get-started/locally/) - please check that it is also available in the environment in which you are running GemPy (import torch should work)
  • Set the backend to PyTorch in the GemPy compute step:
from gempy_engine.config import AvailableBackends
gp.compute_model(geo_model, engine_config=gp.data.GemPyEngineConfig(
        backend=AvailableBackends.PYTORCH
    ))

Best to check with a single model iteration first, but it should definitely speed up the compute process. If you are running the modeling in IPython (for example, in Jupyter notebooks), you can use the %%timeit-magic for a simple check.

Testing this approach with the "combination model" https://docs.gempy.org/examples/geometries/g07_combination.html#sphx-glr-examples-geometries-g07-combination-py

Results for me (standard Laptop, could also be a lot faster on a better computer) in:

  • Normal backend (numpy): 3min 33s ± 4.15 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
  • PyTorch backend: 1min 31s ± 2.89 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

For more examples on UQ and inversion, see also this repo: https://vector-raw-materials.github.io/vector-geology/index.html

flohorovicic avatar Dec 29 '24 10:12 flohorovicic

Closed due to inactivity, feel free to reopen if necessary.

javoha avatar Aug 18 '25 11:08 javoha