1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2019 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 has_key(self, name):
63 # "D.has_key(k) -> True if D has a key k, else False"
64 # return name in self.__values
66 def __contains__(self, name):
67 "D.__contains__(k) -> True if D has a key k, else False"
68 return name in self.__values
71 "x.__len__() <==> len(x)"
72 return len(self.__values)
74 def __getitem__(self, name=None ):
75 "x.__getitem__(y) <==> x[y]"
76 return self.__values[name]['content']
78 def getdoc(self, name = None, lang = "fr_FR"):
79 "D.getdoc(k, l) -> Return documentation of key k in language l"
80 if lang not in self.__values[name]: lang = self.__preferedLanguage
81 return self.__values[name][lang]
83 def keys_in_presentation_order(self):
84 "D.keys_in_presentation_order() -> list of D's keys in presentation order"
87 __orders.append( self.__values[k]['order'] )
88 __reorder = numpy.array(__orders).argsort()
89 return list(numpy.array(self.keys())[__reorder])
91 # ==============================================================================
92 ObserverTemplates = TemplateStorage()
94 ObserverTemplates.store(
95 name = "ValuePrinter",
96 content = """print(str(info)+" "+str(var[-1]))""",
97 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable",
98 en_EN = "Print on standard output the current value of the variable",
101 ObserverTemplates.store(
102 name = "ValueAndIndexPrinter",
103 content = """print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))""",
104 fr_FR = "Imprime sur la sortie standard la valeur courante de la variable, en ajoutant son index",
105 en_EN = "Print on standard output the current value of the variable, adding its index",
108 ObserverTemplates.store(
109 name = "ValueSeriePrinter",
110 content = """print(str(info)+" "+str(var[:]))""",
111 fr_FR = "Imprime sur la sortie standard la série des valeurs de la variable",
112 en_EN = "Print on standard output the value series of the variable",
115 ObserverTemplates.store(
117 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)""",
118 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",
119 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",
122 ObserverTemplates.store(
123 name = "ValueSerieSaver",
124 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)""",
125 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",
126 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",
129 ObserverTemplates.store(
130 name = "ValuePrinterAndSaver",
131 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)""",
132 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",
133 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",
136 ObserverTemplates.store(
137 name = "ValueIndexPrinterAndSaver",
138 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)""",
139 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",
140 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",
143 ObserverTemplates.store(
144 name = "ValueSeriePrinterAndSaver",
145 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)""",
146 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",
147 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",
150 ObserverTemplates.store(
151 name = "ValueGnuPlotter",
152 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' ) )""",
153 fr_FR = "Affiche graphiquement avec Gnuplot la valeur courante de la variable",
154 en_EN = "Graphically plot with Gnuplot the current value of the variable",
157 ObserverTemplates.store(
158 name = "ValueSerieGnuPlotter",
159 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' ) )""",
160 fr_FR = "Affiche graphiquement avec Gnuplot la série des valeurs de la variable",
161 en_EN = "Graphically plot with Gnuplot the value series of the variable",
164 ObserverTemplates.store(
165 name = "ValuePrinterAndGnuPlotter",
166 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' ) )""",
167 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la valeur courante de la variable",
168 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable",
171 ObserverTemplates.store(
172 name = "ValueSeriePrinterAndGnuPlotter",
173 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' ) )""",
174 fr_FR = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la série des valeurs de la variable",
175 en_EN = "Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable",
178 ObserverTemplates.store(
179 name = "ValuePrinterSaverAndGnuPlotter",
180 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' ) )""",
181 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",
182 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",
185 ObserverTemplates.store(
186 name = "ValueSeriePrinterSaverAndGnuPlotter",
187 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' ) )""",
188 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",
189 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",
192 ObserverTemplates.store(
194 content = """import numpy\nprint(str(info)+" "+str(numpy.nanmean(var[-1])))""",
195 fr_FR = "Imprime sur la sortie standard la moyenne de la valeur courante de la variable",
196 en_EN = "Print on standard output the mean of the current value of the variable",
199 ObserverTemplates.store(
200 name = "ValueStandardError",
201 content = """import numpy\nprint(str(info)+" "+str(numpy.nanstd(var[-1])))""",
202 fr_FR = "Imprime sur la sortie standard l'écart-type de la valeur courante de la variable",
203 en_EN = "Print on standard output the standard error of the current value of the variable",
206 ObserverTemplates.store(
207 name = "ValueVariance",
208 content = """import numpy\nprint(str(info)+" "+str(numpy.nanvar(var[-1])))""",
209 fr_FR = "Imprime sur la sortie standard la variance de la valeur courante de la variable",
210 en_EN = "Print on standard output the variance of the current value of the variable",
213 ObserverTemplates.store(
214 name = "ValueL2Norm",
215 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+" "+str(float( numpy.linalg.norm(v) )))""",
216 fr_FR = "Imprime sur la sortie standard la norme L2 de la valeur courante de la variable",
217 en_EN = "Print on standard output the L2 norm of the current value of the variable",
220 ObserverTemplates.store(
222 content = """import numpy\nv = numpy.ravel( var[-1] )\nprint(str(info)+" "+str(float( numpy.sqrt((1./v.size)*numpy.dot(v,v)) )))""",
223 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",
224 en_EN = "Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable",
228 # ==============================================================================
229 if __name__ == "__main__":
230 print('\n AUTODIAGNOSTIC\n')