|
| 1 | +import dataclasses |
| 2 | +import os |
| 3 | + |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from gempy_engine.API.interp_single._interp_scalar_field import _solve_interpolation, _evaluate_sys_eq |
| 8 | +from gempy_engine.API.interp_single._interp_single_feature import input_preprocess |
| 9 | +from gempy_engine.config import AvailableBackends |
| 10 | +from gempy_engine.core.data.internal_structs import SolverInput |
| 11 | +from gempy_engine.modules.activator.activator_interface import activate_formation_block |
| 12 | +from gempy_engine.core.backend_tensor import BackendTensor |
| 13 | + |
| 14 | +dir_name = os.path.dirname(__file__) |
| 15 | + |
| 16 | +plot = True |
| 17 | + |
| 18 | + |
| 19 | +def test_activator_3_layers_segmentation_function(simple_model_3_layers, simple_grid_3d_more_points_grid): |
| 20 | + Z_x, grid, ids_block, interpolation_input = _run_test( |
| 21 | + backend=AvailableBackends.numpy, |
| 22 | + ids=np.array([1, 20, 3, 4]), |
| 23 | + simple_grid_3d_more_points_grid=simple_grid_3d_more_points_grid, |
| 24 | + simple_model_3_layers=simple_model_3_layers |
| 25 | + ) |
| 26 | + |
| 27 | + if plot: |
| 28 | + _plot_continious(grid, ids_block, interpolation_input) |
| 29 | + |
| 30 | + |
| 31 | +def test_activator_3_layers_segmentation_function_II(simple_model_3_layers, simple_grid_3d_more_points_grid): |
| 32 | + Z_x, grid, ids_block, interpolation_input = _run_test( |
| 33 | + backend=AvailableBackends.numpy, |
| 34 | + ids=np.array([1, 2, 3, 4]), |
| 35 | + simple_grid_3d_more_points_grid=simple_grid_3d_more_points_grid, |
| 36 | + simple_model_3_layers=simple_model_3_layers |
| 37 | + ) |
| 38 | + |
| 39 | + BackendTensor.change_backend_gempy(AvailableBackends.numpy) |
| 40 | + |
| 41 | + if plot: |
| 42 | + _plot_continious(grid, ids_block, interpolation_input) |
| 43 | + |
| 44 | + |
| 45 | +def test_activator_3_layers_segmentation_function_torch(simple_model_3_layers, simple_grid_3d_more_points_grid): |
| 46 | + Z_x, grid, ids_block, interpolation_input = _run_test( |
| 47 | + backend=AvailableBackends.PYTORCH, |
| 48 | + ids=np.array([1, 2, 3, 4]), |
| 49 | + simple_grid_3d_more_points_grid=simple_grid_3d_more_points_grid, |
| 50 | + simple_model_3_layers=simple_model_3_layers |
| 51 | + ) |
| 52 | + |
| 53 | + BackendTensor.change_backend_gempy(AvailableBackends.numpy) |
| 54 | + if plot: |
| 55 | + _plot_continious(grid, ids_block, interpolation_input) |
| 56 | + |
| 57 | + |
| 58 | +def _run_test(backend, ids, simple_grid_3d_more_points_grid, simple_model_3_layers): |
| 59 | + interpolation_input = simple_model_3_layers[0] |
| 60 | + options = simple_model_3_layers[1] |
| 61 | + data_shape = simple_model_3_layers[2].tensors_structure |
| 62 | + grid = dataclasses.replace(simple_grid_3d_more_points_grid) |
| 63 | + interpolation_input.set_temp_grid(grid) |
| 64 | + interp_input: SolverInput = input_preprocess(data_shape, interpolation_input) |
| 65 | + weights = _solve_interpolation(interp_input, options.kernel_options) |
| 66 | + exported_fields = _evaluate_sys_eq(interp_input, weights, options) |
| 67 | + exported_fields.set_structure_values( |
| 68 | + reference_sp_position=data_shape.reference_sp_position, |
| 69 | + slice_feature=interpolation_input.slice_feature, |
| 70 | + grid_size=interpolation_input.grid.len_all_grids) |
| 71 | + Z_x: np.ndarray = exported_fields.scalar_field |
| 72 | + sasp = exported_fields.scalar_field_at_surface_points |
| 73 | + print(Z_x, Z_x.shape[0]) |
| 74 | + print(sasp) |
| 75 | + BackendTensor.change_backend_gempy(backend) |
| 76 | + ids_block = activate_formation_block( |
| 77 | + exported_fields=exported_fields, |
| 78 | + ids=ids, |
| 79 | + sigmoid_slope=500 * 4 |
| 80 | + )[0, :-7] |
| 81 | + return Z_x, grid, ids_block, interpolation_input |
| 82 | + |
| 83 | + |
| 84 | +def _plot_continious(grid, ids_block, interpolation_input): |
| 85 | + block__ = ids_block[grid.dense_grid_slice] |
| 86 | + unique = np.unique(block__) |
| 87 | + t = block__.reshape(50, 5, 50)[:, 2, :].T |
| 88 | + unique = np.unique(t) |
| 89 | + |
| 90 | + levels = np.linspace(t.min(), t.max(), 40) |
| 91 | + plt.contourf( |
| 92 | + t, |
| 93 | + levels=levels, |
| 94 | + cmap="jet", |
| 95 | + extent=(.25, .75, .25, .75) |
| 96 | + ) |
| 97 | + xyz = interpolation_input.surface_points.sp_coords |
| 98 | + plt.plot(xyz[:, 0], xyz[:, 2], "o") |
| 99 | + plt.colorbar() |
| 100 | + plt.show() |
0 commit comments