Changeset 5275


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

change partial pickle file contents; make partial .csv optional

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIdataGUI.py

    r5274 r5275  
    767767        self.Refine.append(item)
    768768        self.Bind(wx.EVT_MENU, self.OnRefine, id=item.GetId())
    769         # this could be disabled during sequential fits:
    770769        item = parent.Append(wx.ID_ANY,'Compute partials','Record the contribution from each phase')
     770        self.Refine.append(item)
     771        item.Enable(state) # disabled during sequential fits
    771772        self.Bind(wx.EVT_MENU, self.OnRefinePartials, id=item.GetId())
    772773        item = parent.Append(wx.ID_ANY,'&Run Fprime','X-ray resonant scattering')
     
    809810           
    810811        for item in self.Refine:
    811             if 'Le Bail' in item.GetItemLabel():
    812                 if seqSetting: item.Enable(False)
     812            if 'Le Bail' in item.GetItemLabel() or 'partials' in item.GetItemLabel() :
     813                item.Enable(not seqSetting)
    813814            elif seqSetting:
    814815                item.SetItemLabel('Se&quential refine\tCtrl+R')    #might fail on old wx
     
    27542755            defnam = os.path.splitext(os.path.split(self.GSASprojectfile)[1])[0]+'.gcmd'
    27552756            dlg = wx.FileDialog(self,
    2756                 'Choose an file to save past actions', '.', defnam,
     2757                'Choose a file to save past actions', '.', defnam,
    27572758                'GSAS-II cmd output (*.gcmd)|*.gcmd',
    27582759                wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
     
    55125513        any fit.
    55135514        '''
    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)
     5515        PhasePartials = os.path.abspath(os.path.splitext(self.GSASprojectfile)[0]+'.partials')
     5516        if os.path.exists(PhasePartials):
     5517            os.remove(PhasePartials)
     5518            print('file deleted:',PhasePartials)
    55235519
    55245520
     
    55325528        info in :meth:`GSASIIstrMath.getPowderProfile` and then clear that.
    55335529        '''
    5534         if self.testSeqRefineMode():
     5530        if self.testSeqRefineMode():  # should not happen, as should not be enabled
    55355531            G2G.G2MessageBox(self,
    5536                 'Phase partials cannot be computed for seqiential fits'
    5537                 'not allowed')
     5532                'Phase partials cannot be computed for sequential fits',
     5533                'Sequential not allowed')
    55385534            return
    55395535        Controls = self.GPXtree.GetItemPyData(GetGPXtreeItemId(self,self.root, 'Controls'))
    55405536        savCyc,Controls['max cyc'] = Controls['max cyc'],0
    5541         Controls['PhasePartials'] = os.path.abspath(os.path.splitext(self.GSASprojectfile)[0])
     5537        Controls['PhasePartials'] = os.path.abspath(os.path.splitext(self.GSASprojectfile)[0]+'.partials')
    55425538        self.OnFileSave(event)
    55435539        dlg = G2G.RefinementProgress(parent=self)
     
    55455541        self.GPXtree.SaveExposedItems()             # save the exposed/hidden tree items
    55465542       
    5547         import pickle
    5548        
    55495543        try:
    55505544            OK,Rvals = G2stMn.Refine(self.GSASprojectfile,dlg,refPlotUpdate=None,newLeBail=False)
    55515545        except Exception as msg:
    55525546            print('Refinement failed with message',msg)
     5547            Controls['max cyc'] = savCyc
     5548            Controls['PhasePartials'] = None
     5549            self.OnFileSave(event)
    55535550            return True
    55545551        finally:
    55555552            dlg.Update(101.) # forces the Auto_Hide; needed after move w/Win & wx3.0
    5556             # assemble the arrays by histogram
     5553        dlg.Destroy()
     5554
     5555        while True: # loop until we get a name or the user says no to export
     5556            dlg = wx.MessageDialog(self.GetTopLevelParent(),
     5557                'Export the partial intensities by phase as a comma-separated-variable file?',
     5558                caption='Export partials?',style=wx.YES_NO)
     5559            dlg.CenterOnParent()
     5560            result = wx.ID_NO
     5561            try:
     5562                result = dlg.ShowModal()
     5563            finally:
     5564                dlg.Destroy()
     5565            if result == wx.ID_NO:
     5566                Controls['max cyc'] = savCyc
     5567                Controls['PhasePartials'] = None
     5568                self.OnFileSave(event)
     5569                return
     5570
     5571            filename = None
     5572            defpath,defnam = os.path.split(os.path.abspath(
     5573                os.path.splitext(self.GSASprojectfile)[0]+'_part_N.csv'))
    55575574           
     5575            dlg = wx.FileDialog(self,
     5576                'Choose a file prefix to save the partials', defpath, defnam,
     5577                'spreadsheet input (*.csv)|*.csv',wx.FD_SAVE)
     5578            dlg.CenterOnParent()
     5579            try:
     5580                if dlg.ShowModal() == wx.ID_OK:
     5581                    filename = os.path.splitext(dlg.GetPath())[0]   # drop extension
     5582                    if not filename.endswith('_part_N'):
     5583                        filename += '_part_N'
     5584                    break
     5585            finally:
     5586                dlg.Destroy()
     5587        # write the .csv file(s)
     5588        import pickle
    55585589        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],
     5590        phPartialFile = Controls['PhasePartials']
     5591        fp = open(phPartialFile,'rb')
     5592        pickle.load(fp)   # skip over initial None
     5593        done = False
     5594        while not done:   # looping over histograms
     5595            valList = None
     5596            lblList = ['x','obs','wgt','calc','background']
     5597            try:
     5598                hId = pickle.load(fp)   # get histogram number
     5599                pickle.load(fp)   # skip over x values
     5600                pickle.load(fp)   # skip over bkg values
     5601                for h in histograms:
     5602                    if 'PWDR' not in h[:4]: continue
     5603                    if hId == histograms[h]['hId']: break
     5604                else:
     5605                    print('Error histogram',hId,'not found. This should not happen!')
     5606                    fp.close()
     5607                    Controls['max cyc'] = savCyc
     5608                    Controls['PhasePartials'] = None
     5609                    self.OnFileSave(event)
     5610                    return
     5611                Limits = histograms[h]['Limits'][1]
     5612                x = histograms[h]['Data'][0]
     5613                xB = np.searchsorted(x,Limits[0])
     5614                xF = np.searchsorted(x,Limits[1])+1
     5615                valList = [x,histograms[h]['Data'][1],
    55715616                           histograms[h]['Data'][2],histograms[h]['Data'][3],
    55725617                           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)
     5618                while True: # read until we hit an EOF or a None
     5619                    phase = pickle.load(fp)   # get histogram number
     5620                    if phase is None: break
     5621                    lblList.append(phase)
    55785622                    ypartial = np.zeros_like(x)
    55795623                    ypartial[xB:xF] = pickle.load(fp)
    55805624                    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()
     5625            except EOFError:
     5626                done = True
     5627            if valList:
     5628                phPartialFile = filename.replace('_part_N',
     5629                                                 '_part_'+str(hId)+'.csv')
     5630                fp1 = open(phPartialFile,'w')
     5631                fp1.write(', '.join(lblList))
     5632                fp1.write('\n')
     5633                for l in zip(*valList):
     5634                    fp1.write(', '.join([str(i) for i in l])+'\n')
     5635                fp1.close()
     5636                print('File',phPartialFile,'written')
     5637
    55935638        Controls['max cyc'] = savCyc
    55945639        Controls['PhasePartials'] = None
  • trunk/GSASIIstrMath.py

    r5274 r5275  
    32753275        return alp,bet
    32763276
    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()
    32913277    hId = Histogram['hId']
    32923278    hfx = ':%d:'%(hId)
     
    32973283    cw = np.diff(ma.getdata(x))
    32983284    cw = np.append(cw,cw[-1])
     3285    # set up for save of phase partials if triggered in GSASIIdataGUI.OnRefinePartials
     3286    phasePartials = calcControls.get('PhasePartials',None)
     3287    if phasePartials:
     3288        phPartialFP = open(phasePartials,'ab')  # create histogram header
     3289        pickle.dump(None,phPartialFP)
     3290        pickle.dump(hId,phPartialFP)
     3291        pickle.dump(x,phPartialFP)
     3292        pickle.dump(yb,phPartialFP)
     3293        phPartialFP.close()
     3294        def SavePartial(phase,y):
     3295            phPartialFP = open(phasePartials,'ab')  # append to file
     3296            pickle.dump(phase,phPartialFP)
     3297            pickle.dump(y,phPartialFP)
     3298            phPartialFP.close()
    32993299       
    33003300    if 'C' in calcControls[hfx+'histType']:   
     
    42624262    histoList = list(Histograms.keys())
    42634263    histoList.sort()
     4264    phasePartials = calcControls.get('PhasePartials',None)
     4265    if phasePartials:
     4266        print('Storing intensity by phase in',phasePartials)
     4267        phPartialFP = open(phasePartials,'wb')  # create/clear partials file
     4268        phPartialFP.close()
    42644269    for histogram in histoList:
    42654270        if 'PWDR' in histogram[:4]:
  • trunk/help/gsasII.html

    r5274 r5275  
    55275527</span></span></span><![endif]><b>Compute partials - </b>This runs a
    55285528zero-cycle refinement where the contributions from each phase (phase
    5529 partial intensities) are written separately to a file for each histogram in the
    5530 refinement. This information is written into files named
    5531 <tt><I>project</I>.partials</tt><I>N</I> where <I>N</I> is the
    5532 histogram number and <tt><I>project</I></tt> is the GSAS-II project (.gpx)
    5533 name. From those file(s), files named
    5534 <tt><I>project</I>_part_</tt><I>N</I><tt>.csv</tt> are also created.
    5535 The first set of files are intended for future use internally by
    5536 GSAS-II and the latter files are for use in external software. The
    5537 former set will be deleted if a refinement is run. Use this menu
    5538 command to recreate them if needed.
     5529partial intensities) are written for each histogram
     5530and each phase in that histogram into a single file named
     5531<tt><I>project</I>.partials</tt> where
     5532<tt><I>project</I></tt> is the GSAS-II project (.gpx)
     5533name. This file is intended for internal use in GSAS-II and will be
     5534deleted if additional refinements are performed (making the
     5535information in them obsolete; use this menu
     5536command to recreate them if needed.)
     5537When the <tt>.partials</tt> file is created, the user can optionally choose to
     5538export the intensity information in a series of ASCII files named
     5539<tt><I>prefix</I>_part_</tt><I>N</I><tt>.csv</tt>, which can be read by
     5540spreadsheets and most scientific software.
    55395541</P>
    55405542
     
    97639765</span></div>
    97649766
    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>
     9767<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: Fri May 13 16:26:04 CDT 2022 <!-- hhmts end --></div>
    97669768
    97679769</body>
Note: See TracChangeset for help on using the changeset viewer.