Compare commits

..
15 changed files with 191 additions and 102 deletions

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

+2 -1
View File
@@ -4,7 +4,7 @@ PyQt5-sip==12.18.0
PyQtWebEngine==5.15.6
QScintilla>=2.14.1
pyqtgraph>=0.12.1
matplotlib==3.8.3
matplotlib==3.10.8
numpy>=1.24.2
colorama>=0.4.3
pyinstaller>=5.11.0
@@ -14,3 +14,4 @@ platformdirs>=4.2.0
pyshp>=2.3.1
rasterio==1.3.11
#fortranformat==2.0.3
psutil>=7.2.2
+70 -4
View File
@@ -46,6 +46,8 @@ class Distribution(SQLSubModel):
self._script = script
self._labels = []
self._default_values = []
self._params_checkers = []
self._params_tips = []
@property
def name(self):
@@ -145,13 +147,28 @@ class Distribution(SQLSubModel):
if predicate(self, data):
modifier(self, data)
def is_valid_parameters(self, parameters):
valid = True
tips = []
for i, checker in enumerate(self._params_checkers):
ok = checker(parameters)
if not ok:
tips.append(self._params_tips[i])
valid = False
return (valid, tips)
def check_params(self, params):
return True
def random(self, r):
raise NotImplementedMethodeError(cls, cls._load)
###########################
###############################
# DISTRIBUTION IMPLEMENTATION #
###########################
###############################
class Uniform(Distribution):
_sub_classes = []
@@ -166,6 +183,11 @@ class Uniform(Distribution):
self._labels = ["low", "high"]
self._default_values = [20.0, 40.0]
self._params_checkers = [self.check_params, self.check_params]
self._params_tips = ["uniform_low_tips", "uniform_high_tips"]
def check_params(self, params):
return (params[0] < params[1])
def random(self, params, nb):
lst = params.copy()
@@ -187,6 +209,11 @@ class Normal(Distribution):
self._labels = ["loc", "scale"]
self._default_values = [30.0, 5.0]
self._params_checkers = [self.check_params, self.check_scale]
self._params_tips = ["normal_loc_tips", "normal_scale_tips"]
def check_scale(self, params):
return 0 <= params[1]
def random(self, params, nb):
lst = params.copy()
@@ -195,6 +222,37 @@ class Normal(Distribution):
return np.random.normal(*lst)
class Binomial(Distribution):
_sub_classes = []
def __init__(self, id: int = -1,
name: str = "binomial", script: str = "",
status=None):
super(Binomial, self).__init__(
id=id, status=status,
name=name, script=script
)
self._labels = ["binomial_n", "binomial_p"]
self._default_values = [100.0, 0.3]
self._params_checkers = [self.check_n, self.check_p]
self._params_tips = ["binomial_n_tips", "binomial_p_tips"]
def check_n(self, params):
n = params[0]
return (0 <= n) and (n == round(n))
def check_p(self, params):
p = params[1]
return 0 <= p <= 1
def random(self, params, nb):
lst = params.copy()
lst.append(nb)
return np.random.binomial(*lst)
class Poisson(Distribution):
_sub_classes = []
@@ -206,8 +264,14 @@ class Poisson(Distribution):
name=name, script=script
)
self._labels = ["lam"]
self._labels = ["poisson_lam"]
self._default_values = [5.0]
self._params_checkers = [self.check_lam]
self._params_tips = ["poisson_lam_tips"]
def check_lam(self, params):
lam = params[0]
return 0 <= lam
def random(self, params, nb):
lst = params.copy()
@@ -229,7 +293,9 @@ class Custom(Distribution):
self._labels = ["custom"]
self._type = "custom"
self._default_values = ""
self._default_values = [""]
self._params_checkers = [self.check_params]
self._params_tips = ["custom_params_tips"]
def random(self, params, nb):
try:
+5 -3
View File
@@ -30,10 +30,12 @@ class DistributionList(PamhyrModelList):
new = cls(status=data['status'])
# Default distributions
d = {"status": data['status']}
new._lst = [
Uniform(status=data['status']),
Normal(status=data['status']),
Poisson(status=data['status']),
Uniform(**d),
Normal(**d),
Binomial(**d),
Poisson(**d),
]
# DB custom distributions
+64
View File
@@ -0,0 +1,64 @@
# Translate.py -- Pamhyr
# Copyright (C) 2023-2026 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.Ensembles.Translate import EnsemblesTranslate
_translate = QCoreApplication.translate
class EnsembleEditTranslate(EnsemblesTranslate):
def __init__(self):
super(EnsembleEditTranslate, self).__init__()
self._dict["binomial_n"] = _translate("Ensemble", "n")
self._dict["binomial_p"] = _translate("Ensemble", "p")
self._dict["poisson_lam"] = _translate("Ensemble", "Lam")
self._dict["checker_failed_title"] = _translate(
"Ensemble",
"Invalid parameter"
)
self._dict["checker_failed_text"] = _translate(
"Ensemble",
"This parameter is not valid for selected distribution: "
)
uniform = _translate(
"Ensemble", "(High - Low) must be non-negative."
)
self._dict["uniform_low_tips"] = uniform
self._dict["uniform_high_tips"] = uniform
self._dict["normal_scale_tips"] = _translate(
"Ensemble", "Scale must be non-negative."
)
self._dict["binomial_n_tips"] = _translate(
"Ensemble", "N must be non-negative and must be an integers."
)
self._dict["binomial_p_tips"] = _translate(
"Ensemble", "P must be >= 0 and <=1."
)
self._dict["poisson_lam_tips"] = _translate(
"Ensemble", "Lam must be >= 0."
)
+25 -4
View File
@@ -45,6 +45,8 @@ from PyQt5.QtWidgets import (
QGroupBox, QTreeWidget,
)
from View.Ensembles.Edit.Translate import EnsembleEditTranslate
logger = logging.getLogger()
@@ -74,7 +76,9 @@ class EnsembleEditDialog(PamhyrDialog):
_pamhyr_name = "Parameters"
def __init__(self, study=None, ensemble=None,
trad=None, parent=None):
parent=None):
trad = EnsembleEditTranslate()
super(EnsembleEditDialog, self).__init__(
title=trad[self._pamhyr_name],
trad=trad,
@@ -209,13 +213,26 @@ class EnsembleEditDialog(PamhyrDialog):
),
None
)
parameters = self._params_widget.values
if distribution is not None:
ok, tips = distribution.is_valid_parameters(parameters)
if not ok:
self.message_box(
text=self._trad["checker_failed_text"],
informative_text="\n".join(
map(lambda t: " - " + self._trad[t], tips)
),
window_title=self._trad["checker_failed_title"],
)
return
self._values = {
"name": self.get_line_edit_text("lineEdit_name"),
"data_type": self._target_data[0],
"target_data": self._target_data[1],
"distribution": distribution,
"params": self._params_widget.values,
"params": parameters,
}
super().accept()
@@ -248,7 +265,9 @@ class Ensemble1ParamsWidget(PamhyrWidget):
def _init_default_values(self):
self._values = self._ensemble.params
if len(self._values) > 0:
if len(self._values) != 1:
self._values = self._distribution.default_values.copy()
self.set_double_spin_box("doubleSpinBox_0", self._values[0])
@property
@@ -285,7 +304,9 @@ class Ensemble2ParamsWidget(PamhyrWidget):
def _init_default_values(self):
self._values = self._ensemble.params
if len(self._values) > 0:
if len(self._values) != 2:
self._values = self._distribution.default_values.copy()
self.set_double_spin_box("doubleSpinBox_0", self._values[0])
self.set_double_spin_box("doubleSpinBox_1", self._values[1])
+1
View File
@@ -114,6 +114,7 @@ class AddCommand(QUndoCommand):
else:
self._ensembles.insert(self._index, self._new)
class ImportCommand(QUndoCommand):
def __init__(self, ensembles, data, index):
QUndoCommand.__init__(self)
-86
View File
@@ -197,7 +197,6 @@ class EnsemblesWindow(PamhyrWindow):
dlg = EnsembleEditDialog(
study=self._study,
ensemble=ens,
trad=self._trad,
parent=self
)
@@ -279,88 +278,3 @@ class EnsemblesWindow(PamhyrWindow):
)
)
self._table.update()
class Ensemble1ParamsDialog(PamhyrDialog):
_pamhyr_ui = "Ensemble1ParamsDialog"
_pamhyr_name = "Parameters"
def __init__(self, ensemble=None,
trad=None, parent=None):
super(Ensemble1ParamsDialog, self).__init__(
title=trad[self._pamhyr_name],
trad=trad,
options=[],
parent=parent
)
self._ensemble = ensemble
self._labels = ensemble.distribution.labels
self._init_default_labels()
self._init_default_values()
def _init_default_labels(self):
self.set_label_text("label_0", self._trad[self._labels[0]])
def _init_default_values(self):
self._values = self._ensemble.params
if len(self._values) > 0:
self.set_double_spin_box("doubleSpinBox_0", self._values[0])
@property
def values(self):
return self._values
def accept(self):
self._values = [
self.get_double_spin_box("doubleSpinBox_0"),
]
super().accept()
def reject(self):
self.close()
class Ensemble2ParamsDialog(PamhyrDialog):
_pamhyr_ui = "Ensemble2ParamsDialog"
_pamhyr_name = "Parameters"
def __init__(self, ensemble=None,
trad=None, parent=None):
super(Ensemble2ParamsDialog, self).__init__(
title=trad[self._pamhyr_name],
trad=trad,
options=[],
parent=parent
)
self._ensemble = ensemble
self._labels = ensemble.distribution.labels
self._init_default_labels()
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):
self._values = self._ensemble.params
if len(self._values) > 0:
self.set_double_spin_box("doubleSpinBox_0", self._values[0])
self.set_double_spin_box("doubleSpinBox_1", self._values[1])
@property
def values(self):
return self._values
def accept(self):
self._values = [
self.get_double_spin_box("doubleSpinBox_0"),
self.get_double_spin_box("doubleSpinBox_1")
]
super().accept()
def reject(self):
self.close()
-1
View File
@@ -45,7 +45,6 @@ class CommonWordTranslate(PamhyrTranslate):
self._dict["loc"] = _translate("CommonWord", "Loc")
self._dict["scale"] = _translate("CommonWord", "Scale")
self._dict["lam"] = _translate("CommonWord", "Lam")
self._dict["reach"] = _translate("CommonWord", "Reach")
self._dict["reaches"] = _translate("CommonWord", "Reaches")
+8 -1
View File
@@ -24,7 +24,14 @@
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="doubleSpinBox_0"/>
<widget class="QDoubleSpinBox" name="doubleSpinBox_0">
<property name="minimum">
<double>-10000000000.000000000000000</double>
</property>
<property name="maximum">
<double>10000000000.000000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
+16 -2
View File
@@ -22,7 +22,14 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBox_0"/>
<widget class="QDoubleSpinBox" name="doubleSpinBox_0">
<property name="minimum">
<double>-10000000000.000000000000000</double>
</property>
<property name="maximum">
<double>10000000000.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_1">
@@ -32,7 +39,14 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBox_1"/>
<widget class="QDoubleSpinBox" name="doubleSpinBox_1">
<property name="minimum">
<double>-10000000000.000000000000000</double>
</property>
<property name="maximum">
<double>10000000000.000000000000000</double>
</property>
</widget>
</item>
</layout>
</widget>
Binary file not shown.