From 650a5c0e4fd460c578f11af384c2842cb721a8b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Ribes?= Date: Mon, 5 Mar 2012 09:59:35 +0100 Subject: [PATCH] Observers in progress --- src/daEficas/generator_adao.py | 5 +- .../daYacsSchemaCreator/help_methods.py | 45 ++++++ src/daSalome/daYacsSchemaCreator/methods.py | 145 +++++++++--------- 3 files changed, 122 insertions(+), 73 deletions(-) diff --git a/src/daEficas/generator_adao.py b/src/daEficas/generator_adao.py index 7c289da..89ebb7e 100644 --- a/src/daEficas/generator_adao.py +++ b/src/daEficas/generator_adao.py @@ -273,11 +273,12 @@ class AdaoGenerator(PythonGenerator): self.text_da += "observers[\"" + observer + "\"] = {}\n" self.text_da += "observers[\"" + observer + "\"][\"nodetype\"] = \"" + observers[observer]["nodetype"] + "\"\n" if observers[observer]["nodetype"] == "pyscript": - self.text_da += "observers[\"" + observer + "\"][\"script\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n" + self.text_da += "observers[\"" + observer + "\"][\"pyscript\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n" else: - self.text_da += "observers[\"" + observer + "\"][\"file\"] = \"" + observers[observer]["file"] + "\"\n" + self.text_da += "observers[\"" + observer + "\"][\"userfile\"] = \"" + observers[observer]["file"] + "\"\n" if "scheduler" in observers[observer].keys(): self.text_da += "observers[\"" + observer + "\"][\"scheduler\"] = \"\"\"" + observers[observer]["scheduler"] + "\"\"\"\n" + self.text_da += "study_config['Observers'] = observers\n" def add_observer_in_dict(self, observer, observers): """ diff --git a/src/daSalome/daYacsSchemaCreator/help_methods.py b/src/daSalome/daYacsSchemaCreator/help_methods.py index f6faa64..9879ca6 100644 --- a/src/daSalome/daYacsSchemaCreator/help_methods.py +++ b/src/daSalome/daYacsSchemaCreator/help_methods.py @@ -119,6 +119,51 @@ def check_study(study_config): logging.fatal("File is %s" % check_file_name) sys.exit(1) + # Check observers + if "Observers" in study_config.keys(): + for obs_var in study_config["Observers"]: + # Check du type + if not isinstance(study_config["Observers"][obs_var], type({})): + logging.fatal("An observer description has to be a Python dictionary") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if "nodetype" not in study_config["Observers"][obs_var].keys(): + logging.fatal("An observer description must provide a nodetype") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + nodetype = study_config["Observers"][obs_var]["nodetype"] + if not isinstance(study_config["Observers"][obs_var]["nodetype"], type("")): + logging.fatal("An observer nodetype description must be a string") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if nodetype != "pyscript" and nodetype != "userfile": + logging.fatal("An observer nodetype must be equal to 'pyscript' or 'userfile'") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if nodetype == "pyscript": + if "pyscript" not in study_config["Observers"][obs_var].keys(): + logging.fatal("An observer with nodetype pyscript must provide a pyscript") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if not isinstance(study_config["Observers"][obs_var]["pyscript"], type("")): + logging.fatal("An observer pyscript description must be a string") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if nodetype == "userfile": + if "userfile" not in study_config["Observers"][obs_var].keys(): + logging.fatal("An observer with nodetype userfileuserfilemust provide a pyscript") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if not isinstance(study_config["Observers"][obs_var]["userfile"], type("")): + logging.fatal("An observer userfile description must be a string") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + if "scheduler" in study_config["Observers"][obs_var].keys(): + if not isinstance(study_config["Observers"][obs_var]["scheduler"], type("")): + logging.fatal("An observer scheduler description must be a string") + logging.fatal("Observer is %s" % obs_var) + sys.exit(1) + def check_variables(name, study_config): if name not in study_config.keys(): diff --git a/src/daSalome/daYacsSchemaCreator/methods.py b/src/daSalome/daYacsSchemaCreator/methods.py index 3c84524..585daae 100644 --- a/src/daSalome/daYacsSchemaCreator/methods.py +++ b/src/daSalome/daYacsSchemaCreator/methods.py @@ -238,80 +238,83 @@ def create_yacs_proc(study_config): CAS_node.getInputPort(port_name).edInitPy(FunctionDict["Script"][FunctionName]) # Step 3: create compute bloc - compute_bloc = runtime.createBloc("compute_bloc") - proc.edAddChild(compute_bloc) - proc.edAddCFLink(CAS_node, compute_bloc) - - if AlgoType[study_config["Algorithm"]] == "Optim": - # We use an optimizer loop - name = "Execute" + study_config["Algorithm"] - algLib = "daYacsIntegration.py" - factoryName = "AssimilationAlgorithm_asynch" - optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "") - compute_bloc.edAddChild(optimizer_node) - proc.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort()) - - # Check if we have a python script for OptimizerLoopNode - data_config = study_config["ObservationOperator"] - if data_config["Type"] == "Function" and data_config["From"] == "FunctionDict": - # Get script - FunctionDict = data_config["Data"] - script_filename = "" - for FunctionName in FunctionDict["Function"]: - # We currently support only one file - script_filename = FunctionDict["Script"][FunctionName] - break - - # We create a new pyscript node - opt_script_node = runtime.createScriptNode("", "FunctionNode") - if repertory: - script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) - try: - script_str= open(script_filename, 'r') - except: - logging.fatal("Exception in opening function script file : " + script_filename) - traceback.print_exc() - sys.exit(1) - node_script = "#-*-coding:iso-8859-1-*-\n" - node_script += "import sys, os \n" - if base_repertory != "": - node_script += "filepath = \"" + base_repertory + "\"\n" - else: - node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" - node_script += "sys.path.insert(0,os.path.dirname(filepath))\n" - node_script += script_str.read() - opt_script_node.setScript(node_script) - opt_script_node.edAddInputPort("computation", t_param_input) - opt_script_node.edAddOutputPort("result", t_param_output) - - # Add it - computation_bloc = runtime.createBloc("computation_bloc") - optimizer_node.edSetNode(computation_bloc) - computation_bloc.edAddChild(opt_script_node) - - # We connect Optimizer with the script - proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation")) - proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool()) + if "Observers" in study_config.keys(): + # Todo + else: + compute_bloc = runtime.createBloc("compute_bloc") + proc.edAddChild(compute_bloc) + proc.edAddCFLink(CAS_node, compute_bloc) + + if AlgoType[study_config["Algorithm"]] == "Optim": + # We use an optimizer loop + name = "Execute" + study_config["Algorithm"] + algLib = "daYacsIntegration.py" + factoryName = "AssimilationAlgorithm_asynch" + optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "") + compute_bloc.edAddChild(optimizer_node) + proc.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort()) + + # Check if we have a python script for OptimizerLoopNode + data_config = study_config["ObservationOperator"] + if data_config["Type"] == "Function" and data_config["From"] == "FunctionDict": + # Get script + FunctionDict = data_config["Data"] + script_filename = "" + for FunctionName in FunctionDict["Function"]: + # We currently support only one file + script_filename = FunctionDict["Script"][FunctionName] + break + + # We create a new pyscript node + opt_script_node = runtime.createScriptNode("", "FunctionNode") + if repertory: + script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) + try: + script_str= open(script_filename, 'r') + except: + logging.fatal("Exception in opening function script file : " + script_filename) + traceback.print_exc() + sys.exit(1) + node_script = "#-*-coding:iso-8859-1-*-\n" + node_script += "import sys, os \n" + if base_repertory != "": + node_script += "filepath = \"" + base_repertory + "\"\n" + else: + node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" + node_script += "sys.path.insert(0,os.path.dirname(filepath))\n" + node_script += script_str.read() + opt_script_node.setScript(node_script) + opt_script_node.edAddInputPort("computation", t_param_input) + opt_script_node.edAddOutputPort("result", t_param_output) + + # Add it + computation_bloc = runtime.createBloc("computation_bloc") + optimizer_node.edSetNode(computation_bloc) + computation_bloc.edAddChild(opt_script_node) + + # We connect Optimizer with the script + proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation")) + proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool()) - # Connect node with InitUserData - if "ObservationOperator" in init_config["Target"]: - opt_node_script = opt_script_node.getScript() - opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script - opt_script_node.setScript(opt_node_script) - opt_script_node.edAddInputPort("init_data", t_pyobj) - proc.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_node.getInputPort("init_data")) - else: - factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode") - opt_script_node = factory_opt_script_node.cloneNode("FakeFunctionNode") + # Connect node with InitUserData + if "ObservationOperator" in init_config["Target"]: + opt_node_script = opt_script_node.getScript() + opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script + opt_script_node.setScript(opt_node_script) + opt_script_node.edAddInputPort("init_data", t_pyobj) + proc.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_node.getInputPort("init_data")) + else: + factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode") + opt_script_node = factory_opt_script_node.cloneNode("FakeFunctionNode") - # Add it - computation_bloc = runtime.createBloc("computation_bloc") - optimizer_node.edSetNode(computation_bloc) - computation_bloc.edAddChild(opt_script_node) + # Add it + computation_bloc = runtime.createBloc("computation_bloc") + optimizer_node.edSetNode(computation_bloc) + computation_bloc.edAddChild(opt_script_node) - # We connect Optimizer with the script - proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation")) - proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool()) + # We connect Optimizer with the script + proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation")) + proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool()) # Step 4: create post-processing from user configuration if "UserPostAnalysis" in study_config.keys(): -- 2.39.2