Skip to content

Commit 39e7b2e

Browse files
committed
[ENH] Ensure unique basement color and improve serialization
Introduce a method to allocate a unique basement color, avoiding duplication among structural elements. Minor enhancements include improved serialization output and cleanup of redundant or commented-out code.
1 parent 99bfdb7 commit 39e7b2e

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

gempy/core/data/structural_frame.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class StructuralFrame:
3333

3434
structural_groups: list[StructuralGroup]
3535
color_generator: ColorsGenerator = Field(default_factory=ColorsGenerator)
36+
basement_color: str = None
3637
# ? Should I create some sort of structural options class? For example, the masking descriptor and faults relations pointer
3738
is_dirty: bool = True
3839

@@ -41,6 +42,9 @@ class StructuralFrame:
4142
def __init__(self, structural_groups: list[StructuralGroup], color_gen: ColorsGenerator):
4243
self.structural_groups = structural_groups # ? This maybe could be optional
4344
self.color_generator = color_gen
45+
46+
def __post_init__(self):
47+
pass
4448

4549
@classmethod
4650
def from_data_tables(cls, surface_points: SurfacePointsTable, orientations: OrientationsTable):
@@ -203,22 +207,28 @@ def n_elements(self) -> int:
203207
"""Returns the total number of elements in the structural frame."""
204208
return len(self.structural_elements)
205209

206-
basement_color: str = None
207210

208211
@property
209212
def _basement_element(self) -> StructuralElement:
210-
# Check if the basement color is already defined
213+
"""Returns the basement structural element with a unique color."""
214+
215+
def _get_unique_basement_color(color_generator: ColorsGenerator, used_colors: list[str]) -> str:
216+
color = next(color_generator)
217+
if color in used_colors:
218+
return _get_unique_basement_color(color_generator, used_colors)
219+
return color
220+
211221
elements = []
212222
for group in self.structural_groups:
213223
elements.extend(group.elements)
214-
basement_color_in_elements = self.basement_color in [element.color for element in elements]
215224

216-
if self.basement_color is None or basement_color_in_elements:
217-
self.basement_color = self.color_generator.up_next()
225+
used_colors = [element.color for element in elements]
218226

219-
if basement_color_in_elements:
220-
warnings.warn(f"The basement color was already used in the structural elements."
221-
f"Changing the basement color to {self.basement_color}.")
227+
if self.basement_color is None or self.basement_color in used_colors:
228+
self.basement_color = _get_unique_basement_color(
229+
color_generator=self.color_generator,
230+
used_colors=used_colors
231+
)
222232

223233
basement = StructuralElement(
224234
name="basement",

gempy/modules/serialization/save_load.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def make_info(name):
169169
return buf.getvalue()
170170

171171
def _load_model_from_bytes(data: bytes) -> GeoModel:
172-
import json, zlib
173172
from ...core.data.encoders.converters import loading_model_from_binary
174173

175174
buf = io.BytesIO(data)

test/test_modules/test_serialize_model.test_generate_horizontal_stratigraphic_model.verify/Horizontal Stratigraphic Model serialization.approved.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@
5959
"solution": null
6060
}
6161
],
62-
"is_dirty": true,
62+
"color_generator": {
63+
"_index": 2
64+
},
6365
"basement_color": "#ffbe00",
66+
"is_dirty": true,
6467
"binary_meta_data": {
6568
"sp_binary_length": 432,
6669
"ori_binary_length": 120

test/test_private/test_terranigma/test_nuggets/test_nugget_effect_optimization.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ def test_optimize_nugget_effect():
6464
validate_serialization=True
6565
)
6666

67-
# TODO: Save model
68-
6967
print(f"Final cond number: {geo_model.interpolation_options.kernel_options.condition_number}")
7068
nugget_effect = geo_model.taped_interpolation_input.surface_points.nugget_effect_scalar.detach().numpy()
7169

0 commit comments

Comments
 (0)