Skip to content

Commit 5f21c7c

Browse files
committed
[WIP] Towards fixing the bar
1 parent 39cae3d commit 5f21c7c

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

gempy_viewer/API/_plot_3d_API.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,21 @@ def plot_3d(
114114
**kwargs_plotter
115115
)
116116

117+
structural_frame = model.structural_frame
117118
if data_to_show.show_topography[0] is True and model.grid.topography is not None:
118119
plot_topography_3d(
119120
gempy_vista=gempy_vista,
120121
topography=model.grid.topography,
121122
solution=solutions_raw_arrays,
122123
topography_scalar_type=topography_scalar_type,
123-
elements_colors=model.structural_frame.elements_colors[::-1],
124+
elements_colors=structural_frame.elements_colors[::-1],
124125
contours=kwargs_plot_topography.get('contours', True),
125126
**kwargs_plot_topography
126127
)
127128

128129
if data_to_show.show_boundaries[0] is True:
129130
# Check elements to plot .vertices are not empty
130-
elements_to_plot = model.structural_frame.structural_elements
131+
elements_to_plot = structural_frame.structural_elements
131132
for element in elements_to_plot:
132133
if element.vertices is None:
133134
elements_to_plot.remove(element)
@@ -174,7 +175,7 @@ def plot_3d(
174175
scalar_data_type=ScalarDataType.LITHOLOGY,
175176
active_scalar_field="lith",
176177
solution=solutions_raw_arrays,
177-
cmap=get_geo_model_cmap(model.structural_frame.elements_colors_volumes),
178+
cmap=get_geo_model_cmap(structural_frame.elements_colors_volumes),
178179
**kwargs_plot_structured_grid
179180
)
180181

@@ -190,11 +191,12 @@ def plot_3d(
190191
**kwargs_plot_structured_grid
191192
)
192193

193-
if True:
194+
if True:
194195
set_scalar_bar(
195196
gempy_vista=gempy_vista,
196-
elements_names = model.structural_frame.elements_names,
197-
surfaces_ids=model.structural_frame.elements_ids - 1
197+
elements_names=structural_frame.elements_names,
198+
surfaces_ids=structural_frame.elements_ids - 1,
199+
custom_colors=structural_frame.elements_colors_volumes
198200
)
199201

200202
if ve is not None:

gempy_viewer/modules/plot_3d/plot_3d_utils.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pandas as pd
5+
from matplotlib.colors import ListedColormap
56

67
from gempy_viewer.modules.plot_3d.vista import GemPyToVista
78

@@ -23,25 +24,56 @@ def select_surfaces_data(data_df: pd.DataFrame, surfaces: Union[str, list[str]]
2324
return geometric_data
2425

2526

26-
def set_scalar_bar(gempy_vista: GemPyToVista, elements_names: list[str], surfaces_ids: np.ndarray):
27+
def set_scalar_bar(gempy_vista: GemPyToVista, elements_names: list[str],
28+
surfaces_ids: np.ndarray, custom_colors: list = None):
2729
import pyvista as pv
28-
30+
2931
# Get mapper actor
3032
if gempy_vista.surface_points_actor is not None:
3133
mapper_actor: pv.Actor = gempy_vista.surface_points_actor
3234
elif gempy_vista.regular_grid_actor is not None:
3335
mapper_actor = gempy_vista.regular_grid_actor
3436
else:
3537
return None # * Not a good mapper for the scalar bar
36-
38+
39+
# Get the lookup table from the mapper
40+
lut = mapper_actor.mapper.lookup_table
41+
42+
# Create annotations mapping integers to element names
3743
annotations = {}
38-
for e, name in enumerate(elements_names):
39-
annotations[e] = name
44+
for e, name in enumerate(elements_names[::-1]):
45+
# Convert integer to string for the annotation key
46+
annotations[str(e)] = name
47+
48+
# Apply annotations to the lookup table
49+
lut.annotations = annotations
50+
51+
# Set number of colors to match the number of categories
52+
n_colors = len(elements_names)
53+
lut.n_values = n_colors
54+
55+
# Apply custom colors if provided
56+
if custom_colors is not None:
57+
# Check if we have enough colors
58+
if len(custom_colors) < n_colors:
59+
raise ValueError(f"Not enough custom colors provided. Got {len(custom_colors)}, need {n_colors}")
4060

41-
mapper_actor.mapper.lookup_table.annotations = annotations
42-
61+
custom_cmap = ListedColormap(custom_colors)
62+
63+
# Apply the custom colormap to the lookup table
64+
lut.apply_cmap(cmap=custom_cmap, n_values=n_colors)
65+
66+
else:
67+
# Apply a default colormap if no custom colors are provided
68+
lut.apply_cmap(cmap='Set1', n_values=n_colors)
69+
70+
# Configure scalar bar arguments
4371
sargs = gempy_vista.scalar_bar_arguments
4472
sargs["mapper"] = mapper_actor.mapper
45-
73+
74+
# Add scalar bar
4675
gempy_vista.p.add_scalar_bar(**sargs)
47-
gempy_vista.p.update_scalar_bar_range((surfaces_ids.min(), surfaces_ids.max())) # * This has to be here to now screw the colors with the volumes
76+
77+
# Update scalar bar range to match surface IDs range
78+
min_id, max_id = surfaces_ids.min(), surfaces_ids.max()
79+
gempy_vista.p.update_scalar_bar_range((min_id, max_id))

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def one_fault_model_topo_solution() -> GeoModel:
3232
topography_resolution=np.array([60, 60])
3333
)
3434

35-
gp.compute_model(one_fault_model)
35+
gp.compute_model(one_fault_model, gp.data.GemPyEngineConfig(backend=gp.data.AvailableBackends.PYTORCH))
3636
return one_fault_model
3737

3838

@@ -43,7 +43,7 @@ def _one_fault_model_generator():
4343
project_name='fault',
4444
extent=[0, 2000, 0, 2000, 0, 1000],
4545
resolution=[20, 5, 20],
46-
refinement=4,
46+
refinement=1,
4747
importer_helper=ImporterHelper(
4848
path_to_surface_points=path_to_data + "model5_surface_points.csv",
4949
path_to_orientations=path_to_data + "model5_orientations.csv",

0 commit comments

Comments
 (0)