1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """Define functions for handling numbers in SI notation.
23 To output a scale, choose any value on the axis and find the
24 multiplier and prefix for it. Use those to format the rest of the
25 scale. As values can span several orders of magnitude, you have
26 to decide what units to use.
28 >>> xs = (985e-12, 1e-9, 112358e-12)
30 Get the power from the first (or last, or middle, ...) value
32 >>> p = get_power(xs[0])
34 ... print ppSI(x, decimals=2, power=p)
38 >>> print prefix_from_value(xs[0]) + 'N'
43 from numpy import isnan
66 """A dictionary of SI prefixes from 10**24 to 10**-24.
78 _DATA_LABEL_REGEXP = re.compile('^([^(]*[^ ]) ?\(([^)]*)\)$')
79 """Used by :func:`data_label_unit`.
83 def ppSI(value, unit='', decimals=None, power=None, pad=False):
84 """Pretty-print `value` in SI notation.
86 The current implementation ignores `pad` if `decimals` is `None`.
90 >>> x = math.pi * 1e-8
91 >>> print ppSI(x, 'N')
93 >>> print ppSI(x, 'N', 3)
95 >>> print ppSI(x, 'N', 4, power=-12)
97 >>> print ppSI(x, 'N', 5, pad=True)
100 If you want the decimal indented by six spaces with `decimal=2`,
101 `pad` should be the sum of
103 * 6 (places before the decimal point)
104 * 1 (length of the decimal point)
105 * 2 (places after the decimal point)
107 >>> print ppSI(-x, 'N', 2, pad=(6+1+2))
112 if value == None or isnan(value):
115 if power == None: # auto-detect power
116 power = get_power(value)
119 format = lambda n: '%f' % n
121 if pad == False: # no padding
122 format = lambda n: '%.*f' % (decimals, n)
124 if pad == True: # auto-generate pad
125 # 1 for ' ', 1 for '-', 3 for number, 1 for '.', and decimals.
127 format = lambda n: '%*.*f' % (pad, decimals, n)
129 prefix = ' '+PREFIX[power]
131 prefix = 'e%d ' % power
132 return '%s%s%s' % (format(value / pow(10,power)), prefix, unit)
135 def get_power(value):
136 """Return the SI power for which `0 <= |value|/10**pow < 1000`.
151 if value != 0 and not isnan(value):
153 value_temp = math.floor(math.log10(math.fabs(value)))
154 # reduce the log10 to a multiple of 3
155 return int(value_temp - (value_temp % 3))
159 def prefix_from_value(value):
160 """Determine the SI power of `value` and return its prefix.
164 >>> prefix_from_value(0)
166 >>> prefix_from_value(1e10)
169 return PREFIX[get_power(value)]
171 def join_data_label(name, unit):
172 """Create laels for `curve.data[i].info['columns']`.
180 >>> join_data_label('z piezo', 'm')
182 >>> join_data_label('deflection', 'N')
185 return '%s (%s)' % (name, unit)
187 def split_data_label(label):
188 """Split `curve.data[i].info['columns']` labels into `(name, unit)`.
196 >>> split_data_label('z piezo (m)')
198 >>> split_data_label('deflection (N)')
201 m = _DATA_LABEL_REGEXP.match(label)
202 assert m != None, label