From 24488753f27cab17336be634f8e31f58209dab5c Mon Sep 17 00:00:00 2001 From: Andreas Eknes Lie Date: Mon, 26 Aug 2024 14:26:55 +0200 Subject: [PATCH] Remove children_list as it shadows children Remove unused RealStatusColorHint Fix get_successful_realizations --- src/ert/ensemble_evaluator/snapshot.py | 2 +- src/ert/gui/model/node.py | 6 ------ src/ert/gui/model/snapshot.py | 11 ++++------- tests/unit_tests/gui/model/test_snapshot.py | 17 ++++++++++++++--- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ert/ensemble_evaluator/snapshot.py b/src/ert/ensemble_evaluator/snapshot.py index 385556f195b..eea8d09896c 100644 --- a/src/ert/ensemble_evaluator/snapshot.py +++ b/src/ert/ensemble_evaluator/snapshot.py @@ -246,7 +246,7 @@ def get_successful_realizations(self) -> typing.List[int]: return [ int(real_idx) for real_idx, real_data in self._realization_states.items() - if real_data[ids.STATUS] == state.REALIZATION_STATE_FINISHED + if real_data.get(ids.STATUS, "") == state.REALIZATION_STATE_FINISHED ] def aggregate_real_states(self) -> typing.Dict[str, int]: diff --git a/src/ert/gui/model/node.py b/src/ert/gui/model/node.py index 398fa687e0b..bdaa0ce413b 100644 --- a/src/ert/gui/model/node.py +++ b/src/ert/gui/model/node.py @@ -40,14 +40,12 @@ def row(self) -> int: class RootNode(_Node): parent: None = field(default=None, init=False) children: dict[str, IterNode] = field(default_factory=dict) - children_list: list[IterNode] = field(default_factory=list) max_memory_usage: Optional[int] = None def add_child(self, node: _Node) -> None: node = cast(IterNode, node) node.parent = self self.children[node.id_] = node - self.children_list.append(node) @dataclass @@ -61,13 +59,11 @@ class IterNode(_Node): parent: Optional[RootNode] = None data: IterNodeData = field(default_factory=IterNodeData) children: dict[str, RealNode] = field(default_factory=dict) - children_list: list[RealNode] = field(default_factory=list) def add_child(self, node: _Node) -> None: node = cast(RealNode, node) node.parent = self self.children[node.id_] = node - self.children_list.append(node) @dataclass @@ -89,13 +85,11 @@ class RealNode(_Node): parent: Optional[IterNode] = None data: RealNodeData = field(default_factory=RealNodeData) children: dict[str, ForwardModelStepNode] = field(default_factory=dict) - children_list: list[ForwardModelStepNode] = field(default_factory=list) def add_child(self, node: _Node) -> None: node = cast(ForwardModelStepNode, node) node.parent = self self.children[node.id_] = node - self.children_list.append(node) @dataclass diff --git a/src/ert/gui/model/snapshot.py b/src/ert/gui/model/snapshot.py index a8ff0a5a146..edd45b0d615 100644 --- a/src/ert/gui/model/snapshot.py +++ b/src/ert/gui/model/snapshot.py @@ -26,7 +26,6 @@ UserRole = Qt.ItemDataRole.UserRole NodeRole = UserRole + 1 RealJobColorHint = UserRole + 2 -RealStatusColorHint = UserRole + 3 RealLabelHint = UserRole + 4 ProgressRole = UserRole + 5 FileRole = UserRole + 6 @@ -269,10 +268,10 @@ def _add_snapshot(self, snapshot: Snapshot, iter_: str) -> None: real_node.add_child(job_node) if iter_ in self.root.children: - self.modelAboutToBeReset.emit() - self.root.children[iter_] = snapshot_tree + self.beginResetModel() snapshot_tree.parent = self.root - self.modelReset.emit() + self.root.children[iter_] = snapshot_tree + self.endResetModel() return parent = QModelIndex() @@ -382,8 +381,6 @@ def _real_data(_: QModelIndex, node: RealNode, role: int) -> Any: return node.id_ if role == IterNum: return node.parent.id_ if node.parent else None - if role == RealStatusColorHint: - return node.data.real_status_color if role == StatusRole: return node.data.status if role == MemoryUsageRole: @@ -491,7 +488,7 @@ def index( parent_item = self.root if not parent.isValid() else parent.internalPointer() try: - child_item = parent_item.children_list[row] + child_item = list(parent_item.children.values())[row] except KeyError: return QModelIndex() else: diff --git a/tests/unit_tests/gui/model/test_snapshot.py b/tests/unit_tests/gui/model/test_snapshot.py index 1ba2ce8ca01..3fb03d2f039 100644 --- a/tests/unit_tests/gui/model/test_snapshot.py +++ b/tests/unit_tests/gui/model/test_snapshot.py @@ -3,7 +3,7 @@ from qtpy.QtGui import QColor from ert.ensemble_evaluator.state import COLOR_FAILED -from ert.gui.model.snapshot import RealJobColorHint, RealStatusColorHint, SnapshotModel +from ert.gui.model.snapshot import RealJobColorHint, SnapshotModel from .gui_models_utils import finish_snapshot @@ -44,9 +44,20 @@ def test_realization_state_is_queue_finalized_state(fail_snapshot): model._add_snapshot(SnapshotModel.prerender(fail_snapshot), "0") first_real = model.index(0, 0, model.index(0, 0)) - queue_color = model.data(first_real, RealStatusColorHint) - assert queue_color == QColor(*COLOR_FAILED) color, done_count, full_count = model.data(first_real, RealJobColorHint) assert color == QColor(*COLOR_FAILED) assert done_count == 1 assert full_count == 1 + + +def test_snapshot_model_data_intact_on_full_update(full_snapshot, fail_snapshot): + model = SnapshotModel() + model._add_snapshot(SnapshotModel.prerender(full_snapshot), "0") + + first_real = model.index(0, 0, model.index(0, 0)) + assert first_real.internalPointer().children["0"].data["status"] == "Pending" + # Update with a different snapshot, -- data should change accordingly + model._add_snapshot(SnapshotModel.prerender(fail_snapshot), "0") + first_real = model.index(0, 0, model.index(0, 0)) + + assert first_real.internalPointer().children["0"].data["status"] == "Finished"