def getStatString(self, unit=None): """ Get a string summarizing the graph data statistics """ min_value, max_value, average, current = self.getStats() tmpList = [] unitString = '' if unit: unitString = str(unit) if max_value: try: s = "Max: " + pretty_float(max_value) + " " + unitString tmpList.append(s.strip()) except BaseException: pass if min_value: try: s = "Min: " + pretty_float(min_value) + " " + unitString tmpList.append(s.strip()) except BaseException: pass if average: try: s = "Average: " + pretty_float(average) + " " + unitString tmpList.append(s.strip()) except BaseException: pass if current: try: s = "Current: " + pretty_float(current) + " " + unitString tmpList.append(s.strip()) except BaseException: pass resultString = ', '.join(tmpList) return resultString
def getStatString( self, unit = None ): """ Get a string summarizing the graph data statistics """ min_value, max_value, average, current = self.getStats() tmpList = [] unitString = '' if unit: unitString = str( unit ) if max_value: try: s = "Max: " + pretty_float( max_value ) + " " + unitString tmpList.append( s.strip() ) except Exception as e: pass if min_value: try: s = "Min: " + pretty_float( min_value ) + " " + unitString tmpList.append( s.strip() ) except Exception as e: pass if average: try: s = "Average: " + pretty_float( average ) + " " + unitString tmpList.append( s.strip() ) except Exception as e: pass if current: try: s = "Current: " + pretty_float( current ) + " " + unitString tmpList.append( s.strip() ) except Exception as e: pass resultString = ', '.join( tmpList ) return resultString
def getStatString(self, unit=None): """ Get a string summarizing the graph data statistics """ min_value, max_value, average, current = self.getStats() tmpList = [] unitString = '' if unit: unitString = str(unit) if max_value: try: s = "Max: " + pretty_float(max_value) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass
def getStatString( self, unit = None ): """ Get a string summarizing the graph data statistics """ min_value, max_value, average, current = self.getStats() tmpList = [] unitString = '' if unit: unitString = str( unit ) if max_value: try: s = "Max: " + pretty_float( max_value ) + " " + unitString tmpList.append( s.strip() ) except Exception, e: pass
class GraphData: def __init__(self, data={}): self.truncated = 0 self.all_keys = [] self.labels = [] self.label_values = [] self.subplots = {} self.plotdata = None self.data = dict(data) self.key_type = 'string' self.initialize() def isEmpty(self): """ Check if there is no data inserted """ return not self.plotdata and not self.subplots def setData(self, data): """ Add data to the GraphData object """ self.data = dict(data) self.initialize() def initialize(self, key_type=None): keys = self.data.keys() if not keys: print "GraphData Error: empty data" start = time.time() if type(self.data[keys[0]]) == types.DictType: for key in self.data: self.subplots[key] = PlotData(self.data[key], key_type=key_type) else: self.plotdata = PlotData(self.data, key_type=key_type) if DEBUG: print "Time: plot data", time.time() - start, len(self.subplots) if self.plotdata: self.all_keys = self.plotdata.getKeys() else: tmpset = set() for sub in self.subplots.values(): for key in sub.getKeys(): tmpset.add(key) self.all_keys = list(tmpset) if key_type: self.key_type = key_type else: self.key_type = get_key_type(self.all_keys) self.sortKeys() self.makeNumKeys() self.sortLabels() def expandKeys(self): if not self.plotdata: for sub in self.subplots: self.subplots[sub].expandKeys(self.all_keys) def isSimplePlot(self): return not self.plotdata is None def sortLabels(self, sort_type='max_value', reverse_order=False): """ Sort labels with a specified method: alpha - alphabetic order max_value - by max value of the subplot sum - by the sum of values of the subplot last_value - by the last value in the subplot """ if self.plotdata: if self.key_type == "string": if sort_type in ['max_value', 'sum']: self.labels = self.plotdata.sortKeys('weight') else: self.labels = self.plotdata.sortKeys() if reverse_order: self.labels.reverse() self.label_values = [ self.plotdata.parsed_data[l] for l in self.labels ] else: if sort_type == 'max_value': pairs = zip(self.subplots.keys(), self.subplots.values()) reverse = not reverse_order pairs.sort(key=lambda x: x[1].max_value, reverse=reverse) self.labels = [x[0] for x in pairs] self.label_values = [x[1].max_value for x in pairs] elif sort_type == 'last_value': pairs = zip(self.subplots.keys(), self.subplots.values()) reverse = not reverse_order pairs.sort(key=lambda x: x[1].last_value, reverse=reverse) self.labels = [x[0] for x in pairs] self.label_values = [x[1].last_value for x in pairs] elif sort_type == 'sum': pairs = [] for key in self.subplots: pairs.append((key, self.subplots[key].sum_value)) reverse = not reverse_order pairs.sort(key=lambda x: x[1], reverse=reverse) self.labels = [x[0] for x in pairs] self.label_values = [x[1] for x in pairs] elif sort_type == 'alpha': self.labels = self.subplots.keys() self.labels.sort() if reverse_order: self.labels.reverse() self.label_values = [ self.subplots[x].sum_value for x in self.labels ] else: self.labels = self.subplots.keys() if reverse_order: self.labels.reverse() def sortKeys(self): """ Sort the graph keys in a natural order """ if self.plotdata: self.plotdata.sortKeys() self.all_keys = self.plotdata.getKeys() else: self.all_keys.sort() self.min_key = min(self.all_keys) self.max_key = max(self.all_keys) def makeNumKeys(self): """ Make numerical representation of the graph keys suitable for plotting """ self.all_num_keys = [] if self.key_type == "string": self.all_string_map = {} next = 0 for key in self.all_keys: self.all_string_map[key] = next self.all_num_keys.append(next) next += 1 elif self.key_type == "time": self.all_num_keys = [ date2num(datetime.datetime.fromtimestamp(to_timestamp(key))) for key in self.all_keys ] elif self.key_type == "numeric": self.all_num_keys = [float(key) for key in self.all_keys] self.min_num_key = min(self.all_num_keys) self.max_num_key = max(self.all_num_keys) def makeCumulativeGraph(self): """ Prepare data for the cumulative graph """ self.expandKeys() if self.plotdata: self.plotdata.makeCumulativePlot() if self.truncated: self.otherPlot.makeCumulativePlot() if self.subplots: for label in self.subplots: self.subplots[label].makeCumulativePlot() self.sortLabels(sort_type='last_value') def getLabels(self): """ Get the graph labels together with the numeric values used for the label sorting """ labels = [] if self.plotdata: if self.key_type != 'string': labels = [('NoLabels', 0.)] else: labels = zip(self.labels, self.label_values) elif self.truncated: tlabels = self.labels[:self.truncated] tvalues = self.label_values[:self.truncated] labels = zip(tlabels, tvalues) labels.append(('Others', sum(self.label_values[self.truncated:]))) else: labels = zip(self.labels, self.label_values) return labels def getStringMap(self): """ Get string to number mapping for numeric type keys """ return self.all_string_map def getNumberOfKeys(self): return len(self.all_keys) def getNumberOfLabels(self): if self.truncated: return self.truncated + 1 else: return len(self.labels) def getPlotNumData(self, label=None, zipFlag=True): """ Get the plot data in a numeric form """ if self.plotdata: if zipFlag: return zip(self.plotdata.getNumKeys(), self.plotdata.getValues(), self.plotdata.getErrors()) else: return self.plotdata.getValues() elif label is not None: if label == "Others": return self.otherPlot.getPlotDataForNumKeys(self.all_num_keys) else: return self.subplots[label].getPlotDataForNumKeys( self.all_num_keys) else: # Get the sum of all the subplots self.expandKeys() arrays = [] for label in self.subplots: arrays.append( numpy.array([ x[1] for x in self.subplots[label].getPlotDataForNumKeys( self.all_num_keys, True) ])) sum_array = sum(arrays) if zipFlag: return zip(self.all_num_keys, list(sum_array)) else: return sum_array def truncateLabels(self, limit=10): """ Truncate the number of labels to the limit, leave the most important ones, accumulate the rest in the 'Others' label """ if self.plotdata: return nLabels = len(self.labels) if nLabels <= limit: return self.truncated = limit new_labels = self.labels[:limit] new_labels.append('Others') other_data = {} for key in self.all_keys: other_data[key] = 0. for label in self.labels: if label not in new_labels: for key in self.all_keys: if self.subplots[label].parsed_data.has_key(key): other_data[key] += self.subplots[label].parsed_data[ key] self.otherPlot = PlotData(other_data) def getStats(self): """ Get statistics of the graph data """ numData = self.getPlotNumData(zipFlag=False) if not len(numData): return 0, 0, 0, 0 numData = numpy.array(numData) min_value = numData.min() max_value = numData.max() average = float(numData.sum()) / len(numData) current = numData[-1] return min_value, max_value, average, current def getStatString(self, unit=None): """ Get a string summarizing the graph data statistics """ min_value, max_value, average, current = self.getStats() tmpList = [] unitString = '' if unit: unitString = str(unit) if max_value: try: s = "Max: " + pretty_float(max_value) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass if min_value: try: s = "Min: " + pretty_float(min_value) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass
unitString = str(unit) if max_value: try: s = "Max: " + pretty_float(max_value) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass if min_value: try: s = "Min: " + pretty_float(min_value) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass if average: try: s = "Average: " + pretty_float(average) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass if current: try: s = "Current: " + pretty_float(current) + " " + unitString tmpList.append(s.strip()) except Exception, e: pass resultString = ', '.join(tmpList) return resultString class PlotData:
unitString = str( unit ) if max_value: try: s = "Max: " + pretty_float( max_value ) + " " + unitString tmpList.append( s.strip() ) except Exception, e: pass if min_value: try: s = "Min: " + pretty_float( min_value ) + " " + unitString tmpList.append( s.strip() ) except Exception, e: pass if average: try: s = "Average: " + pretty_float( average ) + " " + unitString tmpList.append( s.strip() ) except Exception, e: pass if current: try: s = "Current: " + pretty_float( current ) + " " + unitString tmpList.append( s.strip() ) except Exception, e: pass resultString = ', '.join( tmpList ) return resultString class PlotData: