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