def normalize_series_by_unit(series): """Transform series' values into a more human readable form: 1) Determine the data point with the maximum value 2) Decide the unit appropriate for this value (normalize it) 3) Convert other values to this new unit, if necessary """ if not series: return series source_unit = target_unit = series[0]['unit'] if not units.is_supported(source_unit): return series # Find the data point with the largest value and normalize it to # determine its unit - that will be the new unit maximum = max([d['y'] for point in series for d in point['data']]) unit = units.normalize(maximum, source_unit)[1] # If unit needs to be changed, set the new unit for all data points # and convert all values to that unit if units.is_larger(unit, target_unit): target_unit = unit for i, point in enumerate(series[:]): if point['unit'] != target_unit: series[i]['unit'] = target_unit for j, d in enumerate(point['data'][:]): series[i]['data'][j]['y'] = units.convert( d['y'], source_unit, target_unit, fmt=True)[0] return series
def normalize_series_by_unit(series): """Transform series' values into a more human readable form: 1) Determine the data point with the maximum value 2) Decide the unit appropriate for this value (normalize it) 3) Convert other values to this new unit, if necessary """ if not series: return series source_unit = target_unit = series[0]['unit'] if not units.is_supported(source_unit): return series # Find the data point with the largest value and normalize it to # determine its unit - that will be the new unit maximum = max([d['y'] for point in series for d in point['data']]) unit = units.normalize(maximum, source_unit)[1] # If unit needs to be changed, set the new unit for all data points # and convert all values to that unit if units.is_larger(unit, target_unit): target_unit = unit for i, point in enumerate(series[:]): if point['unit'] != target_unit: series[i]['unit'] = target_unit for j, d in enumerate(point['data'][:]): series[i]['data'][j]['y'] = units.convert(d['y'], source_unit, target_unit, fmt=True)[0] return series
def test_is_supported(self): self.assertTrue(units.is_supported('MB')) self.assertTrue(units.is_supported('min')) self.assertFalse(units.is_supported('KWh')) self.assertFalse(units.is_supported('unknown_unit'))
def test_is_supported(self): self.assertTrue(units.is_supported("MB")) self.assertTrue(units.is_supported("min")) self.assertFalse(units.is_supported("KWh")) self.assertFalse(units.is_supported("unknown_unit"))