Changeset 5274


Ignore:
Timestamp:
May 12, 2022 2:57:13 PM (5 months ago)
Author:
toby
Message:

implement save of intensities by phase (partials)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIdataGUI.py

    r5272 r5274  
    767767        self.Refine.append(item)
    768768        self.Bind(wx.EVT_MENU, self.OnRefine, id=item.GetId())
    769 
     769        # this could be disabled during sequential fits:
     770        item = parent.Append(wx.ID_ANY,'Compute partials','Record the contribution from each phase')
     771        self.Bind(wx.EVT_MENU, self.OnRefinePartials, id=item.GetId())
    770772        item = parent.Append(wx.ID_ANY,'&Run Fprime','X-ray resonant scattering')
    771773        self.Bind(wx.EVT_MENU, self.OnRunFprime, id=item.GetId())
     
    53505352        Called from the Calculate/Refine menu.
    53515353        '''
     5354        self._cleanPartials()  # phase partials invalid after a refinement
    53525355        if self.testSeqRefineMode():
    53535356            self.OnSeqRefine(event)
     
    54485451        either single or sequentially
    54495452        '''
     5453        self._cleanPartials()  # phase partials invalid after a refinement
    54505454        self.OnFileSave(event)
    54515455        item = GetGPXtreeItemId(self,self.root,'Covariance')
     
    55025506            return True
    55035507        return False
    5504 
     5508   
     5509    def _cleanPartials(self):
     5510        '''Delete any partials created with :meth:`OnRefinePartials`; used
     5511        in GUI-based refinements, as the partials are no longer correct after
     5512        any fit.
     5513        '''
     5514        PhasePartials = os.path.abspath(os.path.splitext(self.GSASprojectfile)[0])
     5515        histograms,phases = self.GetUsedHistogramsAndPhasesfromTree()
     5516        for h in histograms:
     5517            if 'PWDR' not in h[:4]: continue
     5518            hId = histograms[h]['hId']
     5519            phPartialFile = PhasePartials+'.partials'+str(hId)
     5520            if os.path.exists(phPartialFile):
     5521                os.remove(phPartialFile)
     5522                print('file deleted:',phPartialFile)
     5523
     5524
     5525    def OnRefinePartials(self,event):
     5526        '''Computes and saves the intensities from each phase for each powder
     5527        histogram. These inten
     5528        Do a 0 cycle fit with no variables to pickle intensities for each
     5529        phase into a file.
     5530        Not for sequential fits.
     5531        Sets Controls['PhasePartials'] to a file name to trigger save of
     5532        info in :meth:`GSASIIstrMath.getPowderProfile` and then clear that.
     5533        '''
     5534        if self.testSeqRefineMode():
     5535            G2G.G2MessageBox(self,
     5536                'Phase partials cannot be computed for seqiential fits'
     5537                'not allowed')
     5538            return
     5539        Controls = self.GPXtree.GetItemPyData(GetGPXtreeItemId(self,self.root, 'Controls'))
     5540        savCyc,Controls['max cyc'] = Controls['max cyc'],0
     5541        Controls['PhasePartials'] = os.path.abspath(os.path.splitext(self.GSASprojectfile)[0])
     5542        self.OnFileSave(event)
     5543        dlg = G2G.RefinementProgress(parent=self)
     5544        self.SaveTreeSetting() # save the current tree selection
     5545        self.GPXtree.SaveExposedItems()             # save the exposed/hidden tree items
     5546       
     5547        import pickle
     5548       
     5549        try:
     5550            OK,Rvals = G2stMn.Refine(self.GSASprojectfile,dlg,refPlotUpdate=None,newLeBail=False)
     5551        except Exception as msg:
     5552            print('Refinement failed with message',msg)
     5553            return True
     5554        finally:
     5555            dlg.Update(101.) # forces the Auto_Hide; needed after move w/Win & wx3.0
     5556            # assemble the arrays by histogram
     5557           
     5558        histograms,phases = self.GetUsedHistogramsAndPhasesfromTree()
     5559        for h in histograms:
     5560            if 'PWDR' not in h[:4]: continue
     5561            hId = histograms[h]['hId']
     5562            hfx = ':%d:'%(hId)
     5563            Limits = histograms[h]['Limits'][1]
     5564            x = histograms[h]['Data'][0]
     5565            xB = np.searchsorted(x,Limits[0])
     5566            xF = np.searchsorted(x,Limits[1])+1
     5567            phPartialFile = Controls['PhasePartials']+'.partials'+str(hId)
     5568            fp = open(phPartialFile,'rb')
     5569            pickle.load(fp)   # skip over x values
     5570            valList = [x,histograms[h]['Data'][1],
     5571                           histograms[h]['Data'][2],histograms[h]['Data'][3],
     5572                           histograms[h]['Data'][4]]
     5573            lblList = ['x','obs','wgt','calc','background']
     5574            while True:
     5575                try:
     5576                    ph = pickle.load(fp)
     5577                    lblList.append(ph)
     5578                    ypartial = np.zeros_like(x)
     5579                    ypartial[xB:xF] = pickle.load(fp)
     5580                    valList.append(ypartial)
     5581                except EOFError:
     5582                    break
     5583            fp.close()
     5584            phPartialFile = Controls['PhasePartials']+'_part_'+str(histograms[h]['hId'])+'.csv'
     5585            fp = open(phPartialFile,'w')
     5586            fp.write(', '.join(lblList))
     5587            fp.write('\n')
     5588            for l in zip(*valList):
     5589                fp.write(', '.join([str(i) for i in l])+'\n')
     5590            fp.close()
     5591            print('File',phPartialFile,'written')
     5592        dlg.Destroy()
     5593        Controls['max cyc'] = savCyc
     5594        Controls['PhasePartials'] = None
     5595        self.OnFileSave(event)
     5596        return False
     5597   
    55055598    def reloadFromGPX(self,rtext=None):
    55065599        '''Deletes current data tree & reloads it from GPX file (after a
  • trunk/GSASIIstrMath.py

    r5266 r5274  
    1919import scipy.stats as st
    2020import multiprocessing as mp
     21import pickle
    2122import GSASIIpath
    2223GSASIIpath.SetVersionNumber("$Revision$")
     
    32743275        return alp,bet
    32753276
     3277    # set up for save of phase partials if triggered in GSASIIdataGUI.OnRefinePartials
     3278    phasePartials = calcControls.get('PhasePartials',None)
     3279    def SavePartial(*args): pass
     3280    if phasePartials:
     3281        phPartialFile = phasePartials+'.partials'+str(Histogram['hId'])
     3282        phPartialFP = open(phPartialFile,'wb')  # create/overwrite a file
     3283        pickle.dump(x,phPartialFP)
     3284        phPartialFP.close()
     3285        print('Storing intensity by phase in',phPartialFile)
     3286        def SavePartial(phase,y):
     3287            phPartialFP = open(phPartialFile,'ab')  # append to file
     3288            pickle.dump(phase,phPartialFP)
     3289            pickle.dump(y,phPartialFP)
     3290            phPartialFP.close()
    32763291    hId = Histogram['hId']
    32773292    hfx = ':%d:'%(hId)
     
    33453360        useMP,ncores = G2mp.InitMP()
    33463361        if len(refDict['RefList']) < 100: useMP = False       
     3362        if phasePartials:
     3363            useMP = False
     3364            ypartial = np.zeros_like(yb)
    33473365        if useMP: # multiprocessing: create a set of initialized Python processes
    33483366            MPpool = mp.Pool(ncores,G2mp.InitPwdrProfGlobals,[im,shl,x])
     
    33873405                    fp = G2pwd.getFCJVoigt3(refl[5+im],refl[6+im],refl[7+im],shl,ma.getdata(x[iBeg:iFin]))[0]
    33883406                    yc[iBeg:iFin] += refl[11+im]*refl[9+im]*fp   #>90% of time spent here
     3407                    if phasePartials: ypartial[iBeg:iFin] += refl[11+im]*refl[9+im]*fp
    33893408                if Ka2:
    33903409                    pos2 = refl[5+im]+lamRatio*tand(refl[5+im]/2.0)       # + 360/pi * Dlam/lam * tan(th)
     
    34033422                        fp2 = G2pwd.getFCJVoigt3(pos2,refl[6+im],refl[7+im],shl,ma.getdata(x[iBeg:iFin]))[0]
    34043423                        yc[iBeg:iFin] += refl[11+im]*refl[9+im]*kRatio*fp2       #and here
     3424                        if phasePartials: ypartial[iBeg:iFin] += refl[11+im]*refl[9+im]*kRatio*fp2
     3425            if phasePartials: SavePartial(phase,ypartial)
    34053426        elif 'E' in calcControls[hfx+'histType']:
    34063427           
  • trunk/help/gsasII.html

    r5262 r5274  
    55255525style='font-family:Symbol;mso-fareast-font-family:Symbol;mso-bidi-font-family:
    55265526Symbol'><span style='mso-list:Ignore'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     5527</span></span></span><![endif]><b>Compute partials - </b>This runs a
     5528zero-cycle refinement where the contributions from each phase (phase
     5529partial intensities) are written separately to a file for each histogram in the
     5530refinement. This information is written into files named
     5531<tt><I>project</I>.partials</tt><I>N</I> where <I>N</I> is the
     5532histogram number and <tt><I>project</I></tt> is the GSAS-II project (.gpx)
     5533name. From those file(s), files named
     5534<tt><I>project</I>_part_</tt><I>N</I><tt>.csv</tt> are also created.
     5535The first set of files are intended for future use internally by
     5536GSAS-II and the latter files are for use in external software. The
     5537former set will be deleted if a refinement is run. Use this menu
     5538command to recreate them if needed.
     5539</P>
     5540
     5541<p class=MsoListParagraphCxSpMiddle style='text-indent:-.25in;mso-list:l2 level1 lfo5'><![if !supportLists]><span
     5542style='font-family:Symbol;mso-fareast-font-family:Symbol;mso-bidi-font-family:
     5543Symbol'><span style='mso-list:Ignore'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    55275544</span></span></span><![endif]><b>Run <span class=SpellE>Fprime</span> - </b>This
    55285545run the utility routine <span class=SpellE>Fprime</span> that displays real and
     
    97469763</span></div>
    97479764
    9748 <p class=MsoNormal style='tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt'><!-- hhmts start -->Last
    9749 modified: Wed Feb 23 16:21:54 CST 2022 <o:p></o:p></p>
    9750 
    9751 <!-- hhmts end --></div>
     9765<p class=MsoNormal style='tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt'><!-- hhmts start -->Last modified: Thu May 12 14:50:38 CDT 2022 <!-- hhmts end --></div>
    97529766
    97539767</body>
Note: See TracChangeset for help on using the changeset viewer.