Changeset 4410


Ignore:
Timestamp:
Apr 24, 2020 11:06:35 AM (18 months ago)
Author:
toby
Message:

do much more cleanup after phase delete; refactor some importer imports to avoid G2IO

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r4399 r4410  
    11941194        return None
    11951195
    1196 ######################################################################
    1197 # base classes for reading various types of data files
    1198 #   not used directly, only by subclassing
    1199 ######################################################################
    1200 def BlockSelector(ChoiceList, ParentFrame=None,title='Select a block',
    1201     size=None, header='Block Selector',useCancel=True):
    1202     ''' Provide a wx dialog to select a block if the file contains more
    1203     than one set of data and one must be selected
    1204     '''
    1205     if useCancel:
    1206         dlg = wx.SingleChoiceDialog(
    1207             ParentFrame,title, header,ChoiceList)
    1208     else:
    1209         dlg = wx.SingleChoiceDialog(
    1210             ParentFrame,title, header,ChoiceList,
    1211             style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
    1212     if size: dlg.SetSize(size)
    1213     dlg.CenterOnParent()
    1214     if dlg.ShowModal() == wx.ID_OK:
    1215         sel = dlg.GetSelection()
    1216         return sel
    1217     else:
    1218         return None
    1219     dlg.Destroy()
    1220 
    1221 def MultipleBlockSelector(ChoiceList, ParentFrame=None,
    1222     title='Select a block',size=None, header='Block Selector'):
    1223     '''Provide a wx dialog to select a block of data if the
    1224     file contains more than one set of data and one must be
    1225     selected.
    1226 
    1227     :returns: a list of the selected blocks
    1228     '''
    1229     dlg = wx.MultiChoiceDialog(ParentFrame,title, header,ChoiceList+['Select all'],
    1230         wx.CHOICEDLG_STYLE)
    1231     dlg.CenterOnScreen()
    1232     if size: dlg.SetSize(size)
    1233     if dlg.ShowModal() == wx.ID_OK:
    1234         sel = dlg.GetSelections()
    1235     else:
    1236         return []
    1237     dlg.Destroy()
    1238     selected = []
    1239     if len(ChoiceList) in sel:
    1240         return range(len(ChoiceList))
    1241     else:
    1242         return sel
    1243     return selected
    1244 
    1245 def MultipleChoicesSelector(choicelist, headinglist, ParentFrame=None, **kwargs):
    1246     '''A modal dialog that offers a series of choices, each with a title and a wx.Choice
    1247     widget. Typical input:
    1248    
    1249        * choicelist=[ ('a','b','c'), ('test1','test2'),('no choice',)]
    1250        
    1251        * headinglist = [ 'select a, b or c', 'select 1 of 2', 'No option here']
    1252        
    1253     optional keyword parameters are: head (window title) and title
    1254     returns a list of selected indicies for each choice (or None)
    1255     '''
    1256     result = None
    1257     dlg = MultipleChoicesDialog(choicelist,headinglist,
    1258         parent=ParentFrame, **kwargs)         
    1259     dlg.CenterOnParent()
    1260     if dlg.ShowModal() == wx.ID_OK:
    1261         result = dlg.chosen
    1262     dlg.Destroy()
    1263     return result
    1264 
    1265 def PhaseSelector(ChoiceList, ParentFrame=None,
    1266     title='Select a phase', size=None,header='Phase Selector'):
    1267     ''' Provide a wx dialog to select a phase if the file contains more
    1268     than one phase
    1269     '''
    1270     return BlockSelector(ChoiceList,ParentFrame,title,
    1271         size,header)
    1272 
    1273 ######################################################################
    12741196def striphist(var,insChar=''):
    12751197    'strip a histogram number from a var name'
     
    12791201        sv[1] = insChar
    12801202    return ':'.join(sv)
     1203
     1204######################################################################
     1205# base classes for reading various types of data files
     1206#   not used directly, only by subclassing
     1207######################################################################
    12811208class ExportBaseclass(object):
    12821209    '''Defines a base class for the exporting of GSAS-II results.
  • trunk/GSASIIctrlGUI.py

    r4388 r4410  
    8888:func:`askSaveFile`                Get a file name from user
    8989:func:`askSaveDirectory`           Get a directory name from user
     90:func:`BlockSelector`              Select a single block for instrument parameters
     91:func:`MultipleBlockSelector`      Select one or more blocks of data, used for
     92                                   CIF powder histogram imports only
     93:func:`MultipleChoicesSelector`    Dialog for displaying fairly complex choices, used for
     94                                   CIF powder histogram imports only
     95:func:`PhaseSelector`              Select a phase from a list (used for phase importers)
     96
    9097================================  =================================================================
    9198
     
    19051912        dlg.Destroy()
    19061913
    1907 ################################################################        Single choice Dialog with filter options
     1914###############################################################
     1915#        Single choice Dialog with filter options
    19081916class G2SingleChoiceDialog(wx.Dialog):
    19091917    '''A dialog similar to wx.SingleChoiceDialog except that a filter can be
     
    20902098        self.EndModal(wx.ID_CANCEL)
    20912099
    2092 ###################################################################,#############
     2100################################################################################
    20932101def G2MessageBox(parent,msg,title='Error'):
    20942102    '''Simple code to display a error or warning message
     
    28312839    dlg.Destroy()
    28322840
    2833 ######################################################### Column-order selection dialog
     2841########################################################
     2842# Column-order selection dialog
    28342843def GetItemOrder(parent,keylist,vallookup,posdict):
    28352844    '''Creates a dialog where items can be ordered into columns
     
    46934702    return s.encode('ascii','replace')
    46944703       
     4704######################################################################
     4705# wx classes for reading various types of data files
     4706######################################################################
     4707def BlockSelector(ChoiceList, ParentFrame=None,title='Select a block',
     4708    size=None, header='Block Selector',useCancel=True):
     4709    ''' Provide a wx dialog to select a single block where one must
     4710    be selected. Used for selecting for banks for instrument
     4711    parameters if the file contains more than one set.
     4712    '''
     4713    if useCancel:
     4714        dlg = wx.SingleChoiceDialog(
     4715            ParentFrame,title, header,ChoiceList)
     4716    else:
     4717        dlg = wx.SingleChoiceDialog(
     4718            ParentFrame,title, header,ChoiceList,
     4719            style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
     4720    if size: dlg.SetMinSize(size)
     4721    dlg.CenterOnParent()
     4722    dlg.SendSizeEvent()
     4723    if dlg.ShowModal() == wx.ID_OK:
     4724        sel = dlg.GetSelection()
     4725        return sel
     4726    else:
     4727        return None
     4728    dlg.Destroy()
     4729
     4730def MultipleBlockSelector(ChoiceList, ParentFrame=None,
     4731    title='Select a block',size=None, header='Block Selector'):
     4732    '''Provide a wx dialog to select a block of data if the
     4733    file contains more than one set of data and one must be
     4734    selected. Used in :mod:`G2pwd_CIF` only.
     4735
     4736    :returns: a list of the selected blocks
     4737    '''
     4738    dlg = wx.MultiChoiceDialog(ParentFrame,title, header,ChoiceList+['Select all'],
     4739        wx.CHOICEDLG_STYLE)
     4740    if size: dlg.SetMinSize(size)
     4741    dlg.CenterOnScreen()
     4742    dlg.SendSizeEvent()
     4743    if dlg.ShowModal() == wx.ID_OK:
     4744        sel = dlg.GetSelections()
     4745    else:
     4746        return []
     4747    dlg.Destroy()
     4748    selected = []
     4749    if len(ChoiceList) in sel:
     4750        return range(len(ChoiceList))
     4751    else:
     4752        return sel
     4753    return selected
     4754
     4755def MultipleChoicesSelector(choicelist, headinglist, ParentFrame=None, **kwargs):
     4756    '''A modal dialog that offers a series of choices, each with a title and a wx.Choice
     4757    widget. Used in :mod:`G2pwd_CIF` only.
     4758
     4759    Typical input:
     4760   
     4761       * choicelist=[ ('a','b','c'), ('test1','test2'),('no choice',)]
     4762       
     4763       * headinglist = [ 'select a, b or c', 'select 1 of 2', 'No option here']
     4764       
     4765    optional keyword parameters are: head (window title) and title
     4766    returns a list of selected indicies for each choice (or None)
     4767    '''
     4768    result = None
     4769    dlg = MultipleChoicesDialog(choicelist,headinglist,
     4770        parent=ParentFrame, **kwargs)         
     4771    dlg.CenterOnParent()
     4772    if dlg.ShowModal() == wx.ID_OK:
     4773        result = dlg.chosen
     4774    dlg.Destroy()
     4775    return result
     4776
     4777def PhaseSelector(ChoiceList, ParentFrame=None,
     4778    title='Select a phase', size=None,header='Phase Selector'):
     4779    ''' Provide a wx dialog to select a phase, used in importers if a file
     4780    contains more than one phase
     4781    '''
     4782    return BlockSelector(ChoiceList,ParentFrame,title,
     4783        size,header)
     4784
    46954785################################################################################
    46964786# configuration routines (for editing config.py)
  • trunk/GSASIIdataGUI.py

    r4406 r4410  
    14891489            for i in range(1,1+ibanks):
    14901490                choices.append('Bank '+str(i))
    1491             bank = 1 + G2IO.BlockSelector(
     1491            bank = 1 + G2G.BlockSelector(
    14921492                choices, self,
    14931493                title=u'Select an instrument parameter bank for '+
     
    15381538                for l in dI.defaultIparm_lbl:
    15391539                    choices.append('Defaults for '+l)
    1540                 res = G2IO.BlockSelector(choices,ParentFrame=self,title=head,
     1540                res = G2G.BlockSelector(choices,ParentFrame=self,title=head,
    15411541                    header='Select default inst parms',useCancel=True)
    15421542                if res is None: return None
     
    37933793       
    37943794    def OnDeletePhase(self,event):
    3795         'Delete a phase from the tree. Called by Data/Delete Phase menu'
    3796         #Hmm, also need to delete this phase from Reflection Lists for each PWDR histogram
     3795        '''Delete one or more phases from the tree. Called by Data/Delete Phase menu.
     3796        Also delete this phase from Reflection Lists for each PWDR histogram;
     3797        removes the phase from restraints and deletes any constraints
     3798        with variables from the phase.
     3799        If any deleted phase is marked as Used in a histogram, a more rigorous
     3800        "deep clean" is done and histogram refinement results are cleared, as well as
     3801        the covariance information and all plots are deleted
     3802        '''
     3803        selItem = self.GPXtree.GetSelection()
    37973804        if self.dataWindow:
    37983805            self.dataWindow.ClearData()
     
    38003807        DelList = []
    38013808        DelItemList = []
     3809        consDeleted = 0
     3810        usedPhase = False
    38023811        if GetGPXtreeItemId(self,self.root,'Phases'):
    38033812            sub = GetGPXtreeItemId(self,self.root,'Phases')
     
    38083817        else:
    38093818            subr = 0
    3810         if sub:
    3811             item, cookie = self.GPXtree.GetFirstChild(sub)
    3812             while item:
    3813                 TextList.append(self.GPXtree.GetItemText(item))
    3814                 item, cookie = self.GPXtree.GetNextChild(sub, cookie)               
    3815             dlg = wx.MultiChoiceDialog(self, 'Which phase to delete?', 'Delete phase', TextList, wx.CHOICEDLG_STYLE)
    3816             try:
    3817                 if dlg.ShowModal() == wx.ID_OK:
    3818                     result = dlg.GetSelections()
    3819                     for i in result: DelList.append([i,TextList[i]])
    3820                     item, cookie = self.GPXtree.GetFirstChild(sub)
    3821                     i = 0
    3822                     while item:
    3823                         if [i,self.GPXtree.GetItemText(item)] in DelList: DelItemList.append(item)
    3824                         item, cookie = self.GPXtree.GetNextChild(sub, cookie)
    3825                         i += 1
    3826                     for item in DelItemList:
    3827                         name = self.GPXtree.GetItemText(item)
    3828                         self.GPXtree.Delete(item)
    3829                         self.G2plotNB.Delete(name)
    3830                     item, cookie = self.GPXtree.GetFirstChild(self.root)
    3831                     while item:
    3832                         name = self.GPXtree.GetItemText(item)
    3833                         if 'PWDR' in name:
    3834                             Id = GetGPXtreeItemId(self,item, 'Reflection Lists')
    3835                             refList = self.GPXtree.GetItemPyData(Id)
    3836                             if len(refList):
    3837                                 for i,item in DelList:
    3838                                     if item in refList:
    3839                                         del(refList[item])
    3840                         elif 'HKLF' in name:
    3841                             data = self.GPXtree.GetItemPyData(item)
    3842                             data[0] = {}
    3843                            
    3844                         item, cookie = self.GPXtree.GetNextChild(self.root, cookie)
    3845             finally:
    3846                 dlg.Destroy()
     3819        if GetGPXtreeItemId(self,self.root,'Constraints'):
     3820            id = GetGPXtreeItemId(self,self.root,'Constraints')
     3821            constr = self.GPXtree.GetItemPyData(id)
     3822        else:
     3823            constr = {}
     3824           
     3825        item, cookie = self.GPXtree.GetFirstChild(sub)
     3826        while item:
     3827            TextList.append(self.GPXtree.GetItemText(item))
     3828            item, cookie = self.GPXtree.GetNextChild(sub, cookie)               
     3829        dlg = wx.MultiChoiceDialog(self, 'Which phase to delete?', 'Delete phase', TextList, wx.CHOICEDLG_STYLE)
     3830        try:
     3831            if dlg.ShowModal() == wx.ID_OK:
     3832                result = dlg.GetSelections()
     3833                for i in result: DelList.append([i,TextList[i]])
     3834                item, cookie = self.GPXtree.GetFirstChild(sub)
     3835                i = 0
     3836                while item:
     3837                    if [i,self.GPXtree.GetItemText(item)] in DelList: DelItemList.append(item)
     3838                    item, cookie = self.GPXtree.GetNextChild(sub, cookie)
     3839                    i += 1
     3840                for item in DelItemList:
     3841                    phase = self.GPXtree.GetItemPyData(item)
     3842                    for h in phase['Histograms']:
     3843                        if 'Use' not in phase['Histograms'][h]: continue
     3844                        if phase['Histograms'][h]['Use']:
     3845                            usedPhase = True
     3846                            break
     3847                    if 'pId' in phase:
     3848                        p = phase['pId']
     3849                    else:
     3850                        p = '?'
     3851                    self.GPXtree.Delete(item)
     3852                    if item == selItem: selItem = self.root
     3853                    # look for constraints to remove
     3854                    for key in constr:
     3855                        delThis = []
     3856                        if key.startswith('_'): continue
     3857                        for i,cons in enumerate(constr[key]):
     3858                            for var in cons[0:-3]:
     3859                                if str(var[1]).startswith(str(p)):
     3860                                    delThis.append(i)
     3861                                    break
     3862                        for i in reversed(delThis):
     3863                            consDeleted += 1
     3864                            del constr[key][i]
     3865                # delete refinement results from histograms
     3866                item, cookie = self.GPXtree.GetFirstChild(self.root)
     3867                while item:
     3868                    name = self.GPXtree.GetItemText(item)
     3869                    if 'PWDR' in name:
     3870                        data = self.GPXtree.GetItemPyData(item)
     3871                        if usedPhase: # remove r-factors
     3872                            dellist = [value for value in data[0] if ':' in value]
     3873                            for v in dellist+['Durbin-Watson', 'R', 'wR', 'Rb',
     3874                                                  'wRb', 'wRmin','Nobs']:
     3875                                if v in data[0]: del data[0][v]
     3876                            # could wipe out computed & difference patterns, but does not work
     3877                            #data[1][3] = np.zeros_like(data[1][3])
     3878                            #data[1][5] = np.zeros_like(data[1][5])
     3879                        # always get rid of reflection lists
     3880                        Id = GetGPXtreeItemId(self,item, 'Reflection Lists')
     3881                        refList = self.GPXtree.GetItemPyData(Id)
     3882                        if len(refList):
     3883                            for i,item in DelList:
     3884                                if item in refList:
     3885                                    del(refList[item])
     3886                    elif 'HKLF' in name and usedPhase: # probably not needed if phase is not used
     3887                        data = self.GPXtree.GetItemPyData(item)
     3888                        data[0] = {}
     3889
     3890                    item, cookie = self.GPXtree.GetNextChild(self.root, cookie)
     3891        finally:
     3892            dlg.Destroy()
     3893        if usedPhase: # clear info from last refinement for "deep clean" if a used phase is deleted
     3894            id = GetGPXtreeItemId(self,self.root,'Covariance')
     3895            if DelItemList and id:
     3896                self.GPXtree.SetItemPyData(id,{})
     3897            id = GetGPXtreeItemId(self,self.root,'Sequential results')
     3898            if DelItemList and id:
     3899                self.GPXtree.Delete(id)
     3900                if id == selItem: selItem = self.root
     3901            # delete all plots
     3902            for lbl in self.G2plotNB.plotList:
     3903                self.G2plotNB.Delete(lbl)
    38473904        if subr:        #remove restraints for deleted phase
    38483905            DelList = [itm[1] for itm in DelList]
     
    38523909                if name in DelList:
    38533910                    self.GPXtree.Delete(item)
    3854                 item, cookie = self.GPXtree.GetNextChild(subr, cookie)               
    3855                
     3911                    if item == selItem: selItem = self.root
     3912                item, cookie = self.GPXtree.GetNextChild(subr, cookie)
     3913        # force redisplay of current tree item if it was not deleted
     3914        self.PickIdText = None
     3915        SelectDataTreeItem(self,selItem)
     3916        wx.CallAfter(self.GPXtree.SelectItem,selItem)
     3917        if consDeleted:
     3918            print('\n',consDeleted,'constraints were deleted')
     3919       
    38563920    def OnRenameData(self,event):
    38573921        'Renames an existing phase. Called by Data/Rename Phase menu'
  • trunk/GSASIIpwdGUI.py

    r4334 r4410  
    5050import GSASIIphsGUI as G2phsG
    5151import GSASIIctrlGUI as G2G
    52 import GSASIIIO as G2IO
    5352import GSASIIElemGUI as G2elemGUI
    5453import GSASIIElem as G2elem
     
    18721871                for l in dI.defaultIparm_lbl:
    18731872                    choices.append('Defaults for '+l)
    1874                 res = G2IO.BlockSelector(choices,ParentFrame=G2frame,title=head,
     1873                res = G2G.BlockSelector(choices,ParentFrame=G2frame,title=head,
    18751874                    header='Select default inst parms',useCancel=True)
    18761875                if res is None: return None
  • trunk/imports/G2phase_CIF.py

    r4399 r4410  
    2525import re
    2626import copy
    27 import GSASIIIO as G2IO
    2827import GSASIIobj as G2obj
    2928import GSASIIspc as G2spc
     
    3231import GSASIIpy3 as G2p3
    3332import GSASIIpath
     33try:
     34    import GSASIIctrlGUI as G2G
     35except ImportError:
     36    pass
    3437GSASIIpath.SetVersionNumber("$Revision$")
    3538import CifFile as cif # PyCifRW from James Hester
     
    132135                sg = sg.replace('_','')
    133136                if sg: choice[-1] += ', (' + sg.strip() + ')'
    134             selblk = G2IO.PhaseSelector(choice,ParentFrame=ParentFrame,
     137            selblk = G2G.PhaseSelector(choice,ParentFrame=ParentFrame,
    135138                title= 'Select a phase from one the CIF data_ blocks below',size=(600,100))
    136139        self.errors = 'Error during reading of selected block'
     
    11521155            for atmline in self.Phase['Atoms']:
    11531156                lbl = atmline[0]
    1154                 print( lbl,atmline[6],Occ[lbl])
     1157                if lbl in Occ: print( lbl,atmline[6],Occ[lbl])
    11551158
    11561159            print( 70*'=')
  • trunk/imports/G2phase_GPX.py

    r3828 r4410  
    2424import random as ran
    2525import GSASIIobj as G2obj
    26 import GSASIIIO as G2IO
    2726import GSASIIstrIO as G2stIO
    2827import GSASIIpath
     28try:
     29    import GSASIIctrlGUI as G2G
     30except ImportError:
     31    pass
    2932GSASIIpath.SetVersionNumber("$Revision$")
    3033
     
    6972            selblk = 0
    7073        else:                       # choose from options               
    71             selblk = G2IO.PhaseSelector(phasenames,ParentFrame=ParentFrame,
     74            selblk = G2G.PhaseSelector(phasenames,ParentFrame=ParentFrame,
    7275                title= 'Select a phase from the list below',)
    7376            if selblk is None:
  • trunk/imports/G2pwd_CIF.py

    r4081 r4410  
    1818import os.path
    1919import GSASIIobj as G2obj
    20 import GSASIIIO as G2IO
    2120import CifFile as cif # PyCifRW from James Hester
    2221import GSASIIpath
     22try:
     23    import GSASIIctrlGUI as G2G
     24except ImportError:
     25    pass
    2326asind = lambda x: 180.*np.arcsin(x)/np.pi
    2427GSASIIpath.SetVersionNumber("$Revision$")
     
    182185                    'Block '+str(blk)+', '+str(l)+' points. X='+sx+' & Y='+sy
    183186                    )
    184             selections = G2IO.MultipleBlockSelector(
     187            selections = G2G.MultipleBlockSelector(
    185188                choices,
    186189                ParentFrame=ParentFrame,
     
    221224            chlbls.append('Divide intensities by data item')
    222225            choices.append(['none']+modch)
    223             res = G2IO.MultipleChoicesSelector(choices,chlbls)
     226            res = G2G.MultipleChoicesSelector(choices,chlbls)
    224227            if not res:
    225228                self.errors = "Abort: data items not selected"
  • trunk/imports/G2sfact_CIF.py

    r3245 r4410  
    1919import os.path
    2020import GSASIIobj as G2obj
    21 import GSASIIIO as G2IO
    2221import GSASIIpath
     22try:
     23    import GSASIIctrlGUI as G2G
     24except ImportError:
     25    pass
    2326GSASIIpath.SetVersionNumber("$Revision$")
    2427import CifFile as cif # PyCifRW from James Hester
     
    163166                if self.repeatcount >= len(blklist): self.repeat = False
    164167            else:
    165                 selblk = G2IO.BlockSelector(
     168                selblk = G2G.BlockSelector(
    166169                    choice,
    167170                    ParentFrame=ParentFrame,
Note: See TracChangeset for help on using the changeset viewer.