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 def __init__( self, language = "fr_FR" ):
38 self.__preferedLanguage = language
42 def store( self, name = None, content = None, fr_FR = "", en_EN = "", order = "next" ):
43 "D.store(k, c, fr_FR, en_EN, o) -> Store template k and its main characteristics"
44 if name is None or content is None:
45 raise ValueError("To be consistent, the storage of a template must provide a name and a content.")
49 self.__order = int(order)
50 self.__values[str(name)] = {
51 'content': str(content),
54 'order' : int(self.__order),
58 "D.keys() -> list of D's keys"
59 __keys = sorted(self.__values.keys())
62 def __contains__(self, name):
63 "D.__contains__(k) -> True if D has a key k, else False"
64 return name in self.__values
67 "x.__len__() <==> len(x)"
68 return len(self.__values)
70 def __getitem__(self, name=None ):
71 "x.__getitem__(y) <==> x[y]"
72 return self.__values[name]['content']
74 def getdoc(self, name = None, lang = "fr_FR"):
75 "D.getdoc(k, l) -> Return documentation of key k in language l"
76 if lang not in self.__values[name]: lang = self.__preferedLanguage
77 return self.__values[name][lang]
79 def keys_in_presentation_order(self):
80 "D.keys_in_presentation_order() -> list of D's keys in presentation order"
83 __orders.append( self.__values[k]['order'] )
84 __reorder = numpy.array(__orders).argsort()
85 return list(numpy.array(self.keys())[__reorder])
87 # ==============================================================================
88 ObserverTemplates = TemplateStorage()
90 ObserverTemplates.store(
91 name = "ValuePrinter",
92 content = """print(str(info)+" "+str(var[-1]))""",
93 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable",
94 en_EN = "Print on standard output the current value of the variable",
97 ObserverTemplates.store(
98 name = "ValueAndIndexPrinter",
99 content = """print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))""",
100 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable, en ajoutant son index",
101 en_EN = "Print on standard output the current value of the variable, adding its index",
104 ObserverTemplates.store(
105 name = "ValueSeriePrinter",
106 content = """print(str(info)+" "+str(var[:]))""",
107 fr_FR = "Imprime sur la sortie standard la série des valeurs de la variable",
108 en_EN = "Print on standard output the value series of the variable",
111 ObserverTemplates.store(
113 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)""",
114 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",
115 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",
118 ObserverTemplates.store(
119 name = "ValueSerieSaver",
120 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)""",
121 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",
122 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",
125 ObserverTemplates.store(
126 name = "ValuePrinterAndSaver",
127 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)""",
128 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",
129 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",
132 ObserverTemplates.store(
133 name = "ValueIndexPrinterAndSaver",
134 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)""",
135 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",
136 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",
139 ObserverTemplates.store(
140 name = "ValueSeriePrinterAndSaver",
141 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)""",
142 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",
143 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",
146 ObserverTemplates.store(
147 name = "ValueGnuPlotter",
148 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' ) )""",
149 fr_FR = "Affiche graphiquement avec Gnuplot la valeur courante de la variable",
150 en_EN = "Graphically plot with Gnuplot the current value of the variable",
153 ObserverTemplates.store(
154 name = "ValueSerieGnuPlotter",
155 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' ) )""",
156 fr_FR = "Affiche graphiquement avec Gnuplot la série des valeurs de la variable",
157 en_EN = "Graphically plot with Gnuplot the value series of the variable",
160 ObserverTemplates.store(
161 name = "ValuePrinterAndGnuPlotter",
162 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' ) )""",
163 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la valeur courante de la variable",
164 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable",
167 ObserverTemplates.store(
168 name = "ValueSeriePrinterAndGnuPlotter",
169 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' ) )""",
170 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la série des valeurs de la variable",
171 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable",
174 ObserverTemplates.store(
175 name = "ValuePrinterSaverAndGnuPlotter",
176 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' ) )""",
177 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",
178 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",
181 ObserverTemplates.store(
182 name = "ValueSeriePrinterSaverAndGnuPlotter",
183 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' ) )""",
184 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",
185 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",
188 ObserverTemplates.store(
190 content = """import numpy\nprint(str(info)+" "+str(numpy.nanmean(var[-1])))""",
191 fr_FR = "Imprime sur la sortie standard la moyenne de la valeur courante de la variable",
192 en_EN = "Print on standard output the mean of the current value of the variable",
195 ObserverTemplates.store(
196 name = "ValueStandardError",
197 content = """import numpy\nprint(str(info)+" "+str(numpy.nanstd(var[-1])))""",
198 fr_FR = "Imprime sur la sortie standard l'écart-type de la valeur courante de la variable",
199 en_EN = "Print on standard output the standard error of the current value of the variable",
202 ObserverTemplates.store(
203 name = "ValueVariance",
204 content = """import numpy\nprint(str(info)+" "+str(numpy.nanvar(var[-1])))""",
205 fr_FR = "Imprime sur la sortie standard la variance de la valeur courante de la variable",
206 en_EN = "Print on standard output the variance of the current value of the variable",
209 ObserverTemplates.store(
210 name = "ValueL2Norm",
211 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+" "+str(float( numpy.linalg.norm(v) )))""",
212 fr_FR = "Imprime sur la sortie standard la norme L2 de la valeur courante de la variable",
213 en_EN = "Print on standard output the L2 norm of the current value of the variable",
216 ObserverTemplates.store(
218 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+" "+str(float( numpy.sqrt((1./v.size)*numpy.dot(v,v)) )))""",
219 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",
220 en_EN = "Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable",
224 # ==============================================================================
225 UserPostAnalysisTemplates = TemplateStorage()
227 UserPostAnalysisTemplates.store(
228 name = "AnalysisPrinter",
229 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')[-1]\nprint('Analysis',xa)""",
230 fr_FR = "Imprime sur la sortie standard la valeur optimale",
231 en_EN = "Print on standard output the optimal value",
234 UserPostAnalysisTemplates.store(
235 name = "AnalysisSaver",
236 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)""",
237 fr_FR = "Enregistre la valeur optimale dans un fichier du répertoire '/tmp' nommé 'analysis.txt'",
238 en_EN = "Save the optimal value in a file of the '/tmp' directory named 'analysis.txt'",
241 UserPostAnalysisTemplates.store(
242 name = "AnalysisPrinterAndSaver",
243 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)""",
244 fr_FR = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier du répertoire '/tmp', la valeur optimale",
245 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the optimal value",
248 UserPostAnalysisTemplates.store(
249 name = "AnalysisSeriePrinter",
250 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')\nprint('Analysis',xa)""",
251 fr_FR = "Imprime sur la sortie standard la série des valeurs optimales",
252 en_EN = "Print on standard output the optimal value series",
255 UserPostAnalysisTemplates.store(
256 name = "AnalysisSerieSaver",
257 content = """print('# Post-analysis')\nimport numpy\nxa=ADD.get('Analysis')\nf='/tmp/analysis.txt'\nprint('Analysis saved in \"%s\"'%f)\nnumpy.savetxt(f,xa)""",
258 fr_FR = "Enregistre la série des valeurs optimales dans un fichier du répertoire '/tmp' nommé 'analysis.txt'",
259 en_EN = "Save the optimal value series in a file of the '/tmp' directory named 'analysis.txt'",
262 UserPostAnalysisTemplates.store(
263 name = "AnalysisSeriePrinterAndSaver",
264 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)""",
265 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",
266 en_EN = "Print on standard output and, in the same time save in a file of the '/tmp' directory, the optimal value series",
270 # ==============================================================================
271 if __name__ == "__main__":
272 print('\n AUTODIAGNOSTIC\n')