Salome HOME
Internal structures modification for Python 3 support
[modules/adao.git] / src / daComposant / daCore / Templates.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2017 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 = list(self.__values.keys())
60         __keys.sort()
61         return __keys
62
63     # def has_key(self, name):
64     #     "D.has_key(k) -> True if D has a key k, else False"
65     #     return name in self.__values
66
67     def __contains__(self, name):
68         "D.__contains__(k) -> True if D has a key k, else False"
69         return name in self.__values
70
71     def __len__(self):
72         "x.__len__() <==> len(x)"
73         return len(self.__values)
74
75     def __getitem__(self, name=None ):
76         "x.__getitem__(y) <==> x[y]"
77         return self.__values[name]['content']
78
79     def getdoc(self, name = None, lang = "fr_FR"):
80         "D.getdoc(k, l) -> Return documentation of key k in language l"
81         if lang not in self.__values[name]: lang = self.__preferedLanguage
82         return self.__values[name][lang]
83
84     def keys_in_presentation_order(self):
85         "D.keys_in_presentation_order() -> list of D's keys in presentation order"
86         __orders = []
87         for k in self.keys():
88             __orders.append( self.__values[k]['order'] )
89         __reorder = numpy.array(__orders).argsort()
90         return list(numpy.array(self.keys())[__reorder])
91
92 # ==============================================================================
93 ObserverTemplates = TemplateStorage()
94
95 ObserverTemplates.store(
96     name    = "ValuePrinter",
97     content = """print(str(info)+" "+str(var[-1]))""",
98     fr_FR   = "Imprime sur la sortie standard la valeur courante de la variable",
99     en_EN   = "Print on standard output the current value of the variable",
100     order   = "next",
101     )
102 ObserverTemplates.store(
103     name    = "ValueAndIndexPrinter",
104     content = """print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))""",
105     fr_FR   = "Imprime sur la sortie standard la valeur courante de la variable, en ajoutant son index",
106     en_EN   = "Print on standard output the current value of the variable, adding its index",
107     order   = "next",
108     )
109 ObserverTemplates.store(
110     name    = "ValueSeriePrinter",
111     content = """print(str(info)+" "+str(var[:]))""",
112     fr_FR   = "Imprime sur la sortie standard la série des valeurs de la variable",
113     en_EN   = "Print on standard output the value series of the variable",
114     order   = "next",
115     )
116 ObserverTemplates.store(
117     name    = "ValueSaver",
118     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)""",
119     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",
120     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",
121     order   = "next",
122     )
123 ObserverTemplates.store(
124     name    = "ValueSerieSaver",
125     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)""",
126     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",
127     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",
128     order   = "next",
129     )
130 ObserverTemplates.store(
131     name    = "ValuePrinterAndSaver",
132     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)""",
133     fr_FR   = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier, la valeur courante de la variable",
134     en_EN   = "Print on standard output and, in the same time save in a file, the current value of the variable",
135     order   = "next",
136     )
137 ObserverTemplates.store(
138     name    = "ValueIndexPrinterAndSaver",
139     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)""",
140     fr_FR   = "Imprime sur la sortie standard et, en même temps enregistre dans un fichier, la valeur courante de la variable, en ajoutant son index",
141     en_EN   = "Print on standard output and, in the same time save in a file, the current value of the variable, adding its index",
142     order   = "next",
143     )
144 ObserverTemplates.store(
145     name    = "ValueSeriePrinterAndSaver",
146     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)""",
147     fr_FR   = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier la série des valeurs de la variable",
148     en_EN   = "Print on standard output and, in the same time, save in a file the value series of the variable",
149     order   = "next",
150     )
151 ObserverTemplates.store(
152     name    = "ValueGnuPlotter",
153     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' ) )""",
154     fr_FR   = "Affiche graphiquement avec Gnuplot la valeur courante de la variable",
155     en_EN   = "Graphically plot with Gnuplot the current value of the variable",
156     order   = "next",
157     )
158 ObserverTemplates.store(
159     name    = "ValueSerieGnuPlotter",
160     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' ) )""",
161     fr_FR   = "Affiche graphiquement avec Gnuplot la série des valeurs de la variable",
162     en_EN   = "Graphically plot with Gnuplot the value series of the variable",
163     order   = "next",
164     )
165 ObserverTemplates.store(
166     name    = "ValuePrinterAndGnuPlotter",
167     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' ) )""",
168     fr_FR   = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la valeur courante de la variable",
169     en_EN   = "Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable",
170     order   = "next",
171     )
172 ObserverTemplates.store(
173     name    = "ValueSeriePrinterAndGnuPlotter",
174     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' ) )""",
175     fr_FR   = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la série des valeurs de la variable",
176     en_EN   = "Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable",
177     order   = "next",
178     )
179 ObserverTemplates.store(
180     name    = "ValuePrinterSaverAndGnuPlotter",
181     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' ) )""",
182     fr_FR   = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier et affiche graphiquement la valeur courante de la variable ",
183     en_EN   = "Print on standard output and, in the same, time save in a file and graphically plot the current value of the variable",
184     order   = "next",
185     )
186 ObserverTemplates.store(
187     name    = "ValueSeriePrinterSaverAndGnuPlotter",
188     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' ) )""",
189     fr_FR   = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier et affiche graphiquement la série des valeurs de la variable",
190     en_EN   = "Print on standard output and, in the same, time save in a file and graphically plot the value series of the variable",
191     order   = "next",
192     )
193 ObserverTemplates.store(
194     name    = "ValueMean",
195     content = """import numpy\nprint(str(info)+" "+str(numpy.nanmean(var[-1])))""",
196     fr_FR   = "Imprime sur la sortie standard la moyenne de la valeur courante de la variable",
197     en_EN   = "Print on standard output the mean of the current value of the variable",
198     order   = "next",
199     )
200 ObserverTemplates.store(
201     name    = "ValueStandardError",
202     content = """import numpy\nprint(str(info)+" "+str(numpy.nanstd(var[-1])))""",
203     fr_FR   = "Imprime sur la sortie standard l'écart-type de la valeur courante de la variable",
204     en_EN   = "Print on standard output the standard error of the current value of the variable",
205     order   = "next",
206     )
207 ObserverTemplates.store(
208     name    = "ValueVariance",
209     content = """import numpy\nprint(str(info)+" "+str(numpy.nanvar(var[-1])))""",
210     fr_FR   = "Imprime sur la sortie standard la variance de la valeur courante de la variable",
211     en_EN   = "Print on standard output the variance of the current value of the variable",
212     order   = "next",
213     )
214 ObserverTemplates.store(
215     name    = "ValueL2Norm",
216     content = """import numpy\nv = numpy.matrix( numpy.ravel( var[-1] ) )\nprint(str(info)+" "+str(float( numpy.linalg.norm(v) )))""",
217     fr_FR   = "Imprime sur la sortie standard la norme L2 de la valeur courante de la variable",
218     en_EN   = "Print on standard output the L2 norm of the current value of the variable",
219     order   = "next",
220     )
221 ObserverTemplates.store(
222     name    = "ValueRMS",
223     content = """import numpy\nv = numpy.matrix( numpy.ravel( var[-1] ) )\nprint(str(info)+" "+str(float( numpy.sqrt((1./v.size)*(v*v.T)) )))""",
224     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",
225     en_EN   = "Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable",
226     order   = "next",
227     )
228
229 # ==============================================================================
230 if __name__ == "__main__":
231     print('\n AUTODIAGNOSTIC \n')