From d82bfe35c7c9bf2e4471b3aff881a50ae74c1538 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Thu, 28 Dec 2017 21:34:24 +0100 Subject: [PATCH] Python 3 compatibility improvement (isinstance & has_*) --- src/daComposant/daCore/Aidsm.py | 217 ++------------------- src/daComposant/daCore/BasicObjects.py | 226 +++++++++++++++++++--- src/daComposant/daCore/ExtendedLogging.py | 14 ++ src/daComposant/daCore/PlatformInfo.py | 75 ++++--- 4 files changed, 265 insertions(+), 267 deletions(-) diff --git a/src/daComposant/daCore/Aidsm.py b/src/daComposant/daCore/Aidsm.py index 472bcb3..4931b99 100644 --- a/src/daComposant/daCore/Aidsm.py +++ b/src/daComposant/daCore/Aidsm.py @@ -26,201 +26,21 @@ __author__ = "Jean-Philippe ARGAUD" __all__ = ["Aidsm"] -import os, sys -from daCore import ExtendedLogging ; ExtendedLogging.ExtendedLogging() # A importer en premier -import logging -import numpy +import os +import sys # from daCore.BasicObjects import State, Covariance, FullOperator, Operator from daCore.BasicObjects import AlgorithmAndParameters, DataObserver -from daCore.BasicObjects import DiagnosticAndParameters, ImportFromScript -from daCore.BasicObjects import CaseLogger, GenericCaseViewer -from daCore.Templates import ObserverTemplates -from daCore import Persistence +from daCore.BasicObjects import DiagnosticAndParameters, CaseLogger from daCore import PlatformInfo - -# ============================================================================== -class DICViewer(GenericCaseViewer): - """ - Etablissement des commandes de creation d'un cas DIC - """ - def __init__(self, __name="", __objname="case", __content=None): - "Initialisation et enregistrement de l'entete" - GenericCaseViewer.__init__(self, __name, __objname, __content) - self._addLine("# -*- coding: utf-8 -*-") - self._addLine("#\n# Input for ADAO converter to YACS\n#") - self._addLine("from numpy import array, matrix") - self._addLine("#") - self._addLine("study_config = {}") - self._addLine("study_config['StudyType'] = 'ASSIMILATION_STUDY'") - self._addLine("study_config['Name'] = '%s'"%self._name) - self._addLine("observers = {}") - self._addLine("study_config['Observers'] = observers") - self._addLine("#") - self._addLine("inputvariables_config = {}") - self._addLine("inputvariables_config['Order'] =['adao_default']") - self._addLine("inputvariables_config['adao_default'] = -1") - self._addLine("study_config['InputVariables'] = inputvariables_config") - self._addLine("#") - self._addLine("outputvariables_config = {}") - self._addLine("outputvariables_config['Order'] = ['adao_default']") - self._addLine("outputvariables_config['adao_default'] = -1") - self._addLine("study_config['OutputVariables'] = outputvariables_config") - if __content is not None: - for command in __content: - self._append(*command) - def _append(self, __command=None, __keys=None, __local=None, __pre=None, __switchoff=False): - "Transformation d'une commande individuelle en un enregistrement" - if __command == "set": __command = __local["Concept"] - else: __command = __command.replace("set", "", 1) - # - __text = None - if __command in (None, 'execute', 'executePythonScheme', 'executeYACSScheme', 'get'): - return - elif __command in ['Debug', 'setDebug']: - __text = "#\nstudy_config['Debug'] = '1'" - elif __command in ['NoDebug', 'setNoDebug']: - __text = "#\nstudy_config['Debug'] = '0'" - elif __command in ['Observer', 'setObserver']: - __obs = __local['Variable'] - self._numobservers += 1 - __text = "#\n" - __text += "observers['%s'] = {}\n"%__obs - if __local['String'] is not None: - __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'String') - __text += "observers['%s']['String'] = \"\"\"%s\"\"\"\n"%(__obs, __local['String']) - if __local['Script'] is not None: - __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'Script') - __text += "observers['%s']['Script'] = \"%s\"\n"%(__obs, __local['Script']) - if __local['Template'] is not None: - __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'String') - __text += "observers['%s']['String'] = \"\"\"%s\"\"\"\n"%(__obs, ObserverTemplates[__local['Template']]) - if __local['Info'] is not None: - __text += "observers['%s']['info'] = \"\"\"%s\"\"\"\n"%(__obs, __local['Info']) - else: - __text += "observers['%s']['info'] = \"\"\"%s\"\"\"\n"%(__obs, __obs) - __text += "observers['%s']['number'] = %s"%(__obs, self._numobservers) - elif __local is not None: # __keys is not None and - numpy.set_printoptions(precision=15,threshold=1000000,linewidth=1000*15) - __text = "#\n" - __text += "%s_config = {}\n"%__command - if 'self' in __local: __local.pop('self') - __to_be_removed = [] - for __k,__v in __local.items(): - if __v is None: __to_be_removed.append(__k) - for __k in __to_be_removed: - __local.pop(__k) - for __k,__v in __local.items(): - if __k == "Concept": continue - if __k in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix','OneFunction','ThreeFunctions'] and 'Script' in __local: continue - if __k == 'Algorithm': - __text += "study_config['Algorithm'] = %s\n"%(repr(__v)) - elif __k == 'Script': - __k = 'Vector' - __f = 'Script' - __v = "'"+repr(__v)+"'" - for __lk in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix']: - if __lk in __local and __local[__lk]: __k = __lk - if __command == "AlgorithmParameters": __k = "Dict" - if 'OneFunction' in __local and __local['OneFunction']: - __text += "%s_ScriptWithOneFunction = {}\n"%(__command,) - __text += "%s_ScriptWithOneFunction['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"%(__command,) - __text += "%s_ScriptWithOneFunction['Script'] = {}\n"%(__command,) - __text += "%s_ScriptWithOneFunction['Script']['Direct'] = %s\n"%(__command,__v) - __text += "%s_ScriptWithOneFunction['Script']['Tangent'] = %s\n"%(__command,__v) - __text += "%s_ScriptWithOneFunction['Script']['Adjoint'] = %s\n"%(__command,__v) - __text += "%s_ScriptWithOneFunction['DifferentialIncrement'] = 1e-06\n"%(__command,) - __text += "%s_ScriptWithOneFunction['CenteredFiniteDifference'] = 0\n"%(__command,) - __k = 'Function' - __f = 'ScriptWithOneFunction' - __v = '%s_ScriptWithOneFunction'%(__command,) - if 'ThreeFunctions' in __local and __local['ThreeFunctions']: - __text += "%s_ScriptWithFunctions = {}\n"%(__command,) - __text += "%s_ScriptWithFunctions['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"%(__command,) - __text += "%s_ScriptWithFunctions['Script'] = {}\n"%(__command,) - __text += "%s_ScriptWithFunctions['Script']['Direct'] = %s\n"%(__command,__v) - __text += "%s_ScriptWithFunctions['Script']['Tangent'] = %s\n"%(__command,__v) - __text += "%s_ScriptWithFunctions['Script']['Adjoint'] = %s\n"%(__command,__v) - __k = 'Function' - __f = 'ScriptWithFunctions' - __v = '%s_ScriptWithFunctions'%(__command,) - __text += "%s_config['Type'] = '%s'\n"%(__command,__k) - __text += "%s_config['From'] = '%s'\n"%(__command,__f) - __text += "%s_config['Data'] = %s\n"%(__command,__v) - __text = __text.replace("''","'") - elif __k in ('Stored', 'Checked'): - if bool(__v): - __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v))) - elif __k in ('AvoidRC', 'noDetails'): - if not bool(__v): - __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v))) - else: - if __k is 'Parameters': __k = "Dict" - if isinstance(__v,Persistence.Persistence): __v = __v.values() - if callable(__v): __text = self._missing%__v.__name__+__text - if isinstance(__v,dict): - for val in __v.values(): - if callable(val): __text = self._missing%val.__name__+__text - __text += "%s_config['Type'] = '%s'\n"%(__command,__k) - __text += "%s_config['From'] = '%s'\n"%(__command,'String') - __text += "%s_config['Data'] = \"\"\"%s\"\"\"\n"%(__command,repr(__v)) - __text += "study_config['%s'] = %s_config"%(__command,__command) - numpy.set_printoptions(precision=8,threshold=1000,linewidth=75) - if __switchoff: - self._switchoff = True - if __text is not None: self._addLine(__text) - if not __switchoff: - self._switchoff = False - def _finalize(self): - self.__loadVariablesByScript() - self._addLine("#") - self._addLine("Analysis_config = {}") - self._addLine("Analysis_config['From'] = 'String'") - self._addLine("Analysis_config['Data'] = \"\"\"import numpy") - self._addLine("xa=numpy.ravel(ADD.get('Analysis')[-1])") - self._addLine("print 'Analysis:',xa\"\"\"") - self._addLine("study_config['UserPostAnalysis'] = Analysis_config") - def __loadVariablesByScript(self): - exec("\n".join(self._lineSerie)) - if "Algorithm" in study_config and len(study_config['Algorithm'])>0: - self.__hasAlgorithm = True - else: - self.__hasAlgorithm = False - if not self.__hasAlgorithm and \ - "AlgorithmParameters" in study_config and \ - isinstance(study_config['AlgorithmParameters'], dict) and \ - "From" in study_config['AlgorithmParameters'] and \ - "Data" in study_config['AlgorithmParameters'] and \ - study_config['AlgorithmParameters']['From'] == 'Script': - __asScript = study_config['AlgorithmParameters']['Data'] - __var = ImportFromScript(__asScript).getvalue( "Algorithm" ) - __text = "#\nstudy_config['Algorithm'] = '%s'"%(__var,) - self._addLine(__text) - if self.__hasAlgorithm and \ - "AlgorithmParameters" in study_config and \ - isinstance(study_config['AlgorithmParameters'], dict) and \ - "From" not in study_config['AlgorithmParameters'] and \ - "Data" not in study_config['AlgorithmParameters']: - __text = "#\n" - __text += "AlgorithmParameters_config['Type'] = 'Dict'\n" - __text += "AlgorithmParameters_config['From'] = 'String'\n" - __text += "AlgorithmParameters_config['Data'] = '{}'\n" - self._addLine(__text) - del study_config - -class XMLViewer(GenericCaseViewer): - """ - Etablissement des commandes de creation d'un cas XML - """ - def __init__(self, __name="", __objname="case", __content=None): - "Initialisation et enregistrement de l'entete" - GenericCaseViewer.__init__(self, __name, __objname, __content) - raise NotImplementedError() +# +from daCore import ExtendedLogging ; ExtendedLogging.ExtendedLogging() # A importer en premier +import logging # ============================================================================== class Aidsm(object): """ ADAO Internal Data Structure Model """ - def __init__(self, name = "", addViewers={"DIC":DICViewer}): + def __init__(self, name = "", addViewers=None): self.__name = str(name) self.__case = CaseLogger(self.__name, "case", addViewers) # @@ -323,7 +143,10 @@ class Aidsm(object): except Exception as e: if isinstance(e, SyntaxError): msg = "at %s: %s"%(e.offset, e.text) else: msg = "" - raise ValueError("during settings, the following error occurs:\n\n%s %s\n\nSee also the potential messages, which can show the origin of the above error, in the launching terminal."%(str(e),msg)) + raise ValueError("during settings, the following error occurs:\n"+\ + "\n%s %s\n\nSee also the potential messages, "+\ + "which can show the origin of the above error, "+\ + "in the launching terminal."%(str(e),msg)) # ----------------------------------------------------------- @@ -669,9 +492,7 @@ class Aidsm(object): Variable = None, ObjectFunction = None, ): - """ - Permet de retirer un observer à une ou des variable nommée. - """ + "Permet de retirer un observer à une ou des variable nommée" if "AlgorithmParameters" not in self.__adaoObject: raise ValueError("No algorithm registred, ask for one before removing observers") # @@ -719,17 +540,10 @@ class Aidsm(object): def get(self, Concept=None, noDetails=True ): "Recuperation d'une sortie du calcul" - """ - Permet d'accéder aux informations stockées, diagnostics et résultats - disponibles après l'exécution du calcul. Attention, quand un diagnostic - porte le même nom qu'une variable stockée (paramètre ou résultat), - c'est la variable stockée qui est renvoyée, et le diagnostic est - inatteignable. - """ if Concept is not None: try: self.__case.register("get", dir(), locals(), Concept) # Break pickle in Python 2 - except: + except Exception: pass if Concept in self.__StoredInputs: return self.__StoredInputs[Concept] @@ -875,7 +689,10 @@ class Aidsm(object): except Exception as e: if isinstance(e, SyntaxError): msg = "at %s: %s"%(e.offset, e.text) else: msg = "" - raise ValueError("during execution, the following error occurs:\n\n%s %s\n\nSee also the potential messages, which can show the origin of the above error, in the launching terminal.\n"%(str(e),msg)) + raise ValueError("during execution, the following error occurs:\n"+\ + "\n%s %s\n\nSee also the potential messages, "+\ + "which can show the origin of the above error, "+\ + "in the launching terminal.\n"%(str(e),msg)) return 0 def __executePythonScheme(self, FileName=None): diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index 4668875..32ac40a 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -28,7 +28,10 @@ __author__ = "Jean-Philippe ARGAUD" __all__ = [] -import os, sys, logging, copy +import os +import sys +import logging +import copy import numpy from daCore import Persistence from daCore import PlatformInfo @@ -116,7 +119,7 @@ class Operator(object): self.__NbCallsAsMatrix, self.__NbCallsAsMethod, self.__NbCallsOfCached = 0, 0, 0 self.__AvoidRC = bool( avoidingRedundancy ) if fromMethod is not None: - self.__Method = fromMethod + self.__Method = fromMethod # logtimer(fromMethod) self.__Matrix = None self.__Type = "Method" elif fromMatrix is not None: @@ -415,7 +418,7 @@ class FullOperator(object): # if __appliedInX is not None: self.__FO["AppliedInX"] = {} - if type(__appliedInX) is not dict: + if not isinstance(__appliedInX, dict): raise ValueError("Error: observation operator defined by \"AppliedInX\" need a dictionary as argument.") for key in list(__appliedInX.keys()): if type( __appliedInX[key] ) is type( numpy.matrix([]) ): @@ -1198,13 +1201,13 @@ class AlgorithmAndParameters(object): if not( min(__EM_shape) == max(__EM_shape) ): raise ValueError("Shape characteristic of evolution operator (EM) is incorrect: \"%s\"."%(__EM_shape,)) # - if len(self.__HO) > 0 and not(type(self.__HO) is type({})) and not( __HO_shape[1] == max(__Xb_shape) ): + if len(self.__HO) > 0 and not isinstance(self.__HO, dict) and not( __HO_shape[1] == max(__Xb_shape) ): raise ValueError("Shape characteristic of observation operator (H) \"%s\" and state (X) \"%s\" are incompatible."%(__HO_shape,__Xb_shape)) - if len(self.__HO) > 0 and not(type(self.__HO) is type({})) and not( __HO_shape[0] == max(__Y_shape) ): + if len(self.__HO) > 0 and not isinstance(self.__HO, dict) and not( __HO_shape[0] == max(__Y_shape) ): raise ValueError("Shape characteristic of observation operator (H) \"%s\" and observation (Y) \"%s\" are incompatible."%(__HO_shape,__Y_shape)) - if len(self.__HO) > 0 and not(type(self.__HO) is type({})) and len(self.__B) > 0 and not( __HO_shape[1] == __B_shape[0] ): + if len(self.__HO) > 0 and not isinstance(self.__HO, dict) and len(self.__B) > 0 and not( __HO_shape[1] == __B_shape[0] ): raise ValueError("Shape characteristic of observation operator (H) \"%s\" and a priori errors covariance matrix (B) \"%s\" are incompatible."%(__HO_shape,__B_shape)) - if len(self.__HO) > 0 and not(type(self.__HO) is type({})) and len(self.__R) > 0 and not( __HO_shape[0] == __R_shape[1] ): + if len(self.__HO) > 0 and not isinstance(self.__HO, dict) and len(self.__R) > 0 and not( __HO_shape[0] == __R_shape[1] ): raise ValueError("Shape characteristic of observation operator (H) \"%s\" and observation errors covariance matrix (R) \"%s\" are incompatible."%(__HO_shape,__R_shape)) # if self.__B is not None and len(self.__B) > 0 and not( __B_shape[1] == max(__Xb_shape) ): @@ -1220,10 +1223,10 @@ class AlgorithmAndParameters(object): if self.__R is not None and len(self.__R) > 0 and not( __R_shape[1] == max(__Y_shape) ): raise ValueError("Shape characteristic of observation errors covariance matrix (R) \"%s\" and observation (Y) \"%s\" are incompatible."%(__R_shape,__Y_shape)) # - if self.__EM is not None and len(self.__EM) > 0 and not(type(self.__EM) is type({})) and not( __EM_shape[1] == max(__Xb_shape) ): + if self.__EM is not None and len(self.__EM) > 0 and not isinstance(self.__EM, dict) and not( __EM_shape[1] == max(__Xb_shape) ): raise ValueError("Shape characteristic of evolution model (EM) \"%s\" and state (X) \"%s\" are incompatible."%(__EM_shape,__Xb_shape)) # - if self.__CM is not None and len(self.__CM) > 0 and not(type(self.__CM) is type({})) and not( __CM_shape[1] == max(__U_shape) ): + if self.__CM is not None and len(self.__CM) > 0 and not isinstance(self.__CM, dict) and not( __CM_shape[1] == max(__U_shape) ): raise ValueError("Shape characteristic of control model (CM) \"%s\" and control (U) \"%s\" are incompatible."%(__CM_shape,__U_shape)) # if ("Bounds" in self.__P) \ @@ -1354,14 +1357,14 @@ class State(object): self.size = self.__V.size elif __Series is not None: self.__is_series = True - if type(__Series) in (tuple, list, numpy.ndarray, numpy.matrix): + if isinstance(__Series, (tuple, list, numpy.ndarray, numpy.matrix)): self.__V = Persistence.OneVector(self.__name, basetype=numpy.matrix) for member in __Series: self.__V.store( numpy.matrix( numpy.asmatrix(member).A1, numpy.float ).T ) import sys ; sys.stdout.flush() else: self.__V = __Series - if type(self.__V.shape) in (tuple, list): + if isinstance(self.__V.shape, (tuple, list)): self.shape = self.__V.shape else: self.shape = self.__V.shape() @@ -1490,6 +1493,8 @@ class Covariance(object): def __validate(self): "Validation" + if self.__C is None: + raise UnboundLocalError("%s covariance matrix value has not been set!"%(self.__name,)) if self.ismatrix() and min(self.shape) != max(self.shape): raise ValueError("The given matrix for %s is not a square one, its shape is %s. Please check your matrix input."%(self.__name,self.shape)) if self.isobject() and min(self.shape) != max(self.shape): @@ -1658,19 +1663,14 @@ class Covariance(object): "x.__mul__(y) <==> x*y" if self.ismatrix() and isinstance(other,numpy.matrix): return self.__C * other - elif self.ismatrix() and (isinstance(other,numpy.ndarray) \ - or isinstance(other,list) \ - or isinstance(other,tuple)): + elif self.ismatrix() and isinstance(other, (list, numpy.ndarray, tuple)): if numpy.ravel(other).size == self.shape[1]: # Vecteur return self.__C * numpy.asmatrix(numpy.ravel(other)).T elif numpy.asmatrix(other).shape[0] == self.shape[1]: # Matrice return self.__C * numpy.asmatrix(other) else: raise ValueError("operands could not be broadcast together with shapes %s %s in %s matrix"%(self.shape,numpy.asmatrix(other).shape,self.__name)) - elif self.isvector() and (isinstance(other,numpy.matrix) \ - or isinstance(other,numpy.ndarray) \ - or isinstance(other,list) \ - or isinstance(other,tuple)): + elif self.isvector() and isinstance(other, (list, numpy.matrix, numpy.ndarray, tuple)): if numpy.ravel(other).size == self.shape[1]: # Vecteur return numpy.asmatrix(self.__C * numpy.ravel(other)).T elif numpy.asmatrix(other).shape[0] == self.shape[1]: # Matrice @@ -1679,9 +1679,7 @@ class Covariance(object): raise ValueError("operands could not be broadcast together with shapes %s %s in %s matrix"%(self.shape,numpy.ravel(other).shape,self.__name)) elif self.isscalar() and isinstance(other,numpy.matrix): return self.__C * other - elif self.isscalar() and (isinstance(other,numpy.ndarray) \ - or isinstance(other,list) \ - or isinstance(other,tuple)): + elif self.isscalar() and isinstance(other, (list, numpy.ndarray, tuple)): if len(numpy.asarray(other).shape) == 1 or numpy.asarray(other).shape[1] == 1 or numpy.asarray(other).shape[0] == 1: return self.__C * numpy.asmatrix(numpy.ravel(other)).T else: @@ -1732,14 +1730,16 @@ class CaseLogger(object): """ Conservation des commandes de creation d'un cas """ - def __init__(self, __name="", __objname="case", __addViewers={}, __addLoaders={}): + def __init__(self, __name="", __objname="case", __addViewers=None, __addLoaders=None): self.__name = str(__name) self.__objname = str(__objname) self.__logSerie = [] self.__switchoff = False - self.__viewers = self.__loaders = {"TUI":_TUIViewer} - self.__viewers.update(__addViewers) - self.__loaders.update(__addLoaders) + self.__viewers = self.__loaders = {"TUI":_TUIViewer, "DIC":_DICViewer} + if __addViewers is not None: + self.__viewers.update(dict(__addViewers)) + if __addLoaders is not None: + self.__loaders.update(dict(__addLoaders)) def register(self, __command=None, __keys=None, __local=None, __pre=None, __switchoff=False): "Enregistrement d'une commande individuelle" @@ -1882,6 +1882,182 @@ class _TUIViewer(GenericCaseViewer): exec(__content) return 0 +class _DICViewer(GenericCaseViewer): + """ + Etablissement des commandes de creation d'un cas DIC + """ + def __init__(self, __name="", __objname="case", __content=None): + "Initialisation et enregistrement de l'entete" + GenericCaseViewer.__init__(self, __name, __objname, __content) + self._addLine("# -*- coding: utf-8 -*-") + self._addLine("#\n# Input for ADAO converter to YACS\n#") + self._addLine("from numpy import array, matrix") + self._addLine("#") + self._addLine("study_config = {}") + self._addLine("study_config['StudyType'] = 'ASSIMILATION_STUDY'") + self._addLine("study_config['Name'] = '%s'"%self._name) + self._addLine("observers = {}") + self._addLine("study_config['Observers'] = observers") + self._addLine("#") + self._addLine("inputvariables_config = {}") + self._addLine("inputvariables_config['Order'] =['adao_default']") + self._addLine("inputvariables_config['adao_default'] = -1") + self._addLine("study_config['InputVariables'] = inputvariables_config") + self._addLine("#") + self._addLine("outputvariables_config = {}") + self._addLine("outputvariables_config['Order'] = ['adao_default']") + self._addLine("outputvariables_config['adao_default'] = -1") + self._addLine("study_config['OutputVariables'] = outputvariables_config") + if __content is not None: + for command in __content: + self._append(*command) + def _append(self, __command=None, __keys=None, __local=None, __pre=None, __switchoff=False): + "Transformation d'une commande individuelle en un enregistrement" + if __command == "set": __command = __local["Concept"] + else: __command = __command.replace("set", "", 1) + # + __text = None + if __command in (None, 'execute', 'executePythonScheme', 'executeYACSScheme', 'get'): + return + elif __command in ['Debug', 'setDebug']: + __text = "#\nstudy_config['Debug'] = '1'" + elif __command in ['NoDebug', 'setNoDebug']: + __text = "#\nstudy_config['Debug'] = '0'" + elif __command in ['Observer', 'setObserver']: + __obs = __local['Variable'] + self._numobservers += 1 + __text = "#\n" + __text += "observers['%s'] = {}\n"%__obs + if __local['String'] is not None: + __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'String') + __text += "observers['%s']['String'] = \"\"\"%s\"\"\"\n"%(__obs, __local['String']) + if __local['Script'] is not None: + __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'Script') + __text += "observers['%s']['Script'] = \"%s\"\n"%(__obs, __local['Script']) + if __local['Template'] is not None and __local['Template'] in Templates.ObserverTemplates: + __text += "observers['%s']['nodetype'] = '%s'\n"%(__obs, 'String') + __text += "observers['%s']['String'] = \"\"\"%s\"\"\"\n"%(__obs, Templates.ObserverTemplates[__local['Template']]) + if __local['Info'] is not None: + __text += "observers['%s']['info'] = \"\"\"%s\"\"\"\n"%(__obs, __local['Info']) + else: + __text += "observers['%s']['info'] = \"\"\"%s\"\"\"\n"%(__obs, __obs) + __text += "observers['%s']['number'] = %s"%(__obs, self._numobservers) + elif __local is not None: # __keys is not None and + numpy.set_printoptions(precision=15,threshold=1000000,linewidth=1000*15) + __text = "#\n" + __text += "%s_config = {}\n"%__command + if 'self' in __local: __local.pop('self') + __to_be_removed = [] + for __k,__v in __local.items(): + if __v is None: __to_be_removed.append(__k) + for __k in __to_be_removed: + __local.pop(__k) + for __k,__v in __local.items(): + if __k == "Concept": continue + if __k in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix','OneFunction','ThreeFunctions'] and 'Script' in __local: continue + if __k == 'Algorithm': + __text += "study_config['Algorithm'] = %s\n"%(repr(__v)) + elif __k == 'Script': + __k = 'Vector' + __f = 'Script' + __v = "'"+repr(__v)+"'" + for __lk in ['ScalarSparseMatrix','DiagonalSparseMatrix','Matrix']: + if __lk in __local and __local[__lk]: __k = __lk + if __command == "AlgorithmParameters": __k = "Dict" + if 'OneFunction' in __local and __local['OneFunction']: + __text += "%s_ScriptWithOneFunction = {}\n"%(__command,) + __text += "%s_ScriptWithOneFunction['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"%(__command,) + __text += "%s_ScriptWithOneFunction['Script'] = {}\n"%(__command,) + __text += "%s_ScriptWithOneFunction['Script']['Direct'] = %s\n"%(__command,__v) + __text += "%s_ScriptWithOneFunction['Script']['Tangent'] = %s\n"%(__command,__v) + __text += "%s_ScriptWithOneFunction['Script']['Adjoint'] = %s\n"%(__command,__v) + __text += "%s_ScriptWithOneFunction['DifferentialIncrement'] = 1e-06\n"%(__command,) + __text += "%s_ScriptWithOneFunction['CenteredFiniteDifference'] = 0\n"%(__command,) + __k = 'Function' + __f = 'ScriptWithOneFunction' + __v = '%s_ScriptWithOneFunction'%(__command,) + if 'ThreeFunctions' in __local and __local['ThreeFunctions']: + __text += "%s_ScriptWithFunctions = {}\n"%(__command,) + __text += "%s_ScriptWithFunctions['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"%(__command,) + __text += "%s_ScriptWithFunctions['Script'] = {}\n"%(__command,) + __text += "%s_ScriptWithFunctions['Script']['Direct'] = %s\n"%(__command,__v) + __text += "%s_ScriptWithFunctions['Script']['Tangent'] = %s\n"%(__command,__v) + __text += "%s_ScriptWithFunctions['Script']['Adjoint'] = %s\n"%(__command,__v) + __k = 'Function' + __f = 'ScriptWithFunctions' + __v = '%s_ScriptWithFunctions'%(__command,) + __text += "%s_config['Type'] = '%s'\n"%(__command,__k) + __text += "%s_config['From'] = '%s'\n"%(__command,__f) + __text += "%s_config['Data'] = %s\n"%(__command,__v) + __text = __text.replace("''","'") + elif __k in ('Stored', 'Checked'): + if bool(__v): + __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v))) + elif __k in ('AvoidRC', 'noDetails'): + if not bool(__v): + __text += "%s_config['%s'] = '%s'\n"%(__command,__k,int(bool(__v))) + else: + if __k == 'Parameters': __k = "Dict" + if isinstance(__v,Persistence.Persistence): __v = __v.values() + if callable(__v): __text = self._missing%__v.__name__+__text + if isinstance(__v,dict): + for val in __v.values(): + if callable(val): __text = self._missing%val.__name__+__text + __text += "%s_config['Type'] = '%s'\n"%(__command,__k) + __text += "%s_config['From'] = '%s'\n"%(__command,'String') + __text += "%s_config['Data'] = \"\"\"%s\"\"\"\n"%(__command,repr(__v)) + __text += "study_config['%s'] = %s_config"%(__command,__command) + numpy.set_printoptions(precision=8,threshold=1000,linewidth=75) + if __switchoff: + self._switchoff = True + if __text is not None: self._addLine(__text) + if not __switchoff: + self._switchoff = False + def _finalize(self): + self.__loadVariablesByScript() + self._addLine("#") + self._addLine("Analysis_config = {}") + self._addLine("Analysis_config['From'] = 'String'") + self._addLine("Analysis_config['Data'] = \"\"\"import numpy") + self._addLine("xa=numpy.ravel(ADD.get('Analysis')[-1])") + self._addLine("print 'Analysis:',xa\"\"\"") + self._addLine("study_config['UserPostAnalysis'] = Analysis_config") + def __loadVariablesByScript(self): + __ExecVariables = {} # Necessaire pour recuperer la variable + exec("\n".join(self._lineSerie), __ExecVariables) + study_config = __ExecVariables['study_config'] + self.__hasAlgorithm = bool(study_config['Algorithm']) + if not self.__hasAlgorithm and \ + "AlgorithmParameters" in study_config and \ + isinstance(study_config['AlgorithmParameters'], dict) and \ + "From" in study_config['AlgorithmParameters'] and \ + "Data" in study_config['AlgorithmParameters'] and \ + study_config['AlgorithmParameters']['From'] == 'Script': + __asScript = study_config['AlgorithmParameters']['Data'] + __var = ImportFromScript(__asScript).getvalue( "Algorithm" ) + __text = "#\nstudy_config['Algorithm'] = '%s'"%(__var,) + self._addLine(__text) + if self.__hasAlgorithm and \ + "AlgorithmParameters" in study_config and \ + isinstance(study_config['AlgorithmParameters'], dict) and \ + "From" not in study_config['AlgorithmParameters'] and \ + "Data" not in study_config['AlgorithmParameters']: + __text = "#\n" + __text += "AlgorithmParameters_config['Type'] = 'Dict'\n" + __text += "AlgorithmParameters_config['From'] = 'String'\n" + __text += "AlgorithmParameters_config['Data'] = '{}'\n" + self._addLine(__text) + del study_config + +class _XMLViewer(GenericCaseViewer): + """ + Etablissement des commandes de creation d'un cas XML + """ + def __init__(self, __name="", __objname="case", __content=None): + "Initialisation et enregistrement de l'entete" + GenericCaseViewer.__init__(self, __name, __objname, __content) + raise NotImplementedError() + # ============================================================================== class ImportFromScript(object): """ diff --git a/src/daComposant/daCore/ExtendedLogging.py b/src/daComposant/daCore/ExtendedLogging.py index 7f0fc5d..aecc05b 100644 --- a/src/daComposant/daCore/ExtendedLogging.py +++ b/src/daComposant/daCore/ExtendedLogging.py @@ -76,6 +76,8 @@ __all__ = [] import os import sys import logging +import functools +import time from daCore import PlatformInfo LOGFILE = os.path.join(os.path.abspath(os.curdir),"AssimilationStudy.log") @@ -145,6 +147,18 @@ class ExtendedLogging(object): """ return logging.getLevelName( logging.getLogger().getEffectiveLevel() ) +# ============================================================================== +def logtimer(f): + @functools.wraps(f) + def wrapper(*args, **kwargs): + start = time.clock() # time.time() + result = f(*args, **kwargs) + end = time.clock() # time.time() + msg = 'TIMER Durée elapsed de la fonction utilisateur "{}": {:.3f}s' + logging.debug(msg.format(f.__name__, end-start)) + return result + return wrapper + # ============================================================================== if __name__ == "__main__": print('\n AUTODIAGNOSTIC \n') diff --git a/src/daComposant/daCore/PlatformInfo.py b/src/daComposant/daCore/PlatformInfo.py index 381ef5b..fde6791 100644 --- a/src/daComposant/daCore/PlatformInfo.py +++ b/src/daComposant/daCore/PlatformInfo.py @@ -39,7 +39,8 @@ __author__ = "Jean-Philippe ARGAUD" __all__ = [] -import os, sys +import os +import sys # ============================================================================== class PlatformInfo(object): @@ -76,47 +77,47 @@ class PlatformInfo(object): def getScipyVersion(self): "Retourne la version de scipy disponible" - try: - import scipy.version - return scipy.version.version - except ImportError: - return "0.0.0" + if has_scipy: + __version = scipy.version.version + else: + __version = "0.0.0" + return __version def getMatplotlibVersion(self): "Retourne la version de matplotlib disponible" - try: - import matplotlib - return matplotlib.__version__ - except ImportError: - return "0.0.0" + if has_matplotlib: + __version = matplotlib.__version__ + else: + __version = "0.0.0" + return __version def getGnuplotVersion(self): "Retourne la version de gnuplotpy disponible" - try: - import Gnuplot - return Gnuplot.__version__ - except ImportError: - return "0.0" + if has_gnuplot: + __version = Gnuplot.__version__ + else: + __version = "0.0" + return __version def getSphinxVersion(self): "Retourne la version de sphinx disponible" - try: - import sphinx - return sphinx.__version__ - except ImportError: - return "0.0.0" + if has_sphinx: + __version = sphinx.__version__ + else: + __version = "0.0.0" + return __version def getNloptVersion(self): "Retourne la version de nlopt disponible" - try: - import nlopt - return "%s.%s.%s"%( + if has_nlopt: + __version = "%s.%s.%s"%( nlopt.version_major(), nlopt.version_minor(), nlopt.version_bugfix(), ) - except ImportError: - return "0.0.0" + else: + __version = "0.0.0" + return __version def getCurrentMemorySize(self): "Retourne la taille mémoire courante utilisée" @@ -126,9 +127,9 @@ class PlatformInfo(object): "Retourne la precision maximale flottante pour Numpy" import numpy try: - x = numpy.array([1.,], dtype='float128') + numpy.array([1.,], dtype='float128') mfp = 'float128' - except: + except Exception: mfp = 'float64' return mfp @@ -147,6 +148,7 @@ class PlatformInfo(object): # ============================================================================== try: import scipy + import scipy.version import scipy.optimize has_scipy = True except ImportError: @@ -176,20 +178,9 @@ try: except ImportError: has_nlopt = False -if "ROOT_SALOME" in os.environ: - has_salome = True -else: - has_salome = False - -if "YACS_ROOT_DIR" in os.environ: - has_yacs = True -else: - has_yacs = False - -if "ADAO_ROOT_DIR" in os.environ: - has_adao = True -else: - has_adao = False +has_salome = bool( "ROOT_SALOME" in os.environ ) +has_yacs = bool( "YACS_ROOT_DIR" in os.environ ) +has_adao = bool( "ADAO_ROOT_DIR" in os.environ ) # ============================================================================== def uniq(sequence): -- 2.39.2