11#!/usr/bin/env python
2- import copy
32import math
43import rospy
54
65from atf_core import ATFAnalyserError , ATFConfigurationError
76from atf_msgs .msg import Api
8- from atf_msgs .msg import MetricResult , Groundtruth , KeyValue , DataStamped
7+ from atf_msgs .msg import MetricResult , Groundtruth , KeyValue , DataStamped , TestblockStatus
98from atf_metrics import metrics_helper
109
1110class CalculateInterfaceParamHandler :
@@ -61,29 +60,25 @@ def __init__(self, name, testblock_name, params, mode, series_mode):
6160 Class for calculating the interface type.
6261 """
6362 self .name = name
64- self .started = False
65- self .finished = False
66- self .active = False
63+ self .testblock_name = testblock_name
64+ self .status = TestblockStatus ()
6765 self .groundtruth = Groundtruth ()
6866 self .groundtruth .available = True
6967 self .groundtruth .data = 100 # this is the max score
7068 self .groundtruth .epsilon = 0 # no deviation from max score allowed
71- self .testblock_name = testblock_name
7269 self .mode = mode
7370 self .series_mode = series_mode
7471 self .series = []
75- self . data = DataStamped ()
72+
7673 self .interface_details = {}
7774 self .api_dict = {}
7875 self .params = params
7976
8077 def start (self , status ):
81- self .active = True
82- self .started = True
78+ self .status = status
8379
8480 def stop (self , status ):
85- self .active = False
86- self .finished = True
81+ self .status = status
8782
8883 def pause (self , status ):
8984 pass
@@ -93,13 +88,14 @@ def purge(self, status):
9388
9489 def update (self , topic , msg , t ):
9590 # get data if testblock is active
96- if self .active :
91+ if self .status . status == TestblockStatus . ACTIVE :
9792 if topic == "/atf/api" and msg .testblock_name == self .testblock_name :
9893 self .api_dict = self .msg_to_dict (msg )
9994 interface_data , self .interface_details = self .calculate_data_and_details ()
100- self .data .stamp = t
101- self .data .data = interface_data
102- self .series .append (copy .deepcopy (self .data )) # FIXME handle fixed rates
95+ data = DataStamped ()
96+ data .stamp = t
97+ data .data = interface_data
98+ self .series .append (data ) # FIXME handle fixed rates
10399
104100 def msg_to_dict (self , msg ):
105101 api_dict = {}
@@ -196,78 +192,32 @@ def get_result(self):
196192 metric_result = MetricResult ()
197193 metric_result .name = self .name
198194 metric_result .mode = self .mode
199- metric_result .started = self .started # FIXME remove
200- metric_result .finished = self .finished # FIXME remove
195+ metric_result .status = self .status .status
201196 metric_result .series = []
202197 metric_result .groundtruth .available = self .groundtruth .available
203198 metric_result .groundtruth .data = self .groundtruth .data
204199 metric_result .groundtruth .epsilon = self .groundtruth .epsilon
205200
206- if not self .started :
207- error_message = "testblock %s never started" % self .testblock_name
208- #print error_message
209- metric_result .groundtruth .result = False
210- metric_result .groundtruth .error_message = error_message
211- return metric_result
212-
213- if not self .finished :
214- error_message = "testblock %s never stopped" % self .testblock_name
215- #print error_message
216- metric_result .groundtruth .result = False
217- metric_result .groundtruth .error_message = error_message
201+ if self .status .status != TestblockStatus .SUCCEEDED :
202+ metric_result .groundtruth .result = Groundtruth .FAILED
203+ metric_result .groundtruth .error_message = metrics_helper .extract_error_message (self .status )
218204 return metric_result
219205
220206 # check if result is available
221207 if len (self .series ) == 0 :
222208 # let the analyzer know that this test failed
223- metric_result .groundtruth .result = False
209+ metric_result .groundtruth .result = Groundtruth . FAILED
224210 metric_result .groundtruth .error_message = "testblock %s stopped without result" % self .testblock_name
225211 return metric_result
226212
227213 # at this point we're sure that any result is available
228214
229- # calculate metric data
215+ # set series
230216 if self .series_mode != None :
231217 metric_result .series = self .series
232- if metric_result .mode == MetricResult .SNAP :
233- metric_result .data = self .series [- 1 ] # take last element from self.series for data and stamp
234- metric_result .min = metric_result .data
235- metric_result .max = metric_result .data
236- metric_result .mean = metric_result .data .data
237- metric_result .std = 0.0
238- elif metric_result .mode == MetricResult .SPAN_MEAN :
239- metric_result .min = metrics_helper .get_min (self .series )
240- metric_result .max = metrics_helper .get_max (self .series )
241- metric_result .mean = metrics_helper .get_mean (self .series )
242- metric_result .std = metrics_helper .get_std (self .series )
243- metric_result .data .data = metric_result .mean # take mean for data
244- metric_result .data .stamp = self .series [- 1 ].stamp # take stamp from last element in self.series for stamp
245- elif metric_result .mode == MetricResult .SPAN_MIN :
246- metric_result .min = metrics_helper .get_min (self .series )
247- metric_result .max = metrics_helper .get_max (self .series )
248- metric_result .mean = metrics_helper .get_mean (self .series )
249- metric_result .std = metrics_helper .get_std (self .series )
250- metric_result .data = metric_result .min
251- elif metric_result .mode == MetricResult .SPAN_ABSMIN :
252- metric_result .min = metrics_helper .get_absmin (self .series )
253- metric_result .max = metrics_helper .get_absmax (self .series )
254- metric_result .mean = metrics_helper .get_mean (self .series )
255- metric_result .std = metrics_helper .get_std (self .series )
256- metric_result .data = metric_result .min
257- elif metric_result .mode == MetricResult .SPAN_MAX :
258- metric_result .min = metrics_helper .get_min (self .series )
259- metric_result .max = metrics_helper .get_max (self .series )
260- metric_result .mean = metrics_helper .get_mean (self .series )
261- metric_result .std = metrics_helper .get_std (self .series )
262- metric_result .data = metric_result .max
263- elif metric_result .mode == MetricResult .SPAN_ABSMAX :
264- metric_result .min = metrics_helper .get_absmin (self .series )
265- metric_result .max = metrics_helper .get_absmax (self .series )
266- metric_result .mean = metrics_helper .get_mean (self .series )
267- metric_result .std = metrics_helper .get_std (self .series )
268- metric_result .data = metric_result .max
269- else : # invalid mode
270- raise ATFAnalyserError ("Analysing failed, invalid mode '%s' for metric '%s'." % (metric_result .mode , metric_result .name ))
218+
219+ # calculate metric data
220+ [metric_result .data , metric_result .min , metric_result .max , metric_result .mean , metric_result .std ] = metrics_helper .calculate_metric_data (metric_result .name , metric_result .mode , self .series )
271221
272222 # fill details as KeyValue messages
273223 details = []
@@ -277,14 +227,14 @@ def get_result(self):
277227 # evaluate metric data
278228 if metric_result .groundtruth .available : # groundtruth available
279229 if math .fabs (metric_result .groundtruth .data - metric_result .data .data ) <= metric_result .groundtruth .epsilon :
280- metric_result .groundtruth .result = True
230+ metric_result .groundtruth .result = Groundtruth . SUCCEEDED
281231 metric_result .groundtruth .error_message = "all OK"
282232 else :
283- metric_result .groundtruth .result = False
233+ metric_result .groundtruth .result = Groundtruth . FAILED
284234 metric_result .groundtruth .error_message = "groundtruth missmatch: %f not within %f+-%f" % (metric_result .data .data , metric_result .groundtruth .data , metric_result .groundtruth .epsilon )
285235
286236 else : # groundtruth not available
287- metric_result .groundtruth .result = True
237+ metric_result .groundtruth .result = Groundtruth . SUCCEEDED
288238 metric_result .groundtruth .error_message = "all OK (no groundtruth available)"
289239
290240 return metric_result
0 commit comments