refactoring: refactor Results.

setup.py
Pierre-Antoine Rouby 2023-10-06 14:25:44 +02:00
parent ba087f12a5
commit 2ff4f63d23
14 changed files with 149 additions and 137 deletions

View File

@ -671,7 +671,7 @@ method to draw the plot from scratch, the second to update the plot if
data has changed. data has changed.
#+begin_src python :python python3 :results output :noweb yes #+begin_src python :python python3 :results output :noweb yes
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
class MyPlot(APlot): class MyPlot(APlot):
def __init__(self, canvas=None, data=None, toolbar=None): def __init__(self, canvas=None, data=None, toolbar=None):

View File

@ -43,6 +43,10 @@ class Results(object):
def river(self): def river(self):
return self._river return self._river
@property
def study(self):
return self._study
def set(self, key, value): def set(self, key, value):
self._meta_data[key] = value self._meta_data[key] = value

View File

@ -26,6 +26,9 @@ class Profile(object):
self._profile = profile # Source profile in the study self._profile = profile # Source profile in the study
self._data = {} # Dict of dict {<ts>: {<key>: <value>, ...}, ...} self._data = {} # Dict of dict {<ts>: {<key>: <value>, ...}, ...}
def __len__(self):
return len(self._data)
@property @property
def name(self): def name(self):
return self._profile.name return self._profile.name
@ -66,6 +69,9 @@ class Reach(object):
) )
) )
def __len__(self):
return len(self._profiles)
@property @property
def name(self): def name(self):
return self._reach.name return self._reach.name
@ -91,6 +97,9 @@ class River(object):
# Dict with timestamps as key # Dict with timestamps as key
self._reachs = [] self._reachs = []
def __len__(self):
return len(self._reachs)
@property @property
def reachs(self): def reachs(self):
return self._reachs.copy() return self._reachs.copy()

View File

@ -0,0 +1,32 @@
# Translate.py -- Pamhyr
# Copyright (C) 2023 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QCoreApplication
from View.Tools.PamhyrTranslate import PamhyrTranslate
_translate = QCoreApplication.translate
class CheckListTranslate(PamhyrTranslate):
def __init__(self):
super(CheckListTranslate, self).__init__()
self._sub_dict["table_headers"] = {
"name": _translate("CheckList", "Name"),
"status": _translate("CheckList", "Status"),
}

View File

@ -55,7 +55,7 @@ from View.SedimentLayers.Reach.Window import ReachSedimentLayersWindow
from View.SolverParameters.Window import SolverParametersWindow from View.SolverParameters.Window import SolverParametersWindow
from View.RunSolver.Window import SelectSolverWindow, SolverLogWindow from View.RunSolver.Window import SelectSolverWindow, SolverLogWindow
from View.CheckList.Window import CheckListWindow from View.CheckList.Window import CheckListWindow
# from View.Results.Window import ResultsWindow from View.Results.Window import ResultsWindow
from View.Debug.Window import ReplWindow from View.Debug.Window import ReplWindow
from Model.Study import Study from Model.Study import Study

View File

@ -17,7 +17,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from tools import timer from tools import timer
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -25,14 +25,16 @@ from PyQt5.QtCore import (
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
class PlotAC(APlot): class PlotAC(PamhyrPlot):
def __init__(self, canvas=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None): parent=None):
super(PlotAC, self).__init__( super(PlotAC, self).__init__(
canvas = canvas, canvas = canvas,
trad = trad,
data = results, data = results,
toolbar=toolbar toolbar = toolbar,
parent = parent
) )
self._current_timestamp = max(results.get("timestamps")) self._current_timestamp = max(results.get("timestamps"))

View File

@ -22,7 +22,7 @@ from functools import reduce
from datetime import datetime from datetime import datetime
from tools import timer, trace from tools import timer, trace
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -32,14 +32,16 @@ _translate = QCoreApplication.translate
logger = logging.getLogger() logger = logging.getLogger()
class PlotH(APlot): class PlotH(PamhyrPlot):
def __init__(self, canvas=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None): parent=None):
super(PlotH, self).__init__( super(PlotH, self).__init__(
canvas = canvas, canvas = canvas,
trad = trad,
data = results, data = results,
toolbar=toolbar toolbar = toolbar,
parent = parent
) )
self._mode = "time" self._mode = "time"

View File

@ -17,7 +17,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from tools import timer from tools import timer
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -25,14 +25,16 @@ from PyQt5.QtCore import (
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
class PlotKPC(APlot): class PlotKPC(PamhyrPlot):
def __init__(self, canvas=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None): parent=None):
super(PlotKPC, self).__init__( super(PlotKPC, self).__init__(
canvas = canvas, canvas = canvas,
trad = trad,
data = results, data = results,
toolbar=toolbar toolbar = toolbar,
parent = parent
) )
self._current_timestamp = max(results.get("timestamps")) self._current_timestamp = max(results.get("timestamps"))

View File

@ -5,7 +5,7 @@ import logging
from functools import reduce from functools import reduce
from tools import timer from tools import timer
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -15,14 +15,16 @@ _translate = QCoreApplication.translate
logger = logging.getLogger() logger = logging.getLogger()
class PlotSedProfile(APlot): class PlotSedProfile(PamhyrPlot):
def __init__(self, canvas=None, study=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None): parent=None):
super(PlotSedProfile, self).__init__( super(PlotSedProfile, self).__init__(
canvas = canvas, canvas = canvas,
data=study, trad = trad,
toolbar=toolbar data = results,
toolbar = toolbar,
parent = parent
) )
self._results = results self._results = results

View File

@ -5,7 +5,7 @@ import logging
from functools import reduce from functools import reduce
from tools import timer from tools import timer
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -15,14 +15,16 @@ _translate = QCoreApplication.translate
logger = logging.getLogger() logger = logging.getLogger()
class PlotSedReach(APlot): class PlotSedReach(PamhyrPlot):
def __init__(self, canvas=None, study=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None): parent=None):
super(PlotSedReach, self).__init__( super(PlotSedReach, self).__init__(
canvas = canvas, canvas = canvas,
data=study, trad = trad,
toolbar=toolbar data = results,
toolbar = toolbar,
parent = parent
) )
self._results = results self._results = results

View File

@ -21,7 +21,7 @@ import logging
from functools import reduce from functools import reduce
from tools import timer, trace from tools import timer, trace
from View.Plot.APlot import APlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication
@ -31,14 +31,16 @@ _translate = QCoreApplication.translate
logger = logging.getLogger() logger = logging.getLogger()
class PlotXY(APlot): class PlotXY(PamhyrPlot):
def __init__(self, canvas=None, results=None, def __init__(self, canvas=None, trad=None, toolbar=None,
reach_id=0, profile_id=0, results=None, reach_id=0, profile_id=0,
toolbar=None, display_current=True): display_current=True, parent=None):
super(PlotXY, self).__init__( super(PlotXY, self).__init__(
canvas = canvas, canvas = canvas,
trad = trad,
data = results, data = results,
toolbar=toolbar toolbar = toolbar,
parent = parent
) )
self.display_current = display_current self.display_current = display_current

View File

@ -34,6 +34,7 @@ from PyQt5.QtWidgets import (
QComboBox, QComboBox,
) )
from View.Tools.PamhyrTable import PamhyrTableModel
from View.Results.translate import * from View.Results.translate import *
logger = logging.getLogger() logger = logging.getLogger()
@ -41,33 +42,16 @@ logger = logging.getLogger()
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
class TableModel(QAbstractTableModel): class TableModel(PamhyrTableModel):
def __init__(self, results = None, study = None, mode = None, undo=None): def _setup_lst(self):
super(QAbstractTableModel, self).__init__() _river = self._data.river
if self._opt_data == "reach":
self._lst = _river.reachs
else:
self._lst = _river.reach(0).profiles
self._results = results def __init__(self, **kwargs):
self._study = study super(TableModel, self).__init__(**kwargs)
self._mode = mode
self._undo_stack = undo
self._table_headers = table_headers_reach
if mode != "reach":
self._table_headers = table_headers_profile
self._headers = list(self._table_headers.keys())
self._selected = 0
self._timestamp = 0
def rowCount(self, parent=QModelIndex()):
if self._mode == "reach":
return len(self._results.river.reachs)
current_reach = self._results.river.reach(self._selected)
return len(current_reach.profiles)
def columnCount(self, parent=QModelIndex()):
return len(self._headers)
def data(self, index, role=Qt.DisplayRole): def data(self, index, role=Qt.DisplayRole):
if role != Qt.ItemDataRole.DisplayRole: if role != Qt.ItemDataRole.DisplayRole:
@ -76,32 +60,23 @@ class TableModel(QAbstractTableModel):
row = index.row() row = index.row()
column = index.column() column = index.column()
if self._mode == "reach": if self._opt_data == "reach":
if self._headers[column] == "name": if self._headers[column] == "name":
v = self._results.river.reach(row).name v = self._lst[row].name
return str(v) return str(v)
else: else:
current_reach = self._results.river.reach(self._selected)
if self._headers[column] == "name": if self._headers[column] == "name":
v = current_reach.profile(row).name v = self._lst[row].name
return str(v) return str(v)
elif self._headers[column] == "kp": elif self._headers[column] == "kp":
v = current_reach.profile(row).kp v = self._lst[row].kp
return f"{v:.4f}" return f"{v:.4f}"
return QVariant() return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): def update(self, reach):
if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.Orientation.Horizontal: _river = self._data.river
return self._table_headers[self._headers[section]] if self._opt_data == "reach":
self._lst = _river.reachs
return QVariant() else:
self._lst = _river.reach(reach).profiles
def index(self, row, column, parent=QModelIndex()):
if not self.hasIndex(row, column, parent):
return QModelIndex()
return self.createIndex(row, column, QModelIndex())
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable

View File

@ -41,8 +41,8 @@ from PyQt5.QtWidgets import (
QSlider, QLabel, QSlider, QLabel,
) )
from View.Plot.MplCanvas import MplCanvas from View.Tools.Plot.PamhyrCanvas import MplCanvas
from View.Plot.PamhyrToolbar import PamhyrPlotToolbar from View.Tools.Plot.PamhyrToolbar import PamhyrPlotToolbar
from View.Results.PlotXY import PlotXY from View.Results.PlotXY import PlotXY
from View.Results.PlotAC import PlotAC from View.Results.PlotAC import PlotAC
@ -52,10 +52,7 @@ from View.Results.PlotSedReach import PlotSedReach
from View.Results.PlotSedProfile import PlotSedProfile from View.Results.PlotSedProfile import PlotSedProfile
from View.Results.Table import TableModel from View.Results.Table import TableModel
from View.Results.translate import ( from View.Results.translate import ResultsTranslate
table_headers_reach, table_headers_profile,
retranslate,
)
from View.Stricklers.Window import StricklersWindow from View.Stricklers.Window import StricklersWindow
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
@ -85,42 +82,27 @@ class ResultsWindow(PamhyrWindow):
title = name, title = name,
study = study, study = study,
config = config, config = config,
trad = ResultsTranslate(),
parent=parent parent=parent
) )
self.setup_table() self.setup_table()
self.setup_graph() self.setup_plot()
self.setup_slider() self.setup_slider()
self.setup_statusbar() self.setup_statusbar()
self.setup_connections() self.setup_connections()
self.ui.setWindowTitle(self._title)
def setup_title(self, title):
self._title = (
title + " - "
+ self._study.name + " - "
+ self._solver.name + " - "
+ self._results.date
)
def setup_table(self): def setup_table(self):
retranslate()
self._table = {} self._table = {}
for t in ["reach", "profile"]: for t in ["reach", "profile"]:
table = self.find(QTableView, f"tableView_{t}") table = self.find(QTableView, f"tableView_{t}")
self._table[t] = TableModel( self._table[t] = TableModel(
results = self._results, table_view = table,
study = self._study, table_headers = self._trad.get_dict(f"table_headers_{t}"),
mode = t, data = self._results,
undo = self._undo_stack, undo = self._undo_stack,
opt_data = t
) )
table.setModel(self._table[t])
table.setSelectionBehavior(QAbstractItemView.SelectRows)
table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
table.setAlternatingRowColors(True)
def setup_slider(self): def setup_slider(self):
self._slider_profile = self.find(QSlider, f"verticalSlider_profile") self._slider_profile = self.find(QSlider, f"verticalSlider_profile")
@ -132,7 +114,7 @@ class ResultsWindow(PamhyrWindow):
self._slider_time.setMaximum(len(self._timestamps) - 1) self._slider_time.setMaximum(len(self._timestamps) - 1)
self._slider_time.setValue(len(self._timestamps) - 1) self._slider_time.setValue(len(self._timestamps) - 1)
def setup_graph(self): def setup_plot(self):
self.canvas = MplCanvas(width=5, height=4, dpi=100) self.canvas = MplCanvas(width=5, height=4, dpi=100)
self.canvas.setObjectName("canvas") self.canvas.setObjectName("canvas")
self.toolbar = PamhyrPlotToolbar( self.toolbar = PamhyrPlotToolbar(
@ -219,7 +201,6 @@ class ResultsWindow(PamhyrWindow):
self.plot_sed_reach = PlotSedReach( self.plot_sed_reach = PlotSedReach(
canvas = self.canvas_5, canvas = self.canvas_5,
results = self._results, results = self._results,
study = self._study,
reach_id = 0, reach_id = 0,
profile_id = 0, profile_id = 0,
toolbar = self.toolbar_5 toolbar = self.toolbar_5
@ -239,7 +220,6 @@ class ResultsWindow(PamhyrWindow):
self.plot_sed_profile = PlotSedProfile( self.plot_sed_profile = PlotSedProfile(
canvas = self.canvas_6, canvas = self.canvas_6,
results = self._results, results = self._results,
study = self._study,
reach_id = 0, reach_id = 0,
profile_id = 0, profile_id = 0,
toolbar = self.toolbar_6 toolbar = self.toolbar_6

View File

@ -18,19 +18,19 @@
from PyQt5.QtCore import QCoreApplication from PyQt5.QtCore import QCoreApplication
from View.Tools.PamhyrTranslate import PamhyrTranslate
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
table_headers_reach = { class ResultsTranslate(PamhyrTranslate):
def __init__(self):
super(ResultsTranslate, self).__init__()
self._sub_dict["table_headers_reach"] = {
"name": _translate("Results", "Reach name"), "name": _translate("Results", "Reach name"),
} }
table_headers_profile = { self._sub_dict["table_headers_profile"] = {
"name": _translate("Results", "Name"), "name": _translate("Results", "Name"),
"kp": _translate("Results", "KP (m)"), "kp": _translate("Results", "KP (m)"),
} }
def retranslate():
table_headers_reach["name"] = _translate("Results", "Reach name")
table_headers_profile["name"] = _translate("Results", "Name")
table_headers_profile["kp"] = _translate("Results", "KP (m)")