Ensemble: Add normal low and generalise minmaxdialog.

scenario-dev-pa
Pierre-Antoine 2026-09-10 14:57:48 +02:00
parent 6e029beafc
commit 8e77d31a5a
5 changed files with 69 additions and 26 deletions

View File

@ -44,11 +44,16 @@ class Function(SQLSubModel):
self._name = name self._name = name
self._type = "generic" self._type = "generic"
self._script = script self._script = script
self._labels = []
@property @property
def name(self): def name(self):
return self._name return self._name
@property
def labels(self):
return self._labels
@classmethod @classmethod
def _db_create(cls, execute, ext=""): def _db_create(cls, execute, ext=""):
execute(f""" execute(f"""
@ -154,13 +159,35 @@ class Uniform(Function):
name=name, script=script name=name, script=script
) )
def random(self, ens_range, nb): self._labels = ["low", "high"]
lst = ens_range.copy()
def random(self, params, nb):
lst = params.copy()
lst.append(nb) lst.append(nb)
return np.random.uniform(*lst) return np.random.uniform(*lst)
class Normal(Function):
_sub_classes = []
def __init__(self, id: int = -1,
name: str = "normal", script: str = "",
status=None):
super(Normal, self).__init__(
id=id, status=status,
name=name, script=script
)
self._labels = ["loc", "scale"]
def random(self, params, nb):
lst = params.copy()
lst.append(nb)
return np.random.normal(*lst)
class Custom(Function): class Custom(Function):
_sub_classes = [] _sub_classes = []
@ -172,13 +199,14 @@ class Custom(Function):
name=name, script=script name=name, script=script
) )
self._labels = ["custom"]
self._type = "custom" self._type = "custom"
def random(self, ens_range, nb): def random(self, params, nb):
try: try:
# Run script to (re)define the sample function # Run script to (re)define the sample function
f = eval(self._script) f = eval(self._script)
return f(ens_range, nb) return f(params, nb)
except Exception as e: except Exception as e:
logger_exception(e) logger_exception(e)
return None # TODO: Raise helpful exception return None # TODO: Raise helpful exception

View File

@ -32,6 +32,7 @@ class FunctionList(PamhyrModelList):
# Default functions # Default functions
new._lst = [ new._lst = [
Uniform(status=data['status']), Uniform(status=data['status']),
Normal(status=data['status']),
] ]
# DB custom functions # DB custom functions

View File

@ -184,25 +184,26 @@ class EnsemblesWindow(PamhyrWindow):
for row in rows: for row in rows:
try: try:
dlg = MinMaxDialog( ens = self._ensembles.get(row)
ensemble=self._ensembles.get(row), dlg = Ensemble2ParamsDialog(
ensemble=ens,
trad=self._trad, trad=self._trad,
parent=self parent=self
) )
if dlg.exec(): if dlg.exec():
self._ensembles.get(row).params = dlg._minmax self._ensembles.get(row).params = dlg._values
except Exception as e: except Exception as e:
logger_exception(e) logger_exception(e)
return return
class MinMaxDialog(PamhyrDialog): class Ensemble2ParamsDialog(PamhyrDialog):
_pamhyr_ui = "MinMaxDialog" _pamhyr_ui = "Ensemble2ParamsDialog"
_pamhyr_name = "Minmax" _pamhyr_name = "Parameters"
def __init__(self, ensemble=None, def __init__(self, ensemble=None,
trad=None, parent=None): trad=None, parent=None):
super(MinMaxDialog, self).__init__( super(Ensemble2ParamsDialog, self).__init__(
title=trad[self._pamhyr_name], title=trad[self._pamhyr_name],
trad=trad, trad=trad,
options=[], options=[],
@ -210,23 +211,30 @@ class MinMaxDialog(PamhyrDialog):
) )
self._ensemble = ensemble self._ensemble = ensemble
self._labels = ensemble.function.labels
self._init_default_labels()
self._init_default_values() self._init_default_values()
def _init_default_labels(self):
self.set_label_text("label_0", self._trad[self._labels[0]])
self.set_label_text("label_1", self._trad[self._labels[1]])
def _init_default_values(self): def _init_default_values(self):
self._minmax = self._ensemble.params self._values = self._ensemble.params
if len(self._minmax) > 0: if len(self._values) > 0:
self.set_double_spin_box("doubleSpinBox_min", self._minmax[0]) self.set_double_spin_box("doubleSpinBox_0", self._values[0])
self.set_double_spin_box("doubleSpinBox_max", self._minmax[1]) self.set_double_spin_box("doubleSpinBox_1", self._values[1])
@property @property
def minmax(self): def values(self):
return self._minmax return self._values
def accept(self): def accept(self):
self._minmax = [ self._values = [
self.get_double_spin_box("doubleSpinBox_min"), self.get_double_spin_box("doubleSpinBox_0"),
self.get_double_spin_box("doubleSpinBox_max") self.get_double_spin_box("doubleSpinBox_1")
] ]
super().accept() super().accept()

View File

@ -40,6 +40,12 @@ class CommonWordTranslate(PamhyrTranslate):
self._dict["time"] = _translate("CommonWord", "Time") self._dict["time"] = _translate("CommonWord", "Time")
self._dict["date"] = _translate("CommonWord", "Date") self._dict["date"] = _translate("CommonWord", "Date")
self._dict["low"] = _translate("CommonWord", "Low")
self._dict["high"] = _translate("CommonWord", "High")
self._dict["loc"] = _translate("CommonWord", "Loc")
self._dict["scale"] = _translate("CommonWord", "Scale")
self._dict["reach"] = _translate("CommonWord", "Reach") self._dict["reach"] = _translate("CommonWord", "Reach")
self._dict["reaches"] = _translate("CommonWord", "Reaches") self._dict["reaches"] = _translate("CommonWord", "Reaches")
self._dict["Select reach"] = _translate("CommonWord", "Select reach") self._dict["Select reach"] = _translate("CommonWord", "Select reach")

View File

@ -17,14 +17,14 @@
<item row="0" column="0"> <item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label_0">
<property name="text"> <property name="text">
<string>Low:</string> <string>@param0</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QDoubleSpinBox" name="doubleSpinBox_min"> <widget class="QDoubleSpinBox" name="doubleSpinBox_0">
<property name="minimum"> <property name="minimum">
<double>-100000.000000000000000</double> <double>-100000.000000000000000</double>
</property> </property>
@ -38,14 +38,14 @@
<item row="0" column="1"> <item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_1">
<property name="text"> <property name="text">
<string>High:</string> <string>@param1</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QDoubleSpinBox" name="doubleSpinBox_max"> <widget class="QDoubleSpinBox" name="doubleSpinBox_1">
<property name="minimum"> <property name="minimum">
<double>-100000.000000000000000</double> <double>-100000.000000000000000</double>
</property> </property>