Package PyFoam :: Package Applications :: Module PrintData2DStatistics
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.PrintData2DStatistics

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