From 1f0d49bd4def2ea9cc512898377255a0fd15c388 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Tue, 8 Aug 2023 16:36:28 +0200 Subject: [PATCH] Results: Add results model. --- src/Model/Results/Results.py | 45 ++++++++++++++++++ src/Model/Results/River/River.py | 81 ++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/Model/Results/Results.py create mode 100644 src/Model/Results/River/River.py diff --git a/src/Model/Results/Results.py b/src/Model/Results/Results.py new file mode 100644 index 00000000..b3a83e43 --- /dev/null +++ b/src/Model/Results/Results.py @@ -0,0 +1,45 @@ +# Results.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/>. + +import logging +import numpy as np + +from copy import deepcopy +from datetime import datetime + +from Model.Results.River.River import River + +logger = logging.getLogger() + +class Results(object): + def __init__(self, study = None): + self._study = study + self._river = River(self._study) + + self._meta_data = { + # Keep results creation date + "creation_date": datetime.now(), + } + + @property + def river(self): + return self._river + + def set(self, key, value): + self._meta_data[key] = value + + def get(self, key): + return self._meta_data[key] diff --git a/src/Model/Results/River/River.py b/src/Model/Results/River/River.py new file mode 100644 index 00000000..7425f532 --- /dev/null +++ b/src/Model/Results/River/River.py @@ -0,0 +1,81 @@ +# River.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/>. + +import logging + +from datetime import datetime + +logger = logging.getLogger() + +class Profile(object): + def __init__(self, profile, study): + self._study = study + self._profile = profile # Source profile in the study + self._data = {} # Dict of dict {<ts>: {<key>: <value>, ...}, ...} + + def set(self, timestamp, key, data): + if timestamp not in self._data: + self._data[timestamp] = {} + + self._data[timestamp][key] = data + + def get_ts(self, timestamp): + return self._data[timestamp] + + def get_key(self, timestamp): + return list( + map(lambda ts: self._data[ts][key], self._data) + ) + + def get_ts_key(self, timestamp, key): + return self._data[timestamp][key] + +class Reach(object): + def __init__(self, reach, study): + self._study = study + self._reach = reach # Source reach in the study + self._profiles = list( + map( + lambda p: Profile(p, self._study), + reach.profiles + ) + ) + + @property + def profiles(self): + return self._profiles.copy() + + def set(self, profile_id, timestamp, key, data): + self._profiles[profile_id].set(timestamp, key, data) + +class River(object): + def __init__(self, study): + self._study = study + + # Dict with timestamps as key + self._reachs = [] + + @property + def reachs(self): + return self.reachs.copy() + + def add(self, reach_id): + reachs = self._study.river.enable_edges() + + new = Reach(reachs[reach_id].reach, self._study) + + self._reachs.append(new) + return new -- GitLab