Index: Invariant/invariant.py
===================================================================
--- Invariant/invariant.py	(revision 9e37661a5a41bee28880e103e20a293255a1beef)
+++ Invariant/invariant.py	(revision 793e52fc7d1ce02a4e480778cc652f6fe43d36a9)
@@ -66,17 +66,26 @@
         """
         self.data  = data
-        
-        # Set qmin as the lowest non-zero value
-        self.qmin = Q_MINIMUM
-        for q_value in self.data.x:
-            if q_value>0: 
-                self.qmin = q_value
-                break
-        self.qmax = max(self.data.x)
-       
+        x_len = len(self.data.x) -1
+        #fitting range 
+        self.qmin =  self.data.x[0]    
+        if self.qmin == 0:
+            self.qmin = Q_MINIMUM 
+    
+        self.qmax = self.data.x[x_len]
+        #Unsmeared q range
+        self._qmin_unsmeared = 0
+        self._qmax_unsmeared = self.data.x[x_len]
+        
+        #bin for smear data
+        self._first_unsmeared_bin = 0
+        self._last_unsmeared_bin  = x_len
+        
+        # Identify the bin range for the unsmeared and smeared spaces
+        self.idx = (self.data.x >= self.qmin) & (self.data.x <= self.qmax)
+        self.idx_unsmeared = (self.data.x >= self._qmin_unsmeared) \
+                            & (self.data.x <= self._qmax_unsmeared)
+  
         #get the smear object of data
         self.smearer = smear_selection( self.data )
-        # Set the q-range information to allow smearing
-        self.set_fit_range()
       
     def set_fit_range(self ,qmin=None, qmax=None):
@@ -92,5 +101,8 @@
         self._qmax_unsmeared = self.qmax    
         
-        if self.smearer is not None:
+        self._first_unsmeared_bin = 0
+        self._last_unsmeared_bin  = len(self.data.x)-1
+        
+        if self.smearer!=None:
             self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax)
             self._qmin_unsmeared = self.data.x[self._first_unsmeared_bin]
@@ -107,31 +119,32 @@
            @param power = a fixed, otherwise None
         """
+        power = power
         fx = numpy.zeros(len(self.data.x))
-
-        # Uncertainty
-        if type(self.data.y)==numpy.ndarray and len(self.data.y)==len(self.data.x):
-            sigma = self.data.y
-        else:
-            sigma = numpy.ones(len(self.data.x))
+        one = numpy.ones(len(self.data.x))
+
+        #define dy^2
+        try:
+            sigma = self.data.dy[self.idx_unsmeared ]
+        except:
+            print "The dy data for Invariant calculation should be prepared before get to FitFunctor.fit()..."
+            sigma = one[self.idx_unsmeared ]
+        sigma2 = sigma * sigma
 
         # Compute theory data f(x)
-        fx[self.idx_unsmeared] = self.data.y[self.idx_unsmeared]
+        fx = self.data.y[self.idx_unsmeared ]/sigma
         ## Smear theory data
         if self.smearer is not None:
             fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin)
         
-        fx[self.idx_unsmeared] = fx[self.idx_unsmeared]/sigma[self.idx_unsmeared]
-        
         ##power is given only for function = power_law    
         if power != None:
-            sigma2 = sigma * sigma
             a = -(power)
-            b = (numpy.sum(fx[self.idx]/sigma[self.idx]) - a*numpy.sum(self.data.x[self.idx]/sigma2[self.idx]))/numpy.sum(numpy.ones(len(sigma2[self.idx]))/sigma2[self.idx])
+            b = (numpy.sum(fx/sigma) - a*numpy.sum(self.data.x[self.idx]/sigma2))/numpy.sum(numpy.ones(len(sigma2))/sigma2)
             return a, b
         else:
-            A = numpy.vstack([ self.data.x[self.idx]/sigma[self.idx],
-                               numpy.ones(len(self.data.x[self.idx]))/sigma[self.idx]]).T
+            A = numpy.vstack([ self.data.x[self.idx]/sigma,
+                               numpy.ones(len(self.data.x[self.idx]))/sigma]).T
            
-            a, b = numpy.linalg.lstsq(A, fx[self.idx])[0]
+            a, b = numpy.linalg.lstsq(A, fx)[0]
             return a, b
 
@@ -152,6 +165,6 @@
             Initialize variables
             @param data: data must be of type DataLoader.Data1D
-            @param background: Background value. The data will be corrected before processing
-            @param scale: Scaling factor for I(q). The data will be corrected before processing
+            @param contrast: contrast value of type float
+            @param pConst: Porod Constant of type float
         """
         # Background and scale should be private data member if the only way to
@@ -189,11 +202,15 @@
             #Process only data that inherited from DataLoader.Data_info.Data1D
             raise ValueError,"Data must be of type DataLoader.Data1D"
-            
-        new_data = (self._scale * data) - self._background
-        
-        # Copy data that is not copied by the operations
-        #TODO: fix this in DataLoader
-        new_data.dxl = data.dxl
-        new_data.dxw = data.dxw        
+        new_data = self._scale * data - self._background
+        try:
+            #All data should pass here.
+            new_data.dy = data.dy
+            new_data.dxl = data.dxl
+            new_data.dxw = data.dxw        
+        except:
+            #in case...
+            new_data.dy = numpy.ones(len(data.x))
+            new_data.dxl = numpy.zeros(len(data.x))
+            new_data.dxw = numpy.zeros(len(data.x))       
 
         return new_data
@@ -393,4 +410,7 @@
             note: if data doesn't contain dy assume dy= math.sqrt(data.y)
         """
+        if data is None:
+            data = self.data
+            
         if len(data.x) <= 1 or len(data.y) <= 1 or \
             len(self.data.x) != len(self.data.y):
@@ -528,11 +548,13 @@
         qmin = self._data.x[0]
         qmax = self._data.x[self._low_extrapolation_npts - 1]
-        
-        # Extrapolate the low-Q data
-        #TODO: this fit fails. Fix it.
-        a, b = self._fit(function=self._low_extrapolation_function,
-                          qmin=qmin,
-                          qmax=qmax,
-                          power=self._low_extrapolation_power)
+        try:
+            # fit the data with a model to get the appropriate parameters
+            a, b = self._fit(function=self._low_extrapolation_function,
+                              qmin=qmin,
+                              qmax=qmax,
+                              power=self._low_extrapolation_power)
+        except:
+            #raise
+            return None
         
         #q_start point
