]> SALOME platform Git repositories - modules/parametric.git/commitdiff
Salome HOME
Add GUI edition and persistence for new sample definition methods (no implementation...
authorRenaud Barate <renaud.barate@edf.fr>
Thu, 28 Feb 2013 17:27:45 +0000 (18:27 +0100)
committerRenaud Barate <renaud.barate@edf.fr>
Thu, 28 Feb 2013 17:27:45 +0000 (18:27 +0100)
src/PARAMETRIC/PARAMETRIC.py
src/salome/parametric/__init__.py
src/salome/parametric/gui/definevalues.py
src/salome/parametric/gui/definevalues.ui
src/salome/parametric/gui/selectvars.py
src/salome/parametric/gui/wizard.py
src/salome/parametric/persistence.py
src/salome/parametric/study.py

index ad7da3b811d30fee701d7d74d94465d632ade55c..b821eb9b70150cc008024a17f64b4c60f90e12db 100644 (file)
@@ -245,19 +245,16 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM
         param_study.generate_data()
       
       seqsamples = []
-      refsample = {"inputVarList": [],
-                   "outputVarList": [],
+      refsample = {"inputVarList": param_study.input_vars,
+                   "outputVarList": param_study.output_vars,
                    "inputValues": [[[]]],
                    "specificParameters": [],
                   }
-      for varname in param_study.output_vars:
-        refsample["outputVarList"].append(varname)
 
       for i in range(param_study.datasize):
         sample = copy.deepcopy(refsample)
         for var in param_study.input_vars:
-          sample["inputVarList"].append(var.name)
-          sample["inputValues"][0][0].append([param_study.data[var.name][i]])
+          sample["inputValues"][0][0].append([param_study.data[var][i]])
         seqsamples.append(sample)
       foreach.edGetSeqOfSamplesPort().edInitPy(seqsamples)
 
index b7f0b55e8cd8d4dc986fd92e9c9bd3f0036dae0a..a56664d0fc554c6b1150457ad63f124933224b39 100644 (file)
@@ -20,7 +20,7 @@ import genjob
 
 PARAM_STUDY_TYPE_ID = study.PARAM_STUDY_TYPE_ID
 ParametricStudy = study.ParametricStudy
-ParametricVariable = study.ParametricVariable
+VariableRange = study.VariableRange
 ParametricStudyEditor = study.ParametricStudyEditor
 generate_job = genjob.generate_job
 parse_entry = genjob.parse_entry
index 4b322f758eb947453701bf671aca13971bbf2a00..c5c44cb7e4848ee0568bc9bae302ea8871954d63 100644 (file)
@@ -21,8 +21,9 @@ from PyQt4.QtCore import Qt
 from varrange_ui import Ui_VariableRange
 from definevalues_ui import Ui_SampleDefinition
 
+from salome.parametric import ParametricStudy, VariableRange
 
-class VariableRange(QtGui.QWidget, Ui_VariableRange):
+class VariableRangeWidget(QtGui.QWidget, Ui_VariableRange):
 
   def __init__(self, parent = None):
     QtGui.QWidget.__init__(self, parent)
@@ -39,28 +40,52 @@ class DefineValuesFrame(QtGui.QWidget, Ui_SampleDefinition):
 
   def set_variables(self, varlist):
     previous_set = set(self.varwidgets.keys())
-    new_set = set([var.name for var in varlist])
-    var_to_remove = previous_set - new_set
-    var_to_add = new_set - previous_set
+    new_list = [var.name for var in varlist]
+    var_to_remove = previous_set - set(new_list)
     for var in var_to_remove:
       self.variablesRangesWidget.layout().removeWidget(self.varwidgets[var])
       self.varwidgets[var].close()
       del self.varwidgets[var]
-    for var in var_to_add:
-      varrange = VariableRange(self)
-      varrange.nameLabel.setText(var)
-      self.varwidgets[var] = varrange
-      self.variablesRangesWidget.layout().addWidget(varrange)
+    for idx_var, var in enumerate(new_list):
+      if var not in self.varwidgets:
+        range_widget = VariableRangeWidget(self)
+        range_widget.nameLabel.setText(var)
+        self.varwidgets[var] = range_widget
+        self.variablesRangesWidget.layout().insertWidget(idx_var, range_widget)
 
-  def set_ranges_from_param_study(self, param_study):
-    for var in param_study.input_vars:
-      varrange = VariableRange(self)
-      varrange.nameLabel.setText(var.name)
-      varrange.fromSpinBox.setValue(var.min)
-      varrange.toSpinBox.setValue(var.max)
-      varrange.stepSpinBox.setValue(var.step)
-      self.varwidgets[var.name] = varrange
-      self.variablesRangesWidget.layout().addWidget(varrange)
+  def study_to_gui(self, param_study):
+    if param_study.sample_definition_method == ParametricStudy.SAMPLE_VAR_RANGE:
+      self.variableRangeRB.setChecked(True)
+      for varname in param_study.input_vars:
+        varrange = param_study.sample_var_range[varname]
+        range_widget = VariableRangeWidget(self)
+        range_widget.nameLabel.setText(varname)
+        range_widget.fromSpinBox.setValue(varrange.min)
+        range_widget.toSpinBox.setValue(varrange.max)
+        range_widget.stepSpinBox.setValue(varrange.step)
+        self.varwidgets[varname] = range_widget
+        self.variablesRangesWidget.layout().addWidget(range_widget)
+    elif param_study.sample_definition_method == ParametricStudy.SAMPLE_PYTHON_SCRIPT:
+      self.pythonScriptRB.setChecked(True)
+      self.pythonScriptTE.setText(param_study.sample_python_script)
+    else:
+      self.loadSampleRB.setChecked(True)
+      self.csvFileLE.setText(param_study.sample_csv_file)
+
+  def gui_to_study(self, param_study):
+    if self.variableRangeRB.isChecked():
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_VAR_RANGE
+      for (name, range_widget) in self.varwidgets.iteritems():
+        minval = range_widget.fromSpinBox.value()
+        maxval = range_widget.toSpinBox.value()
+        step = range_widget.stepSpinBox.value()
+        param_study.set_variable_range(name, VariableRange(minval, maxval, step))
+    elif self.pythonScriptRB.isChecked():
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_PYTHON_SCRIPT
+      param_study.sample_python_script = str(self.pythonScriptTE.toPlainText())
+    else:
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_CSV_FILE
+      param_study.sample_csv_file = str(self.csvFileLE.text())
 
   def check_values(self):
     return True
index 4e138abc963c1aca7c3304d4b5581d849e509f76..b80e7b299bc4d94b2c707b8630e07178b98a91ab 100644 (file)
         <bool>true</bool>
        </property>
        <widget class="QWidget" name="variablesRangesWidget">
+        <property name="geometry">
+         <rect>
+          <x>0</x>
+          <y>0</y>
+          <width>712</width>
+          <height>76</height>
+         </rect>
+        </property>
         <layout class="QVBoxLayout" name="verticalLayout_3"/>
        </widget>
       </widget>
@@ -72,9 +80,6 @@
    </item>
    <item>
     <widget class="QRadioButton" name="pythonScriptRB">
-     <property name="enabled">
-      <bool>false</bool>
-     </property>
      <property name="text">
       <string>Define sample with a Python script</string>
      </property>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <widget class="QLabel" name="pyscriptLabel">
-         <property name="enabled">
-          <bool>false</bool>
-         </property>
          <property name="font">
           <font>
            <italic>true</italic>
        </item>
        <item>
         <widget class="QTextEdit" name="pythonScriptTE">
-         <property name="enabled">
-          <bool>false</bool>
-         </property>
          <property name="font">
           <font>
            <family>DejaVu Sans Mono</family>
    </item>
    <item>
     <widget class="QRadioButton" name="loadSampleRB">
-     <property name="enabled">
-      <bool>false</bool>
-     </property>
      <property name="text">
       <string>Load sample from a CSV file</string>
      </property>
      </item>
      <item>
       <widget class="QLabel" name="label">
-       <property name="enabled">
-        <bool>false</bool>
-       </property>
        <property name="text">
         <string>CSV File:</string>
        </property>
       </widget>
      </item>
      <item>
-      <widget class="QLineEdit" name="lineEdit">
-       <property name="enabled">
-        <bool>false</bool>
-       </property>
-      </widget>
+      <widget class="QLineEdit" name="csvFileLE"/>
      </item>
      <item>
-      <widget class="QPushButton" name="pushButton">
-       <property name="enabled">
-        <bool>false</bool>
-       </property>
+      <widget class="QPushButton" name="chooseCsvFileButton">
        <property name="text">
         <string>Choose File...</string>
        </property>
index 6f4f04b34565c82f0dff6e2c8dd780879255e2f6..5c6e6414eb7680bbd786a854f1dbe354b221b557 100644 (file)
@@ -15,7 +15,7 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with SALOME PARAMETRIC module.  If not, see <http://www.gnu.org/licenses/>.
 
-from PyQt4 import QtGui, QtCore
+from PyQt4 import QtGui
 
 from salome.kernel.parametric import study_exchange_vars
 from salome.gui.selectvars import MySelectVarsDialog
@@ -27,12 +27,17 @@ class SelectVarsFrame(MySelectVarsDialog):
     self.OKButton.hide()
     self.cancelButton.hide()
 
-  def set_vars_from_param_study(self, param_study):
-    input_var_list = [study_exchange_vars.Variable(var.name) for var in param_study.input_vars]
+  def study_to_gui(self, param_study):
+    input_var_list = [study_exchange_vars.Variable(varname) for varname in param_study.input_vars]
     output_var_list = [study_exchange_vars.Variable(varname) for varname in param_study.output_vars]
     exchange_vars = study_exchange_vars.ExchangeVariables(input_var_list, output_var_list)
     self.setExchangeVariables(exchange_vars)
 
+  def gui_to_study(self, param_study):
+    exch_vars = self.getSelectedExchangeVariables()
+    param_study.input_vars = [invar.name for invar in exch_vars.inputVarList]
+    param_study.output_vars = [outvar.name for outvar in exch_vars.outputVarList]
+
   def check_values(self):
     if self.selectedInputVarListWidget.count() == 0:
       QtGui.QMessageBox.critical(self, self.tr("Error"),
index 4aa49b49aea8f575e336d38aa2c9eca779766843..109695ef05d755fb63fad6b4c5aedba8c5e4dc34 100644 (file)
@@ -25,7 +25,7 @@ from wizard_ui import Ui_Wizard
 from selectvars import SelectVarsFrame
 from definevalues import DefineValuesFrame
 from execparams import ExecParamsFrame
-from salome.parametric import ParametricVariable, ParametricStudy, ParametricStudyEditor
+from salome.parametric import ParametricStudy, ParametricStudyEditor
 
 
 class Wizard(QtGui.QWidget, Ui_Wizard):
@@ -98,19 +98,8 @@ class Wizard(QtGui.QWidget, Ui_Wizard):
       if not self.step_frames[self.curstep].check_values():
         return
       param_study = ParametricStudy()
-      # Input variables
-      for (name, range_widget) in self.define_values_frame.varwidgets.iteritems():
-        minval = range_widget.fromSpinBox.value()
-        maxval = range_widget.toSpinBox.value()
-        step = range_widget.stepSpinBox.value()
-        var = ParametricVariable(name, minval, maxval, step)
-        param_study.add_input_variable(var)
-      # Output variables
-      exch_vars = self.select_vars_frame.getSelectedExchangeVariables()
-      for outvar in exch_vars.outputVarList:
-        param_study.add_output_variable(outvar.name)
-      # Execution parameters
-      self.exec_params_frame.gui_to_study(param_study)
+      for frame in self.step_frames:
+        frame.gui_to_study(param_study)
   
       # Save to Salome study
       ed = ParametricStudyEditor()
@@ -125,9 +114,8 @@ class Wizard(QtGui.QWidget, Ui_Wizard):
 
   def set_study(self, param_study):
     self.entry = param_study.entry
-    self.select_vars_frame.set_vars_from_param_study(param_study)
-    self.define_values_frame.set_ranges_from_param_study(param_study)
-    self.exec_params_frame.study_to_gui(param_study)
+    for frame in self.step_frames:
+      frame.study_to_gui(param_study)
 
   def close(self):
     QtGui.QWidget.close(self)
index c38e404702189f2abbb85a399ade181699459391..7cd6a268e1361b0481ace10e9962bd89f13f699b 100644 (file)
@@ -17,7 +17,7 @@
 
 import h5py
 
-from study import ParametricStudy, ParametricVariable
+from study import ParametricStudy, VariableRange
 
 def save_param_study_dict(param_study_dict, filename):
   with h5py.File(filename, "w") as f:
@@ -29,18 +29,28 @@ def save_param_study_dict(param_study_dict, filename):
         param_study_group.attrs["name"] = param_study.name
       
       # Store input and output variables definitions
-      ordered_vars = []
-      input_vars_group = param_study_group.create_group("input_vars")
-      for input_var in param_study.input_vars:
-        ordered_vars.append(input_var.name)
-        var_group = input_vars_group.create_group(input_var.name)
-        var_group.attrs["min"] = input_var.min
-        var_group.attrs["max"] = input_var.max
-        var_group.attrs["step"] = input_var.step
-      output_vars_group = param_study_group.create_group("output_vars")
-      for output_varname in param_study.output_vars:
-        ordered_vars.append(output_varname)
-        var_group = output_vars_group.create_group(output_varname)
+      param_study_group.attrs["input_vars"] = param_study.input_vars
+      param_study_group.attrs["output_vars"] = param_study.output_vars
+
+      # Store sample definition
+      sample_def_group = param_study_group.create_group("sample_definition")
+      if param_study.sample_definition_method == ParametricStudy.SAMPLE_VAR_RANGE:
+        sample_def_group.attrs["sample_definition_method"] = "variables_ranges"
+        if param_study.sample_var_range is not None:
+          var_range_group = sample_def_group.create_group("variables_ranges")
+          for varname, varrange in param_study.sample_var_range.iteritems():
+            var_group = var_range_group.create_group(varname)
+            var_group.attrs["min"] = varrange.min
+            var_group.attrs["max"] = varrange.max
+            var_group.attrs["step"] = varrange.step
+      elif param_study.sample_definition_method == ParametricStudy.SAMPLE_PYTHON_SCRIPT:
+        sample_def_group.attrs["sample_definition_method"] = "python_script"
+        if param_study.sample_python_script is not None:
+          sample_def_group.attrs["python_script"] = param_study.sample_python_script
+      else:
+        sample_def_group.attrs["sample_definition_method"] = "csv_file"
+        if param_study.sample_csv_file is not None:
+          sample_def_group.attrs["csv_file"] = param_study.sample_csv_file
 
       # Store solver definition
       solver_group = param_study_group.create_group("solver")
@@ -61,6 +71,7 @@ def save_param_study_dict(param_study_dict, filename):
 
       # Data
       if param_study.data is not None:
+        ordered_vars = param_study.input_vars + param_study.output_vars
         dset = param_study_group.create_dataset("data", (param_study.datasize, len(ordered_vars)))
         dset.attrs["ordered_vars"] = ordered_vars
         for idx_var, var in enumerate(ordered_vars):
@@ -97,15 +108,27 @@ def _load_param_study_dict_7_2(hdffile):
     param_study.name = param_study_group.attrs.get("name")
 
     # Load input and output variables definitions
-    input_vars_group = param_study_group["input_vars"]
-    for varname, var_group in input_vars_group.iteritems():
-      minval = var_group.attrs["min"]
-      maxval = var_group.attrs["max"]
-      step = var_group.attrs["step"]
-      param_study.add_input_variable(ParametricVariable(str(varname), minval, maxval, step))
-    output_vars_group = param_study_group["output_vars"]
-    for output_varname in output_vars_group.iterkeys():
-      param_study.add_output_variable(str(output_varname))
+    param_study.input_vars = list(param_study_group.attrs["input_vars"])
+    param_study.output_vars = list(param_study_group.attrs["output_vars"])
+
+    # Load sample definition
+    sample_def_group = param_study_group["sample_definition"]
+    if sample_def_group.attrs["sample_definition_method"] == "variables_ranges":
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_VAR_RANGE
+      if "variables_ranges" in sample_def_group:
+        var_range_group = sample_def_group["variables_ranges"]
+        param_study.sample_var_range = {}
+        for varname, var_group in var_range_group.iteritems():
+          minval = var_group.attrs["min"]
+          maxval = var_group.attrs["max"]
+          step = var_group.attrs["step"]
+          param_study.set_variable_range(str(varname), VariableRange(minval, maxval, step))
+    elif sample_def_group.attrs["sample_definition_method"] == "python_script":
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_PYTHON_SCRIPT
+      param_study.sample_python_script = sample_def_group.attrs.get("python_script")
+    else:
+      param_study.sample_definition_method = ParametricStudy.SAMPLE_CSV_FILE
+      param_study.sample_csv_file = sample_def_group.attrs.get("csv_file")
 
     # Load solver definition
     solver_group = param_study_group["solver"]
@@ -119,7 +142,7 @@ def _load_param_study_dict_7_2(hdffile):
 
     # Load execution parameters
     execution_group = param_study_group["execution"]
-    param_study.nb_parallel_computations = execution_group.attrs["nb_parallel_computations"]
+    param_study.nb_parallel_computations = int(execution_group.attrs["nb_parallel_computations"])
 
     # Load data
     if "data" in param_study_group:
index 6c13dbd6244e7a13bb547c9eb9f068cf67a7cdec..40aa7f7588e37c3e6640d15aa1754c0642595073 100644 (file)
@@ -72,10 +72,9 @@ class ParametricStudyEditor:
     return param_study
 
 
-class ParametricVariable:
+class VariableRange:
 
-  def __init__(self, name, minval = None, maxval = None, step = None):
-    self.name = name
+  def __init__(self, minval = None, maxval = None, step = None):
     self.min = minval
     self.max = maxval
     self.step = step
@@ -85,10 +84,18 @@ class ParametricStudy:
   
   SALOME_COMPONENT = 0
   PYTHON_SCRIPT = 1
+  
+  SAMPLE_VAR_RANGE = 0
+  SAMPLE_PYTHON_SCRIPT = 1
+  SAMPLE_CSV_FILE = 2
 
   def __init__(self):
     self.input_vars = []
     self.output_vars = []
+    self.sample_definition_method = ParametricStudy.SAMPLE_VAR_RANGE
+    self.sample_var_range = None
+    self.sample_python_script = None
+    self.sample_csv_file = None
     self.solver_code_type = ParametricStudy.SALOME_COMPONENT
     self.salome_component_name = None
     self.solver_case_entry = None
@@ -100,31 +107,39 @@ class ParametricStudy:
     self._value_dict = None
     self.entry = None
 
-  def add_input_variable(self, var):
-    self.input_vars.append(var)
-
-  def add_output_variable(self, varname):
-    self.output_vars.append(varname)
+  def set_variable_range(self, varname, varrange):
+    if varname not in self.input_vars:
+      raise Exception('Can\'t define range for variable "%s", which is not an input variable' % varname)
+    if self.sample_var_range is None:
+      self.sample_var_range = {}
+    self.sample_var_range[varname] = varrange
 
   def generate_data(self):
+    if self.sample_definition_method == ParametricStudy.SAMPLE_VAR_RANGE:
+      self.generate_data_complete_sampling()
+    else:
+      raise Exception("This sample definition method is not implemented")
+
+  def generate_data_complete_sampling(self):
     self.data = {}
     self.datasize = 0
-    for var in self.input_vars:
-      self.data[var.name] = []
+    for varname in self.input_vars:
+      self.data[varname] = []
     self._value_dict = {}
-    self._fill_data(self.input_vars)
+    self._fill_data_complete_sampling(self.input_vars)
 
-  def _fill_data(self, remaining_var_list):
+  def _fill_data_complete_sampling(self, remaining_var_list):
     if len(remaining_var_list) == 0:
       for (name, value) in self._value_dict.iteritems():
         self.data[name].append(value)
       self.datasize += 1
     else:
-      var = remaining_var_list[0]
+      varname = remaining_var_list[0]
       next_var_list = remaining_var_list[1:]
-      for value in numpy.arange(var.min, var.max, var.step):
-        self._value_dict[var.name] = value
-        self._fill_data(next_var_list)
+      varrange = self.sample_var_range[varname]
+      for value in numpy.arange(varrange.min, varrange.max, varrange.step):
+        self._value_dict[varname] = value
+        self._fill_data_complete_sampling(next_var_list)
 
   def export_data_to_csv_file(self, filepath, sep = ","):
     if self.data is None:
@@ -132,16 +147,16 @@ class ParametricStudy:
     f = open(filepath, "w")
     
     # Header
-    for invar in self.input_vars:
-      f.write(invar.name + sep)
+    for invarname in self.input_vars:
+      f.write(invarname + sep)
     for outvarname in self.output_vars:
       f.write(outvarname + sep)
     f.write("Error message\n")
     
     # Data
     for i in range(self.datasize):
-      for invar in self.input_vars:
-        f.write(self._format_value(self.data[invar.name][i]) + sep)
+      for invarname in self.input_vars:
+        f.write(self._format_value(self.data[invarname][i]) + sep)
       for outvarname in self.output_vars:
         f.write(self._format_value(self.data[outvarname][i]) + sep)
       f.write(self._format_value(self.data["__ERROR_MESSAGE__"][i]) + "\n")