Added illysam branch
[hooke.git] / lib / prettyformat.py
1 #!/usr/bin/env python\r
2 \r
3 '''\r
4 prettyformat.py\r
5 \r
6 Simple Python function to format values with nice prefixes\r
7 Version 1.0.1\r
8 \r
9 History\r
10 2009 07 16: added negative number support\r
11             added decimal-formatted output\r
12 \r
13 Copyright 2009 by Dr. Rolf Schmidt (Concordia University, Canada)\r
14 \r
15 This program is released under the GNU General Public License version 2.\r
16 '''\r
17 \r
18 import math\r
19 from numpy import isnan\r
20 \r
21 def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):\r
22     if fValue != 0:\r
23         iLeadingSpaces = 0\r
24         if bLeadingSpaces:\r
25             iLeadingSpaces = 5\r
26         if iMultiplier == 1:\r
27             iMultiplier=get_multiplier(fValue)\r
28         sUnitString = ''\r
29         if sUnit != '':\r
30             sUnitString = ' ' + get_prefix(iMultiplier) + sUnit\r
31         if iDecimals >= 0:\r
32             formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'\r
33             return formatString % (fValue / iMultiplier) + sUnitString\r
34         else:\r
35             return str(fValue / iMultiplier) + sUnitString\r
36     else:\r
37         return '0'\r
38     return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit\r
39 \r
40 def get_multiplier(fValue):\r
41     return pow(10, get_power(fValue))\r
42 \r
43 def get_power(fValue):\r
44     if fValue != 0 and not isnan(fValue):\r
45         #get the log10 from fValue (make sure the value is not negative)\r
46         dHelp = math.floor(math.log10(math.fabs(fValue)))\r
47         #reduce the log10 to a multiple of 3 and return it\r
48         return dHelp-(dHelp % 3)\r
49     else:\r
50         return 0\r
51 \r
52 def get_prefix(fValue):\r
53     #set up a dictionary to find the prefix\r
54     prefix = {\r
55         24: lambda: 'Y',\r
56         21: lambda: 'Z',\r
57         18: lambda: 'E',\r
58         15: lambda: 'P',\r
59         12: lambda: 'T',\r
60         9: lambda: 'G',\r
61         6: lambda: 'M',\r
62         3: lambda: 'k',\r
63         0: lambda: '',\r
64         -3: lambda: 'm',\r
65         -6: lambda: u'\u00B5',\r
66         -9: lambda: 'n',\r
67         -12: lambda: 'p',\r
68         -15: lambda: 'f',\r
69         -18: lambda: 'a',\r
70         -21: lambda: 'z',\r
71         -24: lambda: 'y',\r
72     }\r
73     if fValue != 0 and not isnan(fValue):\r
74         #get the log10 from fValue\r
75         dHelp = math.floor(math.log10(math.fabs(fValue)))\r
76     else:\r
77         dHelp = 0\r
78     #reduce the log10 to a multiple of 3 and create the return string\r
79     return prefix.get(dHelp - (dHelp % 3))()\r
80 \r
81 '''\r
82 dTestValue=-2.4115665714484597e-008\r
83 print 'Value: '+str(dTestValue)+')'\r
84 print 'pretty_format example (value, unit)'\r
85 print pretty_format(dTestValue, 'N')\r
86 print'-----------------------'\r
87 print 'pretty_format example (value, unit, decimals)'\r
88 print pretty_format(dTestValue, 'N', 3)\r
89 print'-----------------------'\r
90 print 'pretty_format example (value, unit, decimals, multiplier)'\r
91 print pretty_format(dTestValue, 'N', 5, 0.000001)\r
92 print'-----------------------'\r
93 print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'\r
94 print pretty_format(0.0166276297705, 'N', 3, 0.001, True)\r
95 print pretty_format(0.00750520813323, 'N', 3, 0.001, True)\r
96 print pretty_format(0.0136453282825, 'N', 3, 0.001, True)\r
97 '''\r
98 '''\r
99 #example use autoFormatValue\r
100 dTestValue=0.00000000567\r
101 print 'autoFormatValue example ('+str(dTestValue)+')'\r
102 print autoFormatValue(dTestValue, 'N')\r
103 #outputs 5.67 nN\r
104 '''\r
105 '''\r
106 #example use of decimalFormatValue(fValue, iDecimals, sUnit):\r
107 dTestValue=-2.4115665714484597e-008\r
108 iDecimals=3\r
109 print 'decimalFormatValue example ('+str(dTestValue)+')'\r
110 print decimalFormatValue(dTestValue, iDecimals, 'N')\r
111 #outputs -24.116 nN\r
112 #change iDecimals to see the effect\r
113 '''\r
114 '''\r
115 #example use formatValue\r
116 dTestValue=0.000000000567\r
117 print 'formatValue example ('+str(dTestValue)+')'\r
118 #find the (common) multiplier\r
119 iMultiplier=get_multiplier(dTestValue)\r
120 #use the multiplier and a unit to format the value\r
121 print formatValue(dTestValue, iMultiplier, 'N')\r
122 #outputs 567.0 pN\r
123 '''\r
124 '''\r
125 #to output a scale:\r
126 #choose any value on the axis and find the multiplier and prefix for it\r
127 #use those to format the rest of the scale\r
128 #as values can span several orders of magnitude, you have to decide what units to use\r
129 \r
130 #tuple of values:\r
131 scaleValues=0.000000000985, 0.000000001000, 0.000000001015\r
132 #use this element (change to 1 or 2 to see the effect on the scale and label)\r
133 iIndex=0\r
134 #get the multiplier from the value at iIndex\r
135 iMultiplier=get_multiplier(scaleValues[iIndex])\r
136 print '\nScale example'\r
137 iDecimals=3\r
138 #print the scale\r
139 for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),\r
140 #print the scale label using the value at iIndex\r
141 print '\n'+get_prefix(scaleValues[iIndex])+'N'\r
142 '''