Ignore:
Timestamp:
Mar 29, 2017 10:02:34 AM (8 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:
7d077d1
Parents:
6fd4e36
Message:

Plotting residuals in fitting.
PlotHelper? updates.
Minor refactoring.

File:
1 edited

Legend:

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

    r6fd4e36 r0268aed  
    215215    except ValueError: 
    216216        print "Chi2 calculations: Unmatched lengths %s, %s, %s" % (len(fn), len(gn), len(en)) 
    217         return 
     217        return None 
    218218 
    219219    residuals = res[numpy.isfinite(res)] 
     
    222222    return chisqr 
    223223 
     224def residualsData1D(reference_data, current_data): 
     225    """ 
     226    """ 
     227    # temporary default values for index and weight 
     228    index = None 
     229    weight = None 
     230 
     231    # 1d theory from model_thread is only in the range of index 
     232    if current_data.dy == None or current_data.dy == []: 
     233        dy = numpy.ones(len(current_data.y)) 
     234    else: 
     235        if weight == None: 
     236            dy = numpy.ones(len(current_data.y)) 
     237        else: 
     238            dy = weight 
     239        dy[dy == 0] = 1 
     240    fn = current_data.y[index][0] 
     241    gn = reference_data.y 
     242    en = dy[index][0] 
     243    # build residuals 
     244    residuals = Data1D() 
     245    try: 
     246        y = (fn - gn)/en 
     247        residuals.y = -y 
     248    except: 
     249        msg = "ResidualPlot Error: different # of data points in theory" 
     250        print msg 
     251        y = (fn - gn[index][0]) / en 
     252        residuals.y = y 
     253    residuals.x = current_data.x[index][0] 
     254    residuals.dy = numpy.ones(len(residuals.y)) 
     255    residuals.dx = None 
     256    residuals.dxl = None 
     257    residuals.dxw = None 
     258    residuals.ytransform = 'y' 
     259    # For latter scale changes  
     260    residuals.xaxis('\\rm{Q} ', 'A^{-1}') 
     261    residuals.yaxis('\\rm{Residuals} ', 'normalized') 
     262 
     263    return residuals 
     264 
     265def residualsData2D(reference_data, current_data): 
     266    """ 
     267    """ 
     268    # temporary default values for index and weight 
     269    index = None 
     270    weight = None 
     271 
     272    # build residuals 
     273    residuals = Data2D() 
     274    # Not for trunk the line below, instead use the line above 
     275    current_data.clone_without_data(len(current_data.data), residuals) 
     276    residuals.data = None 
     277    fn = current_data.data 
     278    gn = reference_data.data 
     279    if weight == None: 
     280        en = current_data.err_data 
     281    else: 
     282        en = weight 
     283    residuals.data = (fn - gn) / en 
     284    residuals.qx_data = current_data.qx_data 
     285    residuals.qy_data = current_data.qy_data 
     286    residuals.q_data = current_data.q_data 
     287    residuals.err_data = numpy.ones(len(residuals.data)) 
     288    residuals.xmin = min(residuals.qx_data) 
     289    residuals.xmax = max(residuals.qx_data) 
     290    residuals.ymin = min(residuals.qy_data) 
     291    residuals.ymax = max(residuals.qy_data) 
     292    residuals.q_data = current_data.q_data 
     293    residuals.mask = current_data.mask 
     294    residuals.scale = 'linear' 
     295    # check the lengths 
     296    if len(residuals.data) != len(residuals.q_data): 
     297        return None 
     298    return residuals 
     299 
     300def plotResiduals(reference_data, current_data): 
     301    """ 
     302    Create Data1D/Data2D with residuals, ready for plotting 
     303    """ 
     304    data_copy = deepcopy(current_data) 
     305    # Get data: data I, theory I, and data dI in order 
     306 
     307    method_name = current_data.__class__.__name__ 
     308    residuals_dict = {"Data1D": residualsData1D, 
     309                      "Data2D": residualsData2D} 
     310 
     311    residuals = residuals_dict[method_name](reference_data, data_copy) 
     312 
     313    theory_name = str(current_data.name.split()[0]) 
     314    residuals.name = "Residuals for " + str(theory_name) + "[" + \ 
     315                    str(reference_data.filename) + "]" 
     316    residuals.title = residuals.name 
     317    # when 2 data have the same id override the 1 st plotted 
     318    # include the last part if keeping charts for separate models is required 
     319    residuals.id = "res" + str(reference_data.id) # + str(theory_name) 
     320    # group_id specify on which panel to plot this data 
     321    group_id = reference_data.group_id 
     322    residuals.group_id = "res" + str(group_id) 
     323     
     324    # Symbol 
     325    residuals.symbol = 0 
     326    residuals.hide_error = False 
     327 
     328    return residuals 
     329 
     330 
    224331def binary_encode(i, digits): 
    225332    return [i >> d & 1 for d in xrange(digits)] 
Note: See TracChangeset for help on using the changeset viewer.