3 // prettyformat.py - simple Python function to format values with nice prefixes
\r
7 // 2009 07 16: added negative number support
\r
8 // added decimal-formatted output
\r
10 // Copyright (c) 2009 Rolf Schmidt, Montreal
\r
11 // rschmidt@alcor.concordia.ca
\r
13 // This procedure is released under the GNU General Public License version 2
\r
18 from numpy import isnan
\r
20 def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):
\r
25 if iMultiplier == 1:
\r
26 iMultiplier=get_multiplier(fValue)
\r
29 sUnitString = ' ' + get_prefix(iMultiplier) + sUnit
\r
31 formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'
\r
32 return formatString % (fValue / iMultiplier) + sUnitString
\r
34 return str(fValue / iMultiplier) + sUnitString
\r
37 return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit
\r
39 def get_multiplier(fValue):
\r
40 return pow(10, get_power(fValue))
\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
51 def get_prefix(fValue):
\r
52 #set up a dictionary to find the prefix
\r
64 -6: lambda: u'\u00B5',
\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
77 #reduce the log10 to a multiple of 3 and create the return string
\r
78 return prefix.get(dHelp - (dHelp % 3))()
\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
98 #example use autoFormatValue
\r
99 dTestValue=0.00000000567
\r
100 print 'autoFormatValue example ('+str(dTestValue)+')'
\r
101 print autoFormatValue(dTestValue, 'N')
\r
105 #example use of decimalFormatValue(fValue, iDecimals, sUnit):
\r
106 dTestValue=-2.4115665714484597e-008
\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
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
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
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
133 #get the multiplier from the value at iIndex
\r
134 iMultiplier=get_multiplier(scaleValues[iIndex])
\r
135 print '\nScale example'
\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