]> SALOME platform Git repositories - modules/adao.git/commitdiff
Salome HOME
Simplifying operators definition by function script
authorJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Sun, 14 Oct 2012 22:06:34 +0000 (00:06 +0200)
committerJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Sun, 14 Oct 2012 22:06:34 +0000 (00:06 +0200)
bin/AdaoCatalogGenerator.py
src/daEficas/generator_adao.py
src/daSalome/daYacsSchemaCreator/help_methods.py
src/daSalome/daYacsSchemaCreator/infos_daComposant.py
src/daSalome/daYacsSchemaCreator/methods.py

index 8ae595cc3ee568c01db32b25fac54e1d00427788..a3615b4edea4994718018e30c1a042eceaae3693 100644 (file)
@@ -57,6 +57,11 @@ def F_${data_name}(statut) : return FACT(statut = statut,
 
                                                       SCRIPTWITHFUNCTIONS_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant en variables internes trois fonctions de calcul nommées DirectOperator, TangentOperator et AdjointOperator", ang="Waiting for a script file name, with or without the full path to find it, containing as internal variables three computation functions named DirectOperator, TangentOperator and AdjointOperator"),
                                                      ),
+                                         SCRIPTWITHONEFUNCTION_DATA = BLOC ( condition = " FROM in ( 'ScriptWithOneFunction', ) ",
+
+                                                      SCRIPTWITHONEFUNCTION_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant en variable interne une seule fonction de calcul nommée DirectOperator", ang="Waiting for a script file name, with or without the full path to find it, containing as internal variable only one function named DirectOperator"),
+                                                      DifferentialIncrement = SIMP(statut="o", typ = "R", val_min=0, val_max=1, defaut=0.01, fr="Incrément de la perturbation dX pour calculer la dérivée, construite en multipliant X par l'incrément en évitant les valeurs nulles", ang="Increment of dX perturbation to calculate the derivative, build multiplying X by the increment avoiding null values"),
+                                                     ),
                                          SCRIPTWITHSWITCH_DATA = BLOC ( condition = " FROM in ( 'ScriptWithSwitch', ) ",
 
                                                       SCRIPTWITHSWITCH_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant un switch pour les calculs direct, tangent et adjoint", ang="Waiting for a script file name, with or without the full path to find it, containing a switch for direct, tangent and adjoint computations"),
@@ -135,7 +140,7 @@ ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
                           op=None,
                           repetable           = "n",
                           Study_name          = SIMP(statut="o", typ = "TXM"),
-                          Study_repertory     = SIMP(statut="f", typ = "TXM"),
+                          Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
                           Background          = F_Background("o"),
@@ -157,7 +162,7 @@ CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
                           op=None,
                           repetable           = "n",
                           Study_name          = SIMP(statut="o", typ = "TXM"),
-                          Study_repertory     = SIMP(statut="f", typ = "TXM"),
+                          Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${check_names})),
                           CheckingPoint       = F_CheckingPoint("o"),
index 924d4b9d478ea2cbbb4ad72d938280881b449991..7e7faf6b4133bc86a33ef3804fb63b46f2ed6378 100644 (file)
@@ -158,6 +158,8 @@ class AdaoGenerator(PythonGenerator):
       data = self.dictMCVal[search_type + "SCRIPTWITHSWITCH_DATA__SCRIPTWITHSWITCH_FILE"]
     elif from_type == "ScriptWithFunctions":
       data = self.dictMCVal[search_type + "SCRIPTWITHFUNCTIONS_DATA__SCRIPTWITHFUNCTIONS_FILE"]
+    elif from_type == "ScriptWithOneFunction":
+      data = self.dictMCVal[search_type + "SCRIPTWITHONEFUNCTION_DATA__SCRIPTWITHONEFUNCTION_FILE"]
     elif from_type == "FunctionDict":
       data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
     else:
@@ -198,6 +200,20 @@ class AdaoGenerator(PythonGenerator):
       self.text_da += data_name + "_config['Data'] = " + data_name + "_ScriptWithFunctions\n"
       self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
 
+    if from_type == "ScriptWithOneFunction":
+      self.text_da += data_name + "_ScriptWithOneFunction = {}\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['Script'] = {}\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['Script']['Direct'] = '"  + data + "'\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['Script']['Tangent'] = '" + data + "'\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['Script']['Adjoint'] = '" + data + "'\n"
+      self.text_da += data_name + "_ScriptWithOneFunction['DifferentialIncrement'] = " + str(float(self.dictMCVal[search_type + "SCRIPTWITHONEFUNCTION_DATA__DifferentialIncrement"])) + "\n"
+      self.text_da += data_name + "_config = {}\n"
+      self.text_da += data_name + "_config['Type'] = 'Function'\n"
+      self.text_da += data_name + "_config['From'] = 'ScriptWithOneFunction'\n"
+      self.text_da += data_name + "_config['Data'] = " + data_name + "_ScriptWithOneFunction\n"
+      self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
+
     if from_type == "FunctionDict":
       self.text_da += data_name + "_FunctionDict = {}\n"
       self.text_da += data_name + "_FunctionDict['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
index 639ce6c9b91ee583ef1cc328dc4b34454c8f0bdd..0ac7b2b15355a99d201c94739ec329ec4a1b28fe 100644 (file)
@@ -231,3 +231,13 @@ def check_data(data_name, data_config, repertory_check=False, repertory=""):
         check_file_name = ScriptWithFunctions["Script"][FunctionName]
       if not os.path.exists(check_file_name):
         raise ValueError("\n\n The script file cannot be found for the \"%s\" keyword, please \n check its availability. The given user file is:\n %s\n"%(from_type,check_file_name))
+  elif from_type == "ScriptWithOneFunction":
+    ScriptWithOneFunction = data_config["Data"]
+    for FunctionName in ScriptWithOneFunction["Function"]:
+      check_file_name = ""
+      if repertory_check:
+        check_file_name = os.path.join(repertory, os.path.basename(ScriptWithOneFunction["Script"][FunctionName]))
+      else:
+        check_file_name = ScriptWithOneFunction["Script"][FunctionName]
+      if not os.path.exists(check_file_name):
+        raise ValueError("\n\n The script file cannot be found for the \"%s\" keyword, please \n check its availability. The given user file is:\n %s\n"%(from_type,check_file_name))
index 600a2858e132cf00903db22edf9bf95219f67662..29eba0c042e72d3a68d083c8202d7a19e963c3dd 100644 (file)
@@ -50,7 +50,7 @@ FromNumpyList = {}
 FromNumpyList["Vector"]      = ["String", "Script"]
 FromNumpyList["VectorSerie"] = ["String", "Script"]
 FromNumpyList["Matrix"]      = ["String", "Script"]
-FromNumpyList["Function"]    = ["ScriptWithFunctions", "ScriptWithSwitch", "FunctionDict"]
+FromNumpyList["Function"]    = ["ScriptWithFunctions", "ScriptWithOneFunction", "ScriptWithSwitch", "FunctionDict"]
 FromNumpyList["Dict"]        = ["Script"]
 
 # -- Infos from daAlgorithms --
@@ -134,14 +134,14 @@ AlgoType["QuantileRegression"] = "Optim"
 # catalogue Eficas
 
 # Basic data types
-BasicDataInputs = ["String", "Script", "ScriptWithFunctions", "ScriptWithSwitch", "FunctionDict"]
+BasicDataInputs = ["String", "Script", "ScriptWithFunctions", "ScriptWithOneFunction", "ScriptWithSwitch", "FunctionDict"]
 
 # Data input dict
 DataTypeDict = {}
 DataTypeDict["Vector"]      = ["String", "Script"]
 DataTypeDict["VectorSerie"] = ["String", "Script"]
 DataTypeDict["Matrix"]      = ["String", "Script"]
-DataTypeDict["Function"]    = ["ScriptWithFunctions", "ScriptWithSwitch", "FunctionDict"]
+DataTypeDict["Function"]    = ["ScriptWithFunctions", "ScriptWithOneFunction", "ScriptWithSwitch", "FunctionDict"]
 DataTypeDict["Dict"]        = ["Script"]
 
 DataTypeDefaultDict = {}
index 7cf84eae1ecbb408e67a8e9a892b595e33e81140..d59383fccc4614e1318e98c76964c3867de345f1 100644 (file)
@@ -374,6 +374,26 @@ def create_yacs_proc(study_config):
            else:
              CAS_node.getInputPort(port_name).edInitPy(ScriptWithFunctions["Script"][FunctionName])
 
+      if data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction" and key == "ObservationOperator":
+         ScriptWithOneFunction = data_config["Data"]
+         for FunctionName in ScriptWithOneFunction["Function"]:
+           port_name = "ObservationOperator" + FunctionName
+           CAS_node.edAddInputPort(port_name, t_string)
+           if repertory:
+             CAS_node.getInputPort(port_name).edInitPy(os.path.join(base_repertory, os.path.basename(ScriptWithOneFunction["Script"][FunctionName])))
+           else:
+             CAS_node.getInputPort(port_name).edInitPy(ScriptWithOneFunction["Script"][FunctionName])
+
+      if data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction" and key == "EvolutionModel":
+         ScriptWithOneFunction = data_config["Data"]
+         for FunctionName in ScriptWithOneFunction["Function"]:
+           port_name = "EvolutionModel" + FunctionName
+           CAS_node.edAddInputPort(port_name, t_string)
+           if repertory:
+             CAS_node.getInputPort(port_name).edInitPy(os.path.join(base_repertory, os.path.basename(ScriptWithOneFunction["Script"][FunctionName])))
+           else:
+             CAS_node.getInputPort(port_name).edInitPy(ScriptWithOneFunction["Script"][FunctionName])
+
   # Step 3: create compute bloc
   compute_bloc = runtime.createBloc("compute_bloc")
   ADAO_Case.edAddChild(compute_bloc)
@@ -441,6 +461,7 @@ def create_yacs_proc(study_config):
     opt_script_nodeOO.setScript(node_script)
     opt_script_nodeOO.edAddInputPort("computation", t_param_input)
     opt_script_nodeOO.edAddOutputPort("result", t_param_output)
+
   elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithFunctions":
     # Get script
     ScriptWithFunctions = data_config["Data"]
@@ -520,6 +541,84 @@ def create_yacs_proc(study_config):
     opt_script_nodeOO.setScript(node_script)
     opt_script_nodeOO.edAddInputPort("computation", t_param_input)
     opt_script_nodeOO.edAddOutputPort("result", t_param_output)
+
+  elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
+    # Get script
+    ScriptWithOneFunction = data_config["Data"]
+    script_filename = ""
+    for FunctionName in ScriptWithOneFunction["Function"]:
+      # We currently support only one file
+      script_filename = ScriptWithOneFunction["Script"][FunctionName]
+      break
+
+    # We create a new pyscript node
+    opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
+    if repertory:
+      script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
+    try:
+      script_str= open(script_filename, 'r')
+    except:
+      raise ValueError("Exception in opening function script file: " + script_filename)
+    node_script  = "#-*-coding:iso-8859-1-*-\n"
+    node_script += "import sys, os, numpy, logging\n"
+    node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
+    node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
+    node_script += "  sys.path.insert(0,filepath)\n"
+    node_script += """# ==============================================\n"""
+    node_script += script_str.read()
+    node_script += """# ==============================================\n"""
+    node_script += """__method = None\n"""
+    node_script += """for param in computation["specificParameters"]:\n"""
+    node_script += """  if param["name"] == "method": __method = param["value"]\n"""
+    node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
+    node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
+    node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
+    node_script += """#\n"""
+    node_script += """try:\n"""
+    node_script += """    DirectOperator\n"""
+    node_script += """except NameError:\n"""
+    node_script += """    raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
+    node_script += """import ApproximatedDerivatives\n"""
+    node_script += """FDA = ApproximatedDerivatives.FDApproximation(\n"""
+    node_script += """    Function  = DirectOperator,\n"""
+    node_script += """    increment = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
+    node_script += """    )\n"""
+    node_script += """#\n"""
+    node_script += """__data = []\n"""
+    node_script += """if __method == "Direct":\n"""
+    node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
+    node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
+    node_script += """  __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
+    node_script += """#\n"""
+    node_script += """if __method == "Tangent":\n"""
+    node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
+    node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
+    node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
+    node_script += """  __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
+    node_script += """#\n"""
+    node_script += """if __method == "Adjoint":\n"""
+    node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
+    node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
+    node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
+    node_script += """  __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
+    node_script += """#\n"""
+    node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
+    node_script += """it = numpy.ravel(__data)\n"""
+    node_script += """outputValues = [[[[]]]]\n"""
+    node_script += """for val in it:\n"""
+    node_script += """  outputValues[0][0][0].append(val)\n"""
+    node_script += """#\n"""
+    node_script += """result = {}\n"""
+    node_script += """result["outputValues"]        = outputValues\n"""
+    node_script += """result["specificOutputInfos"] = []\n"""
+    node_script += """result["returnCode"]          = 0\n"""
+    node_script += """result["errorMessage"]        = ""\n"""
+    node_script += """# ==============================================\n"""
+    #
+    opt_script_nodeOO.setScript(node_script)
+    opt_script_nodeOO.edAddInputPort("computation", t_param_input)
+    opt_script_nodeOO.edAddOutputPort("result", t_param_output)
+
   else:
     factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
     opt_script_nodeOO = factory_opt_script_node.cloneNode("FakeFunctionNode")
@@ -554,6 +653,7 @@ def create_yacs_proc(study_config):
       opt_script_nodeEM.setScript(node_script)
       opt_script_nodeEM.edAddInputPort("computation", t_param_input)
       opt_script_nodeEM.edAddOutputPort("result", t_param_output)
+
     elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithSwitch":
       # Get script
       ScriptWithSwitch = data_config["Data"]
@@ -658,6 +758,82 @@ def create_yacs_proc(study_config):
       opt_script_nodeEM.setScript(node_script)
       opt_script_nodeEM.edAddInputPort("computation", t_param_input)
       opt_script_nodeEM.edAddOutputPort("result", t_param_output)
+
+    elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
+      # Get script
+      ScriptWithOneFunction = data_config["Data"]
+      script_filename = ""
+      for FunctionName in ScriptWithOneFunction["Function"]:
+        # We currently support only one file
+        script_filename = ScriptWithOneFunction["Script"][FunctionName]
+        break
+      # We create a new pyscript node
+      opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
+      if repertory:
+        script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
+      try:
+        script_str= open(script_filename, 'r')
+      except:
+        raise ValueError("Exception in opening function script file: " + script_filename)
+      node_script  = "#-*-coding:iso-8859-1-*-\n"
+      node_script += "import sys, os, numpy, logging\n"
+      node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
+      node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
+      node_script += "  sys.path.insert(0,filepath)\n"
+      node_script += script_str.read()
+      node_script += """# ==============================================\n"""
+      node_script += """__method = None\n"""
+      node_script += """for param in computation["specificParameters"]:\n"""
+      node_script += """  if param["name"] == "method": __method = param["value"]\n"""
+      node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
+      node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
+      node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
+      node_script += """#\n"""
+      node_script += """try:\n"""
+      node_script += """    DirectOperator\n"""
+      node_script += """except NameError:\n"""
+      node_script += """    raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
+      node_script += """import ApproximatedDerivatives\n"""
+      node_script += """FDA = ApproximatedDerivatives.FDApproximation(\n"""
+      node_script += """    Function  = DirectOperator,\n"""
+      node_script += """    increment = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
+      node_script += """    )\n"""
+      node_script += """#\n"""
+      node_script += """__data = []\n"""
+      node_script += """if __method == "Direct":\n"""
+      node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
+      node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
+      node_script += """  __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
+      node_script += """#\n"""
+      node_script += """if __method == "Tangent":\n"""
+      node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
+      node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
+      node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
+      node_script += """  __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
+      node_script += """#\n"""
+      node_script += """if __method == "Adjoint":\n"""
+      node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
+      node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
+      node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
+      node_script += """  __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
+      node_script += """#\n"""
+      node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
+      node_script += """it = numpy.ravel(__data)\n"""
+      node_script += """outputValues = [[[[]]]]\n"""
+      node_script += """for val in it:\n"""
+      node_script += """  outputValues[0][0][0].append(val)\n"""
+      node_script += """#\n"""
+      node_script += """result = {}\n"""
+      node_script += """result["outputValues"]        = outputValues\n"""
+      node_script += """result["specificOutputInfos"] = []\n"""
+      node_script += """result["returnCode"]          = 0\n"""
+      node_script += """result["errorMessage"]        = ""\n"""
+      node_script += """# ==============================================\n"""
+      #
+      opt_script_nodeEM.setScript(node_script)
+      opt_script_nodeEM.edAddInputPort("computation", t_param_input)
+      opt_script_nodeEM.edAddOutputPort("result", t_param_output)
+
     else:
       factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
       opt_script_nodeEM = factory_opt_script_node.cloneNode("FakeFunctionNode")