Skip to content

Commit df2e559

Browse files
committed
fix: address review findings on the derived refresh, native solver and export
- derived refresh: take the layer role first, as the recorded inputs do - native solver: forget the fallback state when the data is read again - skip a constraint layer that was removed from the project - skip a damaged parametric fault when a project loads - clear the state of a removed parametric fault and emit model_updated - Model step: changed layers are a to-do, and the text names the real buttons - export: NaN ids have no unit, and the CSV is written with the csv module
1 parent 4a4b185 commit df2e559

9 files changed

Lines changed: 106 additions & 26 deletions

File tree

‎loopstructural/gui/modelling/steps/checks.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ def check_model(data_manager, model_manager=None) -> StepCheck:
116116
problems.append("The model CRS must be a projected CRS (in metres).")
117117
changed = data_manager.get_changed_layers()
118118
if changed:
119-
problems.append("Input layers changed after the model was built: " + ", ".join(changed))
119+
# "Update data and solve" puts the new data into the features
120+
todo.append("Input layers changed after the model was built: " + ", ".join(changed))
120121
state = model_manager.model_state if model_manager is not None else 'empty'
121122
if state == 'empty':
122-
todo.append("Initialise the model.")
123+
todo.append("Build the model.")
123124
elif state == 'stale':
124-
problems.append("Inputs changed after the model was built. Initialise the model again.")
125+
problems.append("Inputs changed after the model was built. Rebuild the model.")
125126
elif state == 'initialized':
126127
todo.append("Solve the model.")
127128
return StepCheck(tuple(problems), tuple(todo))

‎loopstructural/main/data_manager.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,15 @@ def sync_extra_constraints(self):
14041404
if entry.get('processed') or entry.get('layer') is None:
14051405
continue
14061406
row = dict(entry)
1407-
row['df'] = qgsLayerToGeoDataFrame(entry['layer'], target_crs=model_crs)
1407+
try:
1408+
row['df'] = qgsLayerToGeoDataFrame(entry['layer'], target_crs=model_crs)
1409+
except RuntimeError as error:
1410+
# The layer was removed from the project: skip this row only
1411+
self.logger(
1412+
message=f"Could not read the added constraint '{key}' of '{name}': {error}",
1413+
log_level=2,
1414+
)
1415+
continue
14081416
rows[key] = row
14091417
if rows:
14101418
extras[name] = {'data': rows, 'use_z_coordinate': True}

‎loopstructural/main/derived_refresh.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ def _read_contacts_job(self):
8989
dm = self.data_manager
9090
settings = dm.get_widget_settings('basal_contacts_widget', {}) or {}
9191
geology = dm.get_layer_role(layer_roles.GEOLOGY)
92-
faults = (
93-
dm.find_layer_by_name(settings['faults_layer'])
94-
if settings.get('faults_layer')
95-
else dm.get_layer_role(layer_roles.FAULT_TRACES)
96-
)
92+
faults = dm.get_layer_role(layer_roles.FAULT_TRACES)
93+
if faults is None and settings.get('faults_layer'):
94+
faults = dm.find_layer_by_name(settings['faults_layer'])
9795
unit_field = dm.get_layer_role(layer_roles.GEOLOGY_UNIT_FIELD)
9896
inputs = dm.basal_contacts_inputs()
9997
_check_contacts_inputs(inputs)
@@ -126,10 +124,12 @@ def _read_thickness_job(self):
126124
)
127125

128126
def layer(key, role=None):
129-
name = settings.get(key)
130-
if name:
131-
return dm.find_layer_by_name(name)
132-
return dm.get_layer_role(role) if role else None
127+
# The role first, as in `thickness_inputs`, so that the layer of a
128+
# run is the layer that its inputs record.
129+
found = dm.get_layer_role(role) if role else None
130+
if found is None and settings.get(key):
131+
found = dm.find_layer_by_name(settings[key])
132+
return found
133133

134134
geology = layer('geology_layer', layer_roles.GEOLOGY)
135135
structure = layer('structure_layer', layer_roles.STRUCTURE)

‎loopstructural/main/model_export.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
and `get_stratigraphic_unit_names`) and write files with pyvista.
66
"""
77

8+
import csv
89
import re
910
from pathlib import Path
1011

@@ -73,7 +74,8 @@ def _check_extension(extension, formats):
7374
def unit_names_for_ids(model_manager, ids) -> np.ndarray:
7475
"""Return the unit name for each stratigraphic id. An id of -1 has no unit."""
7576
names = list(model_manager.get_stratigraphic_unit_names())
76-
ids = np.asarray(ids, dtype=int)
77+
# NaN (a point with no unit) has the id -1
78+
ids = np.nan_to_num(np.asarray(ids, dtype=float), nan=-1).astype(int)
7779
return np.array([names[i] if 0 <= i < len(names) else '' for i in ids], dtype=object)
7880

7981

@@ -84,6 +86,9 @@ def add_stratigraphy(model_manager, mesh, *, cells=False):
8486
"""
8587
points = mesh.cell_centers().points if cells else mesh.points
8688
ids = np.asarray(model_manager.evaluate_stratigraphy_on_points(points))
89+
if ids.dtype.kind == 'f':
90+
# a point with no unit has the id -1
91+
ids = np.nan_to_num(ids, nan=-1).astype(int)
8792
data = mesh.cell_data if cells else mesh.point_data
8893
data[STRATIGRAPHY_ID_FIELD] = ids
8994
# a VTK string array: '' where there is no unit
@@ -108,9 +113,11 @@ def existing_surface_files(folder, extension):
108113
def _write_csv(path, points, ids, names):
109114
points = np.asarray(points)
110115
with open(path, 'w', encoding='utf-8', newline='') as handle:
111-
handle.write(f"x,y,z,{STRATIGRAPHY_ID_FIELD},{UNIT_FIELD}\n")
116+
writer = csv.writer(handle, lineterminator='\n')
117+
writer.writerow(['x', 'y', 'z', STRATIGRAPHY_ID_FIELD, UNIT_FIELD])
112118
for (x, y, z), unit_id, name in zip(points, ids, names):
113-
handle.write(f"{x:.6f},{y:.6f},{z:.6f},{int(unit_id)},\"{name}\"\n")
119+
unit_id = -1 if np.isnan(unit_id) else int(unit_id)
120+
writer.writerow([f"{x:.6f}", f"{y:.6f}", f"{z:.6f}", unit_id, name])
114121

115122

116123
def export_surfaces(

‎loopstructural/main/model_manager.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,15 @@ def _solve_feature(self, feature, rebuild=True):
11041104
self._use_fallback(name, builders, unconverged, fallback, "did not converge")
11051105
feature.builder.up_to_date()
11061106

1107+
def _forget_native_state(self, name=None):
1108+
"""Let the feature `name` (or every feature) try the native solver again."""
1109+
if name is None:
1110+
self._native_failed.clear()
1111+
self._native_fallbacks.clear()
1112+
return
1113+
self._native_failed.discard(name)
1114+
self._native_fallbacks.pop(name, None)
1115+
11071116
@staticmethod
11081117
def _set_fallback(builder, fallback):
11091118
# the native iteration limit is not for the other solvers
@@ -1659,7 +1668,13 @@ def mark_data_dirty(self):
16591668
def remove_parametric_fault(self, name: str):
16601669
"""Stop building the parametric fault `name`. The model is stale until the next build."""
16611670
if self.parametric_faults.pop(name, None) is not None:
1671+
self.nelements_overrides.pop(name, None)
1672+
self.nelements_used.pop(name, None)
1673+
self._data_summaries.pop(name, None)
1674+
self._needs_inequalities.discard(name)
1675+
self._forget_native_state(name)
16621676
self._data_dirty = True
1677+
self._emit('model_updated')
16631678

16641679
def _build_parametric_faults(self):
16651680
for name, spec in self.parametric_faults.items():
@@ -1681,9 +1696,16 @@ def parametric_faults_to_dict(self) -> dict:
16811696

16821697
def parametric_faults_from_dict(self, faults: dict):
16831698
"""Replace `parametric_faults` with the specs from `parametric_faults_to_dict`."""
1684-
self.parametric_faults = {
1685-
name: parametric_fault.spec_from_dict(spec) for name, spec in (faults or {}).items()
1686-
}
1699+
self.parametric_faults = {}
1700+
for name, spec in (faults or {}).items():
1701+
# one damaged fault must not stop the restore of the project
1702+
try:
1703+
self.parametric_faults[name] = parametric_fault.spec_from_dict(spec)
1704+
except ValueError as error:
1705+
if self._debug_manager is not None:
1706+
self._debug_manager.log(
1707+
f"Skipped the saved parametric fault '{name}': {error}", log_level=2
1708+
)
16871709

16881710
def _get_feature_by_name_or_none(self, name):
16891711
"""Non-raising counterpart to `GeologicalModel.get_feature_by_name`.
@@ -1883,6 +1905,8 @@ def update_model(
18831905
self.model.features = []
18841906
self.model.feature_name_index = {}
18851907
self.skipped_constraints = {}
1908+
# The data is read again, so a feature can use the native solver again
1909+
self._forget_native_state()
18861910

18871911
# Notify start (if requested) so UI can react
18881912
if notify_observers:
@@ -1980,6 +2004,8 @@ def refresh_feature_data(self) -> dict:
19802004
"""
19812005
updated = []
19822006
needs_initialize = []
2007+
# The data paths below add the features that need inequalities again
2008+
self._needs_inequalities.clear()
19832009

19842010
def refresh(name, data):
19852011
feature = self._get_feature_by_name_or_none(name)
@@ -2000,6 +2026,8 @@ def refresh(name, data):
20002026
if isinstance(current, pd.DataFrame) and current.equals(data):
20012027
return
20022028
target.add_data_from_data_frame(data)
2029+
# new data can be solved by the native solver
2030+
self._forget_native_state(name)
20032031
# add_data_from_data_frame does not reset this flag, and while
20042032
# it is set the builder keeps the constraints of the old data
20052033
target.data_added = False

‎loopstructural/main/parametric_fault.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,18 @@ def clean_spec(spec: dict) -> dict:
183183

184184

185185
def spec_from_dict(data: Optional[dict]) -> dict:
186-
"""Read a spec that was saved. The centre is a tuple again."""
187-
spec = clean_spec(data)
186+
"""Read a spec that was saved. The centre is a tuple again.
187+
188+
Raises
189+
------
190+
ValueError
191+
If the saved spec is empty or has no name or centre.
192+
"""
193+
if not isinstance(data, dict) or 'name' not in data or 'centre' not in data:
194+
raise ValueError("The saved parametric fault has no name or centre.")
195+
try:
196+
spec = clean_spec(data)
197+
except (TypeError, ValueError) as error:
198+
raise ValueError(f"The saved parametric fault is not valid: {error}") from error
188199
spec['centre'] = tuple(spec['centre'])
189200
return spec

‎tests/unit/test_model_export.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def test_export_block_model_csv(tmp_path):
129129
)
130130
lines = path.read_text(encoding='utf-8').splitlines()
131131
assert lines[0] == 'x,y,z,stratigraphy_id,unit'
132-
assert lines[1].endswith('0,"lower"')
133-
assert lines[2].endswith('1,"upper"')
132+
assert lines[1].endswith('0,lower')
133+
assert lines[2].endswith('1,upper')
134134

135135

136136
def test_export_cross_section_plane_and_line(tmp_path):
@@ -153,3 +153,21 @@ def test_export_cross_section_needs_input(tmp_path):
153153
model_export.export_cross_section(
154154
manager, tmp_path / 'x.vtk', origin=(0, 0, 0), normal=(0, 0, 0), size=1
155155
)
156+
157+
158+
def test_csv_escapes_a_quote_in_a_unit_name(tmp_path):
159+
import csv
160+
161+
path = tmp_path / 'quote.csv'
162+
model_export._write_csv(path, [(0, 0, 0)], [3], ['Unit "A"'])
163+
with open(path, newline='', encoding='utf-8') as handle:
164+
rows = list(csv.reader(handle))
165+
assert rows[1][3:] == ['3', 'Unit "A"']
166+
167+
168+
def test_nan_id_has_no_unit():
169+
import numpy as np
170+
171+
manager = FakeManager(FakeModel())
172+
names = model_export.unit_names_for_ids(manager, np.array([0.0, np.nan]))
173+
assert names[1] == ''

‎tests/unit/test_parametric_fault.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@ def test_the_default_spec_is_in_the_middle_of_the_area(self):
146146
assert spec['major_axis'] == 1000.0
147147
assert spec['minor_axis'] == 500.0
148148
assert parametric_fault.problems({**spec, 'name': 'F'}) == []
149+
150+
151+
@pytest.mark.parametrize('saved', [None, {}, {'name': 'F'}, {'centre': [0, 0, 0]}])
152+
def test_spec_from_dict_rejects_a_damaged_spec(saved):
153+
with pytest.raises(ValueError):
154+
parametric_fault.spec_from_dict(saved)

‎tests/unit/test_step_checks.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,13 @@ def test_out_of_date_data_is_shown_on_step_2_only(self, dm):
241241
assert checks.check_stratigraphy(dm).status == Status.PROBLEM
242242
assert checks.check_model(dm, FakeModelManager('solved')).status == Status.DONE
243243

244-
def test_changed_layers_are_a_problem(self, dm):
244+
def test_changed_layers_are_a_todo(self, dm):
245+
# "Update data and solve" fixes this with one click, so it is not a problem
245246
dm.bounding_box_set = True
246247
dm.changed_layers = ['geology']
247248
check = checks.check_model(dm, FakeModelManager('solved'))
248-
assert check.status == Status.PROBLEM
249-
assert 'geology' in check.problems[0]
249+
assert not check.problems
250+
assert 'geology' in check.todo[0]
250251

251252

252253
class TestViewStep:

0 commit comments

Comments
 (0)