Salome HOME
Updating copyright date information
[modules/adao.git] / src / daComposant / daCore / Templates.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2022 EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 """
24     Modèles généraux pour les observers, le post-processing.
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27 __all__ = ["ObserverTemplates"]
28
29 import numpy
30
31 # ==============================================================================
32 class TemplateStorage(object):
33     """
34     Classe générale de stockage de type dictionnaire étendu
35     (Template)
36     """
37     def __init__( self, language = "fr_FR" ):
38         self.__preferedLanguage = language
39         self.__values           = {}
40         self.__order            = -1
41
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.")
46         if order == "next":
47             self.__order += 1
48         else:
49             self.__order = int(order)
50         self.__values[str(name)] = {
51             'content': str(content),
52             'fr_FR'  : str(fr_FR),
53             'en_EN'  : str(en_EN),
54             'order'  : int(self.__order),
55             }
56
57     def keys(self):
58         "D.keys() -> list of D's keys"
59         __keys = sorted(self.__values.keys())
60         return __keys
61
62     def __contains__(self, name):
63         "D.__contains__(k) -> True if D has a key k, else False"
64         return name in self.__values
65
66     def __len__(self):
67         "x.__len__() <==> len(x)"
68         return len(self.__values)
69
70     def __getitem__(self, name=None ):
71         "x.__getitem__(y) <==> x[y]"
72         return self.__values[name]['content']
73
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]
78
79     def keys_in_presentation_order(self):
80         "D.keys_in_presentation_order() -> list of D's keys in presentation order"
81         __orders = []
82         for k in self.keys():
83             __orders.append( self.__values[k]['order'] )
84         __reorder = numpy.array(__orders).argsort()
85         return list(numpy.array(self.keys())[__reorder])
86
87 # ==============================================================================
88 ObserverTemplates = TemplateStorage()
89
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",
95     order   = "next",
96     )
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",
102     order   = "next",
103     )
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",
109     order   = "next",
110     )
111 ObserverTemplates.store(
112     name    = "ValueSaver",
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",
116     order   = "next",
117     )
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",
123     order   = "next",
124     )
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",
130     order   = "next",
131     )
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",
137     order   = "next",
138     )
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",
144     order   = "next",
145     )
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",
151     order   = "next",
152     )
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",
158     order   = "next",
159     )
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",
165     order   = "next",
166     )
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",
172     order   = "next",
173     )
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",
179     order   = "next",
180     )
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",
186     order   = "next",
187     )
188 ObserverTemplates.store(
189     name    = "ValueMean",
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",
193     order   = "next",
194     )
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",
200     order   = "next",
201     )
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",
207     order   = "next",
208     )
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",
214     order   = "next",
215     )
216 ObserverTemplates.store(
217     name    = "ValueRMS",
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",
221     order   = "next",
222     )
223
224 # ==============================================================================
225 UserPostAnalysisTemplates = TemplateStorage()
226
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",
232     order   = "next",
233     )
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'",
239     order   = "next",
240     )
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",
246     order   = "next",
247     )
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",
253     order   = "next",
254     )
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'",
260     order   = "next",
261     )
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",
267     order   = "next",
268     )
269
270 # ==============================================================================
271 if __name__ == "__main__":
272     print('\n AUTODIAGNOSTIC\n')