1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2022 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 Modèles généraux pour les observers, le post-processing.
26 __author__ = "Jean-Philippe ARGAUD"
27 __all__ = ["ObserverTemplates"]
31 # ==============================================================================
32 class TemplateStorage(object):
34 Classe générale de stockage de type dictionnaire étendu
37 __slots__ = ("__preferedLanguage", "__values", "__order")
39 def __init__( self, language = "fr_FR" ):
40 self.__preferedLanguage = language
44 def store( self, name = None, content = None, fr_FR = "", en_EN = "", order = "next" ):
45 "D.store(k, c, fr_FR, en_EN, o) -> Store template k and its main characteristics"
46 if name is None or content is None:
47 raise ValueError("To be consistent, the storage of a template must provide a name and a content.")
51 self.__order = int(order)
52 self.__values[str(name)] = {
53 'content': str(content),
56 'order' : int(self.__order),
60 "D.keys() -> list of D's keys"
61 __keys = sorted(self.__values.keys())
64 def __contains__(self, name):
65 "D.__contains__(k) -> True if D has a key k, else False"
66 return name in self.__values
69 "x.__len__() <==> len(x)"
70 return len(self.__values)
72 def __getitem__(self, name=None ):
73 "x.__getitem__(y) <==> x[y]"
74 return self.__values[name]['content']
76 def getdoc(self, name = None, lang = "fr_FR"):
77 "D.getdoc(k, l) -> Return documentation of key k in language l"
78 if lang not in self.__values[name]: lang = self.__preferedLanguage
79 return self.__values[name][lang]
81 def keys_in_presentation_order(self):
82 "D.keys_in_presentation_order() -> list of D's keys in presentation order"
85 __orders.append( self.__values[k]['order'] )
86 __reorder = numpy.array(__orders).argsort()
87 return list(numpy.array(self.keys())[__reorder])
89 # ==============================================================================
90 ObserverTemplates = TemplateStorage()
92 ObserverTemplates.store(
93 name = "ValuePrinter",
94 content = """print(str(info)+" "+str(var[-1]))""",
95 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable",
96 en_EN = "Print on standard output the current value of the variable",
99 ObserverTemplates.store(
100 name = "ValueAndIndexPrinter",
101 content = """print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))""",
102 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable, en ajoutant son index",
103 en_EN = "Print on standard output the current value of the variable, adding its index",
106 ObserverTemplates.store(
107 name = "ValueSeriePrinter",
108 content = """print(str(info)+" "+str(var[:]))""",
109 fr_FR = "Imprime sur la sortie standard la série des valeurs de la variable",
110 en_EN = "Print on standard output the value series of the variable",
113 ObserverTemplates.store(
115 content = """import numpy, re\nv=numpy.array(var[-1], ndmin=1)\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)""",
116 fr_FR = "Enregistre la valeur courante de la variable dans un fichier du répertoire '/tmp' nommé 'value...txt' selon le nom de la variable et l'étape d'enregistrement",
117 en_EN = "Save the current value of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step",
120 ObserverTemplates.store(
121 name = "ValueSerieSaver",
122 content = """import numpy, re\nv=numpy.array(var[:], ndmin=1)\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)""",
123 fr_FR = "Enregistre la série des valeurs de la variable dans un fichier du répertoire '/tmp' nommé 'value...txt' selon le nom de la variable et l'étape",
124 en_EN = "Save the value series of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step",
127 ObserverTemplates.store(
128 name = "ValuePrinterAndSaver",
129 content = """import numpy, re\nv=numpy.array(var[-1], ndmin=1)\nprint(str(info)+" "+str(v))\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)""",
130 fr_FR = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier du répertoire '/tmp', la valeur courante de la variable",
131 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the current value of the variable",
134 ObserverTemplates.store(
135 name = "ValueIndexPrinterAndSaver",
136 content = """import numpy, re\nv=numpy.array(var[-1], ndmin=1)\nprint(str(info)+(" index %i:"%(len(var)-1))+" "+str(v))\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)""",
137 fr_FR = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier du répertoire '/tmp', la valeur courante de la variable, en ajoutant son index",
138 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the current value of the variable, adding its index",
141 ObserverTemplates.store(
142 name = "ValueSeriePrinterAndSaver",
143 content = """import numpy, re\nv=numpy.array(var[:], ndmin=1)\nprint(str(info)+" "+str(v))\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)""",
144 fr_FR = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier du répertoire '/tmp', la série des valeurs de la variable",
145 en_EN = "Print on standard output and, in the same time, save in a file of the '/tmp' directory, the value series of the variable",
148 ObserverTemplates.store(
149 name = "ValueGnuPlotter",
150 content = """import numpy, Gnuplot\nv=numpy.array(var[-1], ndmin=1)\nglobal ifig, gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
151 fr_FR = "Affiche graphiquement avec Gnuplot la valeur courante de la variable",
152 en_EN = "Graphically plot with Gnuplot the current value of the variable",
155 ObserverTemplates.store(
156 name = "ValueSerieGnuPlotter",
157 content = """import numpy, Gnuplot\nv=numpy.array(var[:], ndmin=1)\nglobal ifig, gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
158 fr_FR = "Affiche graphiquement avec Gnuplot la série des valeurs de la variable",
159 en_EN = "Graphically plot with Gnuplot the value series of the variable",
162 ObserverTemplates.store(
163 name = "ValuePrinterAndGnuPlotter",
164 content = """print(str(info)+' '+str(var[-1]))\nimport numpy, Gnuplot\nv=numpy.array(var[-1], ndmin=1)\nglobal ifig,gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
165 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la valeur courante de la variable",
166 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable",
169 ObserverTemplates.store(
170 name = "ValueSeriePrinterAndGnuPlotter",
171 content = """print(str(info)+' '+str(var[:]))\nimport numpy, Gnuplot\nv=numpy.array(var[:], ndmin=1)\nglobal ifig,gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
172 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la série des valeurs de la variable",
173 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable",
176 ObserverTemplates.store(
177 name = "ValuePrinterSaverAndGnuPlotter",
178 content = """print(str(info)+' '+str(var[-1]))\nimport numpy, re\nv=numpy.array(var[-1], ndmin=1)\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)\nimport Gnuplot\nglobal ifig,gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
179 fr_FR = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier du répertoire '/tmp' et affiche graphiquement la valeur courante de la variable",
180 en_EN = "Print on standard output and, in the same, time save in a file of the '/tmp' directory and graphically plot the current value of the variable",
183 ObserverTemplates.store(
184 name = "ValueSeriePrinterSaverAndGnuPlotter",
185 content = """print(str(info)+' '+str(var[:]))\nimport numpy, re\nv=numpy.array(var[:], ndmin=1)\nglobal istep\ntry:\n istep+=1\nexcept:\n istep=0\nf='/tmp/value_%s_%05i.txt'%(info,istep)\nf=re.sub('\\s','_',f)\nprint('Value saved in \"%s\"'%f)\nnumpy.savetxt(f,v)\nimport Gnuplot\nglobal ifig,gp\ntry:\n ifig+=1\n gp('set style data lines')\nexcept:\n ifig=0\n gp=Gnuplot.Gnuplot(persist=1)\n gp('set style data lines')\ngp('set title \"%s (Figure %i)\"'%(info,ifig))\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )""",
186 fr_FR = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier du répertoire '/tmp' et affiche graphiquement la série des valeurs de la variable",
187 en_EN = "Print on standard output and, in the same, time save in a file of the '/tmp' directory and graphically plot the value series of the variable",
190 ObserverTemplates.store(
192 content = """import numpy\nprint(str(info)+' '+str(numpy.nanmean(var[-1])))""",
193 fr_FR = "Imprime sur la sortie standard la moyenne de la valeur courante de la variable",
194 en_EN = "Print on standard output the mean of the current value of the variable",
197 ObserverTemplates.store(
198 name = "ValueStandardError",
199 content = """import numpy\nprint(str(info)+' '+str(numpy.nanstd(var[-1])))""",
200 fr_FR = "Imprime sur la sortie standard l'écart-type de la valeur courante de la variable",
201 en_EN = "Print on standard output the standard error of the current value of the variable",
204 ObserverTemplates.store(
205 name = "ValueVariance",
206 content = """import numpy\nprint(str(info)+' '+str(numpy.nanvar(var[-1])))""",
207 fr_FR = "Imprime sur la sortie standard la variance de la valeur courante de la variable",
208 en_EN = "Print on standard output the variance of the current value of the variable",
211 ObserverTemplates.store(
212 name = "ValueL2Norm",
213 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+' '+str(float( numpy.linalg.norm(v) )))""",
214 fr_FR = "Imprime sur la sortie standard la norme L2 de la valeur courante de la variable",
215 en_EN = "Print on standard output the L2 norm of the current value of the variable",
218 ObserverTemplates.store(
220 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+' '+str(float( numpy.sqrt((1./v.size)*numpy.dot(v,v)) )))""",
221 fr_FR = "Imprime sur la sortie standard la racine de la moyenne des carrés (RMS), ou moyenne quadratique, de la valeur courante de la variable",
222 en_EN = "Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable",
226 # ==============================================================================
227 UserPostAnalysisTemplates = TemplateStorage()
229 UserPostAnalysisTemplates.store(
230 name = "AnalysisPrinter",
231 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')[-1]\nprint('Analysis',xa)""",
232 fr_FR = "Imprime sur la sortie standard la valeur optimale",
233 en_EN = "Print on standard output the optimal value",
236 UserPostAnalysisTemplates.store(
237 name = "AnalysisSaver",
238 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')[-1]\nf='/tmp/analysis.txt'\nprint('Analysis saved in \"%s\"'%f)\nnumpy.savetxt(f,xa)""",
239 fr_FR = "Enregistre la valeur optimale dans un fichier du répertoire '/tmp' nommé 'analysis.txt'",
240 en_EN = "Save the optimal value in a file of the '/tmp' directory named 'analysis.txt'",
243 UserPostAnalysisTemplates.store(
244 name = "AnalysisPrinterAndSaver",
245 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')[-1]\nprint('Analysis',xa)\nf='/tmp/analysis.txt'\nprint('Analysis saved in \"%s\"'%f)\nnumpy.savetxt(f,xa)""",
246 fr_FR = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier du répertoire '/tmp', la valeur optimale",
247 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the optimal value",
250 UserPostAnalysisTemplates.store(
251 name = "AnalysisSeriePrinter",
252 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')\nprint('Analysis',xa)""",
253 fr_FR = "Imprime sur la sortie standard la série des valeurs optimales",
254 en_EN = "Print on standard output the optimal value series",
257 UserPostAnalysisTemplates.store(
258 name = "AnalysisSerieSaver",
259 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')\nf='/tmp/analysis.txt'\nprint('Analysis saved in \"%s\"'%f)\nnumpy.savetxt(f,xa)""",
260 fr_FR = "Enregistre la série des valeurs optimales dans un fichier du répertoire '/tmp' nommé 'analysis.txt'",
261 en_EN = "Save the optimal value series in a file of the '/tmp' directory named 'analysis.txt'",
264 UserPostAnalysisTemplates.store(
265 name = "AnalysisSeriePrinterAndSaver",
266 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')\nprint('Analysis',xa)\nf='/tmp/analysis.txt'\nprint('Analysis saved in \"%s\"'%f)\nnumpy.savetxt(f,xa)""",
267 fr_FR = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier du répertoire '/tmp', la série des valeurs optimales",
268 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the optimal value series",
272 # ==============================================================================
273 if __name__ == "__main__":
274 print('\n AUTODIAGNOSTIC\n')