From ae8ceb287fdfa74cd43c207afbcebf0edff5d4f0 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Mon, 8 Jul 2019 16:20:33 +0200 Subject: [PATCH] Add a global result for a study. Preparation for a insitu feature. --- src/cpp/TMonoPyJob.hxx | 3 ++- src/pydefx/CMakeLists.txt | 2 ++ src/pydefx/pystudy.py | 24 ++++++++++++++++-------- src/pydefx/samplecsvmanager.py | 7 ++++--- src/pyexample/syrthes_launch.py | 3 ++- src/pyexample/temposcen.py | 5 ++++- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/cpp/TMonoPyJob.hxx b/src/cpp/TMonoPyJob.hxx index 72b2e85..54bc43f 100644 --- a/src/cpp/TMonoPyJob.hxx +++ b/src/cpp/TMonoPyJob.hxx @@ -82,7 +82,8 @@ public: { py2cpp::PyFunction pyFn; pyFn.loadExp(_pyStudy, "getResult"); - fetchResults(pyFn(), _sample); + pyFn(); // python call: _pyStudy.getResult() + fetchResults(_pyStudy.getAttr("sample"), _sample); } catch(std::exception& e) { diff --git a/src/pydefx/CMakeLists.txt b/src/pydefx/CMakeLists.txt index 6d5d94c..8d532e4 100644 --- a/src/pydefx/CMakeLists.txt +++ b/src/pydefx/CMakeLists.txt @@ -26,6 +26,8 @@ SET(SCRIPTS samplecsviterator.py samplecsvmanager.py defaultschemabuilder.py + studyexception.py + studyresult.py ) INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx) diff --git a/src/pydefx/pystudy.py b/src/pydefx/pystudy.py index 0b43a16..6172bb3 100644 --- a/src/pydefx/pystudy.py +++ b/src/pydefx/pystudy.py @@ -27,6 +27,8 @@ from . import samplecsvmanager from . import parameters from . import configuration from . import defaultschemabuilder +from .studyexception import StudyUseException, StudyRunException +from .studyresult import StudyResult def defaultSampleManager(): return samplecsvmanager.SampleManager() @@ -35,6 +37,7 @@ class PyStudy: JOB_DUMP_NAME = "jobDump.xml" def __init__(self, sampleManager=None, schemaBuilder=None): self.job_id = -1 + self.global_result = StudyResult() if sampleManager is None: self.sampleManager = defaultSampleManager() else: @@ -98,7 +101,7 @@ class PyStudy: salome_params.result_directory) self.getResult() else: - raise Exception("Failed to restore the job.") + raise StudyRunException("Failed to restore the job.") def loadFromId(self, jobid): """ @@ -140,7 +143,7 @@ class PyStudy: The job should have been already created. """ if self.job_id < 0 : - raise Exception("Nothing to launch! Job is not created!") + raise StudyUseException("Nothing to launch! Job is not created!") tmp_workdir = self.params.salome_parameters.result_directory # run the job launcher = salome.naming_service.Resolve('/SalomeLauncher') @@ -156,9 +159,11 @@ class PyStudy: Try to get the result file and if it was possible the results are loaded in the sample. An exception may be thrown if it was not possible to get the file. + Return a StudyResult object. """ + self.global_result = StudyResult() if self.job_id < 0 : - raise Exception("Cannot get the results if the job is not created!") + raise StudyUseException("Cannot get the results if the job is not created!") launcher = salome.naming_service.Resolve('/SalomeLauncher') state = launcher.getJobState(self.job_id) tmp_workdir = self.params.salome_parameters.result_directory @@ -166,7 +171,7 @@ class PyStudy: errorIfNoResults = False errorMessage = "" if state == "CREATED" : - raise Exception("Cannot get the results if the job is not launched!") + raise StudyUseException("Cannot get the results if the job is not launched!") elif state == "QUEUED" or state == "IN_PROCESS": # no results available at this point. Try again later! Not an error. searchResults = False @@ -180,6 +185,7 @@ class PyStudy: with open(exit_code_file) as myfile: exit_code = myfile.read() exit_code = exit_code.strip() + self.global_result.exit_code = exit_code if exit_code == "0" : errorIfNoResults = True # we expect to have full results else: @@ -200,7 +206,8 @@ class PyStudy: self.sampleManager.getResultFileName(), tmp_workdir): try: - self.sampleManager.loadResult(self.sample, tmp_workdir) + res = self.sampleManager.loadResult(self.sample, tmp_workdir) + self.global_result.result = res except Exception as err: if errorIfNoResults: raise err @@ -213,8 +220,9 @@ For further details, see {}/logs directory on {}.""".format( self.params.salome_parameters.work_directory, self.params.salome_parameters.resource_required.name) errorMessage += warningMessage - raise Exception(errorMessage) - return self.sample + self.global_result.error_message = errorMessage + raise StudyRunException(errorMessage) + return self.global_result def resultAvailable(self): """ @@ -247,7 +255,7 @@ For further details, see {}/logs directory on {}.""".format( def dump(self): if self.job_id < 0 : - raise Exception("Cannot dump the job if it is not created!") + raise StudyUseException("Cannot dump the job if it is not created!") launcher = salome.naming_service.Resolve('/SalomeLauncher') return launcher.dumpJob(self.job_id) diff --git a/src/pydefx/samplecsvmanager.py b/src/pydefx/samplecsvmanager.py index 05db2f9..013ffa3 100644 --- a/src/pydefx/samplecsvmanager.py +++ b/src/pydefx/samplecsvmanager.py @@ -78,7 +78,8 @@ class SampleManager: """ The directory should contain a RESULTDIR directory with the result files. The results are loaded into the sample. - Return the modified sample. + Return the global result of the study which can be used by an insitu + computation. """ resultdir = os.path.join(directory, SampleIterator.RESULTDIR) datapath = os.path.join(resultdir, SampleIterator.RESULTFILE) @@ -97,10 +98,10 @@ class SampleManager: sample.checkId(index, input_vals) except Exception as err: extraInfo = "Error on processing file {} index number {}:".format( - datapath, str(index)) + datapath, str(index)) raise Exception(extraInfo + str(err)) sample.addResult(index, output_vals, elt[SampleIterator.ERRORCOLUMN]) - return sample + return None def restoreSample(self, directory): """ The directory should contain the files created by prepareRun. A new diff --git a/src/pyexample/syrthes_launch.py b/src/pyexample/syrthes_launch.py index 93904b2..9158ead 100755 --- a/src/pyexample/syrthes_launch.py +++ b/src/pyexample/syrthes_launch.py @@ -37,4 +37,5 @@ myStudy = pydefix.PyStudy(myScript, mySample, myParams) myStudy.run() print(myStudy.getJobState()) -print(myStudy.getResult().progressRate()) +print(myStudy.getResult()) +print(myStudy.getProgress()) diff --git a/src/pyexample/temposcen.py b/src/pyexample/temposcen.py index 573739f..cd62e48 100644 --- a/src/pyexample/temposcen.py +++ b/src/pyexample/temposcen.py @@ -33,4 +33,7 @@ myStudy.createNewJob(myScript, mySample, myParams) myStudy.launch() print(myStudy.getJobState()) -print(myStudy.getResult().progressRate()) +print(myStudy.getResult()) +print(myStudy.sample) +print(myStudy.global_result) +print(myStudy.getProgress()) -- 2.30.2