]> SALOME platform Git repositories - modules/adao.git/commitdiff
Salome HOME
Correction of Python 3 compatibility with YACS types V9_2_0b_ok_ADAO
authorJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Tue, 16 Oct 2018 20:07:16 +0000 (22:07 +0200)
committerJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Tue, 16 Oct 2018 20:07:16 +0000 (22:07 +0200)
resources/ADAOSchemaCatalog.xml
src/daSalome/daYacsIntegration/daOptimizerLoop.py

index 07792d23c3b3a43f8d50f5501cefff0a528af221..9cab17f2440448366925e1e9aced8baf5b2e5a6e 100644 (file)
@@ -33,7 +33,7 @@
   <type name="long" kind="int"/>
   <struct name="SALOME_TYPES/Parameter">
     <member type="string" name="name"></member>
-    <member type="string" name="value"></member>
+    <member type="pyobj" name="value"></member>
   </struct>
   <sequence content="SALOME_TYPES/Parameter" name="SALOME_TYPES/ParameterList"></sequence>
   <sequence content="double" name="SALOME_TYPES/Variable"></sequence>
@@ -238,6 +238,7 @@ if (matrix_in_string.find("array")>-1) or (matrix_in_string.find("matrix")>-1):
     exec("matrix="+matrix_in_string)
     matrix = numpy.matrix(matrix)
 else:
+    matrix_in_string=matrix_in_string.replace("'","").replace('"','')
     matrix = numpy.matrix(matrix_in_string)
 type = "Matrix"
 logging.debug("CREATE YI Matrix is %s"%matrix)
@@ -258,6 +259,7 @@ if (matrix_in_string.find("array")>-1) or (matrix_in_string.find("matrix")>-1):
     exec("matrix="+matrix_in_string)
     matrix = numpy.matrix(matrix)
 else:
+    matrix_in_string=matrix_in_string.replace("'","").replace('"','')
     matrix = numpy.matrix(matrix_in_string)
 type = "ScalarSparseMatrix"
 logging.debug("CREATE YI ScalarSparseMatrix is %s"%matrix)
@@ -278,6 +280,7 @@ if (matrix_in_string.find("array")>-1) or (matrix_in_string.find("matrix")>-1):
     exec("matrix="+matrix_in_string)
     matrix = numpy.matrix(matrix)
 else:
+    matrix_in_string=matrix_in_string.replace("'","").replace('"','')
     matrix = numpy.matrix(matrix_in_string)
 type = "DiagonalSparseMatrix"
 logging.debug("CREATE YI DiagonalSparseMatrix is %s"%matrix)
@@ -370,6 +373,7 @@ if (vector_in_string.find("array")>-1) or (vector_in_string.find("matrix")>-1):
     exec("vector="+vector_in_string)
     vector = numpy.matrix(vector)
 else:
+    vector_in_string=vector_in_string.replace("'","").replace('"','')
     vector = numpy.matrix(vector_in_string)
 type = "Vector"
 logging.debug("Vector is %s"%vector)
@@ -458,9 +462,12 @@ ADD.analyze()
   <inline name="SimpleUserAnalysis">
     <script><code><![CDATA[
 #-*- coding: utf-8 -*-
-import logging
+import sys, logging
 logging.debug("TERMINATE Entering in SimpleUserAnalysis")
-ADD = Study.getResults()
+if sys.version_info.major < 3:
+    ADD = Study.getResults()
+else:
+    ADD = Study
 # User code is below
 
 ]]></code></script>
@@ -530,10 +537,10 @@ if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index
   </inline>
 
   <inline name="ReadForSwitchNode">
-    <script><code><![CDATA[
-import logging
+    <script><code><![CDATA[import sys, logging
+if sys.version_info.major > 3: import adao
 logging.debug("CREATE YI Entering in ReadForSwitchNode")
-logging.debug("       with input data : "+str(data["specificParameters"]))
+logging.debug("       with input data : ",list(data["specificParameters"]))
 switch_value = -1
 for param in data["specificParameters"]:
   if param["name"] == "switch_value":
@@ -546,19 +553,15 @@ logging.debug("       switching to value : "+str(switch_value))
   </inline>
 
   <inline name="ExtractDataNode">
-    <script><code><![CDATA[
-import sys, logging
+    <script><code><![CDATA[import sys, logging
+if sys.version_info.major > 3: import adao
 logging.debug("TERMINATE Entering in ExtractDataNode")
-if sys.version_info.major < 3:
-  import cPickle as pickle
-else:
-  import pickle
 from daCore.AssimilationStudy import AssimilationStudy
 var = None
 info = None
 for param in data["specificParameters"]:
   if param["name"] == "var":
-    var = pickle.loads(param["value"])
+    var = param["value"]
   if param["name"] == "info":
     info = param["value"]
 ]]></code></script>
index 14e5a220e30e197d8d231daa3c6cd4647b57de93..0a0591805e907aa3d938c7acc645082ca527f3f6 100644 (file)
@@ -31,12 +31,12 @@ except:
 import numpy
 import threading
 import sys
+import traceback
 
 # Pour disposer des classes dans l'espace de nommage lors du pickle
 from daCore.AssimilationStudy import AssimilationStudy
 from daYacsIntegration import daStudy
 
-
 class OptimizerHooks:
 
   def __init__(self, optim_algo, switch_value=-1):
@@ -60,18 +60,27 @@ class OptimizerHooks:
     specificParameters = pilot.SequenceAny_New(self.optim_algo.runtime.getTypeCode("SALOME_TYPES/Parameter"))
     method_name = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter'))
     method_name.setEltAtRank("name", "method")
-    method_name.setEltAtRank("value", method)
+    if sys.version_info.major < 3:
+      method_name.setEltAtRank("value", method)
+    else:
+      method_name.setEltAtRank("value", pickle.dumps(method))
     specificParameters.pushBack(method_name)
     # print self.optim_algo.has_observer
     if self.optim_algo.has_observer:
       obs_switch = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter'))
       obs_switch.setEltAtRank("name", "switch_value")
-      obs_switch.setEltAtRank("value", "1")
+      if sys.version_info.major < 3:
+        obs_switch.setEltAtRank("value", "1")
+      else:
+        obs_switch.setEltAtRank("value", pickle.dumps("1"))
       specificParameters.pushBack(obs_switch)
     if self.optim_algo.has_evolution_model:
       obs_switch = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter'))
       obs_switch.setEltAtRank("name", "switch_value")
-      obs_switch.setEltAtRank("value", self.switch_value)
+      if sys.version_info.major < 3:
+        obs_switch.setEltAtRank("value", self.switch_value)
+      else:
+        obs_switch.setEltAtRank("value", pickle.dumps(self.switch_value))
       specificParameters.pushBack(obs_switch)
     sample.setEltAtRank("specificParameters", specificParameters)
 
@@ -290,8 +299,11 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync):
     #print "Algorithme initialize"
 
     # get the daStudy
-    #print "[Debug] Input is ", input
-    str_da_study = input.getStringValue()
+    # print "[Debug] Input is ", input
+    if sys.version_info.major < 3:
+        str_da_study = input.getStringValue()
+    else:
+        str_da_study = input.getBytes()
     try:
         self.da_study = pickle.loads(str_da_study)
     except ValueError as e:
@@ -388,7 +400,10 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync):
     # Switch Value
     obs_switch = pilot.StructAny_New(self.runtime.getTypeCode('SALOME_TYPES/Parameter'))
     obs_switch.setEltAtRank("name", "switch_value")
-    obs_switch.setEltAtRank("value", self.da_study.observers_dict[info]["number"])
+    if sys.version_info.major < 3:
+      obs_switch.setEltAtRank("value", self.da_study.observers_dict[info]["number"])
+    else:
+      obs_switch.setEltAtRank("value", pickle.dumps(self.da_study.observers_dict[info]["number"]))
     specificParameters.pushBack(obs_switch)
 
     # Var
@@ -398,19 +413,21 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync):
     # Remove Data Observer, so you can ...
     var.removeDataObserver(self.obs)
     # Pickle then ...
-    var_str = pickle.dumps(var)
+    var_struct.setEltAtRank("value", pickle.dumps(var))
+    specificParameters.pushBack(var_struct)
     # Add Again Data Observer
     if self.da_study.observers_dict[info]["scheduler"] != "":
       self.ADD.setObserver(Variable = info, ObjectFunction = self.obs, Scheduler = self.da_study.observers_dict[info]["scheduler"], Info = info)
     else:
       self.ADD.setObserver(Variable = info, ObjectFunction = self.obs, Info = info)
-    var_struct.setEltAtRank("value", var_str)
-    specificParameters.pushBack(var_struct)
 
     # Info
     info_struct = pilot.StructAny_New(self.runtime.getTypeCode('SALOME_TYPES/Parameter'))
     info_struct.setEltAtRank("name", "info")
-    info_struct.setEltAtRank("value", self.da_study.observers_dict[info]["info"])
+    if sys.version_info.major < 3:
+      info_struct.setEltAtRank("value", self.da_study.observers_dict[info]["info"])
+    else:
+      info_struct.setEltAtRank("value", pickle.dumps(self.da_study.observers_dict[info]["info"]))
     specificParameters.pushBack(info_struct)
 
     sample.setEltAtRank("specificParameters", specificParameters)
@@ -421,7 +438,6 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync):
     self.pool.pushInSample(local_counter, sample)
 
     # Wait
-    import sys, traceback
     try:
       while True:
         self.signalMasterAndWait()