From 466801aa8cc8a6562e3c216e03d7f990bc073ec8 Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Wed, 6 Mar 2013 12:00:44 +0100 Subject: [PATCH] Implement CSV file import --- src/salome/parametric/gui/definevalues.py | 9 +++- src/salome/parametric/gui/definevalues.ui | 56 +++++++++++++++------- src/salome/parametric/study.py | 58 +++++++++++++++++++++++ 3 files changed, 104 insertions(+), 19 deletions(-) diff --git a/src/salome/parametric/gui/definevalues.py b/src/salome/parametric/gui/definevalues.py index c5c44cb..d0f6eea 100644 --- a/src/salome/parametric/gui/definevalues.py +++ b/src/salome/parametric/gui/definevalues.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 +from PyQt4 import QtCore, QtGui from PyQt4.QtCore import Qt from varrange_ui import Ui_VariableRange @@ -36,7 +36,14 @@ class DefineValuesFrame(QtGui.QWidget, Ui_SampleDefinition): def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.setupUi(self) + self.connect(self.chooseCsvFileButton, QtCore.SIGNAL("clicked()"), self.choose_csv_file) self.varwidgets = {} + + def choose_csv_file(self): + filename = QtGui.QFileDialog.getOpenFileName(self, self.tr("Load data from CSV file"), + filter = self.tr("CSV files (*.csv)")) + if filename is not None and len(filename) > 0: + self.csvFileLE.setText(filename) def set_variables(self, varlist): previous_set = set(self.varwidgets.keys()) diff --git a/src/salome/parametric/gui/definevalues.ui b/src/salome/parametric/gui/definevalues.ui index b80e7b2..fa5e5b1 100644 --- a/src/salome/parametric/gui/definevalues.ui +++ b/src/salome/parametric/gui/definevalues.ui @@ -13,7 +13,7 @@ Sample Definition - + @@ -144,7 +144,7 @@ - + @@ -156,27 +156,47 @@ 25 - 10 + 13 - - - CSV File: - - - - - - - - - - Choose File... - - + + + + + + true + + + + The first line of this file must contain the name of the input variables separated by commas.<br>Each other must contain a list of values separated by commas, corresponding to a point of the input sample. + + + + + + + + + CSV File: + + + + + + + + + + Choose File... + + + + + + diff --git a/src/salome/parametric/study.py b/src/salome/parametric/study.py index d4023fe..73c8101 100644 --- a/src/salome/parametric/study.py +++ b/src/salome/parametric/study.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with SALOME PARAMETRIC module. If not, see . +import os +import math import cPickle import numpy @@ -121,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_CSV_FILE: + self.generate_data_from_csv_file() else: raise Exception("This sample definition method is not implemented") @@ -145,6 +149,60 @@ class ParametricStudy: self._value_dict[varname] = value self._fill_data_complete_sampling(next_var_list) + 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") + if not os.path.isfile(self.sample_csv_file): + raise Exception("CSV file for input data %s does not exist" % self.sample_csv_file) + with open(self.sample_csv_file, "r") as f: + # Header + headerline = f.readline() + input_var_tokens = headerline.split(sep) + missing_vars = set(self.input_vars) + extra_vars = [] + var_list = [] + for var_token in input_var_tokens: + var = var_token.strip() + var_list.append(var) + if var in missing_vars: + missing_vars.remove(var) + else: + extra_vars.append(var) + if len(missing_vars) == 1: + raise Exception("Invalid CSV file for input data: Variable %s is missing" % missing_vars.pop()) + elif len(missing_vars) > 1: + missing_vars_str = "" + for var in missing_vars: + missing_vars_str += var + ", " + missing_vars_str = missing_vars_str[:-2] # Remove last comma + raise Exception("Invalid CSV file for input data: Variables %s are missing" % missing_vars_str) + if len(extra_vars) == 1: + raise Exception("Invalid CSV file for input data: File contains an extra variable %s" % extra_vars[0]) + elif len(extra_vars) > 1: + raise Exception("Invalid CSV file for input data: File contains extra variables %s" % extra_vars) + + # Data + self.data = {} + self.datasize = 0 + for varname in self.input_vars: + self.data[varname] = [] + line = f.readline() + line_number = 2 + while line != "": + line = line.strip() + if line != "": + value_tokens = line.split(sep) + if len(value_tokens) != len(var_list): + raise Exception("Invalid CSV file for input data: invalid value on line %d" % line_number) + for var, value in zip(var_list, value_tokens): + float_value = float(value) + if math.isnan(float_value): + raise Exception("Invalid CSV file for input data: invalid value on line %d" % line_number) + self.data[var].append(float_value) + self.datasize += 1 + line = f.readline() + line_number += 1 + def export_data_to_csv_file(self, filepath, sep = ","): if self.data is None: raise Exception("Parametric study does not contain any data") -- 2.39.2