Fixes bad call to wlc/fjc fit
[hooke.git] / libhooke.py
index 8989822895e98ceb1e2ff124991ffddc55cebc70..d035c7187498bfd2ec1bcc89ca4151448625a740 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 
 '''
 libhooke.py
@@ -24,6 +25,8 @@ import xml.dom.minidom
 import os
 import string
 import csv
+from matplotlib.ticker import ScalarFormatter
+
 
 HOOKE_VERSION=['0.8.3_devel', 'Seinei', '2008-04-16']
 WX_GOOD=['2.6','2.8'] 
@@ -251,6 +254,62 @@ class HookeConfig:
         pass    
 
 
+class EngrFormatter(ScalarFormatter):
+    """A variation of the standard ScalarFormatter, using only multiples of 
+three
+in the mantissa. A fixed number of decimals can be displayed with the optional 
+parameter `ndec` . If `ndec` is None (default), the number of decimals is 
+defined
+from the current ticks.
+    """
+    def __init__(self, ndec=None, useOffset=True, useMathText=False):
+        ScalarFormatter.__init__(self, useOffset, useMathText)
+        if ndec is None or ndec < 0:
+            self.format = None
+        elif ndec == 0:
+            self.format = "%d"
+        else:
+            self.format = "%%1.%if" % ndec
+    #........................
+
+    def _set_orderOfMagnitude(self, mrange):
+           """Sets the order of magnitude."""        
+           locs = numpy.absolute(self.locs)
+           if self.offset: 
+               oom = numpy.floor(numpy.log10(mrange))
+           else:
+               if locs[0] > locs[-1]: 
+                   val = locs[0]
+               else: 
+                   val = locs[-1]
+               if val == 0: 
+                   oom = 0
+               else: 
+                   oom = numpy.floor(numpy.log10(val))
+           if oom <= -3:
+               self.orderOfMagnitude = 3*(oom//3)
+           elif oom <= -1:
+               self.orderOfMagnitude = -3
+           elif oom >= 4:
+               self.orderOfMagnitude = 3*(oom//3)
+           else:
+               self.orderOfMagnitude = 0
+
+
+    #........................
+    def _set_format(self):
+        """Sets the format string to format all ticklabels."""
+        # set the format string to format all the ticklabels
+        locs = (numpy.array(self.locs)-self.offset) /  10**self.orderOfMagnitude+1e-15
+        sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
+                   for loc in locs]
+        sigfigs.sort()
+        if self.format is None:
+            self.format = '%1.' + str(sigfigs[-1]) + 'f'
+        if self._usetex or self._useMathText: self.format = '$%s$'%self.format
+
+
+
 class ClickedPoint:
     '''
     this class defines what a clicked point on the curve plot is
@@ -265,13 +324,15 @@ class ClickedPoint:
         self.dest=None #0 or 1 ; 0=top plot 1=bottom plot
                 
         
-    def find_graph_coords(self, xvector, yvector):
+    def find_graph_coords_old(self, xvector, yvector):
         '''
         Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
         corresponds to the clicked point.
+        OLD & DEPRECATED - to be removed
         '''
                    
         #FIXME: a general algorithm using min() is needed!
+        #print '---DEPRECATED FIND_GRAPH_COORDS_OLD---'
         best_index=0
         best_dist=10**9 #should be more than enough given the scale
                 
@@ -285,7 +346,18 @@ class ClickedPoint:
         self.index=best_index
         self.graph_coords=(xvector[best_index],yvector[best_index])
         return
-    
+            
+    def find_graph_coords(self,xvector,yvector):
+        '''
+        Given a clicked point on the plot, finds the nearest point in the dataset that
+        corresponds to the clicked point.
+        '''
+        dists=[]
+        for index in scipy.arange(1,len(xvector),1):
+            dists.append(((self.absolute_coords[0]-xvector[index])**2)+((self.absolute_coords[1]-yvector[index])**2))
+                        
+        self.index=dists.index(min(dists))
+        self.graph_coords=(xvector[self.index],yvector[self.index])
 #-----------------------------------------
 #CSV-HELPING FUNCTIONS        
         
@@ -331,4 +403,4 @@ def debug():
     print confo.load_config('hooke.conf')
 
 if __name__ == '__main__':
-    debug()
\ No newline at end of file
+    debug()