From 1f727be5171afe8348e3f879c04d577d8fa49ecb Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Fri, 26 Apr 2013 16:48:21 +0200 Subject: [PATCH] Add definition of input sample with Python script --- src/salome/parametric/gui/definevalues.py | 13 +++++++++++++ src/salome/parametric/gui/definevalues.ui | 2 +- src/salome/parametric/gui/wizard.py | 1 + src/salome/parametric/study.py | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/salome/parametric/gui/definevalues.py b/src/salome/parametric/gui/definevalues.py index e4cb96b..6517040 100644 --- a/src/salome/parametric/gui/definevalues.py +++ b/src/salome/parametric/gui/definevalues.py @@ -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 sample where n is the number of points in the sample." + if len(input_var_names) > 1: + text += "
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 += " " + var + "" + 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) diff --git a/src/salome/parametric/gui/definevalues.ui b/src/salome/parametric/gui/definevalues.ui index fa5e5b1..7c04f4b 100644 --- a/src/salome/parametric/gui/definevalues.ui +++ b/src/salome/parametric/gui/definevalues.ui @@ -116,7 +116,7 @@ - This script must create a NumPy array of dimension (m,n) named <b>sample</b><br>- where m is the number of points in the sample<br>- where n is the number of input variables + diff --git a/src/salome/parametric/gui/wizard.py b/src/salome/parametric/gui/wizard.py index fd7ab9d..5c5cf72 100644 --- a/src/salome/parametric/gui/wizard.py +++ b/src/salome/parametric/gui/wizard.py @@ -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() diff --git a/src/salome/parametric/study.py b/src/salome/parametric/study.py index 2047884..5f6b966 100644 --- a/src/salome/parametric/study.py +++ b/src/salome/parametric/study.py @@ -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") -- 2.39.2