+++ /dev/null
-# Copyright\r
-\r
-"""Simple Python function to format values with nice prefixes.\r
-"""\r
-\r
-import math\r
-from numpy import isnan\r
-\r
-\r
-def pretty_format(value, unit='', decimals=-1, multiplier=0, leading_spaces=False):\r
- if value == 0:\r
- return '0'\r
- if isnan(value):\r
- return 'NaN'\r
-\r
- output_str = ''\r
- leading_spaces_int = 0\r
- if leading_spaces:\r
- leading_spaces_int = 5\r
- #automatic setting of multiplier\r
- if multiplier == 0:\r
- multiplier=get_multiplier(value)\r
- unit_str = ''\r
- if unit:\r
- unit_str = ' ' + get_prefix(multiplier) + unit\r
- if decimals >= 0:\r
- format_str = '% ' + repr(leading_spaces_int + decimals) + '.' + repr(decimals) + 'f'\r
- output_str = format_str % (value / multiplier) + unit_str\r
- else:\r
- output_str = str(value / multiplier) + unit_str\r
-\r
- if decimals < 0:\r
- output_str = str(value / multiplier) + ' ' + get_prefix(value / multiplier) + unit\r
-\r
- if leading_spaces_int == 0:\r
- output_str = output_str.lstrip()\r
-\r
- return output_str\r
-\r
-def get_multiplier(value):\r
- return pow(10, get_power(value))\r
-\r
-def get_power(value):\r
- if value != 0 and not isnan(value):\r
- #get the log10 from value (make sure the value is not negative)\r
- value_temp = math.floor(math.log10(math.fabs(value)))\r
- #reduce the log10 to a multiple of 3 and return it\r
- return value_temp - (value_temp % 3)\r
- else:\r
- return 0\r
-\r
-def get_exponent(prefix):\r
- #set up a dictionary to find the exponent\r
- exponent = {\r
- 'Y': 24,\r
- 'Z': 21,\r
- 'E': 18,\r
- 'P': 15,\r
- 'T': 12,\r
- 'G': 9,\r
- 'M': 6,\r
- 'k': 3,\r
- '': 0,\r
- 'm': -3,\r
- u'\u00B5': -6,\r
- 'n': -9,\r
- 'p': -12,\r
- 'f': -15,\r
- 'a': -18,\r
- 'z': -21,\r
- 'y': -24,\r
- }\r
- if exponent.has_key(prefix):\r
- return exponent[prefix]\r
- else:\r
- return 1\r
-\r
-def get_prefix(value):\r
- #set up a dictionary to find the prefix\r
- prefix = {\r
- 24: lambda: 'Y',\r
- 21: lambda: 'Z',\r
- 18: lambda: 'E',\r
- 15: lambda: 'P',\r
- 12: lambda: 'T',\r
- 9: lambda: 'G',\r
- 6: lambda: 'M',\r
- 3: lambda: 'k',\r
- 0: lambda: '',\r
- -3: lambda: 'm',\r
- -6: lambda: u'\u00B5',\r
- -9: lambda: 'n',\r
- -12: lambda: 'p',\r
- -15: lambda: 'f',\r
- -18: lambda: 'a',\r
- -21: lambda: 'z',\r
- -24: lambda: 'y',\r
- }\r
- if value != 0 and not isnan(value):\r
- #get the log10 from value\r
- value_temp = math.floor(math.log10(math.fabs(value)))\r
- else:\r
- value_temp = 0\r
- #reduce the log10 to a multiple of 3 and create the return string\r
- return prefix.get(value_temp - (value_temp % 3))()\r
-\r
-'''\r
-test_value=-2.4115665714484597e-008\r
-print 'Value: '+str(test_value)+')'\r
-print 'pretty_format example (value, unit)'\r
-print pretty_format(test_value, 'N')\r
-print'-----------------------'\r
-print 'pretty_format example (value, unit, decimals)'\r
-print pretty_format(test_value, 'N', 3)\r
-print'-----------------------'\r
-print 'pretty_format example (value, unit, decimals, multiplier)'\r
-print pretty_format(test_value, 'N', 5, 0.000001)\r
-print'-----------------------'\r
-print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'\r
-print pretty_format(0.0166276297705, 'N', 3, 0.001, True)\r
-print pretty_format(0.00750520813323, 'N', 3, 0.001, True)\r
-print pretty_format(0.0136453282825, 'N', 3, 0.001, True)\r
-'''\r
-'''\r
-#to output a scale:\r
-#choose any value on the axis and find the multiplier and prefix for it\r
-#use those to format the rest of the scale\r
-#as values can span several orders of magnitude, you have to decide what units to use\r
-\r
-#tuple of values:\r
-scale_values=0.000000000985, 0.000000001000, 0.000000001015\r
-#use this element (change to 1 or 2 to see the effect on the scale and label)\r
-index=0\r
-#get the multiplier from the value at index\r
-multiplier=get_multiplier(scale_values[index])\r
-print '\nScale example'\r
-decimals=3\r
-#print the scale\r
-for aValue in scale_values: print decimalFormat(aValue/multiplier, decimals),\r
-#print the scale label using the value at index\r
-print '\n'+get_prefix(scale_values[index])+'N'\r
-'''\r