Ignore:
Timestamp:
Mar 28, 2017 8:53:29 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
0268aed
Parents:
a9b568c
Message:

Chi2 display + minor refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/FittingUtilities.py

    ra9b568c r6fd4e36  
    11from PyQt4 import QtGui 
    22from PyQt4 import QtCore 
     3 
     4import numpy 
     5from copy import deepcopy 
     6 
     7from sas.sasgui.guiframe.dataFitting import Data1D 
     8from sas.sasgui.guiframe.dataFitting import Data2D 
    39 
    410def replaceShellName(param_name, value): 
     
    159165            model.appendRow([item1, item2, item3, item4, item5]) 
    160166 
     167def calculateChi2(reference_data, current_data): 
     168    """ 
     169    Calculate Chi2 value between two sets of data 
     170    """ 
     171 
     172    # WEIGHING INPUT 
     173    #from sas.sasgui.perspectives.fitting.utils import get_weight 
     174    #flag = self.get_weight_flag() 
     175    #weight = get_weight(data=self.data, is2d=self._is_2D(), flag=flag) 
     176 
     177    if reference_data == None: 
     178       return chisqr 
     179 
     180    # temporary default values for index and weight 
     181    index = None 
     182    weight = None 
     183 
     184    # Get data: data I, theory I, and data dI in order 
     185    if isinstance(reference_data, Data2D): 
     186        if index == None: 
     187            index = numpy.ones(len(current_data.data), dtype=bool) 
     188        if weight != None: 
     189            current_data.err_data = weight 
     190        # get rid of zero error points 
     191        index = index & (current_data.err_data != 0) 
     192        index = index & (numpy.isfinite(current_data.data)) 
     193        fn = current_data.data[index] 
     194        gn = reference_data.data[index] 
     195        en = current_data.err_data[index] 
     196    else: 
     197        # 1 d theory from model_thread is only in the range of index 
     198        if index == None: 
     199            index = numpy.ones(len(current_data.y), dtype=bool) 
     200        if weight != None: 
     201            current_data.dy = weight 
     202        if current_data.dy == None or current_data.dy == []: 
     203            dy = numpy.ones(len(current_data.y)) 
     204        else: 
     205            ## Set consistently w/AbstractFitengine: 
     206            # But this should be corrected later. 
     207            dy = deepcopy(current_data.dy) 
     208            dy[dy == 0] = 1 
     209        fn = current_data.y[index] 
     210        gn = reference_data.y 
     211        en = dy[index] 
     212    # Calculate the residual 
     213    try: 
     214        res = (fn - gn) / en 
     215    except ValueError: 
     216        print "Chi2 calculations: Unmatched lengths %s, %s, %s" % (len(fn), len(gn), len(en)) 
     217        return 
     218 
     219    residuals = res[numpy.isfinite(res)] 
     220    chisqr = numpy.average(residuals * residuals) 
     221 
     222    return chisqr 
     223 
     224def binary_encode(i, digits): 
     225    return [i >> d & 1 for d in xrange(digits)] 
     226 
Note: See TracChangeset for help on using the changeset viewer.