Salome HOME
Add definition of input sample with Python script
authorRenaud Barate <renaud.barate@edf.fr>
Fri, 26 Apr 2013 14:48:21 +0000 (16:48 +0200)
committerRenaud Barate <renaud.barate@edf.fr>
Fri, 26 Apr 2013 14:48:21 +0000 (16:48 +0200)
src/salome/parametric/gui/definevalues.py
src/salome/parametric/gui/definevalues.ui
src/salome/parametric/gui/wizard.py
src/salome/parametric/study.py

index e4cb96bb090fa831b9e323ae481071bf446bdac8..6517040d42e87ed48c398c3597bc4da62f3b4d14 100644 (file)
@@ -60,6 +60,19 @@ class DefineValuesFrame(QtGui.QWidget, Ui_SampleDefinition):
         self.varwidgets[var] = range_widget
         self.variablesRangesWidget.layout().insertWidget(idx_var, range_widget)
 
+  def set_pyscript_label_from_vars(self, exchange_vars):
+    input_var_names = [var.name for var in exchange_vars.inputVarList]
+    text  = "This script must create a NumPy array of dimension (n, %d)" % len(input_var_names)
+    text += " named <b>sample</b> where n is the number of points in the sample."
+    if len(input_var_names) > 1:
+      text += "<br>The order of the input variables (columns of the sample) must be"
+      for i, var in enumerate(input_var_names):
+        if i != 0:
+          text += ","
+        text += " <b>" + var + "</b>"
+      text += "."
+    self.pyscriptLabel.setText(text)
+
   def study_to_gui(self, param_study):
     if param_study.sample_definition_method == ParametricStudy.SAMPLE_VAR_RANGE:
       self.variableRangeRB.setChecked(True)
index fa5e5b1b597713a1a2a570df25423f0f25bad15c..7c04f4b505ab3cf658ccaa13140e959ff594012a 100644 (file)
           </font>
          </property>
          <property name="text">
-          <string>This script must create a NumPy array of dimension (m,n) named &lt;b&gt;sample&lt;/b&gt;&lt;br&gt;- where m is the number of points in the sample&lt;br&gt;- where n is the number of input variables</string>
+          <string/>
          </property>
         </widget>
        </item>
index fd7ab9dbc7149a2e69bac5d58371981428a350bf..5c5cf72676614ad674e7e7da6830984f2c404003 100644 (file)
@@ -88,6 +88,7 @@ class Wizard(QtGui.QWidget, Ui_Wizard):
   def define_values(self):
     exchange_vars = self.select_vars_frame.getSelectedExchangeVariables()
     self.define_values_frame.set_variables(exchange_vars.inputVarList)
+    self.define_values_frame.set_pyscript_label_from_vars(exchange_vars)
 
   def set_pyscript_label(self):
     exchange_vars = self.select_vars_frame.getSelectedExchangeVariables()
index 204788463c4afb0cfd4bea31e2849826efd8cc2a..5f6b96668f9124b91d32a94932b320ff7bf1a7a9 100644 (file)
@@ -123,6 +123,8 @@ class ParametricStudy:
   def generate_data(self):
     if self.sample_definition_method == ParametricStudy.SAMPLE_VAR_RANGE:
       self.generate_data_complete_sampling()
+    elif self.sample_definition_method == ParametricStudy.SAMPLE_PYTHON_SCRIPT:
+      self.generate_data_python_script()
     elif self.sample_definition_method == ParametricStudy.SAMPLE_CSV_FILE:
       self.generate_data_from_csv_file()
     else:
@@ -149,6 +151,27 @@ class ParametricStudy:
         self._value_dict[varname] = value
         self._fill_data_complete_sampling(next_var_list)
 
+  def generate_data_python_script(self):
+    context = {}
+    exec self.sample_python_script in context
+
+    # Basic check on the "sample" array
+    if not context.has_key("sample"):
+      raise Exception('Python script did not create "sample" object')
+    sample = context["sample"]
+    if type(sample) != numpy.ndarray:
+      raise Exception('"sample" object created in Python script is not a numpy array')
+    if sample.ndim != 2:
+      raise Exception('Wrong dimension: "sample" array should be a 2-dimension array')
+    if sample.shape[1] != len(self.input_vars):
+      raise Exception('Wrong size: "sample" array should have %d columns' % len(self.input_vars))
+
+    # Store data in the parametric study
+    self.data = {}
+    for (idx, varname) in enumerate(self.input_vars):
+      self.data[varname] = sample[:,idx]
+    self.datasize = sample.shape[0]
+
   def generate_data_from_csv_file(self, sep = ","):
     if self.sample_csv_file is None:
       raise Exception("CSV file for input data is not defined")