1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 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
23 from daCore import BasicObjects, Persistence
25 # ==============================================================================
26 class ElementaryDiagnostic(BasicObjects.Diagnostic,Persistence.OneScalar):
30 def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
31 BasicObjects.Diagnostic.__init__(self, name, parameters)
32 Persistence.OneScalar.__init__( self, name, unit, basetype = float)
34 def _formula(self, V1, V2):
36 Fait un écart RMS entre deux vecteurs V1 et V2
38 rms = math.sqrt( ((V2 - V1)**2).sum() / float(V1.size) )
42 def calculate(self, vector1 = None, vector2 = None, step = None):
44 Teste les arguments, active la formule de calcul et stocke le résultat
46 if vector1 is None or vector2 is None:
47 raise ValueError("Two vectors must be given to calculate their RMS")
48 V1 = numpy.array(vector1)
49 V2 = numpy.array(vector2)
50 if V1.size < 1 or V2.size < 1:
51 raise ValueError("The given vectors must not be empty")
52 if V1.size != V2.size:
53 raise ValueError("The two given vectors must have the same size")
55 value = self._formula( V1, V2 )
57 self.store( value = value, step = step )
59 # ==============================================================================
60 if __name__ == "__main__":
61 print '\n AUTODIAGNOSTIC \n'
63 D = ElementaryDiagnostic("Ma RMS")
65 vect1 = [1, 2, 1, 2, 1]
66 vect2 = [2, 1, 2, 1, 2]
67 D.calculate(vect1,vect2)
68 vect1 = [1, 3, 1, 3, 1]
69 vect2 = [2, 2, 2, 2, 2]
70 D.calculate(vect1,vect2)
71 vect1 = [1, 1, 1, 1, 1]
72 vect2 = [2, 2, 2, 2, 2]
73 D.calculate(vect1,vect2)
74 vect1 = [1, 1, 1, 1, 1]
75 vect2 = [4, -2, 4, -2, -2]
76 D.calculate(vect1,vect2)
77 vect1 = [0.29, 0.97, 0.73, 0.01, 0.20]
78 vect2 = [0.92, 0.86, 0.11, 0.72, 0.54]
79 D.calculate(vect1,vect2)
80 vect1 = [-0.23262176, 1.36065207, 0.32988102, 0.24400551, -0.66765848, -0.19088483, -0.31082575, 0.56849814, 1.21453443, 0.99657516]
81 vect2 = [0,0,0,0,0,0,0,0,0,0]
82 D.calculate(vect1,vect2)
83 print " Les valeurs de RMS attendues sont les suivantes : [1.0, 1.0, 1.0, 3.0, 0.53162016515553656, 0.73784217096601323]"
84 print " Les RMS obtenues................................:", D.valueserie()
85 print " La moyenne......................................:", D.mean()