Index: pr_inversion/src/sans/pr/distance_explorer.py
===================================================================
--- pr_inversion/src/sans/pr/distance_explorer.py	(revision d9621b1751f8980454b50eecf3a273d8a2b28e3b)
+++ pr_inversion/src/sans/pr/distance_explorer.py	(revision 34f3ad075ba362aae2af6f3188ecba0c506505c4)
@@ -16,8 +16,6 @@
 over that range.
 """
+import sys
 
-import numpy
-import math
-import sys
 
 class Results:
@@ -40,6 +38,7 @@
         self.bck = []
         self.d_max = []
-        ## List of errors found during the last exploration        
+        ## List of errors found during the last exploration
         self.errors = []
+        
         
 class DistExplorer(object):
@@ -55,8 +54,7 @@
         
         """
-        self.pr_state  = pr_state
+        self.pr_state = pr_state
         self._default_min = 0.8 * self.pr_state.d_max
         self._default_max = 1.2 * self.pr_state.d_max
-
         
     def __call__(self, dmin=None, dmax=None, npts=10):
@@ -80,9 +78,9 @@
         
         # Loop over d_max values
-        for i in range(npts):    
-            d = dmin + i * (dmax - dmin)/(npts-1.0)
+        for i in range(npts):
+            d = dmin + i * (dmax - dmin) / (npts - 1.0)
             self.pr_state.d_max = d
             try:
-                out, cov = self.pr_state.invert(self.pr_state.nfunc)    
+                out, cov = self.pr_state.invert(self.pr_state.nfunc)
             
                 # Store results
@@ -100,5 +98,5 @@
                 results.pos.append(pos)
                 results.pos_err.append(pos_err)
-                results.osc.append(osc)           
+                results.osc.append(osc)
             except:
                 # This inversion failed, skip this D_max value
@@ -108,3 +106,2 @@
             
         return results
-        
Index: pr_inversion/src/sans/pr/invertor.py
===================================================================
--- pr_inversion/src/sans/pr/invertor.py	(revision ea59943a6411c5c66abb2c3919b30a4f628d717c)
+++ pr_inversion/src/sans/pr/invertor.py	(revision 34f3ad075ba362aae2af6f3188ecba0c506505c4)
@@ -20,5 +20,5 @@
     Future work: extend this function to allow topic selection
     """
-    info_txt  = "The inversion approach is based on Moore, J. Appl. Cryst. " 
+    info_txt  = "The inversion approach is based on Moore, J. Appl. Cryst. "
     info_txt += "(1980) 13, 168-175.\n\n"
     info_txt += "P(r) is set to be equal to an expansion of base functions "
@@ -62,10 +62,10 @@
     In the following i refers to the ith base function coefficient.
     The matrix has its entries j in its first Npts rows set to
-        A[j][i] = (Fourier transformed base function for point j) 
+        A[j][i] = (Fourier transformed base function for point j)
         
     We them choose a number of r-points, n_r, to evaluate the second
     derivative of P(r) at. This is used as our regularization term.
     For a vector r of length n_r, the following n_r rows are set to
-        A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, 
+        A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2,
         evaluated at r[j])
         
@@ -101,5 +101,4 @@
     info = {}
     
-    
     def __init__(self):
         Cinvertor.__init__(self)
@@ -109,6 +108,6 @@
         restore the state of invertor for pickle
         """
-        (self.__dict__,self.alpha, self.d_max,
-         self.q_min, self.q_max, 
+        (self.__dict__, self.alpha, self.d_max,
+         self.q_min, self.q_max,
          self.x, self.y,
          self.err, self.has_bck,
@@ -120,12 +119,12 @@
         """
 
-        state = (self.__dict__, 
+        state = (self.__dict__,
                  self.alpha, self.d_max,
                  self.q_min, self.q_max,
                  self.x, self.y,
-                 self.err, self.has_bck, 
+                 self.err, self.has_bck,
                  self.slit_height, self.slit_width,
                  )
-        return (Invertor,tuple(), state, None, None)
+        return (Invertor, tuple(), state, None, None)
     
     def __setattr__(self, name, value):
@@ -224,7 +223,7 @@
         
         invertor = Invertor()
-        invertor.chi2    = self.chi2 
-        invertor.elapsed = self.elapsed 
-        invertor.nfunc   = self.nfunc 
+        invertor.chi2    = self.chi2
+        invertor.elapsed = self.elapsed
+        invertor.nfunc   = self.nfunc
         invertor.alpha   = self.alpha
         invertor.d_max   = self.d_max
@@ -237,5 +236,5 @@
         invertor.has_bck = self.has_bck
         invertor.slit_height = self.slit_height
-        invertor.slit_width  = self.slit_width
+        invertor.slit_width = self.slit_width
         
         invertor.info = copy.deepcopy(self.info)
@@ -254,5 +253,5 @@
         In the following i refers to the ith base function coefficient.
         The matrix has its entries j in its first Npts rows set to
-            A[i][j] = (Fourier transformed base function for point j) 
+            A[i][j] = (Fourier transformed base function for point j)
             
         We them choose a number of r-points, n_r, to evaluate the second
@@ -271,5 +270,5 @@
         :param nfunc: number of base functions to use.
         :param nr: number of r points to evaluate the 2nd derivative at for the reg. term.
-        :return: c_out, c_cov - the coefficients with covariance matrix 
+        :return: c_out, c_cov - the coefficients with covariance matrix
         
         """
@@ -295,19 +294,15 @@
         The minimization function is set to
         sum_i[ (I_obs(q_i) - I_theo(q_i))/err**2 ] + alpha * reg_term,
-        where the reg_term is given by Svergun: it is the integral of 
+        where the reg_term is given by Svergun: it is the integral of
         the square of the first derivative
         of P(r), d(P(r))/dr, integrated over the full range of r.
         
         :param nfunc: number of base functions to use.
-        :param nr: number of r points to evaluate the 2nd derivative at 
+        :param nr: number of r points to evaluate the 2nd derivative at
             for the reg. term.
         
-        :return: c_out, c_cov - the coefficients with covariance matrix 
-        
-        """
-        
-        #from scipy import optimize
-        #import time
-        
+        :return: c_out, c_cov - the coefficients with covariance matrix
+        
+        """
         self.nfunc = nfunc
         # First, check that the current data is valid
@@ -333,5 +328,5 @@
         
         if cov_x is None:
-            cov_x = numpy.ones([nfunc,nfunc])
+            cov_x = numpy.ones([nfunc, nfunc])
             cov_x *= math.fabs(chisqr)
         return out, cov_x
@@ -343,8 +338,6 @@
         with a set of base functions.
         
-        This method is provided as a test. 
-        """
-        #from scipy import optimize
-        
+        This method is provided as a test.
+        """
         # First, check that the current data is valid
         if self.is_valid() <= 0:
@@ -354,5 +347,5 @@
         p = numpy.ones(nfunc)
         t_0 = time.time()
-        out, cov_x, info, mesg, success = optimize.leastsq(self.pr_residuals, p,
+        out, cov_x, _, _, _ = optimize.leastsq(self.pr_residuals, p,
                                                             full_output=1)
         
@@ -371,5 +364,5 @@
     
     def pr_err(self, c, c_cov, r):
-        """    
+        """
         Returns the value of P(r) for a given r, and base function
         coefficients, with error.
@@ -403,5 +396,5 @@
         In the following i refers to the ith base function coefficient.
         The matrix has its entries j in its first Npts rows set to
-            A[i][j] = (Fourier transformed base function for point j) 
+            A[i][j] = (Fourier transformed base function for point j)
             
         We them choose a number of r-points, n_r, to evaluate the second
@@ -449,6 +442,6 @@
             nfunc += 1
 
-        a = numpy.zeros([npts+nq, nfunc])
-        b = numpy.zeros(npts+nq)
+        a = numpy.zeros([npts + nq, nfunc])
+        b = numpy.zeros(npts + nq)
         err = numpy.zeros([nfunc, nfunc])
         
@@ -461,5 +454,5 @@
              
         # Perform the inversion (least square fit)
-        c, chi2, rank, n = lstsq(a, b)
+        c, chi2, _, _ = lstsq(a, b)
         # Sanity check
         try:
@@ -469,5 +462,5 @@
         self.chi2 = chi2
                 
-        inv_cov = numpy.zeros([nfunc,nfunc])
+        inv_cov = numpy.zeros([nfunc, nfunc])
         # Get the covariance matrix, defined as inv_cov = a_transposed * a
         self._get_invcov_matrix(nfunc, nr, a, inv_cov)
@@ -477,5 +470,5 @@
                     
         if math.fabs(self.alpha) > 0:
-            new_alpha = sum_sig/(sum_reg/self.alpha)
+            new_alpha = sum_sig / (sum_reg / self.alpha)
         else:
             new_alpha = 0.0
@@ -484,5 +477,5 @@
         try:
             cov = numpy.linalg.pinv(inv_cov)
-            err = math.fabs(chi2/float(npts-nfunc)) * cov
+            err = math.fabs(chi2 / float(npts - nfunc)) * cov
         except:
             # We were not able to estimate the errors
@@ -509,4 +502,7 @@
             self.cov = err_0
             
+        # Store computation time
+        self.elapsed = time.time() - t_0
+        
         return self.out, self.cov
         
@@ -516,5 +512,5 @@
         number of terms
         
-        :param isquit_func: reference to thread function to call to 
+        :param isquit_func: reference to thread function to call to
                             check whether the computation needs to
                             be stopped.
@@ -529,6 +525,6 @@
         except:
             # If we fail, estimate alpha and return the default
-            # number of terms 
-            best_alpha, message, elapsed =self.estimate_alpha(self.nfunc)
+            # number of terms
+            best_alpha, _, _ = self.estimate_alpha(self.nfunc)
             return self.nfunc, best_alpha, "Could not estimate number of terms"
                     
@@ -547,5 +543,5 @@
         """
         #import time
-        try:            
+        try:
             pr = self.clone()
             
@@ -560,5 +556,5 @@
                  
             # Perform inversion to find the largest alpha
-            out, cov = pr.invert(nfunc)
+            out, _ = pr.invert(nfunc)
             elapsed = time.time() - starttime
             initial_alpha = pr.alpha
@@ -567,5 +563,5 @@
             # Try the inversion with the estimated alpha
             pr.alpha = pr.suggested_alpha
-            out, cov = pr.invert(nfunc)
+            out, _ = pr.invert(nfunc)
     
             npeaks = pr.get_peaks(out)
@@ -573,5 +569,5 @@
             # just return the estimate
             if npeaks > 1:
-                #message = "Your P(r) is not smooth, 
+                #message = "Your P(r) is not smooth,
                 #please check your inversion parameters"
                 message = None
@@ -587,5 +583,5 @@
                 for i in range(10):
                     pr.alpha = (0.33)**(i+1) * alpha
-                    out, cov = pr.invert(nfunc)
+                    out, _ = pr.invert(nfunc)
                     
                     peaks = pr.get_peaks(out)
@@ -599,5 +595,5 @@
                 # just return that
                 if not found and initial_peaks == 1 and \
-                    initial_alpha<best_alpha:
+                    initial_alpha < best_alpha:
                     best_alpha = initial_alpha
                     
@@ -608,8 +604,8 @@
                     message = None
                 elif best_alpha >= 0.5 * pr.suggested_alpha:
-                    # best alpha is too big, return a 
+                    # best alpha is too big, return a
                     # reasonable value
                     message  = "The estimated alpha for your system is too "
-                    messsage += "large. "
+                    message += "large. "
                     message += "Try increasing your maximum distance."
                 
@@ -620,5 +616,4 @@
             return 0, message, elapsed
     
-        
     def to_file(self, path, npts=100):
         """
@@ -660,5 +655,4 @@
         file.close()
      
-        
     def from_file(self, path):
         """
@@ -676,6 +670,6 @@
                 fd = open(path, 'r')
                 
-                buff    = fd.read()
-                lines   = buff.split('\n')
+                buff = fd.read()
+                lines = buff.split('\n')
                 for line in lines:
                     if line.startswith('#d_max='):
@@ -736,5 +730,5 @@
                         self.out[i] = float(toks2[0])
                         
-                        self.cov[i][i] = float(toks2[1])                        
+                        self.cov[i][i] = float(toks2[1])
             
             except:
@@ -742,13 +736,4 @@
                 raise RuntimeError, msg
         else:
-            msg = "Invertor.from_file: '%s' is not a file" % str(path) 
+            msg = "Invertor.from_file: '%s' is not a file" % str(path)
             raise RuntimeError, msg
-        
-         
-if __name__ == "__main__":
-    o = Invertor()
-
-    
-    
-    
-    
Index: pr_inversion/src/sans/pr/num_term.py
===================================================================
--- pr_inversion/src/sans/pr/num_term.py	(revision 3fd8390738adf3ded4344e4ee79f2354ff25d855)
+++ pr_inversion/src/sans/pr/num_term.py	(revision 34f3ad075ba362aae2af6f3188ecba0c506505c4)
@@ -1,12 +1,9 @@
-
-#import unittest
 import math
 import numpy
-#import sys
-#import string
 import copy
 from sans.pr.invertor import Invertor
 
-class Num_terms():    
+
+class Num_terms():
     """
     """
@@ -62,5 +59,5 @@
     def get0_out(self):
         """
-        """ 
+        """
         inver = self.invertor
         self.osc_list = []
@@ -70,5 +67,5 @@
             if self.isquit_func != None:
                 self.isquit_func()
-            best_alpha, message, elapsed = inver.estimate_alpha(k)
+            best_alpha, message, _ = inver.estimate_alpha(k)
             inver.alpha = best_alpha
             inver.out, inver.cov = inver.lstsq(k)
@@ -82,8 +79,4 @@
             self.mess_list.append(message)
          
-        #print "osc", self.osc_list
-        #print "err", self.err_list
-        #print "alpha", self.alpha_list
-        #new_ls = []
         new_osc1 = []
         new_osc2 = []
@@ -101,5 +94,5 @@
             if self.err_list[i] < 0.8 and self.err_list[i] >= 0.7:
                 new_osc3.append(self.osc_list[i])
-                falg7 = True
+                flag7 = True
                  
         if flag9 == True:
@@ -110,5 +103,4 @@
             self.dataset = new_osc3
          
-        #print "dataset", self.dataset
         return self.dataset
         
@@ -148,8 +140,5 @@
         try:
             self.isquit_func = isquit_func
-            #self.nterm_max = len(self.invertor.x)
-            #self.nterm_max = 32
             nts = self.compare_err()
-            #print "nts", nts
             div = len(nts)
             tem = float(div)/2.0
@@ -163,7 +152,7 @@
             return self.nterm_min, self.alpha_list[10], self.mess_list[10]
 
+
 #For testing
 def load(path):
-    #import numpy, math, sys
     # Read the data from the data file
     data_x   = numpy.zeros(0)
@@ -187,5 +176,5 @@
                         scale = 0.05 * math.sqrt(test_y)
                         #scale = 0.05/math.sqrt(y)
-                        min_err = 0.01*y
+                        min_err = 0.01 * y
                     err = scale * math.sqrt(test_y) + min_err
                     #err = 0
@@ -204,18 +193,12 @@
     x, y, erro = load("test/Cyl_A_D102.txt")
     i.d_max = 102.0
-    i.nfunc = 10    
+    i.nfunc = 10
     #i.q_max = 0.4
     #i.q_min = 0.07
-    i.x   = x
-    i.y   = y
-    i.err = erro   
+    i.x = x
+    i.y = y
+    i.err = erro
     #i.out, i.cov = i.lstsq(10)
     # Testing estimator
     est = Num_terms(i)
     print est.num_terms()
-    
-        
-        
-        
-        
-        
