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