1
2 """
3 Application class that implements pyFoamPrintData2DStatistics
4 """
5
6 from optparse import OptionGroup
7
8 from .PyFoamApplication import PyFoamApplication
9
10 from .CommonPickledDataInput import CommonPickledDataInput
11
12 from PyFoam.Basics.Data2DStatistics import Data2DStatistics
13
14 from PyFoam.ThirdParty.six import print_
15
18 - def __init__(self,args=None,inputApp=None):
19 description="""\
20 Reads a file with pickled information with statistics about data
21 series (as it is usually gnerated by pyFoamTimelinePlot.py and
22 pyFoamSamplePlot.py) and prints it in a human-readable form.
23 """
24
25 PyFoamApplication.__init__(self,
26 args=args,
27 description=description,
28 usage="%prog [options]",
29 nr=0,
30 changeVersion=False,
31 interspersed=True,
32 inputApp=inputApp)
33
35 CommonPickledDataInput.addOptions(self)
36
37 output=OptionGroup(self.parser,
38 "2D Statistics output",
39 "Options that determine what should be output")
40 self.parser.add_option_group(output)
41 output.add_option("--field",
42 action="append",
43 default=[],
44 dest="field",
45 help="""\
46 Name of the field that should be printed. Can be specified more than once
47 """)
48 output.add_option("--function",
49 action="append",
50 default=[],
51 dest="function",
52 help="""\ Name of a function that should be
53 calculated on the data. Either a function in the lambda-syntax or a
54 function from the math-module
55 """)
56 output.add_option("--relative-error",
57 action="store_true",
58 default=False,
59 dest="relativeError",
60 help="""\
61 Print the relative error as calculated from the metrics and the compare-data
62 """)
63 output.add_option("--relative-average-error",
64 action="store_true",
65 default=False,
66 dest="relativeAverageError",
67 help="""\
68 Print the relative average error as calculated from the metrics and the compare-data (weighted average))
69 """)
70 output.add_option("--range",
71 action="store_true",
72 default=False,
73 dest="range",
74 help="""\
75 Print the range (minimum and maximum) of the data
76 """)
77
78 input=OptionGroup(self.parser,
79 "2D Statistics intput",
80 "Options that determine what should be used as input")
81 self.parser.add_option_group(input)
82 input.add_option("--metrics-name",
83 action="store",
84 default="metrics",
85 dest="metricsName",
86 help="""\
87 Name of the data metric (the main input). Default: %default
88 """)
89 input.add_option("--compare-name",
90 action="store",
91 default="compare",
92 dest="compareName",
93 help="""\
94 Name of the comparison metric (the secondary input). Default:
95 %default. Ignored if not present in the data
96 """)
97
98 parameters=OptionGroup(self.parser,
99 "2D Statistics Paramters",
100 "Options that determine the behaviour of the 2D statistics")
101 self.parser.add_option_group(parameters)
102 parameters.add_option("--small-threshold",
103 action="store",
104 default=1e-10,
105 type="float",
106 dest="smallThreshold",
107 help="""\
108 Value that is considered to be close enough to 0. Default:
109 %default. Used for instance for the relative error calculations
110 """)
111
112
114 data=self.readPickledData()
115 result={"originalData":data}
116 if self.opts.metricsName in data:
117 metrics=data[self.opts.metricsName]
118 else:
119 self.error("Metrics set",self.opts.metricsName,"not in",list(data.keys()))
120 if self.opts.metricsName==self.opts.compareName:
121 self.warning("Metrics and comparison",self.opts.compareName,
122 "are the same. No comparison used")
123 self.opts.compareName=None
124
125 if self.opts.compareName==None:
126 compare=None
127 elif self.opts.compareName in data:
128 compare=data[self.opts.compareName]
129 else:
130 self.error("Compare set",self.opts.compareName,"not in",list(data.keys()))
131
132 stat=Data2DStatistics(metrics,
133 compare=compare,
134 small=self.opts.smallThreshold)
135
136 result["statistics"]=stat
137
138 for f in self.opts.field:
139 print_("\nField",f)
140 try:
141 val=stat[f]
142 print_(val)
143 result[f]=val
144 except KeyError:
145 print_(" .... not present in",stat.names())
146
147 for f in self.opts.function:
148 for v in self.opts.field:
149 print_("\nFunction",f,"on field",v)
150 try:
151 val=stat.func(f,v)
152 print_(val)
153 result["%s on %s" % (f,v)]=val
154 except KeyError:
155 print_(" .... not present in",stat.names())
156
157 if self.opts.relativeError:
158 print_("\nRelative Error")
159 val=stat.relativeError()
160 print_(val)
161 result["relativeError"]=val
162
163 if self.opts.relativeAverageError:
164 print_("\nRelative Average Error")
165 val=stat.relativeAverageError()
166 print_(val)
167 result["relativeAverageError"]=val
168
169 if self.opts.range:
170 print_("\nData range")
171 val=stat.range()
172 print_(val)
173 result["dataRange"]=val
174
175 self.setData(result)
176
177
178