1 # Copyright (C) 2009-2010 Rolf Schmidt <rschmidt@alcor.concordia.ca>
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
6 # Hooke is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
11 # Hooke is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke. If not, see
18 # <http://www.gnu.org/licenses/>.
20 """Format values with nice prefixes.
25 * added negative number support
26 * added decimal-formatted output
31 from numpy import isnan
33 def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):
39 iMultiplier=get_multiplier(fValue)
42 sUnitString = ' ' + get_prefix(iMultiplier) + sUnit
44 formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'
45 return formatString % (fValue / iMultiplier) + sUnitString
47 return str(fValue / iMultiplier) + sUnitString
50 return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit
52 def get_multiplier(fValue):
53 return pow(10, get_power(fValue))
55 def get_power(fValue):
56 if fValue != 0 and not isnan(fValue):
57 #get the log10 from fValue (make sure the value is not negative)
58 dHelp = math.floor(math.log10(math.fabs(fValue)))
59 #reduce the log10 to a multiple of 3 and return it
60 return dHelp-(dHelp % 3)
64 def get_prefix(fValue):
65 #set up a dictionary to find the prefix
77 -6: lambda: u'\u00B5',
85 if fValue != 0 and not isnan(fValue):
86 #get the log10 from fValue
87 dHelp = math.floor(math.log10(math.fabs(fValue)))
90 #reduce the log10 to a multiple of 3 and create the return string
91 return prefix.get(dHelp - (dHelp % 3))()
94 dTestValue=-2.4115665714484597e-008
95 print 'Value: '+str(dTestValue)+')'
96 print 'pretty_format example (value, unit)'
97 print pretty_format(dTestValue, 'N')
98 print'-----------------------'
99 print 'pretty_format example (value, unit, decimals)'
100 print pretty_format(dTestValue, 'N', 3)
101 print'-----------------------'
102 print 'pretty_format example (value, unit, decimals, multiplier)'
103 print pretty_format(dTestValue, 'N', 5, 0.000001)
104 print'-----------------------'
105 print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'
106 print pretty_format(0.0166276297705, 'N', 3, 0.001, True)
107 print pretty_format(0.00750520813323, 'N', 3, 0.001, True)
108 print pretty_format(0.0136453282825, 'N', 3, 0.001, True)
111 #example use autoFormatValue
112 dTestValue=0.00000000567
113 print 'autoFormatValue example ('+str(dTestValue)+')'
114 print autoFormatValue(dTestValue, 'N')
118 #example use of decimalFormatValue(fValue, iDecimals, sUnit):
119 dTestValue=-2.4115665714484597e-008
121 print 'decimalFormatValue example ('+str(dTestValue)+')'
122 print decimalFormatValue(dTestValue, iDecimals, 'N')
124 #change iDecimals to see the effect
127 #example use formatValue
128 dTestValue=0.000000000567
129 print 'formatValue example ('+str(dTestValue)+')'
130 #find the (common) multiplier
131 iMultiplier=get_multiplier(dTestValue)
132 #use the multiplier and a unit to format the value
133 print formatValue(dTestValue, iMultiplier, 'N')
138 #choose any value on the axis and find the multiplier and prefix for it
139 #use those to format the rest of the scale
140 #as values can span several orders of magnitude, you have to decide what units to use
143 scaleValues=0.000000000985, 0.000000001000, 0.000000001015
144 #use this element (change to 1 or 2 to see the effect on the scale and label)
146 #get the multiplier from the value at iIndex
147 iMultiplier=get_multiplier(scaleValues[iIndex])
148 print '\nScale example'
151 for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),
152 #print the scale label using the value at iIndex
153 print '\n'+get_prefix(scaleValues[iIndex])+'N'