Index: Invariant/invariant.py
===================================================================
--- Invariant/invariant.py	(revision 4e80ae0b12cbe6e6c3786a9527e2dc9df7e0ff4c)
+++ Invariant/invariant.py	(revision 9b6497bb77e9fe4f94a1caa9f3b72552d19deb2a)
@@ -6,4 +6,5 @@
 import math 
 import numpy
+import scipy
 
 from DataLoader.data_info import Data1D as LoaderData1D
@@ -20,5 +21,5 @@
 INTEGRATION_NSTEPS = 1000
 
-def guinier(x, scale=1, radius=0.1):
+def guinier(x, scale=1, radius=60):
     """
         Compute a F(x) = scale* e-((radius*x)**2/3).
@@ -28,8 +29,13 @@
         @return F(x)
     """   
+    if radius <= 0:
+        raise ValueError("Rg expected positive value, but got %s"%radius)  
     value = numpy.array([math.exp(-((radius * i)**2/3)) for i in x ]) 
+    scale = numpy.sqrt(scale*scale)      
+    if scale == 0:
+        raise ValueError("scale expected positive value, but got %s"%scale) 
     return scale * value
 
-def power_law(x, scale=1, power=0):
+def power_law(x, scale=1, power=4):
     """
         F(x) = scale* (x)^(-power)
@@ -43,7 +49,10 @@
     """  
     if power <=0:
-        raise ValueError("Power_law function expected posive power, but got %s"%power)
+        raise ValueError("Power_law function expected positive power, but got %s"%power)
     
     value = numpy.array([ math.pow(i, -power) for i in x ])  
+    scale = numpy.sqrt(scale*scale)
+    if scale == 0:
+        raise ValueError("scale expected positive value, but got %s"%scale) 
     return scale * value
 
@@ -111,13 +120,20 @@
            
         """
+        
+        fx = numpy.zeros(len(self.data.x))
+        sigma = numpy.zeros(len(self.data.x))
+
+        #define dy^2
+        sigma = self.data.dy[self.idx_unsmeared ]
+        sigma2 = sigma*sigma
+
         # Compute theory data f(x)
-        fx = numpy.zeros(len(self.data.x))
-        fx = self.data.y[self.idx_unsmeared ]
+        fx = self.data.y[self.idx_unsmeared ]/sigma2
         ## 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[self.idx],
-                           numpy.ones(len(self.data.x[self.idx]))]).T
+        A = numpy.vstack([ self.data.x[self.idx]/sigma2,
+                           numpy.ones(len(self.data.x[self.idx]))/sigma2]).T
 
         a, b = numpy.linalg.lstsq(A, fx)[0]
@@ -178,4 +194,5 @@
             raise ValueError,"Data must be of type DataLoader.Data1D"
         new_data = self._scale * data - self._background
+        new_data.dy = data.dy
         new_data.dxl = data.dxl
         new_data.dxw = data.dxw
@@ -199,24 +216,33 @@
         if function.__name__ == "guinier":
             fit_x = numpy.array([x * x for x in self._data.x])
+            
             qmin = qmin**2
             qmax = qmax**2
             fit_y = numpy.array([math.log(y) for y in self._data.y])
+            fit_dy = numpy.array([y for y in self._data.y])
+            fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy
+
         elif function.__name__ == "power_law":
             if power is None:
                 fit_x = numpy.array([math.log(x) for x in self._data.x])
+
                 qmin = math.log(qmin)
                 qmax = math.log(qmax)
-                fit_y = numpy.array([math.log(y) for y in self._data.y])
+            fit_y = numpy.array([math.log(y) for y in self._data.y])
+            fit_dy = numpy.array([y for y in self._data.y])
+            fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy
+
         else:
             raise ValueError("Unknown function used to fit %s"%function.__name__)
-            
-        
-        if function.__name__ == "power_law" and  power is None:
+        
+        if function.__name__ == "power_law" and  power != None:
             b = math.fabs(power)
-            fit_x = numpy.array([math.pow(x, -b) for x in self._data.x])
             fit_y = numpy.array([math.log(y) for y in self._data.y])
-            a = (fit_y - fit_x)/(len(self._data.x))
-        else:
-            fit_data = LoaderData1D(x=fit_x, y=fit_y)
+            fit_dy = numpy.array([y for y in self._data.y])
+            fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy
+            sigma2 = fit_dy*fit_dy
+            a = scipy.sum(fit_y/sigma2) - scipy.sum(fit_x/sigma2*b)/scipy.sum(sigma2)
+        else:
+            fit_data = LoaderData1D(x=fit_x, y=fit_y, dy=fit_dy)
             fit_data.dxl = self._data.dxl
             fit_data.dxw = self._data.dxw   
@@ -225,13 +251,21 @@
             b, a = functor.fit()
         
+                  
         if function.__name__ == "guinier":
             # b is the radius value of the guinier function
-            b = math.sqrt(-3 * b)
+            if b>=0:
+                raise ValueError("Guinier fit was not converged")
+            else:
+                b = math.sqrt(-3 * b)
+
+
         if function.__name__ == "power_law":
-            b = -1 * b
+            if power == None:
+                b = -1 * b
             if b <= 0:
                 raise ValueError("Power_law fit expected posive power, but got %s"%power)
         # a is the scale of the guinier function
         a = math.exp(a)
+        
         return a, b
     
@@ -502,4 +536,5 @@
             @return: a new data of type Data1D
         """
+        
         # Data boundaries for fiiting
         qmin = self._data.x[0]
@@ -514,5 +549,5 @@
         except:
             return None
-       
+        
         #q_start point
         q_start = Q_MINIMUM
@@ -534,10 +569,10 @@
             dxw = numpy.ones(INTEGRATION_NSTEPS)
             dxw = dxw * self._data.dxw[0]
-            
+
         data_min = LoaderData1D(x=new_x, y=new_y)
         data_min.dxl = dxl
         data_min.dxw = dxw
         self._data.clone_without_data( clone= data_min)
-        
+
         return data_min
           
Index: Invariant/test/utest_use_cases.py
===================================================================
--- Invariant/test/utest_use_cases.py	(revision 4e80ae0b12cbe6e6c3786a9527e2dc9df7e0ff4c)
+++ Invariant/test/utest_use_cases.py	(revision 9b6497bb77e9fe4f94a1caa9f3b72552d19deb2a)
@@ -345,5 +345,5 @@
         
         # Test results
-        self.assertAlmostEquals(qstar, 0.002037677)
+        self.assertAlmostEquals(qstar, 0.00138756,2)
         self.assertAlmostEquals(v, 0.115352622)
         self.assertAlmostEquals(s + _ERR_SURFACE, 9.42e+2, 1)
@@ -389,5 +389,5 @@
         
         # Test results
-        self.assertAlmostEquals(qstar, 0.0021621, 3)
+        self.assertAlmostEquals(qstar, 0.00460319, 3)
         self.assertAlmostEquals(v, 0.202846825)
         self.assertAlmostEquals(s+ _ERR_SURFACE, 9.42e+2, 1)
