Salome HOME
aa84de77d8ae9d190edd0e2dbe90672f4b676eea
[modules/adao.git] / src / daComposant / daCore / Templates.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2015 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         if name is None or content is None:
44             raise ValueError("To be consistent, the storage of a template must provide a name and a content.")
45         if order == "next":
46             self.__order += 1
47         else:
48             self.__order = int(order)
49         self.__values[str(name)] = {
50             'content': str(content),
51             'fr_FR'  : str(fr_FR),
52             'en_EN'  : str(en_EN),
53             'order'  : int(self.__order),
54             }
55
56     def keys(self):
57         __keys = self.__values.keys()
58         __keys.sort()
59         return __keys
60
61     def has_key(self, name):
62         return self.__values.has_key(name)
63
64     def __len__(self):
65         return len(self.__values)
66
67     def __getitem__(self, name=None ):
68         return self.__values[name]['content']
69
70     def getdoc(self, name = None, lang = "fr_FR"):
71         if lang not in self.__values[name]: lang = self.__preferedLanguage
72         return self.__values[name][lang]
73
74     def keys_in_presentation_order(self):
75         "Restitue l'ordre de présentation requis"
76         __orders = []
77         for k in self.keys():
78             __orders.append( self.__values[k]['order'] )
79         __reorder = numpy.array(__orders).argsort()
80         return list(numpy.array(self.keys())[__reorder])
81
82 # ==============================================================================
83 ObserverTemplates = TemplateStorage()
84
85 ObserverTemplates.store(
86     name    = "ValuePrinter",
87     content = """print info, var[-1]""",
88     fr_FR   = "Imprime sur la sortie standard la valeur courante de la variable",
89     en_EN   = "Print on standard output the current value of the variable",
90     order   = "next",
91     )
92 ObserverTemplates.store(
93     name    = "ValueSeriePrinter",
94     content = """print info, var[:]""",
95     fr_FR   = "Imprime sur la sortie standard la série des valeurs de la variable",
96     en_EN   = "Print on standard output the value serie of the variable",
97     order   = "next",
98     )
99 ObserverTemplates.store(
100     name    = "ValueSaver",
101     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)""",
102     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",
103     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",
104     order   = "next",
105     )
106 ObserverTemplates.store(
107     name    = "ValueSerieSaver",
108     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)""",
109     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",
110     en_EN   = "Save the value serie of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step",
111     order   = "next",
112     )
113 ObserverTemplates.store(
114     name    = "ValuePrinterAndSaver",
115     content = """import numpy, re\nv=numpy.array(var[-1], ndmin=1)\nprint info,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)""",
116     fr_FR   = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier la valeur courante de la variable",
117     en_EN   = "Print on standard output and, in the same time, save in a file the current value of the variable",
118     order   = "next",
119     )
120 ObserverTemplates.store(
121     name    = "ValueSeriePrinterAndSaver",
122     content = """import numpy, re\nv=numpy.array(var[:],  ndmin=1)\nprint info,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)""",
123     fr_FR   = "Imprime sur la sortie standard et, en même temps, enregistre dans un fichier la série des valeurs de la variable",
124     en_EN   = "Print on standard output and, in the same time, save in a file the value serie of the variable",
125     order   = "next",
126     )
127 ObserverTemplates.store(
128     name    = "ValueGnuPlotter",
129     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' ) )""",
130     fr_FR   = "Affiche graphiquement avec Gnuplot la valeur courante de la variable",
131     en_EN   = "Graphically plot with Gnuplot the current value of the variable",
132     order   = "next",
133     )
134 ObserverTemplates.store(
135     name    = "ValueSerieGnuPlotter",
136     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' ) )""",
137     fr_FR   = "Affiche graphiquement avec Gnuplot la série des valeurs de la variable",
138     en_EN   = "Graphically plot with Gnuplot the value serie of the variable",
139     order   = "next",
140     )
141 ObserverTemplates.store(
142     name    = "ValuePrinterAndGnuPlotter",
143     content = """print info, 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' ) )""",
144     fr_FR   = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la valeur courante de la variable",
145     en_EN   = "Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable",
146     order   = "next",
147     )
148 ObserverTemplates.store(
149     name    = "ValueSeriePrinterAndGnuPlotter",
150     content = """print info, 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' ) )""",
151     fr_FR   = "Imprime sur la sortie standard et, en même temps, affiche graphiquement avec Gnuplot la série des valeurs de la variable",
152     en_EN   = "Print on standard output and, in the same time, graphically plot with Gnuplot the value serie of the variable",
153     order   = "next",
154     )
155 ObserverTemplates.store(
156     name    = "ValuePrinterSaverAndGnuPlotter",
157     content = """print info, 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' ) )""",
158     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 ",
159     en_EN   = "Print on standard output and, in the same, time save in a file and graphically plot the current value of the variable",
160     order   = "next",
161     )
162 ObserverTemplates.store(
163     name    = "ValueSeriePrinterSaverAndGnuPlotter",
164     content = """print info, 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' ) )""",
165     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",
166     en_EN   = "Print on standard output and, in the same, time save in a file and graphically plot the value serie of the variable",
167     order   = "next",
168     )
169 ObserverTemplates.store(
170     name    = "ValueMean",
171     content = """import numpy\nprint info, numpy.nanmean(var[-1])""",
172     fr_FR   = "Imprime sur la sortie standard la moyenne de la valeur courante de la variable",
173     en_EN   = "Print on standard output the mean of the current value of the variable",
174     order   = "next",
175     )
176 ObserverTemplates.store(
177     name    = "ValueStandardError",
178     content = """import numpy\nprint info, numpy.nanstd(var[-1])""",
179     fr_FR   = "Imprime sur la sortie standard l'écart-type de la valeur courante de la variable",
180     en_EN   = "Print on standard output the standard error of the current value of the variable",
181     order   = "next",
182     )
183 ObserverTemplates.store(
184     name    = "ValueVariance",
185     content = """import numpy\nprint info, numpy.nanvar(var[-1])""",
186     fr_FR   = "Imprime sur la sortie standard la variance de la valeur courante de la variable",
187     en_EN   = "Print on standard output the variance of the current value of the variable",
188     order   = "next",
189     )
190 ObserverTemplates.store(
191     name    = "ValueRMS",
192     content = """import numpy\nv = numpy.matrix( numpy.ravel( var[-1] ) )\nprint info, float( numpy.sqrt((1./v.size)*(v*v.T)) )""",
193     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",
194     en_EN   = "Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable",
195     order   = "next",
196     )
197
198 # ==============================================================================
199 if __name__ == "__main__":
200     print '\n AUTODIAGNOSTIC \n'