Index: Invariant/invariant.py
===================================================================
--- Invariant/invariant.py	(revision ef9ed58d169ae2cfe12d204ef72e695b1c53eb7e)
+++ Invariant/invariant.py	(revision 437a9f0957954babe5f940a58e4ca2389a217b51)
@@ -8,5 +8,4 @@
 
 from DataLoader.data_info import Data1D as LoaderData1D
-#from DataLoader.data_info import Data1D as LoaderData1D
 from DataLoader.qsmearing import smear_selection
 
@@ -50,10 +49,9 @@
         compute f(x)
     """
-    def __init__(self,data , function):
-        """
-            @param function :the function used for computing residuals
-            @param Data: data used for computing residuals
-        """
-        self.function = function
+    def __init__(self,data):
+        """
+            Determine a and b given a linear equation y = ax + b
+            @param Data: data containing x and y  such as  y = ax + b 
+        """
         self.data  = data
         x_len = len(self.data.x) -1
@@ -111,10 +109,13 @@
            
         """
-        fx = self.data.y
+        # Compute theory data f(x)
+        fx = numpy.zeros(len(self.data.x))
+        fx = self.data.y[self.idx_unsmeared ]
         ## Smear theory data
         if self.smearer is not None:
-            fx = self.smearer(fx, self._first_unsmeared_bin,
-                               self._last_unsmeared_bin)                                                         
-        A = numpy.vstack([ self.data.x, numpy.ones(len(self.data.x))]).T
+            fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin)
+    
+        A = numpy.vstack([ self.data.x[self.idx],
+                           numpy.ones(len(self.data.x[self.idx]))]).T
 
         a, b = numpy.linalg.lstsq(A, fx)[0]
@@ -223,13 +224,13 @@
         """
         if function.__name__ == "guinier":
-            fit_x = self._data.x * self._data.x
+            fit_x = numpy.array([x * x for x in self._data.x])
             qmin = qmin**2
             qmax = qmax**2
-            fit_y = [math.log(y) for y in self._data.y]
+            fit_y = numpy.array([math.log(y) for y in self._data.y])
         elif function.__name__ == "power_law":
-            fit_x = [math.log(x) for x in self._data.x]
+            fit_x = numpy.array([math.log(x) for x in self._data.x])
             qmin = math.log(qmin)
             qmax = math.log(qmax)
-            fit_y = [math.log(y) for y in self._data.y]
+            fit_y = numpy.array([math.log(y) for y in self._data.y])
         else:
             raise ValueError("Unknown function used to fit %s"%function.__name__)
@@ -239,16 +240,16 @@
         fit_data.dxw = self._data.dxw   
         
-        functor = FitFunctor(data=fit_data, function= function)
+        functor = FitFunctor(data=fit_data)
         functor.set_fit_range(qmin=qmin, qmax=qmax)
-        a, b = functor.fit()
+        b, a = functor.fit()
         
         if function.__name__ == "guinier":
             # b is the radius value of the guinier function
-            print "b",b
             b = math.sqrt(-3 * b)
-       
+        if function.__name__ == "power_law":
+            b = -1 * b
         # a is the scale of the guinier function
         a = math.exp(a)
-        return a, math.fabs(b)
+        return a, b
     
     def _get_qstar(self, data):
@@ -446,5 +447,5 @@
             n = len(data.x) - 1
             #compute the first delta
-            dx0 = (data.x[1] - data.x[0])/2
+            dx0 = (data.x[1] + data.x[0])/2
             #compute the last delta
             dxn= data.x[n] - data.x[n-1]
@@ -499,5 +500,5 @@
             n = len(data.x) - 1
             #compute the first delta
-            dx0 = (data.x[1] - data.x[0])/2
+            dx0 = (data.x[1] + data.x[0])/2
             #compute the last delta
             dxn = data.x[n] - data.x[n-1]
@@ -570,18 +571,17 @@
             raise RuntimeError, "invariant must be greater than zero"
         
-        print "self._qstar",self._qstar
         # Compute intermediate constant
         k =  1.e-8 * self._qstar/(2 * (math.pi * math.fabs(float(contrast)))**2)
         #Check discriminant value
         discrim = 1 - 4 * k
-        print "discrim",k,discrim
+        
         # Compute volume fraction
         if discrim < 0:
             raise RuntimeError, "could not compute the volume fraction: negative discriminant"
-        elif discrim ==0:
+        elif discrim == 0:
             return 1/2
         else:
-            volume1 = 0.5 *(1 - math.sqrt(discrim))
-            volume2 = 0.5 *(1 + math.sqrt(discrim))
+            volume1 = 0.5 * (1 - math.sqrt(discrim))
+            volume2 = 0.5 * (1 + math.sqrt(discrim))
             
             if 0 <= volume1 and volume1 <= 1:
@@ -640,5 +640,5 @@
         # Data boundaries for fiiting
         qmin = self._data.x[0]
-        qmax = self._data.x[self._low_extrapolation_npts]
+        qmax = self._data.x[self._low_extrapolation_npts - 1]
         
         try:
@@ -648,5 +648,5 @@
         except:
             raise
-            return None
+            #return None
        
         #create new Data1D to compute the invariant
@@ -655,7 +655,5 @@
                                num=INTEGRATION_NSTEPS,
                                endpoint=True)
-        new_y = self._low_extrapolation_function(x=new_x,
-                                                          scale=a, 
-                                                          radius=b)
+        new_y = self._low_extrapolation_function(x=new_x, scale=a, radius=b)
         dxl = None
         dxw = None
@@ -664,6 +662,6 @@
             dxl = dxl * self._data.dxl[0]
         if self._data.dxw is not None:
-            dwl = numpy.ones(1, INTEGRATION_NSTEPS)
-            dwl = dwl * self._data.dwl[0]
+            dxw = numpy.ones(1, INTEGRATION_NSTEPS)
+            dxw = dxw * self._data.dxw[0]
             
         data_min = LoaderData1D(x=new_x, y=new_y)
@@ -690,5 +688,5 @@
         # Data boundaries for fiiting
         x_len = len(self._data.x) - 1
-        qmin = self._data.x[x_len - self._high_extrapolation_npts]
+        qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1) ]
         qmax = self._data.x[x_len]
         
@@ -699,5 +697,5 @@
         except:
             raise
-            return None
+            #return None
        
         #create new Data1D to compute the invariant
@@ -706,7 +704,7 @@
                                num=INTEGRATION_NSTEPS,
                                endpoint=True)
-        new_y = self._high_extrapolation_function(x=new_x,
-                                                  scale=a, 
-                                                  power=b)
+       
+        new_y = self._high_extrapolation_function(x=new_x, scale=a, power=b)
+        
         dxl = None
         dxw = None
@@ -715,6 +713,6 @@
             dxl = dxl * self._data.dxl[0]
         if self._data.dxw is not None:
-            dwl = numpy.ones(1, INTEGRATION_NSTEPS)
-            dwl = dwl * self._data.dwl[0]
+            dxw = numpy.ones(1, INTEGRATION_NSTEPS)
+            dxw = dxw * self._data.dxw[0]
             
         data_max = LoaderData1D(x=new_x, y=new_y)
@@ -746,5 +744,5 @@
             dV = 0.5 * (4*k* dq_star) /(2* math.sqrt(1-k* q_star))
                                  
-            for k = 10^(8)*q_star/(2*(pi*|contrast|)**2)
+            for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2)
             
             q_star: the invariant value including extrapolated value if existing
@@ -760,5 +758,5 @@
             raise ValueError, "invariant must be greater than zero"
         
-        k =  1.e-8 * self._qstar /(2*(math.pi* math.fabs(float(contrast)))**2)
+        k =  1.e-8 * self._qstar /(2 * (math.pi* math.fabs(float(contrast)))**2)
         #check value inside the sqrt function
         value = 1 - k * self._qstar
@@ -766,5 +764,5 @@
             raise ValueError, "Cannot compute incertainty on volume"
         # Compute uncertainty
-        uncertainty = (0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1- k * self._qstar))
+        uncertainty = (0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1 - k * self._qstar))
         
         return volume, math.fabs(uncertainty)
Index: Invariant/test/utest_use_cases.py
===================================================================
--- Invariant/test/utest_use_cases.py	(revision ef9ed58d169ae2cfe12d204ef72e695b1c53eb7e)
+++ Invariant/test/utest_use_cases.py	(revision 437a9f0957954babe5f940a58e4ca2389a217b51)
@@ -93,5 +93,5 @@
         #    parameters. For instance, you might not want to allow 'high' and 'guinier'.
         # The power parameter (not shown below) should default to 4.
-        inv.set_extrapolation(range='low', npts=20, function='guinier')
+        inv.set_extrapolation(range='low', npts=10, function='guinier')
         
         # The version of the call without error
@@ -108,5 +108,5 @@
         
         # Test results
-        self.assertAlmostEquals(qstar, 7.49e-5,2)
+        self.assertAlmostEquals(qstar, 7.49e-5)
         self.assertAlmostEquals(v, 0.005648401)
         self.assertAlmostEquals(s, 9.42e+2,2)
@@ -120,5 +120,5 @@
         
         # Set the extrapolation parameters for the high-Q range
-        inv.set_extrapolation(range='high', npts=20, function='power_law', power=4)
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         
         # The version of the call without error
@@ -146,6 +146,6 @@
         
         # Set the extrapolation parameters for the low- and high-Q ranges
-        inv.set_extrapolation(range='low', npts=5, function='guinier')
-        inv.set_extrapolation(range='high', npts=5, function='power_law', power=4)
+        inv.set_extrapolation(range='low', npts=10, function='guinier')
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         
         # The version of the call without error
@@ -161,5 +161,5 @@
         
         # Test results
-        self.assertAlmostEquals(qstar, 7.90069e-5,2)
+        self.assertAlmostEquals(qstar, 7.88981e-5,2)
         self.assertAlmostEquals(v, 0.005952674)
         self.assertAlmostEquals(s, 9.42e+2,2)
@@ -213,5 +213,5 @@
         inv = invariant.InvariantCalculator(data=self.data_slit_smear)
         # Set the extrapolation parameters for the low-Q range
-        inv.set_extrapolation(range='low', npts=20, function='guinier')
+        inv.set_extrapolation(range='low', npts=10, function='guinier')
         # The version of the call without error
         # At this point, we could still compute Q* without extrapolation by calling
@@ -237,5 +237,5 @@
         
         # Set the extrapolation parameters for the high-Q range
-        inv.set_extrapolation(range='high', npts=20, function='power_law', power=4)
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         
         # The version of the call without error
@@ -263,6 +263,6 @@
         
         # Set the extrapolation parameters for the low- and high-Q ranges
-        inv.set_extrapolation(range='low', npts=20, function='guinier')
-        inv.set_extrapolation(range='high', npts=20, function='power_law', power=4)
+        inv.set_extrapolation(range='low', npts=10, function='guinier')
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         
         # The version of the call without error
@@ -351,5 +351,5 @@
         inv = invariant.InvariantCalculator(data=self.data_q_smear)
         # Set the extrapolation parameters for the high-Q range
-        inv.set_extrapolation(range='high', npts=20, function='power_law', power=4)
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         # The version of the call without error
         qstar = inv.get_qstar(extrapolation='high')
@@ -372,6 +372,6 @@
         inv = invariant.InvariantCalculator(data=self.data_q_smear)
         # Set the extrapolation parameters for the low- and high-Q ranges
-        inv.set_extrapolation(range='low', npts=20, function='guinier')
-        inv.set_extrapolation(range='high', npts=20, function='power_law', power=4)
+        inv.set_extrapolation(range='low', npts=10, function='guinier')
+        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
         # The version of the call without error
         # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
@@ -384,5 +384,5 @@
         
         # Test results
-        self.assertAlmostEquals(qstar, 0.002157677)
+        self.assertAlmostEquals(qstar, 0.0021621)
         self.assertAlmostEquals(v, 0.202846825)
         self.assertAlmostEquals(s, 9.42e+2,2)
