Skip to content

Commit ad2fa8e

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 41667e8 + c8f7a14 commit ad2fa8e

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

gempy/core/color_generator.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from functools import cached_property
23
from typing import Optional
34

45
import numpy as np
@@ -36,11 +37,10 @@ def __init__(self):
3637
@staticmethod
3738
def _random_hexcolor() -> str:
3839
"""Generates a random hex color string."""
39-
return "#"+str(hex(np.random.randint(0, 16777215))).lstrip("0x")
40+
return f"#{np.random.randint(0, 0xffffff):06x}"
4041

4142
def regenerate_color_palette(self, seaborn_palettes: Optional[list[str]] = None):
4243
try:
43-
4444
import seaborn as sns
4545
seaborn_installed = True
4646
except ImportError:
@@ -54,7 +54,7 @@ def regenerate_color_palette(self, seaborn_palettes: Optional[list[str]] = None)
5454
elif seaborn_palettes and not seaborn_installed:
5555
raise ImportError("Seaborn is not installed. Please install it to use color palettes.")
5656
else:
57-
hex_colors = self._gempy_default_colors
57+
hex_colors = list(self._gempy_default_colors)
5858

5959
self.hex_colors = hex_colors
6060

@@ -65,14 +65,18 @@ def __iter__(self) -> 'ColorsGenerator':
6565

6666
def __next__(self) -> str:
6767
"""Generator that yields the next color."""
68-
for color in self.hex_colors:
69-
result = self.hex_colors[self._index]
70-
self._index += 1
71-
return result
68+
color = self.up_next()
69+
self._index += 1
70+
del self._next_color
71+
return color
7272

73-
while True:
74-
return self._random_hexcolor()
75-
7673
def up_next(self) -> str:
7774
"""Returns the next color without incrementing the index."""
78-
return self.hex_colors[self._index]
75+
return self._next_color
76+
77+
@cached_property
78+
def _next_color(self) -> str:
79+
if self._index < len(self.hex_colors):
80+
return self.hex_colors[self._index]
81+
82+
return self._random_hexcolor()

gempy/modules/data_manipulation/manipulate_points.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def add_orientations(geo_model: GeoModel,
9494
y: Sequence[float],
9595
z: Sequence[float],
9696
elements_names: Sequence[str],
97-
pole_vector: Optional[Sequence[np.ndarray]] = None,
98-
orientation: Optional[Sequence[np.ndarray]] = None,
97+
pole_vector: Optional[Union[Sequence[np.ndarray], np.ndarray]] = None,
98+
orientation: Optional[Union[Sequence[np.ndarray], np.ndarray]] = None,
9999
nugget: Optional[Sequence[float]] = None
100100
) -> StructuralFrame:
101101
"""Add orientation data to the geological model.
@@ -110,8 +110,8 @@ def add_orientations(geo_model: GeoModel,
110110
y (Sequence[float]): Sequence of y-coordinates for the orientation points.
111111
z (Sequence[float]): Sequence of z-coordinates for the orientation points.
112112
elements_names (Sequence[str]): Sequence of element names corresponding to each orientation point.
113-
pole_vector (Optional[Sequence[np.ndarray]]): Sequence of pole vectors for the orientation points.
114-
orientation (Optional[Sequence[np.ndarray]]): Sequence of orientation angles (azimuth, dip, polarity) for the orientation points.
113+
pole_vector (Optional[Union[Sequence[np.ndarray], np.ndarray]]): Sequence of pole vectors for each orientation point. If is np.ndarray, it should have shape (n, 3).
114+
orientation (Optional[Union[Sequence[np.ndarray], np.ndarray]]): Sequence of orientation angles for each orientation point. If is np.ndarray, it should have shape (n, 3).
115115
nugget (Optional[Sequence[float]]): Sequence of nugget values for each orientation point. If not provided,
116116
a default value will be used for all points.
117117
@@ -125,7 +125,8 @@ def add_orientations(geo_model: GeoModel,
125125
if pole_vector is None and orientation is None:
126126
raise ValueError("Either pole_vector or orientation must be provided.")
127127

128-
if orientation: # Convert orientation to pole_vector (or gradient)
128+
if orientation is not None:
129+
orientation = np.array(orientation)
129130
pole_vector = convert_orientation_to_pole_vector(
130131
azimuth=orientation[:, 0],
131132
dip=orientation[:, 1],
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from gempy.core.color_generator import ColorsGenerator
1+
import gempy.core.color_generator as cg
22

33

44
def test_color_generator():
55

66
# create an instance of ColorsGenerator
7-
color_iterator: iter = ColorsGenerator()
7+
color_iterator: iter = cg.ColorsGenerator()
88

99
# generate color dictionary (with optional seaborn palettes if you want)
1010

@@ -13,4 +13,32 @@ def test_color_generator():
1313
# now you can get colors one by one
1414
print(next(color_iterator)) # prints first color
1515
print(next(color_iterator)) # prints second color
16+
17+
left_colors = color_iterator._gempy_default_colors[2:]
18+
19+
assert left_colors == [
20+
color for color, _ in zip(color_iterator, left_colors)
21+
]
22+
23+
# get a random color
24+
assert color_iterator.up_next() == next(color_iterator)
25+
26+
# override the random color generator
27+
cg.np.random.randint = lambda a, b: 0x00ff42
28+
29+
assert color_iterator._random_hexcolor() == "#00ff42"
30+
assert next(color_iterator) == "#00ff42"
31+
32+
# trigger up_next method for the cache
33+
assert color_iterator.up_next() == "#00ff42"
34+
35+
cg.np.random.randint = lambda a, b: 0x000000
36+
37+
# ensure that the up_next method still returns the same color
38+
assert color_iterator.up_next() == "#00ff42"
39+
40+
# trigger the cache
41+
assert next(color_iterator) == "#00ff42"
42+
assert next(color_iterator) == "#000000"
43+
1644
# etc...

0 commit comments

Comments
 (0)