mirror of https://gitlab.com/pamhyr/pamhyr2
Ensemble: Function: Some minor fix and add it to study.
parent
e6d12713e3
commit
6dc42b637a
|
|
@ -34,10 +34,10 @@ class Function(SQLSubModel):
|
||||||
|
|
||||||
def __init__(self, id: int = -1,
|
def __init__(self, id: int = -1,
|
||||||
name: str = "", script: str = "",
|
name: str = "", script: str = "",
|
||||||
status=None, owner_scenario=-1):
|
status=None):
|
||||||
super(Function, self).__init__(
|
super(Function, self).__init__(
|
||||||
id=id, status=status,
|
id=id, status=status,
|
||||||
owner_scenario=owner_scenario
|
owner_scenario=0
|
||||||
)
|
)
|
||||||
|
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
@ -52,9 +52,7 @@ class Function(SQLSubModel):
|
||||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
script TEXT NOT NULL,
|
script TEXT NOT NULL,
|
||||||
{Scenario.create_db_add_scenario()},
|
PRIMARY KEY(pamhyr_id)
|
||||||
{Scenario.create_db_add_scenario_fk()},
|
|
||||||
PRIMARY KEY(pamhyr_id, scenario)
|
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
@ -81,10 +79,9 @@ class Function(SQLSubModel):
|
||||||
loaded = data['loaded_pid']
|
loaded = data['loaded_pid']
|
||||||
|
|
||||||
table = execute(
|
table = execute(
|
||||||
"SELECT pamhyr_id, deleted, name, script, scenario " +
|
"SELECT pamhyr_id, deleted, name, script " +
|
||||||
"FROM ensemble_function " +
|
"FROM ensemble_function " +
|
||||||
f"WHERE scenario = {scenario.id} " +
|
f"WHERE pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
|
||||||
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for row in table:
|
for row in table:
|
||||||
|
|
@ -94,12 +91,10 @@ class Function(SQLSubModel):
|
||||||
deleted = (next(it) == 1)
|
deleted = (next(it) == 1)
|
||||||
name = next(it)
|
name = next(it)
|
||||||
script = next(it)
|
script = next(it)
|
||||||
owner_scenario = next(it)
|
|
||||||
|
|
||||||
new_function = cls(
|
new_function = cls(
|
||||||
id, name=name, script=script,
|
id, name=name, script=script,
|
||||||
status=data["status"],
|
status=data["status"]
|
||||||
owner_scenario=owner_scenario
|
|
||||||
)
|
)
|
||||||
if deleted:
|
if deleted:
|
||||||
new_function.set_as_deleted()
|
new_function.set_as_deleted()
|
||||||
|
|
@ -110,28 +105,20 @@ class Function(SQLSubModel):
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def _db_save(self, execute, data=None):
|
def _db_save(self, execute, data=None):
|
||||||
|
# Only save custom function
|
||||||
if self._type != "custom":
|
if self._type != "custom":
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if not self.must_be_saved():
|
if not self.must_be_saved():
|
||||||
return True
|
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(
|
execute(
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"ensemble_function(pamhyr_id, deleted, " +
|
"ensemble_function(pamhyr_id, deleted, " +
|
||||||
" name, script, scenario) " +
|
" name, script) " +
|
||||||
"VALUES (?, ?, ?, ?, ?, )",
|
"VALUES (?, ?, ?, ?)",
|
||||||
self.pamhyr_id, self.is_deleted(),
|
self.pamhyr_id, self.is_deleted(),
|
||||||
self._name, self._script,
|
self._name, self._script
|
||||||
self._status.scenario_id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
@ -156,11 +143,10 @@ class Uniform(Function):
|
||||||
|
|
||||||
def __init__(self, id: int = -1,
|
def __init__(self, id: int = -1,
|
||||||
name: str = "uniform", script: str = "",
|
name: str = "uniform", script: str = "",
|
||||||
status=None, owner_scenario=-1):
|
status=None):
|
||||||
super(Custom, self).__init__(
|
super(Uniform, self).__init__(
|
||||||
id=id, status=status,
|
id=id, status=status,
|
||||||
name=name, script=script,
|
name=name, script=script
|
||||||
owner_scenario=owner_scenario
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_sample(self, dist, ens_range):
|
def get_sample(self, dist, ens_range):
|
||||||
|
|
@ -172,11 +158,10 @@ class Custom(Function):
|
||||||
|
|
||||||
def __init__(self, id: int = -1,
|
def __init__(self, id: int = -1,
|
||||||
name: str = "", script: str = "",
|
name: str = "", script: str = "",
|
||||||
status=None, owner_scenario=-1):
|
status=None):
|
||||||
super(Custom, self).__init__(
|
super(Custom, self).__init__(
|
||||||
id=id, status=status,
|
id=id, status=status,
|
||||||
name=name, script=script,
|
name=name, script=script
|
||||||
owner_scenario=owner_scenario
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self._type = "custom"
|
self._type = "custom"
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
from tools import trace, timer
|
from tools import trace, timer
|
||||||
|
|
||||||
from Model.Tools.PamhyrListExt import PamhyrModelList
|
from Model.Tools.PamhyrListExt import PamhyrModelList
|
||||||
from Model.Ensembles.Function import Function
|
from Model.Ensembles.Function import *
|
||||||
|
|
||||||
|
|
||||||
class FunctionList(PamhyrModelList):
|
class FunctionList(PamhyrModelList):
|
||||||
|
|
@ -29,24 +29,20 @@ class FunctionList(PamhyrModelList):
|
||||||
def _db_load(cls, execute, data=None):
|
def _db_load(cls, execute, data=None):
|
||||||
new = cls(status=data['status'])
|
new = cls(status=data['status'])
|
||||||
|
|
||||||
new._lst = [Uniform]
|
new._lst = [Uniform(status=data['status'])]
|
||||||
new._lst += Function._db_load(
|
new._lst += Function._db_load(
|
||||||
execute, data
|
execute, data
|
||||||
)
|
)
|
||||||
|
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def _db_save(self, execute, data=None):
|
def _db_save(self, execute, data={}):
|
||||||
execute(
|
execute(
|
||||||
"DELETE FROM ensemble_function " +
|
"DELETE FROM ensemble_function"
|
||||||
f"WHERE scenario = {self._status.scenario_id}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if data is None:
|
for fun in self._lst:
|
||||||
data = {}
|
fun._db_save(execute, data)
|
||||||
|
|
||||||
for ens in self._lst:
|
|
||||||
ens._db_save(execute, data=data)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ from Model.Status import StudyStatus
|
||||||
from Model.Except import NotImplementedMethodeError
|
from Model.Except import NotImplementedMethodeError
|
||||||
from Model.River import River
|
from Model.River import River
|
||||||
from Model.Geometry.Reach import Reach
|
from Model.Geometry.Reach import Reach
|
||||||
|
from Model.Ensembles.FunctionList import FunctionList
|
||||||
from Model.HydraulicStructures.HydraulicStructures import (
|
from Model.HydraulicStructures.HydraulicStructures import (
|
||||||
HydraulicStructure
|
HydraulicStructure
|
||||||
)
|
)
|
||||||
|
|
@ -50,6 +51,7 @@ class Study(SQLModel):
|
||||||
|
|
||||||
_sub_classes = [
|
_sub_classes = [
|
||||||
Scenario,
|
Scenario,
|
||||||
|
FunctionList,
|
||||||
River,
|
River,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -84,6 +86,9 @@ class Study(SQLModel):
|
||||||
self.scenarios = Scenarios(status=self.status)
|
self.scenarios = Scenarios(status=self.status)
|
||||||
self.scenarios[0] = s0
|
self.scenarios[0] = s0
|
||||||
self.status.scenario = s0
|
self.status.scenario = s0
|
||||||
|
|
||||||
|
self._ens_function = FunctionList(status=self.status)
|
||||||
|
|
||||||
self._river = River(status=self.status)
|
self._river = River(status=self.status)
|
||||||
else:
|
else:
|
||||||
self._init_db_file(filename, is_new=False)
|
self._init_db_file(filename, is_new=False)
|
||||||
|
|
@ -394,6 +399,11 @@ class Study(SQLModel):
|
||||||
data["scenario"] = scenario
|
data["scenario"] = scenario
|
||||||
data["loaded_pid"] = set()
|
data["loaded_pid"] = set()
|
||||||
|
|
||||||
|
# Get ensemble function
|
||||||
|
new._ens_function = FunctionList._db_load(
|
||||||
|
sql_exec, data=data
|
||||||
|
)
|
||||||
|
|
||||||
# Load river data
|
# Load river data
|
||||||
new._river = River._db_load(
|
new._river = River._db_load(
|
||||||
sql_exec, data=data
|
sql_exec, data=data
|
||||||
|
|
@ -455,7 +465,10 @@ class Study(SQLModel):
|
||||||
progress()
|
progress()
|
||||||
|
|
||||||
self._save_submodel(
|
self._save_submodel(
|
||||||
[self.scenarios, self._river],
|
[
|
||||||
|
self.scenarios, self._ens_function,
|
||||||
|
self._river
|
||||||
|
],
|
||||||
data=progress
|
data=progress
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue