Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[hooke.git] / hooke / ui / gui / formatter.py
1 # Copyright (C) 2006-2010 Francesco Musiani
2 #                         Massimo Sandal <devicerandom@gmail.com>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Hooke.
6 #
7 # Hooke is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
11 #
12 # Hooke is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke.  If not, see
19 # <http://www.gnu.org/licenses/>.
20
21 """A number formatting utilities for Hooke.
22 """
23
24 from matplotlib.ticker import ScalarFormatter
25
26 from . import curve as lhc
27
28
29 class EngrFormatter(ScalarFormatter):
30     """A variation of the standard ScalarFormatter, using only multiples of 
31 three
32 in the mantissa. A fixed number of decimals can be displayed with the optional 
33 parameter `ndec` . If `ndec` is None (default), the number of decimals is 
34 defined
35 from the current ticks.
36     """
37     def __init__(self, ndec=None, useOffset=True, useMathText=False):
38         ScalarFormatter.__init__(self, useOffset, useMathText)
39         if ndec is None or ndec < 0:
40             self.format = None
41         elif ndec == 0:
42             self.format = "%d"
43         else:
44             self.format = "%%1.%if" % ndec
45
46     def _set_orderOfMagnitude(self, mrange):
47             """Sets the order of magnitude."""        
48             locs = numpy.absolute(self.locs)
49             if self.offset: 
50                 oom = numpy.floor(numpy.log10(mrange))
51             else:
52                 if locs[0] > locs[-1]: 
53                     val = locs[0]
54                 else: 
55                     val = locs[-1]
56                 if val == 0: 
57                     oom = 0
58                 else: 
59                     oom = numpy.floor(numpy.log10(val))
60             if oom <= -3:
61                 self.orderOfMagnitude = 3*(oom//3)
62             elif oom <= -1:
63                 self.orderOfMagnitude = -3
64             elif oom >= 4:
65                 self.orderOfMagnitude = 3*(oom//3)
66             else:
67                 self.orderOfMagnitude = 0
68
69     def _set_format(self):
70         """Sets the format string to format all ticklabels."""
71         # set the format string to format all the ticklabels
72         locs = (numpy.array(self.locs)-self.offset) /  10**self.orderOfMagnitude+1e-15
73         sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
74                    for loc in locs]
75         sigfigs.sort()
76         if self.format is None:
77             self.format = '%1.' + str(sigfigs[-1]) + 'f'
78         if self._usetex or self._useMathText: self.format = '$%s$'%self.format