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