Skip to content

Commit 9955147

Browse files
committed
fix: use debug manager logger and avoid adding color twice for strat surfaces
1 parent 2d2f848 commit 9955147

1 file changed

Lines changed: 57 additions & 31 deletions

File tree

loopstructural/gui/visualisation/feature_list_widget.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from typing import Optional, Union
33

4+
import numpy as np
45
from PyQt5.QtWidgets import QMenu, QPushButton, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
56

67
from LoopStructural.datatypes import VectorPoints
@@ -186,22 +187,22 @@ def add_data(self, feature_name):
186187
source_feature=feature_name,
187188
source_type='feature_data',
188189
)
189-
logger.debug(f"Adding data to feature: {feature_name}")
190+
logger.info(f"Adding data to feature: {feature_name}")
190191

191192
def add_model_bounding_box(self):
192193
if not self.model_manager:
193-
logger.debug("Model manager is not set.")
194+
logger.info("Model manager is not set.")
194195
return
195196
bb = self.model_manager.model.bounding_box.vtk().outline()
196197
self.viewer.add_mesh_object(
197198
bb, name='model_bounding_box', source_feature='__model__', source_type='bounding_box'
198199
)
199200
# Logic for adding model bounding box
200-
logger.debug("Adding model bounding box...")
201+
logger.info("Adding model bounding box...")
201202

202203
def add_fault_surfaces(self):
203204
if not self.model_manager:
204-
logger.debug("Model manager is not set.")
205+
logger.info("Model manager is not set.")
205206
return
206207
self.model_manager.update_all_features(subset='faults')
207208
fault_surfaces = self.model_manager.model.get_fault_surfaces()
@@ -213,19 +214,21 @@ def add_fault_surfaces(self):
213214
source_type='fault_surface',
214215
isovalue=0.0,
215216
)
216-
logger.debug("Adding fault surfaces...")
217+
logger.info("Adding fault surfaces...")
217218

218219
def add_stratigraphic_surfaces(self):
219220
if not self.model_manager:
220-
logger.debug("Model manager is not set.")
221+
logger.info("Model manager is not set.")
221222
return
222223
stratigraphic_surfaces = self.model_manager.model.get_stratigraphic_surfaces()
224+
223225
for surface in stratigraphic_surfaces:
224226
self.viewer.add_mesh_object(
225227
surface.vtk(),
226228
name=surface.name,
227229
color=surface.colour,
228230
source_feature=surface.name,
231+
isovalue=np.mean(surface.values),
229232
source_type='stratigraphic_surface',
230233
)
231234

@@ -241,8 +244,32 @@ def _on_model_update(self, event: str, *args):
241244
'model_updated' notifications the previous behaviour (re-add all
242245
affected feature representations) is preserved.
243246
"""
244-
logger.debug(f"Model update event received: {event} with args: {args}")
245-
logger.debug([f"Mesh: {name}, Meta: {meta}" for name, meta in self.viewer.meshes.items()])
247+
248+
# Prefer the DebugManager for logging when available (it forwards to
249+
# the plugin/toolbelt logger and handles debug mode). Fall back to the
250+
# module logger if no debug manager is present.
251+
def _log(msg, level=0):
252+
try:
253+
dbg = None
254+
if getattr(self, 'model_manager', None) is not None:
255+
dbg = getattr(self.model_manager, '_debug_manager', None)
256+
if dbg is not None and hasattr(dbg, 'log'):
257+
# DebugManager.log expects message and log_level keyword
258+
dbg.log(str(msg), log_level=level)
259+
else:
260+
logger.info(str(msg))
261+
except Exception:
262+
try:
263+
logger.info(str(msg))
264+
except Exception:
265+
pass
266+
267+
_log(f"Model update event received: {event} with args: {args}")
268+
try:
269+
_log([f"Mesh: {name}, Meta: {meta}" for name, meta in self.viewer.meshes.items()])
270+
except Exception:
271+
_log("Model update: failed to enumerate viewer meshes")
272+
246273
if not self.model_manager or not self.viewer:
247274
return
248275
if event not in ('model_updated', 'feature_updated'):
@@ -256,14 +283,14 @@ def _on_model_update(self, event: str, *args):
256283
if feature_name is not None:
257284
if meta.get('source_feature', None) == feature_name:
258285
affected_features.add(feature_name)
259-
logger.debug(f"Updating visualisation for feature: {feature_name}")
286+
_log(f"Updating visualisation for feature: {feature_name}")
260287
continue
261288

262289
sf = meta.get('source_feature', None)
263290

264291
if sf is not None:
265292
affected_features.add(sf)
266-
logger.debug(f"Affected features to update: {affected_features}")
293+
_log(f"Affected features to update: {affected_features}")
267294
# For each affected feature, only update existing meshes tied to that feature
268295
for feature_name in affected_features:
269296
# collect mesh names that belong to this feature (snapshot to avoid mutation while iterating)
@@ -272,7 +299,7 @@ def _on_model_update(self, event: str, *args):
272299
for name, meta in list(self.viewer.meshes.items())
273300
if meta.get('source_feature') == feature_name
274301
]
275-
logger.debug(f"Re-adding meshes for feature: {feature_name}: {meshes_for_feature}")
302+
_log(f"Re-adding meshes for feature: {feature_name}: {meshes_for_feature}")
276303

277304
for mesh_name in meshes_for_feature:
278305
meta = self.viewer.meshes.get(mesh_name, {})
@@ -283,9 +310,9 @@ def _on_model_update(self, event: str, *args):
283310
# remove existing actor/entry so add_mesh_object can recreate with same name
284311
try:
285312
self.viewer.remove_object(mesh_name)
286-
logger.debug(f"Removed existing mesh: {mesh_name}")
313+
_log(f"Removed existing mesh: {mesh_name}")
287314
except Exception:
288-
logger.debug(f"Failed to remove existing mesh: {mesh_name}")
315+
_log(f"Failed to remove existing mesh: {mesh_name}")
289316
pass
290317

291318
try:
@@ -300,9 +327,11 @@ def _on_model_update(self, event: str, *args):
300327

301328
if surfaces:
302329
add_name = mesh_name
303-
logger.debug(
304-
f"Re-adding surface for feature: {feature_name} with isovalue: {isovalue}"
330+
_log(
331+
f"Re-adding surface for feature: {feature_name} with isovalue: {isovalue} and {kwargs}"
305332
)
333+
kwargs['isovalue'] = isovalue
334+
306335
self.viewer.add_mesh_object(
307336
surfaces[0].vtk(),
308337
name=add_name,
@@ -313,7 +342,7 @@ def _on_model_update(self, event: str, *args):
313342
)
314343
continue
315344
except Exception as e:
316-
logger.debug(
345+
_log(
317346
f"Failed to find matching surface for feature: {feature_name} with isovalue: {isovalue}, trying all surfaces. Error: {e}"
318347
)
319348

@@ -326,7 +355,7 @@ def _on_model_update(self, event: str, *args):
326355
None,
327356
)
328357
if match is not None:
329-
logger.debug(f"Re-adding fault surface for: {feature_name}")
358+
_log(f"Re-adding fault surface for: {feature_name}")
330359
self.viewer.add_mesh_object(
331360
match.vtk(),
332361
name=mesh_name,
@@ -337,7 +366,7 @@ def _on_model_update(self, event: str, *args):
337366
)
338367
continue
339368
except Exception as e:
340-
logger.debug(f"Failed to re-add fault surface for {feature_name}: {e}")
369+
_log(f"Failed to re-add fault surface for {feature_name}: {e}")
341370

342371
# Stratigraphic surfaces (added via add_stratigraphic_surfaces)
343372
if source_type == 'stratigraphic_surface':
@@ -348,64 +377,61 @@ def _on_model_update(self, event: str, *args):
348377
None,
349378
)
350379
if match is not None:
351-
logger.debug(f"Re-adding stratigraphic surface for: {feature_name}")
380+
_log(f"Re-adding stratigraphic surface for: {feature_name}")
381+
kwargs['color'] = getattr(match, 'colour', None)
382+
352383
self.viewer.add_mesh_object(
353384
match.vtk(),
354385
name=mesh_name,
355-
color=getattr(match, 'colour', None),
356386
source_feature=feature_name,
357387
source_type='stratigraphic_surface',
358388
**kwargs,
359389
)
360390
continue
361391
except Exception as e:
362-
logger.debug(
363-
f"Failed to re-add stratigraphic surface for {feature_name}: {e}"
364-
)
392+
_log(f"Failed to re-add stratigraphic surface for {feature_name}: {e}")
365393

366394
# Vectors, points, scalar fields and other feature related objects
367395
if source_type == 'feature_vector' or source_type == 'feature_vectors':
368396
try:
369397
self.add_vector_field(feature_name)
370398
continue
371399
except Exception as e:
372-
logger.debug(f"Failed to re-add vector field for {feature_name}: {e}")
400+
_log(f"Failed to re-add vector field for {feature_name}: {e}")
373401

374402
if source_type in ('feature_points', 'feature_data'):
375403
try:
376404
self.add_data(feature_name)
377405
continue
378406
except Exception as e:
379-
logger.debug(f"Failed to re-add data for {feature_name}: {e}")
407+
_log(f"Failed to re-add data for {feature_name}: {e}")
380408

381409
if source_type == 'feature_scalar':
382410
try:
383411
self.add_scalar_field(feature_name)
384412
continue
385413
except Exception as e:
386-
logger.debug(f"Failed to re-add scalar field for {feature_name}: {e}")
414+
_log(f"Failed to re-add scalar field for {feature_name}: {e}")
387415

388416
if source_type == 'bounding_box' or mesh_name == 'model_bounding_box':
389417
try:
390418
self.add_model_bounding_box()
391419
continue
392420
except Exception as e:
393-
logger.debug(f"Failed to re-add bounding box: {e}")
421+
_log(f"Failed to re-add bounding box: {e}")
394422

395423
# Fallback: if nothing matched, attempt to re-add by using viewer metadata
396424
# Many viewer entries store the vtk source under meta['vtk'] or similar; try best-effort
397425
try:
398426
vtk_src = meta.get('vtk')
399427
if vtk_src is not None:
400-
logger.debug(f"Fallback re-add for mesh {mesh_name}")
428+
_log(f"Fallback re-add for mesh {mesh_name}")
401429
self.viewer.add_mesh_object(vtk_src, name=mesh_name, **kwargs)
402430
except Exception:
403431
pass
404432

405433
except Exception as e:
406-
logger.debug(
407-
f"Failed to update visualisation for feature: {feature_name}. Error: {e}"
408-
)
434+
_log(f"Failed to update visualisation for feature: {feature_name}. Error: {e}")
409435

410436
# Refresh the viewer
411437
try:

0 commit comments

Comments
 (0)