From: Renaud Barate Date: Wed, 27 Feb 2013 13:50:17 +0000 (+0100) Subject: Finish HDF5 persistence implementation X-Git-Tag: V7_2_0~15 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f1634d8cd949abb442101aec625bd51123c996f6;p=modules%2Fparametric.git Finish HDF5 persistence implementation --- diff --git a/src/PARAMETRIC/PARAMETRIC.py b/src/PARAMETRIC/PARAMETRIC.py index 5b6f3e1..ad7da3b 100644 --- a/src/PARAMETRIC/PARAMETRIC.py +++ b/src/PARAMETRIC/PARAMETRIC.py @@ -331,7 +331,7 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM buf = f.read() # Delete the temporary file - #os.remove(filename) + os.remove(filename) return buf else: @@ -342,7 +342,30 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM def Load(self, theComponent, theStream, theURL, isMultiFile): try: - print "PARAMETRIC load" + if len(theStream) == 0: + return 1 + + # Save the stream in a temporary file + (fd, filename) = tempfile.mkstemp(prefix = "PARAMETRIC_", suffix = ".hdf") + os.close(fd) + with open(filename, "w") as f: + f.write(theStream) + + # Load param studies dictionary + loaded_dict = load_param_study_dict(filename) + + # Delete the temporary file + os.remove(filename) + + # Update param study dictionary with loaded dict + salomeStudyID = theComponent.GetStudy()._get_StudyId() + componentEntry = theComponent.GetID() + if salomeStudyID not in self.param_study_dict: + self.param_study_dict[salomeStudyID] = {} + for (entry, param_study) in loaded_dict.iteritems(): + if entry.startswith(componentEntry): + self.param_study_dict[salomeStudyID][entry] = param_study + print self.param_study_dict return 1 except: logger.exception("Error while trying to load study") diff --git a/src/salome/parametric/persistence.py b/src/salome/parametric/persistence.py index 17b033d..c38e404 100644 --- a/src/salome/parametric/persistence.py +++ b/src/salome/parametric/persistence.py @@ -17,67 +17,124 @@ import h5py -from study import ParametricStudy +from study import ParametricStudy, ParametricVariable def save_param_study_dict(param_study_dict, filename): - f = h5py.File(filename) - f.attrs["filetype"] = "salome_parametric" # simple tag used to identify PARAMETRIC files - f.attrs["version"] = "7.2" # version tag used to ensure backwards compatibility - for entry, param_study in param_study_dict.iteritems(): - param_study_group = f.create_group(entry) - if param_study.name is not None: - 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) - - # Store solver definition - solver_group = param_study_group.create_group("solver") - if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT: - solver_group.attrs["solver_code_type"] = "salome_component" - if param_study.salome_component_name is not None: - solver_group.attrs["salome_component_name"] = param_study.salome_component_name - if param_study.solver_case_entry is not None: - solver_group.attrs["solver_case_entry"] = param_study.solver_case_entry + with h5py.File(filename, "w") as f: + f.attrs["filetype"] = "salome_parametric" # simple tag used to identify PARAMETRIC files + f.attrs["version"] = "7.2" # version tag used to ensure backwards compatibility + for entry, param_study in param_study_dict.iteritems(): + param_study_group = f.create_group(entry) + if param_study.name is not None: + 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) + + # Store solver definition + solver_group = param_study_group.create_group("solver") + if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT: + solver_group.attrs["solver_code_type"] = "salome_component" + if param_study.salome_component_name is not None: + solver_group.attrs["salome_component_name"] = param_study.salome_component_name + if param_study.solver_case_entry is not None: + solver_group.attrs["solver_case_entry"] = param_study.solver_case_entry + else: + solver_group.attrs["solver_code_type"] = "python_script" + if param_study.python_script is not None: + solver_group.attrs["python_script"] = param_study.python_script + + # Execution parameters + execution_group = param_study_group.create_group("execution") + execution_group.attrs["nb_parallel_computations"] = param_study.nb_parallel_computations + + # Data + if param_study.data is not None: + 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): + if var in param_study.data and len(param_study.data[var]) > 0: + for idx_value, value in enumerate(param_study.data[var]): + if value is not None: + dset[idx_value, idx_var] = value + + # Error messages + if "__ERROR_MESSAGE__" in param_study.data and len(param_study.data["__ERROR_MESSAGE__"]) > 0: + dt = h5py.special_dtype(vlen=str) + error_set = param_study_group.create_dataset("error_message", (param_study.datasize,), dt) + for idx_value, value in enumerate(param_study.data["__ERROR_MESSAGE__"]): + if value is not None: + error_set[idx_value] = value + +def load_param_study_dict(filename): + param_study_dict = {} + with h5py.File(filename, 'r') as f: + filetype = f.attrs.get("filetype") + version = f.attrs.get("version") + if filetype is None or filetype != "salome_parametric" or version is None: + raise Exception("The file is not a valid parametric study file") + if version == "7.2": + param_study_dict = _load_param_study_dict_7_2(f) + else: + raise Exception('Invalid version "%s" for parametric study file' % version) + return param_study_dict + +def _load_param_study_dict_7_2(hdffile): + param_study_dict = {} + for entry, param_study_group in hdffile.iteritems(): + param_study = ParametricStudy() + 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)) + + # Load solver definition + solver_group = param_study_group["solver"] + if solver_group.attrs["solver_code_type"] == "salome_component": + param_study.solver_code_type = ParametricStudy.SALOME_COMPONENT + param_study.salome_component_name = solver_group.attrs.get("salome_component_name") + param_study.solver_case_entry = solver_group.attrs.get("solver_case_entry") else: - solver_group.attrs["solver_code_type"] = "python_script" - if param_study.python_script is not None: - solver_group.attrs["python_script"] = param_study.python_script - - # Execution parameters - execution_group = param_study_group.create_group("execution") - execution_group.attrs["nb_parallel_computations"] = param_study.nb_parallel_computations - - # Data - if param_study.data is not None: - dset = param_study_group.create_dataset("data", (param_study.datasize, len(ordered_vars))) - dset.attrs["ordered_vars"] = ordered_vars + param_study.solver_code_type = ParametricStudy.PYTHON_SCRIPT + param_study.python_script = solver_group.attrs.get("python_script") + + # Load execution parameters + execution_group = param_study_group["execution"] + param_study.nb_parallel_computations = execution_group.attrs["nb_parallel_computations"] + + # Load data + if "data" in param_study_group: + dset = param_study_group["data"] + ordered_vars = dset.attrs["ordered_vars"] + param_study.data = {} + param_study.datasize = len(dset) for idx_var, var in enumerate(ordered_vars): - if var in param_study.data and len(param_study.data[var]) > 0: - for idx_value, value in enumerate(param_study.data[var]): - if value is not None: - dset[idx_value, idx_var] = value - + param_study.data[var] = list(dset[:,idx_var]) + # Error messages - if "__ERROR_MESSAGE__" in param_study.data and len(param_study.data["__ERROR_MESSAGE__"]) > 0: - dt = h5py.special_dtype(vlen=str) - error_set = param_study_group.create_dataset("error_message", (param_study.datasize,), dt) - for idx_value, value in enumerate(param_study.data["__ERROR_MESSAGE__"]): - if value is not None: - error_set[idx_value] = value + if "error_message" in param_study_group: + error_set = param_study_group["error_message"] + param_study.data["__ERROR_MESSAGE__"] = list(error_set) - f.close() + param_study_dict[str(entry)] = param_study -def load_param_study_dict(filename): - pass + return param_study_dict