Changeset 3906


Ignore:
Timestamp:
Apr 16, 2019 9:39:40 PM (3 years ago)
Author:
toby
Message:

implement fixed backgound file in Riteveld Fit; show Rietveld & peak fits with bkg file added to calc bkg, not subtracted

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIplot.py

    r3900 r3906  
    27512751        PlotList = [Pattern,]
    27522752        PId = G2gd.GetGPXtreeItemId(G2frame,G2frame.PatternId, 'Background')
    2753         Pattern[0]['BackFile'] = ['',-1.0]
     2753        Pattern[0]['BackFile'] = ['',-1.0,False]
    27542754        if PId:
    2755             Pattern[0]['BackFile'] =  G2frame.GPXtree.GetItemPyData(PId)[1].get('background PWDR',['',-1.0])
     2755            Pattern[0]['BackFile'] =  G2frame.GPXtree.GetItemPyData(PId)[1].get('background PWDR',['',-1.0,False])
    27562756        Parms,Parms2 = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,
    27572757            G2frame.PatternId, 'Instrument Parameters'))
     
    27862786                Page.plotStyle.update({'Offset':[0.0,0.0],'delOffset':0.02*Ymax,'refOffset':-0.1*Ymax,'refDelt':0.1*Ymax,})
    27872787            PId = G2gd.GetGPXtreeItemId(G2frame,G2frame.PatternId, 'Background')
    2788             Pattern[0]['BackFile'] = ['',-1.0]
     2788            Pattern[0]['BackFile'] = ['',-1.0,False]
    27892789            if PId:
    2790                 Pattern[0]['BackFile'] =  G2frame.GPXtree.GetItemPyData(PId)[1].get('background PWDR',['',-1.0])
     2790                Pattern[0]['BackFile'] =  G2frame.GPXtree.GetItemPyData(PId)[1].get('background PWDR',['',-1.0,False])
    27912791            PlotList.append(Pattern)
    27922792            ParmList.append(G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,
     
    28052805    for ip,Pattern in enumerate(PlotList):
    28062806        xye = Pattern[1]
    2807         bxye = G2pdG.GetFileBackground(G2frame,xye,Pattern)
     2807        #bxye = G2pdG.GetFileBackground(G2frame,xye,Pattern)
    28082808        if xye[1] is None: continue
    2809         if Ymax is None: Ymax = max(xye[1]+bxye)
    2810         Ymax = max(Ymax,max(xye[1]+bxye))
     2809        if Ymax is None: Ymax = max(xye[1])
     2810        Ymax = max(Ymax,max(xye[1]))
    28112811    if Ymax is None: return # nothing to plot
    28122812    offsetX = Page.plotStyle['Offset'][1]
     
    28802880        xye = np.array(ma.getdata(Pattern[1])) # strips mask
    28812881        xye0 = Pattern[1][0]  # keeps mask
    2882         bxye = G2pdG.GetFileBackground(G2frame,xye,Pattern)
     2882        #bxye = G2pdG.GetFileBackground(G2frame,xye,Pattern)
    28832883        if PickId:
    28842884            ifpicked = Pattern[2] == G2frame.GPXtree.GetItemText(PatternId)
     
    29532953            if Page.plotStyle['sqrtPlot']:
    29542954                olderr = np.seterr(invalid='ignore') #get around sqrt(-ve) error
    2955                 Y = np.where(xye[1]+bxye>=0.,np.sqrt(xye[1]+bxye),-np.sqrt(-xye[1]-bxye))+bxye+NoffY*Ymax/100.0
     2955                Y = np.where(xye[1]>=0.,np.sqrt(xye[1]),-np.sqrt(-xye[1]))+NoffY*Ymax/100.0
    29562956                np.seterr(invalid=olderr['invalid'])
    29572957            elif 'PWDR' in plottype and G2frame.SinglePlot and not (
    29582958                Page.plotStyle['logPlot'] or Page.plotStyle['sqrtPlot'] or G2frame.Contour):
    2959                 Y = xye[1]*multArray+bxye+NoffY*Ymax/100.0
     2959                Y = xye[1]*multArray+NoffY*Ymax/100.0
    29602960            else:
    2961                 Y = xye[1]+bxye+NoffY*Ymax/100.0
     2961                Y = xye[1]+NoffY*Ymax/100.0
    29622962        elif plottype in ['SASD','REFD']:
    29632963            if plottype == 'SASD':
  • trunk/GSASIIpwd.py

    r3848 r3906  
    716716    return intens*Df(xdata)*DX/dx
    717717
    718 def getBackground(pfx,parmDict,bakType,dataType,xdata):
    719     'needs a doc string'
     718def getBackground(pfx,parmDict,bakType,dataType,xdata,fixedBkg={}):
     719    '''Computes the background from vars pulled from gpx file or tree.
     720    '''
    720721    if 'T' in dataType:
    721722        q = 2.*np.pi*parmDict[pfx+'difC']/xdata
     
    837838        except ValueError:
    838839            print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
    839             break       
     840            break
     841    # fixed background from file
     842    if len(fixedBkg) >= 3:
     843        mult = fixedBkg.get('_fixedMult',0.0)
     844        if len(fixedBkg.get('_fixedValues',[])) != len(yb):
     845            print('Lengths of backgrounds do not agree: yb={}, fixed={}'.format(
     846                len(yb),len(fixedBkg.get('_fixedValues',[]))))
     847        elif mult:
     848            yb -= mult*fixedBkg.get('_fixedValues',[]) # N.B. mult is negative
     849            sumBk[0] = sum(yb)
    840850    return yb,sumBk
    841851   
     
    10871097
    10881098def getPeakProfile(dataType,parmDict,xdata,varyList,bakType):
    1089     'needs a doc string'
     1099    'Computes the profile for a powder pattern'
    10901100   
    10911101    yb = getBackground('',parmDict,bakType,dataType,xdata)[0]
     
    14041414   
    14051415def SetBackgroundParms(Background):
    1406     'needs a doc string'
     1416    'Loads background parameters into dicts/lists to create varylist & parmdict'
    14071417    if len(Background) == 1:            # fix up old backgrounds
    14081418        Background.append({'nDebye':0,'debyeTerms':[]})
     
    14421452            peaksVary.append(item[0])
    14431453    backDict.update(peaksDict)
    1444     backVary += peaksVary   
     1454    backVary += peaksVary
    14451455    return bakType,backDict,backVary
    14461456   
     
    18671877    if dlg: dlg.Destroy()
    18681878    sigDict = dict(zip(varyList,sig))
    1869     yb[xBeg:xFin] = getBackground('',parmDict,bakType,dataType,x[xBeg:xFin])[0]
    1870     yc[xBeg:xFin] = getPeakProfile(dataType,parmDict,x[xBeg:xFin],varyList,bakType)
    1871     yd[xBeg:xFin] = (y+fixback)[xBeg:xFin]-yc[xBeg:xFin]
     1879    yb[xBeg:xFin] = getBackground('',parmDict,bakType,dataType,x[xBeg:xFin])[0]-fixback[xBeg:xFin]
     1880    yc[xBeg:xFin] = getPeakProfile(dataType,parmDict,x[xBeg:xFin],varyList,bakType)-fixback[xBeg:xFin]
     1881    yd[xBeg:xFin] = y[xBeg:xFin]-yc[xBeg:xFin]
    18721882    GetBackgroundParms(parmDict,Background)
    18731883    if bakVary: BackgroundPrint(Background,sigDict)
     
    28482858    fplot.plot(xdata,getBackground('',parmDict1,bakType,'PXC',xdata)[0])   
    28492859    fplot.plot(xdata,getPeakProfile(parmDict1,xdata,varyList,bakType))
    2850     
     2860   
    28512861def test1():
    28522862    if NeedTestData: TestData()
  • trunk/GSASIIpwdGUI.py

    r3886 r3906  
    286286    bxye = np.zeros(len(xye[1]))
    287287    if 'BackFile' in Pattern[0]:
    288         backfile,mult = Pattern[0]['BackFile']
     288        backfile,mult = Pattern[0]['BackFile'][:2]
    289289        if backfile:
    290290            bId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,backfile)
     
    15651565        def OnBackPWDR(event):
    15661566            data[1]['background PWDR'][0] = back.GetValue()
     1567            if  data[1]['background PWDR'][0]:
     1568                curHist = G2frame.GPXtree.GetItemPyData(G2frame.PatternId)
     1569                Id = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,data[1]['background PWDR'][0])
     1570                if not Id:
     1571                    G2G.G2MessageBox(G2frame,'Histogram not found -- how did this happen?','Missing histogram')
     1572                    back.SetValue('')
     1573                    data[1]['background PWDR'][0] = back.GetValue()
     1574                    return
     1575                bkgHist = G2frame.GPXtree.GetItemPyData(Id)
     1576                if len(bkgHist[1][0]) != len(curHist[1][0]):
     1577                    G2G.G2MessageBox(G2frame,'Histogram have different lengths','Mismatched histograms')
     1578                    back.SetValue('')
     1579                    data[1]['background PWDR'][0] = back.GetValue()
     1580                    return
    15671581            G2plt.PlotPatterns(G2frame,plotType='PWDR')
    15681582       
     
    15701584        fileSizer.Add(wx.StaticText(G2frame.dataWindow,-1,' Fixed background file:'),0,WACV)
    15711585        if 'background PWDR' not in data[1]:
    1572             data[1]['background PWDR'] = ['',-1.]
     1586            data[1]['background PWDR'] = ['',-1.,False]
    15731587        backSizer = wx.BoxSizer(wx.HORIZONTAL)
    15741588        Choices = ['',]+G2gd.GetGPXtreeDataNames(G2frame,['PWDR',])
  • trunk/GSASIIstrIO.py

    r3887 r3906  
    510510                        print('For phase "'+phase+
    511511                              '" unresolved reference to histogram "'+hist+'"')
     512    # load the fix background info into the histograms
     513    for hist in Histograms:
     514        fixedBkg = Histograms[hist]['Background'][1].get('background PWDR')
     515        if fixedBkg:
     516            if not fixedBkg[0]: continue
     517            # patch: add refinement flag, if needed
     518            if len(fixedBkg) == 2: fixedBkg += [False]
     519            h = Histograms[hist]['Background'][1]
     520            try:
     521                Limits = Histograms[hist]['Limits'][1]
     522                x = Histograms[hist]['Data'][0]
     523                xB = np.searchsorted(x,Limits[0])
     524                xF = np.searchsorted(x,Limits[1])+1
     525                h['_fixedValues'] = allHistograms[fixedBkg[0]]['Data'][1][xB:xF]
     526                h['_fixedMult'],h['_fixedVary'] = fixedBkg[1:]
     527            except KeyError: # would happen if a referenced histogram were renamed or deleted
     528                print('For hist "{}" unresolved background reference to hist "{}"'
     529                          .format(hist,fixedBkg[0]))
    512530    G2obj.IndexAllIds(Histograms=Histograms,Phases=Phases)
    513531    return Histograms,Phases
     
    604622            for datus in data[1:]:
    605623#                print '    read: ',datus[0]
    606                 if datus[0] in ['Background','Instrument Parameters','Sample Parameters','Reflection Lists']:
     624                if datus[0] in ['Instrument Parameters','Sample Parameters','Reflection Lists']:
    607625                    datus[1] = histogram[datus[0]]
     626                if datus[0] == 'Background': # remove fixed background from file
     627                    d1 = {key:histogram['Background'][1][key]
     628                              for key in histogram['Background'][1]
     629                              if not key.startswith('_fixed')}               
     630                    datus[1] = copy.deepcopy(histogram['Background'])
     631                    datus[1][1] = d1
    608632        except KeyError:
    609633            pass
     
    711735                    'Sample Parameters','Reflection Lists'):
    712736        xfer_dict[key] = hist[key]
     737        if key == 'Background':  # remove fixed background from file
     738            xfer_dict['Background'][1] = {k:hist['Background'][1][k]
     739                      for k in hist['Background'][1]
     740                      if not k.startswith('_fixed')}               
    713741        del hist[key]
    714742    # xform into a gpx-type entry
  • trunk/GSASIIstrMath.py

    r3882 r3906  
    31523152    hfx = ':%d:'%(hId)
    31533153    bakType = calcControls[hfx+'bakType']
    3154     yb,Histogram['sumBk'] = G2pwd.getBackground(hfx,parmDict,bakType,calcControls[hfx+'histType'],x)
     3154    fixedBkg = {i:Histogram['Background'][1][i] for i in Histogram['Background'][1] if i.startswith("_")}
     3155    yb,Histogram['sumBk'] = G2pwd.getBackground(hfx,parmDict,bakType,calcControls[hfx+'histType'],x,fixedBkg)
    31553156    yc = np.zeros_like(yb)
    31563157    cw = np.diff(ma.getdata(x))
Note: See TracChangeset for help on using the changeset viewer.