# 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
+from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt
from varrange_ui import Ui_VariableRange
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())
<property name="windowTitle">
<string>Sample Definition</string>
</property>
- <layout class="QVBoxLayout" name="verticalLayout">
+ <layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
</widget>
</item>
<item>
- <layout class="QHBoxLayout" name="horizontalLayout_3">
+ <layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<property name="sizeHint" stdset="0">
<size>
<width>25</width>
- <height>10</height>
+ <height>13</height>
</size>
</property>
</spacer>
</item>
<item>
- <widget class="QLabel" name="label">
- <property name="text">
- <string>CSV File:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLineEdit" name="csvFileLE"/>
- </item>
- <item>
- <widget class="QPushButton" name="chooseCsvFileButton">
- <property name="text">
- <string>Choose File...</string>
- </property>
- </widget>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QLabel" name="pyscriptLabel_2">
+ <property name="font">
+ <font>
+ <italic>true</italic>
+ </font>
+ </property>
+ <property name="text">
+ <string>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.</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_3">
+ <item>
+ <widget class="QLabel" name="label">
+ <property name="text">
+ <string>CSV File:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="csvFileLE"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="chooseCsvFileButton">
+ <property name="text">
+ <string>Choose File...</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
</item>
</layout>
</item>
# 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/>.
+import os
+import math
import cPickle
import numpy
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")
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")