Skip to content

Commit 3d14c7c

Browse files
authored
[ENH] Fix color generator initialization and improve basement color handling (#1038)
# Description This PR refactors the `ColorsGenerator` class to use `__post_init__` instead of `__init__` and improves the basement color generation in `StructuralFrame`. The changes include: - Refactored `ColorsGenerator` to use `__post_init__` for proper dataclass initialization - Added `color_generator` as a proper field with default factory in `StructuralFrame` - Improved basement color generation logic to ensure uniqueness - Fixed a tensor detachment issue in nugget optimization by converting to numpy - Removed unused imports in serialization and test modules - Updated test validation settings Relates to #optimization-and-refactoring # Checklist - [x] My code uses type hinting for function and method arguments and return values. - [x] I have created tests which cover my code. - [x] The test code either 1. demonstrates at least one valuable use case (e.g. integration tests) or 2. verifies that outputs are as expected for given inputs (e.g. unit tests). - [x] New tests pass locally with my changes.
2 parents 0fb74e5 + d10218d commit 3d14c7c

File tree

11 files changed

+54
-24
lines changed

11 files changed

+54
-24
lines changed

gempy/core/color_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ColorsGenerator:
3535
)
3636
_index: int = 0
3737

38-
def __init__(self):
38+
def __post_init__(self):
3939
self.regenerate_color_palette()
4040

4141
@staticmethod

gempy/core/data/structural_frame.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import warnings
55
from dataclasses import dataclass
6-
from pydantic import model_validator, computed_field, ValidationError
6+
from pydantic import model_validator, computed_field, ValidationError, Field
77
from pydantic.functional_validators import ModelWrapValidatorHandler
88
from typing import Generator, Union
99

@@ -32,14 +32,19 @@ class StructuralFrame:
3232
"""
3333

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

3840
# region Constructor
39-
41+
#
4042
def __init__(self, structural_groups: list[StructuralGroup], color_gen: ColorsGenerator):
4143
self.structural_groups = structural_groups # ? This maybe could be optional
4244
self.color_generator = color_gen
45+
46+
def __post_init__(self):
47+
pass
4348

4449
@classmethod
4550
def from_data_tables(cls, surface_points: SurfacePointsTable, orientations: OrientationsTable):
@@ -202,22 +207,28 @@ def n_elements(self) -> int:
202207
"""Returns the total number of elements in the structural frame."""
203208
return len(self.structural_elements)
204209

205-
basement_color: str = None
206210

207211
@property
208212
def _basement_element(self) -> StructuralElement:
209-
# 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+
210221
elements = []
211222
for group in self.structural_groups:
212223
elements.extend(group.elements)
213-
basement_color_in_elements = self.basement_color in [element.color for element in elements]
214224

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

218-
if basement_color_in_elements:
219-
warnings.warn(f"The basement color was already used in the structural elements."
220-
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+
)
221232

222233
basement = StructuralElement(
223234
name="basement",

gempy/modules/optimize_nuggets/_ops.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def run_optimization(lr, max_epochs, min_impr, model, nugget, patience, target_c
4343
if _has_converged(cur_cond, prev_cond, target_cond_num, epoch, min_impr, patience):
4444
break
4545
prev_cond = cur_cond
46-
46+
47+
# Condition number to numpy
48+
model.interpolation_options.kernel_options.condition_number = model.interpolation_options.kernel_options.condition_number.detach().numpy()
4749
return nugget
4850

4951

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/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@
5353
"solution": null
5454
}
5555
],
56-
"is_dirty": true,
56+
"color_generator": {
57+
"_index": 3
58+
},
5759
"basement_color": "#ffbe00",
60+
"is_dirty": true,
5861
"binary_meta_data": {
5962
"sp_binary_length": 144,
6063
"ori_binary_length": 60

test/test_modules/test_faults/test_finite_faults.test_finite_fault_scalar_field_on_fault.verify/fault.approved.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@
127127
"solution": null
128128
}
129129
],
130-
"is_dirty": true,
130+
"color_generator": {
131+
"_index": 4
132+
},
131133
"basement_color": "#728f02",
134+
"is_dirty": true,
132135
"binary_meta_data": {
133136
"sp_binary_length": 792,
134137
"ori_binary_length": 300

test/test_modules/test_grids/test_custom_grid.test_custom_grid.verify/fold.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": 3
64+
},
6365
"basement_color": "#ffbe00",
66+
"is_dirty": true,
6467
"binary_meta_data": {
6568
"sp_binary_length": 1296,
6669
"ori_binary_length": 120

test/test_modules/test_grids/test_grids_sections.test_section_grids.verify/fold.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": 3
64+
},
6365
"basement_color": "#ffbe00",
66+
"is_dirty": true,
6467
"binary_meta_data": {
6568
"sp_binary_length": 1296,
6669
"ori_binary_length": 120

test/test_modules/test_grids/test_grids_sections.test_topography_II.verify/Model1.approved.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"name": "fault1",
1515
"is_active": true,
16-
"_color": "#728f02",
16+
"_color": "#443988",
1717
"surface_points": {
1818
"name_id_map": {
1919
"fault1": 26244822
@@ -56,7 +56,7 @@
5656
{
5757
"name": "surface2",
5858
"is_active": true,
59-
"_color": "#9f0052",
59+
"_color": "#ffbe00",
6060
"surface_points": {
6161
"name_id_map": {
6262
"surface2": 21816406
@@ -73,7 +73,7 @@
7373
{
7474
"name": "surface3",
7575
"is_active": true,
76-
"_color": "#ffbe00",
76+
"_color": "#728f02",
7777
"surface_points": {
7878
"name_id_map": {
7979
"surface3": 98435767
@@ -94,8 +94,11 @@
9494
"solution": null
9595
}
9696
],
97+
"color_generator": {
98+
"_index": 5
99+
},
100+
"basement_color": "#9f0052",
97101
"is_dirty": true,
98-
"basement_color": "#443988",
99102
"binary_meta_data": {
100103
"sp_binary_length": 360,
101104
"ori_binary_length": 120

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": 3
64+
},
6365
"basement_color": "#ffbe00",
66+
"is_dirty": true,
6467
"binary_meta_data": {
6568
"sp_binary_length": 432,
6669
"ori_binary_length": 120

0 commit comments

Comments
 (0)