Index: pr_inversion/test/sphere_80.txt
===================================================================
--- pr_inversion/test/sphere_80.txt	(revision eca05c8ee390007a16bb46b22163b8750ee5ab98)
+++ pr_inversion/test/sphere_80.txt	(revision eca05c8ee390007a16bb46b22163b8750ee5ab98)
@@ -0,0 +1,100 @@
+<X>   <Y>
+0.0010101  2.14186e+008
+0.0020202  2.13348e+008
+0.0030303  2.11958e+008
+0.0040404  2.10025e+008
+0.00505051  2.07561e+008
+0.00606061  2.04584e+008
+0.00707071  2.01112e+008
+0.00808081  1.97169e+008
+0.00909091  1.92781e+008
+0.010101  1.87975e+008
+0.0111111  1.82783e+008
+0.0121212  1.77237e+008
+0.0131313  1.71373e+008
+0.0141414  1.65226e+008
+0.0151515  1.58833e+008
+0.0161616  1.52233e+008
+0.0171717  1.45465e+008
+0.0181818  1.38567e+008
+0.0191919  1.31578e+008
+0.020202  1.24536e+008
+0.0212121  1.17479e+008
+0.0222222  1.10442e+008
+0.0232323  1.03461e+008
+0.0242424  9.65692e+007
+0.0252525  8.97973e+007
+0.0262626  8.31748e+007
+0.0272727  7.67288e+007
+0.0282828  7.04839e+007
+0.0292929  6.4462e+007
+0.030303  5.86828e+007
+0.0313131  5.31627e+007
+0.0323232  4.79158e+007
+0.0333333  4.29533e+007
+0.0343434  3.82836e+007
+0.0353535  3.39126e+007
+0.0363636  2.98434e+007
+0.0373737  2.60768e+007
+0.0383838  2.2611e+007
+0.0393939  1.9442e+007
+0.040404  1.65639e+007
+0.0414141  1.39687e+007
+0.0424242  1.16467e+007
+0.0434343  9.58662e+006
+0.0444444  7.77602e+006
+0.0454545  6.20118e+006
+0.0464646  4.84751e+006
+0.0474747  3.69971e+006
+0.0484848  2.74197e+006
+0.0494949  1.95815e+006
+0.0505051  1.33203e+006
+0.0515152  847402
+0.0525253  488319
+0.0535354  239201
+0.0545455  84994.7
+0.0555556  11292.7
+0.0565657  4445.63
+0.0575758  51653.5
+0.0585859  141041
+0.059596  261717
+0.0606061  403816
+0.0616162  558522
+0.0626263  718084
+0.0636364  875805
+0.0646465  1.02603e+006
+0.0656566  1.1641e+006
+0.0666667  1.28634e+006
+0.0676768  1.38998e+006
+0.0686869  1.47309e+006
+0.069697  1.53454e+006
+0.0707071  1.57391e+006
+0.0717172  1.59141e+006
+0.0727273  1.5878e+006
+0.0737374  1.5643e+006
+0.0747475  1.52256e+006
+0.0757576  1.46449e+006
+0.0767677  1.39227e+006
+0.0777778  1.30823e+006
+0.0787879  1.21477e+006
+0.079798  1.11433e+006
+0.0808081  1.00933e+006
+0.0818182  902079
+0.0828283  794770
+0.0838384  689421
+0.0848485  587856
+0.0858586  491670
+0.0868687  402219
+0.0878788  320609
+0.0888889  247687
+0.089899  184050
+0.0909091  130047
+0.0919192  85790.3
+0.0929293  51176.4
+0.0939394  25900.5
+0.0949495  9481.12
+0.0959596  1283.93
+0.0969697  547.497
+0.0979798  6409.98
+0.0989899  17935.9
+0.1  34142.4
Index: pr_inversion/test/utest_invertor.py
===================================================================
--- pr_inversion/test/utest_invertor.py	(revision 9e8dc223ee841261f4cf577f54920873d455660d)
+++ pr_inversion/test/utest_invertor.py	(revision eca05c8ee390007a16bb46b22163b8750ee5ab98)
@@ -8,5 +8,5 @@
 
 
-import unittest, math, numpy
+import unittest, math, numpy, pylab
 from sans.pr.invertor import Invertor
         
@@ -21,5 +21,5 @@
         self.x_in = numpy.ones(self.ntest)
         for i in range(self.ntest):
-            self.x_in[i] = 1.0*i
+            self.x_in[i] = 1.0*(i+1)
 
 
@@ -31,4 +31,12 @@
         self.invertor.d_max = value
         self.assertEqual(self.invertor.d_max, value)
+        
+    def testset_alpha(self):
+        """
+            Set and read alpha
+        """
+        value = 15.0
+        self.invertor.alpha = value
+        self.assertEqual(self.invertor.alpha, value)
         
     def testset_x_1(self):
@@ -113,4 +121,160 @@
         self.assertEqual(self.invertor.test_no_data, None)
         
+    def test_inversion(self):
+        """
+            Test an inversion for which we know the answer
+        """
+        x, y, err = load("sphere_80.txt")
+
+        # Choose the right d_max...
+        self.invertor.d_max = 160.0
+        # Set a small alpha
+        self.invertor.alpha = 1e-7
+        # Set data
+        self.invertor.x   = x
+        self.invertor.y   = y
+        self.invertor.err = err
+        # Perform inversion
+        out, cov = self.invertor.invert(10)
+        # This is a very specific case
+        # We should make sure it always passes
+        self.assertTrue(self.invertor.chi2/len(x)<200.00)
+        
+        # Check the computed P(r) with the theory
+        # for shpere of radius 80
+        x = pylab.arange(0.01, self.invertor.d_max, self.invertor.d_max/51.0)
+        y = numpy.zeros(len(x))
+        dy = numpy.zeros(len(x))
+        y_true = numpy.zeros(len(x))
+
+        sum = 0.0
+        sum_true = 0.0
+        for i in range(len(x)):
+            #y[i] = self.invertor.pr(out, x[i])
+            (y[i], dy[i]) = self.invertor.pr_err(out, cov, x[i])
+            sum += y[i]
+            if x[i]<80.0:
+                y_true[i] = pr_theory(x[i], 80.0)
+            else:
+                y_true[i] = 0
+            sum_true += y_true[i]
+            
+        y = y/sum*self.invertor.d_max/len(x)
+        dy = dy/sum*self.invertor.d_max/len(x)
+        y_true = y_true/sum_true*self.invertor.d_max/len(x)
+        
+        chi2 = 0.0
+        for i in range(len(x)):
+            res = (y[i]-y_true[i])/dy[i]
+            chi2 += res*res
+            
+        try:
+            self.assertTrue(chi2/51.0<10.0)
+        except:
+            print "chi2 =", chi2/51.0
+            raise
+            
+    def test_q_zero(self):
+        """
+            Test error condition where a point has q=0
+        """
+        x, y, err = load("sphere_80.txt")
+        x[0] = 0.0
+        
+        # Choose the right d_max...
+        self.invertor.d_max = 160.0
+        # Set a small alpha
+        self.invertor.alpha = 1e-7
+        # Set data
+        def doit():
+            self.invertor.x   = x
+        self.assertRaises(ValueError, doit)
+        
+                            
+    def test_q_neg(self):
+        """
+            Test error condition where a point has q<0
+        """
+        x, y, err = load("sphere_80.txt")
+        x[0] = -0.2
+        
+        # Choose the right d_max...
+        self.invertor.d_max = 160.0
+        # Set a small alpha
+        self.invertor.alpha = 1e-7
+        # Set data
+        self.invertor.x   = x
+        self.invertor.y   = y
+        self.invertor.err = err
+        # Perform inversion
+        out, cov = self.invertor.invert(4)
+        
+        try:
+            self.assertTrue(self.invertor.chi2>0)
+        except:
+            print "Chi2 =", self.invertor.chi2
+            raise
+                            
+    def test_Iq_zero(self):
+        """
+            Test error condition where a point has q<0
+        """
+        x, y, err = load("sphere_80.txt")
+        y[0] = 0.0
+        
+        # Choose the right d_max...
+        self.invertor.d_max = 160.0
+        # Set a small alpha
+        self.invertor.alpha = 1e-7
+        # Set data
+        self.invertor.x   = x
+        self.invertor.y   = y
+        self.invertor.err = err
+        # Perform inversion
+        out, cov = self.invertor.invert(4)
+        
+        try:
+            self.assertTrue(self.invertor.chi2>0)
+        except:
+            print "Chi2 =", self.invertor.chi2
+            raise
+                            
+        
+
+def pr_theory(r, R):
+    """
+       P(r) for a sphere
+    """
+    if r<=2*R:
+        return 12.0* ((0.5*r/R)**2) * ((1.0-0.5*r/R)**2) * ( 2.0 + 0.5*r/R )
+    else:
+        return 0.0
+    
+def load(path = "sphere_60_q0_2.txt"):
+    import numpy, math, sys
+    # Read the data from the data file
+    data_x   = numpy.zeros(0)
+    data_y   = numpy.zeros(0)
+    data_err = numpy.zeros(0)
+    if not path == None:
+        input_f = open(path,'r')
+        buff    = input_f.read()
+        lines   = buff.split('\n')
+        for line in lines:
+            try:
+                toks = line.split()
+                x = float(toks[0])
+                y = float(toks[1])
+                data_x = numpy.append(data_x, x)
+                data_y = numpy.append(data_y, y)
+                # Set the error of the first point to 5%
+                # to make theory look like data
+                scale = 0.1/math.sqrt(data_x[0])
+                data_err = numpy.append(data_err, scale*math.sqrt(y))
+            except:
+                pass
+               
+    return data_x, data_y, data_err 
+       
 if __name__ == '__main__':
     unittest.main()
