From 6dc42b637a72e23a05061c83cfa4eb07fb934e52 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Fri, 24 Jul 2026 11:44:53 +0200 Subject: [PATCH] Ensemble: Function: Some minor fix and add it to study. --- src/Model/Ensembles/Function.py | 45 ++++++++++------------------- src/Model/Ensembles/FunctionList.py | 16 ++++------ src/Model/Study.py | 15 +++++++++- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/Model/Ensembles/Function.py b/src/Model/Ensembles/Function.py index 7c828306..9db824bd 100644 --- a/src/Model/Ensembles/Function.py +++ b/src/Model/Ensembles/Function.py @@ -34,10 +34,10 @@ class Function(SQLSubModel): def __init__(self, id: int = -1, name: str = "", script: str = "", - status=None, owner_scenario=-1): + status=None): super(Function, self).__init__( id=id, status=status, - owner_scenario=owner_scenario + owner_scenario=0 ) self._name = name @@ -52,9 +52,7 @@ class Function(SQLSubModel): deleted BOOLEAN NOT NULL DEFAULT FALSE, name TEXT NOT NULL, script TEXT NOT NULL, - {Scenario.create_db_add_scenario()}, - {Scenario.create_db_add_scenario_fk()}, - PRIMARY KEY(pamhyr_id, scenario) + PRIMARY KEY(pamhyr_id) ) """) @@ -81,10 +79,9 @@ class Function(SQLSubModel): loaded = data['loaded_pid'] table = execute( - "SELECT pamhyr_id, deleted, name, script, scenario " + + "SELECT pamhyr_id, deleted, name, script " + "FROM ensemble_function " + - f"WHERE scenario = {scenario.id} " + - f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})" + f"WHERE pamhyr_id NOT IN ({', '.join(map(str, loaded))})" ) for row in table: @@ -94,12 +91,10 @@ class Function(SQLSubModel): deleted = (next(it) == 1) name = next(it) script = next(it) - owner_scenario = next(it) new_function = cls( id, name=name, script=script, - status=data["status"], - owner_scenario=owner_scenario + status=data["status"] ) if deleted: new_function.set_as_deleted() @@ -110,28 +105,20 @@ class Function(SQLSubModel): return new def _db_save(self, execute, data=None): + # Only save custom function if self._type != "custom": return True if not self.must_be_saved(): return True - execute( - "DELETE FROM ensemble_function " + - f"WHERE pamhyr_id = {self.pamhyr_id} " + - f"AND scenario = {self._status.scenario_id}" - ) - - brange, length = self._encode_range() - execute( "INSERT INTO " + "ensemble_function(pamhyr_id, deleted, " + - " name, script, scenario) " + - "VALUES (?, ?, ?, ?, ?, )", + " name, script) " + + "VALUES (?, ?, ?, ?)", self.pamhyr_id, self.is_deleted(), - self._name, self._script, - self._status.scenario_id + self._name, self._script ) return True @@ -156,11 +143,10 @@ class Uniform(Function): def __init__(self, id: int = -1, name: str = "uniform", script: str = "", - status=None, owner_scenario=-1): - super(Custom, self).__init__( + status=None): + super(Uniform, self).__init__( id=id, status=status, - name=name, script=script, - owner_scenario=owner_scenario + name=name, script=script ) def get_sample(self, dist, ens_range): @@ -172,11 +158,10 @@ class Custom(Function): def __init__(self, id: int = -1, name: str = "", script: str = "", - status=None, owner_scenario=-1): + status=None): super(Custom, self).__init__( id=id, status=status, - name=name, script=script, - owner_scenario=owner_scenario + name=name, script=script ) self._type = "custom" diff --git a/src/Model/Ensembles/FunctionList.py b/src/Model/Ensembles/FunctionList.py index 7e1a6050..0e04ffbc 100644 --- a/src/Model/Ensembles/FunctionList.py +++ b/src/Model/Ensembles/FunctionList.py @@ -19,7 +19,7 @@ from tools import trace, timer from Model.Tools.PamhyrListExt import PamhyrModelList -from Model.Ensembles.Function import Function +from Model.Ensembles.Function import * class FunctionList(PamhyrModelList): @@ -29,24 +29,20 @@ class FunctionList(PamhyrModelList): def _db_load(cls, execute, data=None): new = cls(status=data['status']) - new._lst = [Uniform] + new._lst = [Uniform(status=data['status'])] new._lst += Function._db_load( execute, data ) return new - def _db_save(self, execute, data=None): + def _db_save(self, execute, data={}): execute( - "DELETE FROM ensemble_function " + - f"WHERE scenario = {self._status.scenario_id}" + "DELETE FROM ensemble_function" ) - if data is None: - data = {} - - for ens in self._lst: - ens._db_save(execute, data=data) + for fun in self._lst: + fun._db_save(execute, data) return True diff --git a/src/Model/Study.py b/src/Model/Study.py index 8b80efb8..cb834d8d 100644 --- a/src/Model/Study.py +++ b/src/Model/Study.py @@ -33,6 +33,7 @@ from Model.Status import StudyStatus from Model.Except import NotImplementedMethodeError from Model.River import River from Model.Geometry.Reach import Reach +from Model.Ensembles.FunctionList import FunctionList from Model.HydraulicStructures.HydraulicStructures import ( HydraulicStructure ) @@ -50,6 +51,7 @@ class Study(SQLModel): _sub_classes = [ Scenario, + FunctionList, River, ] @@ -84,6 +86,9 @@ class Study(SQLModel): self.scenarios = Scenarios(status=self.status) self.scenarios[0] = s0 self.status.scenario = s0 + + self._ens_function = FunctionList(status=self.status) + self._river = River(status=self.status) else: self._init_db_file(filename, is_new=False) @@ -394,6 +399,11 @@ class Study(SQLModel): data["scenario"] = scenario data["loaded_pid"] = set() + # Get ensemble function + new._ens_function = FunctionList._db_load( + sql_exec, data=data + ) + # Load river data new._river = River._db_load( sql_exec, data=data @@ -455,7 +465,10 @@ class Study(SQLModel): progress() self._save_submodel( - [self.scenarios, self._river], + [ + self.scenarios, self._ens_function, + self._river + ], data=progress )