Index: pr_inversion/distance_explorer.py
===================================================================
--- pr_inversion/distance_explorer.py	(revision d84a90c534af8d3f131c880c4fa0f3dce1b3f116)
+++ pr_inversion/distance_explorer.py	(revision 1db4a53c9610a918db967a664ee32df4cd1b225b)
@@ -56,6 +56,6 @@
         """
         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
+        self._default_min = 0.8 * self.pr_state.d_max
+        self._default_max = 1.2 * self.pr_state.d_max
 
         
@@ -103,5 +103,7 @@
             except:
                 # This inversion failed, skip this D_max value
-                results.errors.append("ExploreDialog: inversion failed for D_max=%s\n %s" % (str(d), sys.exc_value))
+                msg = "ExploreDialog: inversion failed for "
+                msg += "D_max=%s\n %s" % (str(d), sys.exc_value)
+                results.errors.append(msg)
             
         return results
Index: pr_inversion/invertor.py
===================================================================
--- pr_inversion/invertor.py	(revision d84a90c534af8d3f131c880c4fa0f3dce1b3f116)
+++ pr_inversion/invertor.py	(revision 1db4a53c9610a918db967a664ee32df4cd1b225b)
@@ -3,9 +3,15 @@
 The module contains the Invertor class.
 """
-from sans.pr.core.pr_inversion import Cinvertor
+
 import numpy
 import sys
-import math, time
+import math
+import time
+import copy
+import os
+import re
 from numpy.linalg import lstsq
+from scipy import optimize
+from sans.pr.core.pr_inversion import Cinvertor
 
 def help():
@@ -14,19 +20,31 @@
     Future work: extend this function to allow topic selection
     """
-    info_txt  = "The inversion approach is based on Moore, J. Appl. Cryst. (1980) 13, 168-175.\n\n"
-    info_txt += "P(r) is set to be equal to an expansion of base functions of the type "
-    info_txt += "phi_n(r) = 2*r*sin(pi*n*r/D_max). The coefficient of each base functions "
-    info_txt += "in the expansion is found by performing a least square fit with the "
+    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 "
+    info_txt += "of the type "
+    info_txt += "phi_n(r) = 2*r*sin(pi*n*r/D_max). The coefficient of each "
+    info_txt += "base functions "
+    info_txt += "in the expansion is found by performing a least square fit "
+    info_txt += "with the "
     info_txt += "following fit function:\n\n"
-    info_txt += "chi**2 = sum_i[ I_meas(q_i) - I_th(q_i) ]**2/error**2 + Reg_term\n\n"
-    info_txt += "where I_meas(q) is the measured scattering intensity and I_th(q) is "
-    info_txt += "the prediction from the Fourier transform of the P(r) expansion. "
-    info_txt += "The Reg_term term is a regularization term set to the second derivative "
-    info_txt += "d**2P(r)/dr**2 integrated over r. It is used to produce a smooth P(r) output.\n\n"
+    info_txt += "chi**2 = sum_i[ I_meas(q_i) - I_th(q_i) ]**2/error**2 +"
+    info_txt += "Reg_term\n\n"
+    info_txt += "where I_meas(q) is the measured scattering intensity and "
+    info_txt += "I_th(q) is "
+    info_txt += "the prediction from the Fourier transform of the P(r) "
+    info_txt += "expansion. "
+    info_txt += "The Reg_term term is a regularization term set to the second"
+    info_txt += " derivative "
+    info_txt += "d**2P(r)/dr**2 integrated over r. It is used to produce "
+    info_txt += "a smooth P(r) output.\n\n"
     info_txt += "The following are user inputs:\n\n"
-    info_txt += "   - Number of terms: the number of base functions in the P(r) expansion.\n\n"
-    info_txt += "   - Regularization constant: a multiplicative constant to set the size of "
+    info_txt += "   - Number of terms: the number of base functions in the P(r)"
+    info_txt += " expansion.\n\n"
+    info_txt += "   - Regularization constant: a multiplicative constant "
+    info_txt += "to set the size of "
     info_txt += "the regularization term.\n\n"
-    info_txt += "   - Maximum distance: the maximum distance between any two points in the system.\n"
+    info_txt += "   - Maximum distance: the maximum distance between any "
+    info_txt += "two points in the system.\n"
      
     return info_txt
@@ -49,5 +67,6 @@
     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, evaluated at r[j])
+        A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, 
+        evaluated at r[j])
         
     The vector b has its first Npts entries set to
@@ -92,33 +111,35 @@
         x, y, err, d_max, q_min, q_max and alpha
         """
-        if   name=='x':
+        if   name == 'x':
             if 0.0 in value:
-                raise ValueError, "Invertor: one of your q-values is zero. Delete that entry before proceeding"
+                msg = "Invertor: one of your q-values is zero. "
+                msg += "Delete that entry before proceeding"
+                raise ValueError, msg
             return self.set_x(value)
-        elif name=='y':
+        elif name == 'y':
             return self.set_y(value)
-        elif name=='err':
+        elif name == 'err':
             value2 = abs(value)
             return self.set_err(value2)
-        elif name=='d_max':
+        elif name == 'd_max':
             return self.set_dmax(value)
-        elif name=='q_min':
-            if value==None:
+        elif name == 'q_min':
+            if value == None:
                 return self.set_qmin(-1.0)
             return self.set_qmin(value)
-        elif name=='q_max':
-            if value==None:
+        elif name == 'q_max':
+            if value == None:
                 return self.set_qmax(-1.0)
             return self.set_qmax(value)
-        elif name=='alpha':
+        elif name == 'alpha':
             return self.set_alpha(value)
-        elif name=='slit_height':
+        elif name == 'slit_height':
             return self.set_slit_height(value)
-        elif name=='slit_width':
+        elif name == 'slit_width':
             return self.set_slit_width(value)
-        elif name=='has_bck':
-            if value==True:
+        elif name == 'has_bck':
+            if value == True:
                 return self.set_has_bck(1)
-            elif value==False:
+            elif value == False:
                 return self.set_has_bck(0)
             else:
@@ -131,38 +152,38 @@
         Return the value of an attribute
         """
-        import numpy
-        if   name=='x':
+        #import numpy
+        if name == 'x':
             out = numpy.ones(self.get_nx())
             self.get_x(out)
             return out
-        elif name=='y':
+        elif name == 'y':
             out = numpy.ones(self.get_ny())
             self.get_y(out)
             return out
-        elif name=='err':
+        elif name == 'err':
             out = numpy.ones(self.get_nerr())
             self.get_err(out)
             return out
-        elif name=='d_max':
+        elif name == 'd_max':
             return self.get_dmax()
-        elif name=='q_min':
+        elif name == 'q_min':
             qmin = self.get_qmin()
-            if qmin<0:
+            if qmin < 0:
                 return None
             return qmin
-        elif name=='q_max':
+        elif name == 'q_max':
             qmax = self.get_qmax()
-            if qmax<0:
+            if qmax < 0:
                 return None
             return qmax
-        elif name=='alpha':
+        elif name == 'alpha':
             return self.get_alpha()
-        elif name=='slit_height':
+        elif name == 'slit_height':
             return self.get_slit_height()
-        elif name=='slit_width':
+        elif name == 'slit_width':
             return self.get_slit_width()
-        elif name=='has_bck':
+        elif name == 'has_bck':
             value = self.get_has_bck()
-            if value==1:
+            if value == 1:
                 return True
             else:
@@ -176,5 +197,5 @@
         Return a clone of this instance
         """
-        import copy
+        #import copy
         
         invertor = Invertor()
@@ -241,5 +262,5 @@
         
         """
-        return Cinvertor.iq(self, out, q)+self.background
+        return Cinvertor.iq(self, out, q) + self.background
     
     def invert_optimize(self, nfunc=10, nr=20):
@@ -248,10 +269,13 @@
         
         This probably produce more reliable results, but is much slower.
-        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 the square of the first derivative
+        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 
+        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 for the reg. term.
+        :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 
@@ -259,15 +283,18 @@
         """
         
-        from scipy import optimize
-        import time
+        #from scipy import optimize
+        #import time
         
         self.nfunc = nfunc
         # First, check that the current data is valid
-        if self.is_valid()<=0:
-            raise RuntimeError, "Invertor.invert: Data array are of different length"
+        if self.is_valid() <= 0:
+            msg = "Invertor.invert: Data array are of different length"
+            raise RuntimeError, msg
         
         p = numpy.ones(nfunc)
         t_0 = time.time()
-        out, cov_x, info, mesg, success = optimize.leastsq(self.residuals, p, full_output=1, warning=True)
+        out, cov_x, _, _, _ = optimize.leastsq(self.residuals,
+                                                            p, full_output=1,
+                                                             warning=True)
         
         # Compute chi^2
@@ -292,13 +319,16 @@
         This method is provided as a test. 
         """
-        from scipy import optimize
+        #from scipy import optimize
         
         # First, check that the current data is valid
-        if self.is_valid()<=0:
-            raise RuntimeError, "Invertor.invert: Data arrays are of different length"
+        if self.is_valid() <= 0:
+            msg = "Invertor.invert: Data arrays are of different length"
+            raise RuntimeError, msg
         
         p = numpy.ones(nfunc)
         t_0 = time.time()
-        out, cov_x, info, mesg, success = optimize.leastsq(self.pr_residuals, p, full_output=1, warning=True)
+        out, cov_x, info, mesg, success = optimize.leastsq(self.pr_residuals, p,
+                                                            full_output=1,
+                                                            warning=True)
         
         # Compute chi^2
@@ -333,7 +363,7 @@
         Check q-value against user-defined range
         """
-        if not self.q_min==None and q<self.q_min:
+        if not self.q_min == None and q < self.q_min:
             return False
-        if not self.q_max==None and q>self.q_max:
+        if not self.q_max == None and q > self.q_max:
             return False
         return True
@@ -353,5 +383,6 @@
         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[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j])
+            A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2,
+            evaluated at r[j])
             
         The vector b has its first Npts entries set to
@@ -364,5 +395,6 @@
         
         :param nfunc: number of base functions to use.
-        :param nr: number of r points to evaluate the 2nd derivative at for the reg. term.
+        :param nr: number of r points to evaluate the 2nd derivative at
+            for the reg. term.
 
         If the result does not allow us to compute the covariance matrix,
@@ -374,6 +406,7 @@
         # ... before passing it to C
         
-        if self.is_valid()<0:
-            raise RuntimeError, "Invertor: invalid data; incompatible data lengths."
+        if self.is_valid() < 0:
+            msg = "Invertor: invalid data; incompatible data lengths."
+            raise RuntimeError, msg
         
         self.nfunc = nfunc
@@ -383,9 +416,9 @@
         nq   = nr
         sqrt_alpha = math.sqrt(math.fabs(self.alpha))
-        if sqrt_alpha<0.0:
+        if sqrt_alpha < 0.0:
             nq = 0
 
         # If we need to fit the background, add a term
-        if self.has_bck==True:
+        if self.has_bck == True:
             nfunc_0 = nfunc
             nfunc += 1
@@ -415,5 +448,5 @@
         sum_sig, sum_reg = self._get_reg_size(nfunc, nr, a)
                     
-        if math.fabs(self.alpha)>0:
+        if math.fabs(self.alpha) > 0:
             new_alpha = sum_sig/(sum_reg/self.alpha)
         else:
@@ -430,5 +463,5 @@
             
         # Keep a copy of the last output
-        if self.has_bck==False:
+        if self.has_bck == False:
             self.background = 0
             self.out = c
@@ -485,5 +518,5 @@
         elapsed is the computation time
         """
-        import time
+        #import time
         try:            
             pr = self.clone()
@@ -495,10 +528,10 @@
             # If the current alpha is zero, try
             # another value
-            if pr.alpha<=0:
+            if pr.alpha <= 0:
                 pr.alpha = 0.0001
                  
             # Perform inversion to find the largest alpha
             out, cov = pr.invert(nfunc)
-            elapsed = time.time()-starttime
+            elapsed = time.time() - starttime
             initial_alpha = pr.alpha
             initial_peaks = pr.get_peaks(out)
@@ -511,6 +544,7 @@
             # if more than one peak to start with
             # just return the estimate
-            if npeaks>1:
-                #message = "Your P(r) is not smooth, please check your inversion parameters"
+            if npeaks > 1:
+                #message = "Your P(r) is not smooth, 
+                #please check your inversion parameters"
                 message = None
                 return pr.suggested_alpha, message, elapsed
@@ -524,9 +558,9 @@
                 found = False
                 for i in range(10):
-                    pr.alpha = (0.33)**(i+1)*alpha
+                    pr.alpha = (0.33)**(i+1) * alpha
                     out, cov = pr.invert(nfunc)
                     
                     peaks = pr.get_peaks(out)
-                    if peaks>1:
+                    if peaks > 1:
                         found = True
                         break
@@ -536,16 +570,18 @@
                 # the initial alpha already had only one peak,
                 # just return that
-                if not found and initial_peaks==1 and initial_alpha<best_alpha:
+                if not found and initial_peaks == 1 and \
+                    initial_alpha<best_alpha:
                     best_alpha = initial_alpha
                     
                 # Check whether the size makes sense
-                message=''
+                message = ''
                 
                 if not found:
                     message = None
-                elif best_alpha>=0.5*pr.suggested_alpha:
+                elif best_alpha >= 0.5 * pr.suggested_alpha:
                     # best alpha is too big, return a 
                     # reasonable value
-                    message  = "The estimated alpha for your system is too large. "
+                    message  = "The estimated alpha for your system is too "
+                    messsage += "large. "
                     message += "Try increasing your maximum distance."
                 
@@ -577,13 +613,14 @@
         file.write("#slit_width=%g\n" % self.slit_width)
         file.write("#background=%g\n" % self.background)
-        if self.has_bck==True:
+        if self.has_bck == True:
             file.write("#has_bck=1\n")
         else:
             file.write("#has_bck=0\n")
         file.write("#alpha_estimate=%g\n" % self.suggested_alpha)
-        if not self.out==None:
-            if len(self.out)==len(self.cov):
+        if not self.out == None:
+            if len(self.out) == len(self.cov):
                 for i in range(len(self.out)):
-                    file.write("#C_%i=%s+-%s\n" % (i, str(self.out[i]), str(self.cov[i][i])))
+                    file.write("#C_%i=%s+-%s\n" % (i, str(self.out[i]),
+                                                    str(self.cov[i][i])))
         file.write("<r>  <Pr>  <dPr>\n")
         r = numpy.arange(0.0, self.d_max, self.d_max/npts)
@@ -605,6 +642,6 @@
         
         """
-        import os
-        import re
+        #import os
+        #import re
         if os.path.isfile(path):
             try:
@@ -657,8 +694,8 @@
                     elif line.startswith('#has_bck='):
                         toks = line.split('=')
-                        if int(toks[1])==1:
-                            self.has_bck=True
+                        if int(toks[1]) == 1:
+                            self.has_bck = True
                         else:
-                            self.has_bck=False
+                            self.has_bck = False
             
                     # Now read in the parameters
@@ -674,11 +711,11 @@
             
             except:
-                raise RuntimeError, "Invertor.from_file: corrupted file\n%s" % sys.exc_value
+                msg = "Invertor.from_file: corrupted file\n%s" % sys.exc_value
+                raise RuntimeError, msg
         else:
-            raise RuntimeError, "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/num_term.py
===================================================================
--- pr_inversion/num_term.py	(revision d84a90c534af8d3f131c880c4fa0f3dce1b3f116)
+++ pr_inversion/num_term.py	(revision 1db4a53c9610a918db967a664ee32df4cd1b225b)
@@ -1,3 +1,9 @@
-import unittest, math, numpy, sys, string
+
+#import unittest
+import math
+import numpy
+#import sys
+#import string
+import copy
 from sans.pr.invertor import Invertor
 
@@ -11,6 +17,6 @@
         self.nterm_min = 10
         self.nterm_max = len(self.invertor.x)
-        if self.nterm_max>50:
-            self.nterm_max=50
+        if self.nterm_max > 50:
+            self.nterm_max = 50
         self.isquit_func = None
          
@@ -25,10 +31,10 @@
         """
         """
-        return bool(n%2)
+        return bool(n % 2)
 
     def sort_osc(self):
         """
         """
-        import copy
+        #import copy
         osc = copy.deepcopy(self.dataset)
         lis = []
@@ -69,5 +75,5 @@
             osc = inver.oscillations(inver.out)
             err = inver.get_pos_err(inver.out, inver.cov)
-            if osc>10.0:
+            if osc > 10.0:
                 break
             self.osc_list.append(osc)
@@ -79,25 +85,25 @@
         #print "err", self.err_list
         #print "alpha", self.alpha_list
-        new_ls = []
+        #new_ls = []
         new_osc1 = []
-        new_osc2= []
+        new_osc2 = []
         new_osc3 = []
-        flag9=False
-        flag8=False
-        flag7=False
+        flag9 = False
+        flag8 = False
+        flag7 = False
         for i in range(len(self.err_list)):
-            if self.err_list[i] <= 1.0 and self.err_list[i] >=0.9:
+            if self.err_list[i] <= 1.0 and self.err_list[i] >= 0.9:
                 new_osc1.append(self.osc_list[i])
-                flag9=True
-            if self.err_list[i] < 0.9 and self.err_list[i] >=0.8:
+                flag9 = True
+            if self.err_list[i] < 0.9 and self.err_list[i] >= 0.8:
                 new_osc2.append(self.osc_list[i])
-                flag8=True
-            if self.err_list[i] <0.8 and self.err_list[i] >= 0.7:
+                flag8 = True
+            if self.err_list[i] < 0.8 and self.err_list[i] >= 0.7:
                 new_osc3.append(self.osc_list[i])
-                falg7=True
+                falg7 = True
                  
-        if flag9==True:
+        if flag9 == True:
             self.dataset = new_osc1
-        elif flag8==True:
+        elif flag8 == True:
             self.dataset = new_osc2
         else:
@@ -159,5 +165,5 @@
 #For testing
 def load(path):
-    import numpy, math, sys
+    #import numpy, math, sys
     # Read the data from the data file
     data_x   = numpy.zeros(0)
@@ -173,18 +179,18 @@
             try:
                 toks = line.split()
-                x = float(toks[0])
-                y = float(toks[1])
-                if len(toks)>2:
+                test_x = float(toks[0])
+                test_y = float(toks[1])
+                if len(toks) > 2:
                     err = float(toks[2])
                 else:
-                    if scale==None:
-                        scale = 0.05*math.sqrt(y)
+                    if scale == None:
+                        scale = 0.05 * math.sqrt(test_y)
                         #scale = 0.05/math.sqrt(y)
                         min_err = 0.01*y
-                    err = scale*math.sqrt(y)+min_err
+                    err = scale * math.sqrt(test_y) + min_err
                     #err = 0
                     
-                data_x = numpy.append(data_x, x)
-                data_y = numpy.append(data_y, y)
+                data_x = numpy.append(data_x, test_x)
+                data_y = numpy.append(data_y, test_y)
                 data_err = numpy.append(data_err, err)
             except:
