Skip to content

Commit 91cd91b

Browse files
committed
merge: bring the element count fix from feat/fault-topology-button
2 parents 48b1138 + ecb28ff commit 91cd91b

4 files changed

Lines changed: 65 additions & 61 deletions

File tree

‎loopstructural/main/interpolation_size.py‎

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@
1313
ELEMENTS_PER_EQUATION = 25
1414
MAX_SURFACES = 10
1515
SURFACE_STEP = 0.1
16+
MAX_EXTENT_FACTOR = 2.0
1617

1718

1819
@dataclass
1920
class DataSummary:
2021
"""The measures of the data of one feature.
2122
2223
`spread` is 0 for parallel planes and 1 for orientations in all
23-
directions. `spacing` is the median distance from a data point to the
24-
nearest other data point (0 if there are fewer than two points).
24+
directions. `area` is the map (XY) area of the box around the data points
25+
(0 if it is not known).
2526
"""
2627

2728
n_value: int = 0
2829
n_orientation: int = 0
2930
n_surfaces: int = 0
3031
spread: float = 0.0
31-
spacing: float = 0.0
32+
area: float = 0.0
3233

3334

3435
def _normals_from_strike_dip(strike, dip) -> np.ndarray:
@@ -67,21 +68,14 @@ def orientation_spread(normals) -> float:
6768
return float(np.clip((1.0 - largest) / (2.0 / 3.0), 0.0, 1.0))
6869

6970

70-
def point_spacing(points) -> float:
71-
"""Return the median distance from each point to the nearest other point.
72-
73-
Points at the same location count as one point. Returns 0 for fewer than
74-
two points.
75-
"""
76-
from scipy.spatial import cKDTree
77-
78-
points = np.asarray(points, dtype=float).reshape(-1, 3)
71+
def xy_area(points) -> float:
72+
"""Return the map (XY) area of the box around the points, or 0."""
73+
points = np.asarray(points, dtype=float).reshape(-1, 2)
7974
points = points[np.all(np.isfinite(points), axis=1)]
80-
points = np.unique(points, axis=0)
8175
if len(points) < 2:
8276
return 0.0
83-
distances, _ = cKDTree(points).query(points, k=2)
84-
return float(np.median(distances[:, 1]))
77+
width, height = points.max(axis=0) - points.min(axis=0)
78+
return float(width * height)
8579

8680

8781
def _has(df, columns) -> np.ndarray:
@@ -128,32 +122,33 @@ def summarise_data(df) -> DataSummary:
128122
n_surfaces = int(df.loc[_has(df, ['val']), 'val'].nunique())
129123
if 'interface' in df.columns and df['interface'].notna().any():
130124
n_surfaces = max(n_surfaces, int(df['interface'].nunique()))
131-
spacing = 0.0
132-
if all(column in df.columns for column in ('X', 'Y', 'Z')):
133-
spacing = point_spacing(df[['X', 'Y', 'Z']].to_numpy(dtype=float))
125+
area = 0.0
126+
if 'X' in df.columns and 'Y' in df.columns:
127+
area = xy_area(df[['X', 'Y']].to_numpy(dtype=float))
134128
return DataSummary(
135129
n_value=int(value.sum()),
136130
n_orientation=int(orientation.sum()),
137131
n_surfaces=n_surfaces,
138132
spread=spread,
139-
spacing=spacing,
133+
area=area,
140134
)
141135

142136

143-
def suggest_nelements(summary: DataSummary, volume: float = 0.0) -> int:
137+
def suggest_nelements(summary: DataSummary, model_area: float = 0.0) -> int:
144138
"""Return the number of elements for a feature with the data `summary`.
145139
146-
From the number of data rows: elements = equations x 25 x surface factor
147-
x spread factor, where an orientation is two equations, the surface
148-
factor is 1 + 0.1 for each surface after the first (at most 10 surfaces)
149-
and the spread factor is 1 + spread.
140+
elements = equations x 25 x surface factor x spread factor x extent
141+
factor, where an orientation is two equations, the surface factor is
142+
1 + 0.1 for each surface after the first (at most 10 surfaces) and the
143+
spread factor is 1 + spread.
150144
151-
From the model extent: if `volume` (the volume of the model box) and the
152-
data spacing are known, elements = volume / spacing^3, so that an element
153-
is not larger than the distance between the data points.
145+
The extent factor is the map area of the model (`model_area`) divided by
146+
the map area of the data, limited to 1 .. 2. When the data cover only a
147+
part of the model, the elements are spread over a larger area, so more
148+
elements keep the resolution near the data. The limit keeps the solve
149+
time low. The factor is 1 if an area is not known.
154150
155-
The result is the larger of the two, rounded to 1 000 and limited to
156-
5 000 .. 250 000.
151+
The result is rounded to 1 000 and limited to 5 000 .. 250 000.
157152
"""
158153
equations = summary.n_value + 2 * summary.n_orientation
159154
if equations <= 0:
@@ -162,7 +157,7 @@ def suggest_nelements(summary: DataSummary, volume: float = 0.0) -> int:
162157
surface_factor = 1 + SURFACE_STEP * (surfaces - 1)
163158
spread_factor = 1 + min(max(summary.spread, 0.0), 1.0)
164159
elements = equations * ELEMENTS_PER_EQUATION * surface_factor * spread_factor
165-
if volume > 0 and summary.spacing > 0:
166-
elements = max(elements, min(volume / summary.spacing**3, MAX_NELEMENTS))
160+
if model_area > 0 and summary.area > 0:
161+
elements *= min(max(model_area / summary.area, 1.0), MAX_EXTENT_FACTOR)
167162
elements = int(round(elements / ROUND_TO)) * ROUND_TO
168163
return int(min(max(elements, MIN_NELEMENTS), MAX_NELEMENTS))

‎loopstructural/main/model_manager.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,19 +1033,19 @@ def default_nelements(self, name) -> tuple:
10331033
if not settings.interpolator_nelements_auto:
10341034
return int(settings.interpolator_nelements), False
10351035
summary = self._data_summaries.get(name, DataSummary())
1036-
return suggest_nelements(summary, self._model_volume()), True
1036+
return suggest_nelements(summary, self._model_area()), True
10371037

1038-
def _model_volume(self) -> float:
1039-
"""Return the volume of the model box, or 0 if the box is not set."""
1038+
def _model_area(self) -> float:
1039+
"""Return the map (XY) area of the model box, or 0 if the box is not set."""
10401040
bounding_box = getattr(self.model, 'bounding_box', None)
10411041
try:
1042-
lengths = np.asarray(bounding_box.maximum, dtype=float) - np.asarray(
1042+
lengths = np.asarray(bounding_box.maximum, dtype=float)[:2] - np.asarray(
10431043
bounding_box.origin, dtype=float
1044-
)
1045-
except (AttributeError, TypeError, ValueError):
1044+
)[:2]
1045+
except (AttributeError, TypeError, ValueError, IndexError):
10461046
return 0.0
1047-
volume = float(np.prod(lengths))
1048-
return volume if np.isfinite(volume) and volume > 0 else 0.0
1047+
area = float(np.prod(lengths))
1048+
return area if np.isfinite(area) and area > 0 else 0.0
10491049

10501050
def nelements_overrides_to_dict(self) -> dict:
10511051
"""Return `nelements_overrides` in a form that `json.dump` can write."""

‎tests/qgis/test_interpolator_arguments.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_the_number_of_the_user_comes_first(manager, monkeypatch):
5353

5454
def test_automatic_uses_the_data(manager, monkeypatch):
5555
_use_settings(monkeypatch, interpolator_nelements_auto=True, interpolator_nelements=7000)
56-
expected = suggest_nelements(summarise_data(DATA), 100.0 * 100.0 * 100.0)
56+
expected = suggest_nelements(summarise_data(DATA), 100.0 * 100.0)
5757
assert manager.interpolator_arguments('s1', DATA)['nelements'] == expected
5858
assert manager.nelements_used['s1'] == (expected, True)
5959

@@ -68,7 +68,7 @@ def test_the_default_is_not_the_number_of_the_user(manager, monkeypatch):
6868
_use_settings(monkeypatch, interpolator_nelements_auto=True, interpolator_nelements=7000)
6969
manager.set_nelements_override('s1', 12345)
7070
manager.interpolator_arguments('s1', DATA)
71-
expected = suggest_nelements(summarise_data(DATA), 100.0 * 100.0 * 100.0)
71+
expected = suggest_nelements(summarise_data(DATA), 100.0 * 100.0)
7272
assert manager.default_nelements('s1') == (expected, True)
7373

7474

‎tests/unit/test_interpolation_size.py‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
DataSummary,
1313
_normals_from_strike_dip,
1414
orientation_spread,
15-
point_spacing,
15+
xy_area,
1616
suggest_nelements,
1717
summarise_data,
1818
)
@@ -120,27 +120,36 @@ def test_the_normal_points_in_the_dip_direction(self):
120120

121121

122122
class TestExtent:
123-
def test_point_spacing(self):
124-
points = [(x, 0.0, 0.0) for x in (0.0, 10.0, 20.0, 30.0)]
125-
assert point_spacing(points) == 10.0
123+
def test_xy_area(self):
124+
assert xy_area([(0.0, 0.0), (10.0, 5.0), (3.0, 2.0)]) == 50.0
125+
assert xy_area([(1.0, 1.0)]) == 0.0
126126

127-
def test_repeated_points_count_once(self):
128-
assert point_spacing([(0.0, 0.0, 0.0)] * 3 + [(5.0, 0.0, 0.0)]) == 5.0
129-
assert point_spacing([(0.0, 0.0, 0.0)] * 3) == 0.0
127+
def test_summary_has_the_area(self):
128+
df = pd.DataFrame({'X': [0.0, 50.0, 100.0], 'Y': [0.0, 20.0, 40.0], 'Z': 0.0, 'val': 0.0})
129+
assert summarise_data(df).area == 4000.0
130130

131-
def test_summary_has_the_spacing(self):
132-
df = pd.DataFrame({'X': [0.0, 50.0, 100.0], 'Y': 0.0, 'Z': 0.0, 'val': 0.0})
133-
assert summarise_data(df).spacing == 50.0
134-
135-
def test_a_large_model_with_dense_data_gives_more_elements(self):
136-
summary = DataSummary(n_value=20, spacing=100.0)
131+
def test_data_in_part_of_the_model_gives_more_elements(self):
132+
summary = DataSummary(n_value=2000, area=100.0 * 100.0)
133+
from_rows = suggest_nelements(summary)
134+
same = suggest_nelements(summary, model_area=100.0 * 100.0)
135+
quarter = suggest_nelements(summary, model_area=200.0 * 200.0)
136+
assert same == from_rows
137+
# the factor is at most 2
138+
assert quarter == 2 * from_rows
139+
140+
def test_dense_map_data_do_not_give_the_maximum(self):
141+
# five contact lines sampled every 50 m on a 10 km map
142+
x = np.arange(0.0, 10_000.0, 50.0)
143+
df = pd.concat(
144+
pd.DataFrame({'X': x, 'Y': y, 'Z': 0.0, 'val': k * 100.0})
145+
for k, y in enumerate(np.linspace(1_000.0, 9_000.0, 5))
146+
)
147+
summary = summarise_data(df)
137148
from_rows = suggest_nelements(summary)
138-
small = suggest_nelements(summary, volume=1000.0**3)
139-
large = suggest_nelements(summary, volume=10_000.0 * 10_000.0 * 2_000.0)
140-
assert small == from_rows
141-
assert large > from_rows
142-
assert large <= MAX_NELEMENTS
149+
with_extent = suggest_nelements(summary, model_area=10_000.0 * 10_000.0)
150+
assert from_rows <= with_extent <= 2 * from_rows
151+
assert with_extent < MAX_NELEMENTS
143152

144-
def test_no_spacing_uses_only_the_rows(self):
153+
def test_no_area_uses_only_the_rows(self):
145154
summary = DataSummary(n_value=20)
146-
assert suggest_nelements(summary, volume=1e12) == suggest_nelements(summary)
155+
assert suggest_nelements(summary, model_area=1e12) == suggest_nelements(summary)

0 commit comments

Comments
 (0)