From: Renaud Barate Date: Thu, 28 Feb 2013 17:27:45 +0000 (+0100) Subject: Add GUI edition and persistence for new sample definition methods (no implementation... X-Git-Tag: V7_2_0~12 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=908d1c2d903e4094f2a22ac2287c938132a38409;p=modules%2Fparametric.git Add GUI edition and persistence for new sample definition methods (no implementation yet) --- diff --git a/src/PARAMETRIC/PARAMETRIC.py b/src/PARAMETRIC/PARAMETRIC.py index ad7da3b..b821eb9 100644 --- a/src/PARAMETRIC/PARAMETRIC.py +++ b/src/PARAMETRIC/PARAMETRIC.py @@ -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) diff --git a/src/salome/parametric/__init__.py b/src/salome/parametric/__init__.py index b7f0b55..a56664d 100644 --- a/src/salome/parametric/__init__.py +++ b/src/salome/parametric/__init__.py @@ -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 diff --git a/src/salome/parametric/gui/definevalues.py b/src/salome/parametric/gui/definevalues.py index 4b322f7..c5c44cb 100644 --- a/src/salome/parametric/gui/definevalues.py +++ b/src/salome/parametric/gui/definevalues.py @@ -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 diff --git a/src/salome/parametric/gui/definevalues.ui b/src/salome/parametric/gui/definevalues.ui index 4e138ab..b80e7b2 100644 --- a/src/salome/parametric/gui/definevalues.ui +++ b/src/salome/parametric/gui/definevalues.ui @@ -64,6 +64,14 @@ true + + + 0 + 0 + 712 + 76 + + @@ -72,9 +80,6 @@ - - false - Define sample with a Python script @@ -105,9 +110,6 @@ - - false - true @@ -120,9 +122,6 @@ - - false - DejaVu Sans Mono @@ -136,9 +135,6 @@ - - false - Load sample from a CSV file @@ -167,26 +163,16 @@ - - false - CSV File: - - - false - - + - - - false - + Choose File... diff --git a/src/salome/parametric/gui/selectvars.py b/src/salome/parametric/gui/selectvars.py index 6f4f04b..5c6e641 100644 --- a/src/salome/parametric/gui/selectvars.py +++ b/src/salome/parametric/gui/selectvars.py @@ -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 . -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"), diff --git a/src/salome/parametric/gui/wizard.py b/src/salome/parametric/gui/wizard.py index 4aa49b4..109695e 100644 --- a/src/salome/parametric/gui/wizard.py +++ b/src/salome/parametric/gui/wizard.py @@ -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) diff --git a/src/salome/parametric/persistence.py b/src/salome/parametric/persistence.py index c38e404..7cd6a26 100644 --- a/src/salome/parametric/persistence.py +++ b/src/salome/parametric/persistence.py @@ -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: diff --git a/src/salome/parametric/study.py b/src/salome/parametric/study.py index 6c13dbd..40aa7f7 100644 --- a/src/salome/parametric/study.py +++ b/src/salome/parametric/study.py @@ -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")