Changeset 407


Ignore:
Timestamp:
Nov 5, 2011 5:30:24 PM (12 years ago)
Author:
vondreele
Message:

constraints round 2

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIgrid.py

    r406 r407  
    6161] = [wx.NewId() for _init_coll_Sample_Items in range(1)]
    6262
    63 [ wxID_CONSTRAINTADD,
    64 ] = [wx.NewId() for _init_coll_Constraint_Items in range(1)]
     63[ wxID_CONSTRAINTADD,wxID_EQUIVADD,wxID_HOLDADD,
     64] = [wx.NewId() for _init_coll_Constraint_Items in range(3)]
    6565
    6666[ wxID_RESTRAINTADD,
     
    156156    def _init_coll_Constraint_Items(self,parent):
    157157        parent.Append(id=wxID_CONSTRAINTADD, kind=wx.ITEM_NORMAL,text='Add constraint',
    158             help='constraint dummy menu item')
     158            help='Add constraint on parameter values')
     159        parent.Append(id=wxID_EQUIVADD, kind=wx.ITEM_NORMAL,text='Add equivalence',
     160            help='Add equivalence between parameter values')
     161        parent.Append(id=wxID_HOLDADD, kind=wx.ITEM_NORMAL,text='Add hold',
     162            help='Add hold on a parameter value')
    159163       
    160164    def _init_coll_Restraint_Items(self,parent):
     
    757761    self.dataDisplay.AutoSizeColumns(True)
    758762    self.dataFrame.setSizePosLeft([700,350])
    759                
    760 def UpdateConstraints(self,data):
     763   
     764def UpdateConstraints(self,data):             
     765#    data.update({'Hist':[],'HAP':[],'Phase':[]})       #empty dict - fill it
    761766    if not data:
    762767        data.update({'Hist':[],'HAP':[],'Phase':[]})       #empty dict - fill it
    763768    Histograms,Phases = self.GetUsedHistogramsAndPhasesfromTree()
    764     Natoms,phaseVary,phaseDict,pawleyLookup,FFtable = G2str.GetPhaseData(Phases,Print=False)       
     769    Natoms,phaseVary,phaseDict,pawleyLookup,FFtable = G2str.GetPhaseData(Phases,Print=False)
     770    phaseList = []
     771    for item in phaseDict:
     772        if item.split(':')[2] not in ['Ax','Ay','Az','Amul','AI/A','Atype','SHorder']:
     773            phaseList.append(item)
     774    phaseList.sort()
    765775    hapVary,hapDict,controlDict = G2str.GetHistogramPhaseData(Phases,Histograms,Print=False)
     776    hapList = hapDict.keys()
     777    hapList.sort()
    766778    histVary,histDict,controlDict = G2str.GetHistogramData(Histograms,Print=False)
     779    histList = []
     780    for item in histDict:
     781        if item.split(':')[2] not in ['Omega','Type','Chi','Phi','Azimuth','Gonio. radius','Lam1','Lam2']:
     782            histList.append(item)
     783    histList.sort()
    767784    Indx = {}
    768     self.Page = 0
    769    
     785    scope = {}                          #filled out later
     786    self.Page = [0,'phs']
     787   
     788    def GetPHlegends(Phases,Histograms):
     789        plegend = '\n In p::name'
     790        hlegend = '\n In :h:name'
     791        phlegend = '\n In p:h:name'
     792        for phase in Phases:
     793            plegend += '\n p:: = '+str(Phases[phase]['pId'])+':: for '+phase
     794            for histogram in Phases[phase]['Histograms']:
     795                phlegend += '\n p:h: = '+str(Phases[phase]['pId'])+':'+str(Histograms[histogram]['hId'])+': for '+phase+' in '+histogram
     796        for histogram in Histograms:
     797            hlegend += '\n :h: = :'+str(Histograms[histogram]['hId'])+': for '+histogram
     798        return plegend,hlegend,phlegend
     799       
    770800    def FindEquivVarb(name,nameList):
    771801        outList = []
     802        namelist = [name.split(':')[2],]
     803        if 'dA' in name:
     804            namelist = ['dAx','dAy','dAz']
     805        elif 'AU' in name:
     806            namelist = ['AUiso','AU11','AU22','AU33','AU12','AU13','AU23']
    772807        for item in nameList:
    773808            key = item.split(':')[2]
    774             if key in name and item != name:
     809            if key in namelist and item != name:
    775810                outList.append(item)
    776811        return outList
    777812       
    778     def SelectVarbs(FrstVarb,varList,legend):
     813    def SelectVarbs(FrstVarb,varList,legend,constType):
    779814        #future -  add 'all:all:name', '0:all:name', etc. to the varList
    780815        dlg = wx.MultiChoiceDialog(self,'Select more variables:'+legend,FrstVarb+' and:',varList)
     
    786821        dlg.Destroy()
    787822        if len(varbs) > 1:
    788             return map(list,zip([1.0 for i in range(len(varbs))],varbs))
    789         else:
    790             return [[0.0,FrstVarb],]        #setup for "fix" of variable
    791    
     823            if 'equivalents' in constType:
     824                constr = []
     825                for item in varbs[1:]:
     826#                    constr += [[{FrstVarb:1.0},{item:-1.0},0.0,False],]
     827                    constr += [[[1.0,FrstVarb],[-1.0,item],None,None],]
     828                return constr
     829            else:
     830                constr = map(list,zip([1.0 for i in range(len(varbs))],varbs))
     831#                constr = map(dict,zip(varbs,[1.0 for i in range(len(varbs))]))
     832                return [constr+[0.0,False]]
     833        return []
     834             
     835    def OnAddEquivalence(event):
     836        constr = []
     837        page = self.Page
     838        choice = scope[page[1]]
     839        dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+choice[1],choice[0],choice[2])
     840        if dlg.ShowModal() == wx.ID_OK:
     841            sel = dlg.GetSelection()
     842            FrstVarb = choice[2][sel]
     843            moreVarb = FindEquivVarb(FrstVarb,choice[2])
     844            constr = SelectVarbs(FrstVarb,moreVarb,choice[1],'equivalents')
     845            if len(constr) > 0:
     846                data[choice[3]] += constr
     847        dlg.Destroy()
     848        choice[4]()
     849   
     850    def OnAddHold(event):
     851        for phase in Phases:
     852            Phase = Phases[phase]
     853            Atoms = Phase['Atoms']
     854        constr = []
     855        page = self.Page
     856        choice = scope[page[1]]
     857        dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+choice[1],choice[0],choice[2])
     858        if dlg.ShowModal() == wx.ID_OK:
     859            sel = dlg.GetSelection()
     860            FrstVarb = choice[2][sel]
     861            data[choice[3]] += [[[0.0,FrstVarb],0.0,False],]
     862        dlg.Destroy()
     863        choice[4]()
     864       
    792865    def OnAddConstraint(event):
    793866        constr = []
    794         plegend = '\n In p::name'
    795         hlegend = '\n In :h:name'
    796         phlegend = '\n In p:h:name'
    797         for phase in Phases:
    798             plegend += '\n p:: = '+str(Phases[phase]['pId'])+':: for '+phase
    799             for histogram in Phases[phase]['Histograms']:
    800                 phlegend += '\n p:h: = '+str(Phases[phase]['pId'])+':'+str(Histograms[histogram]['hId'])+': for '+phase+' in '+histogram
    801         for histogram in Histograms:
    802             hlegend += '\n :h: = :'+str(Histograms[histogram]['hId'])+': for '+histogram
    803867        page = self.Page
    804         if 'Histogram ' in self.dataDisplay.GetPageText(page):
    805             dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+hlegend,'Histogram variables:',histVary)
    806             if dlg.ShowModal() == wx.ID_OK:
    807                 sel = dlg.GetSelection()
    808                 FrstVarb = histVary[sel]
    809                 moreVarb = FindEquivVarb(FrstVarb,histVary)
    810                 constr = SelectVarbs(FrstVarb,moreVarb,hlegend)
    811                 constr += [0.0,True]        #constant & refine flag
    812                 data['Hist'].append(constr)
    813             dlg.Destroy()
    814             UpdateHistConstr()
    815         elif '/Phase' in self.dataDisplay.GetPageText(page):
    816             legend = 'Select 1st variable: \n '
    817             dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+phlegend,'HAP variables:',hapVary)
    818             if dlg.ShowModal() == wx.ID_OK:
    819                 sel = dlg.GetSelection()
    820                 FrstVarb = hapVary[sel]
    821                 moreVarb = FindEquivVarb(FrstVarb,hapVary)
    822                 constr = SelectVarbs(FrstVarb,moreVarb,phlegend)
    823                 constr += [0.0,True]        #constant & refine flag
    824                 data['HAP'].append(constr)
    825             dlg.Destroy()
    826             UpdateHAPConstr()
    827         elif 'Phase' in self.dataDisplay.GetPageText(page):
    828             #maybe get atom name in phaseVary items?
    829             for phase in Phases:
    830                 Phase = Phases[phase]
    831                 Atoms = Phase['Atoms']
    832             dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+plegend,'Phase variables:',phaseVary)
    833             if dlg.ShowModal() == wx.ID_OK:
    834                 sel = dlg.GetSelection()
    835                 FrstVarb = phaseVary[sel]
    836                 moreVarb = FindEquivVarb(FrstVarb,phaseVary)
    837                 constr = SelectVarbs(FrstVarb,moreVarb,plegend+'\nIf none selected then '+FrstVarb+' is held fixed')
    838                 constr += [0.0,True]        #constant & refine flag
    839                 data['Phase'].append(constr)
    840             dlg.Destroy()
    841             UpdatePhaseConstr()
    842            
     868        choice = scope[page[1]]
     869        dlg = wx.SingleChoiceDialog(self,'Select 1st variable:'+choice[1],choice[0],choice[2])
     870        if dlg.ShowModal() == wx.ID_OK:
     871            sel = dlg.GetSelection()
     872            FrstVarb = choice[2][sel]
     873            moreVarb = FindEquivVarb(FrstVarb,choice[2])
     874            constr = SelectVarbs(FrstVarb,moreVarb,choice[1],'')
     875            if len(constr) > 0:
     876                data[choice[3]] += constr
     877        dlg.Destroy()
     878        choice[4]()
     879                       
    843880    def ConstSizer(name,pageDisplay):
    844881        constSizer = wx.FlexGridSizer(1,4,0,0)
    845882        for Id,item in enumerate(data[name]):
     883            constDel = wx.Button(pageDisplay,-1,'Delete',style=wx.BU_EXACTFIT)
     884            constDel.Bind(wx.EVT_BUTTON,OnConstDel)
     885            Indx[constDel.GetId()] = [Id,name]
    846886            if len(item) < 4:
    847                 constSizer.Add((5,0),0)
    848                 eqString = ' FIXED   '+item[0][1]
     887                constSizer.Add((5,5),0)
     888                constSizer.Add(constDel)
     889                eqString = ' FIXED   '+item[0][1]+'   '
    849890                constSizer.Add((5,0),0)
    850891            else:
     
    853894                Indx[constEdit.GetId()] = [Id,name]
    854895                constSizer.Add(constEdit)           
    855                 constRef = wx.CheckBox(pageDisplay,-1,label=' Refine?')
    856                 constRef.SetValue(item[-1])
    857                 constRef.Bind(wx.EVT_CHECKBOX,OnConstRef)
    858                 Indx[constRef.GetId()] = item
    859                 constSizer.Add(constRef,0,wx.ALIGN_CENTER_VERTICAL)
    860                 eqString = ''
     896                constSizer.Add(constDel)
     897                if isinstance(item[-1],bool):
     898                    constRef = wx.CheckBox(pageDisplay,-1,label=' Refine?')                   
     899                    constRef.SetValue(item[-1])
     900                    constRef.Bind(wx.EVT_CHECKBOX,OnConstRef)
     901                    Indx[constRef.GetId()] = item
     902                    constSizer.Add(constRef,0,wx.ALIGN_CENTER_VERTICAL)
     903                    eqString = ' CONSTR  '
     904                else:
     905                    constSizer.Add((5,5),0)
     906                    eqString = ' EQUIV   '
    861907                for term in item[:-2]:
    862908                    eqString += '%+.3f*%s '%(term[0],term[1])
    863                 eqString += ' = %.3f'%(item[-2])
     909                if isinstance(item[-2],float):
     910                    eqString += ' = %.3f'%(item[-2])+'  '
     911                else:
     912                    eqString += ' = 0   '
    864913            constSizer.Add(wx.StaticText(pageDisplay,-1,eqString),0,wx.ALIGN_CENTER_VERTICAL)
    865             constDel = wx.Button(pageDisplay,-1,'Delete',style=wx.BU_EXACTFIT)
    866             constDel.Bind(wx.EVT_BUTTON,OnConstDel)
    867             Indx[constDel.GetId()] = [Id,name]
    868             constSizer.Add(constDel)
    869914        return constSizer
    870915               
     
    877922        Id,name = Indx[Obj.GetId()]
    878923        del(data[name][Id])
    879         OnPageChanged(event)       
     924        OnPageChanged(None)       
    880925       
    881926    def OnConstEdit(event):
     
    883928        Id,name = Indx[Obj.GetId()]
    884929        const = data[name][Id][-2]
    885         items = data[name][Id][:-2]+[[const,'= constant'],[]]
    886         print items
     930        if isinstance(data[name][Id][-2],float):
     931            items = data[name][Id][:-2]+[[const,'= constant'],[]]
     932        else:
     933            items = data[name][Id][:-2]+[[],]
    887934        dlg = self.SumDialog(self,'Constraint','Enter value for each term in constraint','',items)
    888935        try:
    889936            if dlg.ShowModal() == wx.ID_OK:
    890937                result = dlg.GetData()
    891                 print result
    892                 data[name][Id][:-2] = result[:-1]
    893                 data[name][Id][-2] = result[-2][0]
     938                if isinstance(data[name][Id][-2],float):
     939                    data[name][Id][:-2] = result[:-2]
     940                    data[name][Id][-2] = result[-2][0]
     941                else:
     942                    data[name][Id][:-2] = result[:-1]
    894943        finally:
    895944            dlg.Destroy()           
    896         OnPageChanged(event)       
    897              
     945        OnPageChanged(None)                     
    898946   
    899947    def UpdateHAPConstr():
    900948        HAPConstr.DestroyChildren()
    901         pageDisplay = wx.Panel(HAPConstr)
    902         mainSizer = wx.BoxSizer(wx.VERTICAL)
    903         mainSizer.Add((5,5),0)
    904         mainSizer.Add(ConstSizer('HAP',pageDisplay))
    905         pageDisplay.SetSizer(mainSizer)
    906         Size = mainSizer.Fit(self.dataFrame)
     949        HAPDisplay = wx.Panel(HAPConstr)
     950        HAPSizer = wx.BoxSizer(wx.VERTICAL)
     951        HAPSizer.Add((5,5),0)
     952        HAPSizer.Add(ConstSizer('HAP',HAPDisplay))
     953        HAPSizer.Layout()
     954        HAPDisplay.SetSizer(HAPSizer)
     955        Size = HAPSizer.Fit(self.dataFrame)
     956        Size[0] = max(450,Size[0])
    907957        Size[1] += 26                           #compensate for status bar
    908         pageDisplay.SetSize(Size)
     958        HAPDisplay.SetSize(Size)
    909959        self.dataFrame.setSizePosLeft(Size)
    910960       
    911961    def UpdateHistConstr():
    912962        HistConstr.DestroyChildren()
    913         pageDisplay = wx.Panel(HistConstr)
    914         mainSizer = wx.BoxSizer(wx.VERTICAL)
    915         mainSizer.Add((5,5),0)       
    916         mainSizer.Add(ConstSizer('Hist',pageDisplay))
    917         pageDisplay.SetSizer(mainSizer)
    918         Size = mainSizer.Fit(self.dataFrame)
     963        HistDisplay = wx.Panel(HistConstr)
     964        HistSizer = wx.BoxSizer(wx.VERTICAL)
     965        HistSizer.Add((5,5),0)       
     966        HistSizer.Add(ConstSizer('Hist',HistDisplay))
     967        HistSizer.Layout()
     968        HistDisplay.SetSizer(HistSizer)
     969        Size = HistSizer.Fit(self.dataFrame)
     970        Size[0] = max(450,Size[0])
    919971        Size[1] += 26                           #compensate for status bar
    920         pageDisplay.SetSize(Size)
     972        HistDisplay.SetSize(Size)
    921973        self.dataFrame.setSizePosLeft(Size)
    922974       
    923975    def UpdatePhaseConstr():
    924976        PhaseConstr.DestroyChildren()
    925         pageDisplay = wx.Panel(PhaseConstr)
    926         mainSizer = wx.BoxSizer(wx.VERTICAL)
    927         mainSizer.Add((5,5),0)       
    928         mainSizer.Add(ConstSizer('Phase',pageDisplay))
    929         pageDisplay.SetSizer(mainSizer)
    930         Size = mainSizer.Fit(self.dataFrame)
     977        PhaseDisplay = wx.Panel(PhaseConstr)
     978        PhaseSizer = wx.BoxSizer(wx.VERTICAL)
     979        PhaseSizer.Add((5,5),0)       
     980        PhaseSizer.Add(ConstSizer('Phase',PhaseDisplay))
     981        PhaseSizer.Layout()
     982        PhaseDisplay.SetSizer(PhaseSizer)
     983        Size = PhaseSizer.Fit(self.dataFrame)
    931984        Size[1] += 26                           #compensate for status bar
    932         pageDisplay.SetSize(Size)
     985        Size[0] = max(450,Size[0])
     986        PhaseDisplay.SetSize(Size)
    933987        self.dataFrame.setSizePosLeft(Size)
    934988   
    935989    def OnPageChanged(event):
    936         page = event.GetSelection()
    937         self.Page = page
     990        if event:       #page change event!
     991            page = event.GetSelection()
     992        else:
     993            page = self.dataDisplay.GetSelection()
     994        oldPage = self.dataDisplay.ChangeSelection(page)
    938995        text = self.dataDisplay.GetPageText(page)
    939996        if text == 'Histogram/Phase constraints':
     997            self.Page = [page,'hap']
    940998            UpdateHAPConstr()
    941999        elif text == 'Histogram constraints':
     1000            self.Page = [page,'hst']
    9421001            UpdateHistConstr()
    9431002        elif text == 'Phase constraints':
     1003            self.Page = [page,'phs']
    9441004            UpdatePhaseConstr()
    9451005
     1006    def SetStatusLine(text):
     1007        Status.SetStatusText(text)                                     
     1008       
     1009    plegend,hlegend,phlegend = GetPHlegends(Phases,Histograms)
     1010    scope = {'hst':['Histogram variables:',hlegend,histList,'Hist',UpdateHistConstr],
     1011        'hap':['HAP variables:',phlegend,hapList,'HAP',UpdateHAPConstr],
     1012        'phs':['Phase variables:',plegend,phaseList,'Phase',UpdatePhaseConstr]}
    9461013    if self.dataDisplay:
    9471014        self.dataDisplay.Destroy()
    9481015    self.dataFrame.SetMenuBar(self.dataFrame.ConstraintMenu)
    9491016    self.dataFrame.SetLabel('Constraints')
    950     self.dataFrame.CreateStatusBar()
     1017    if not self.dataFrame.GetStatusBar():
     1018        Status = self.dataFrame.CreateStatusBar()
     1019    SetStatusLine('NB: Set parameters to be varied first')
     1020   
    9511021    self.dataFrame.SetMenuBar(self.dataFrame.ConstraintMenu)
    9521022    self.dataFrame.Bind(wx.EVT_MENU, OnAddConstraint, id=wxID_CONSTRAINTADD)
     1023    self.dataFrame.Bind(wx.EVT_MENU, OnAddEquivalence, id=wxID_EQUIVADD)
     1024    self.dataFrame.Bind(wx.EVT_MENU, OnAddHold, id=wxID_HOLDADD)
    9531025    self.dataDisplay = GSNoteBook(parent=self.dataFrame,size=self.dataFrame.GetClientSize())
    9541026   
  • trunk/GSASIImapvars.py

    r342 r407  
    127127
    128128# prefix for parameter names
    129 paramPrefix = "::constr"
     129paramPrefix = "::constr:"
    130130consNum = 0 # number of the next constraint to be created
    131131
     
    655655        ]
    656656    constrDict,constrFlag,fixedList = InputParse(mapList)
     657    print constrDict
     658    print constrFlag
     659    print fixedList
    657660    groups,parmlist = GroupConstraints(constrDict)
    658661    GenerateConstraints(groups,parmlist,constrDict,constrFlag,fixedList)
     
    665668    #print 'parmdict start',parmdict
    666669    before = parmdict.copy()
    667     Map2Dict(parmdict)
     670    Map2Dict(parmdict,[])
    668671    print 'parmdict before and after Map2Dict'
    669672    print '  key / before / after'
  • trunk/GSASIIphsGUI.py

    r406 r407  
    23982398            parms = zip(['S11','S22','S33','S12','S13','S23'],UseList[item]['Size'][4],
    23992399                UseList[item]['Size'][5],range(6))
    2400             dataSizer = wx.FlexGridSizer(2,6,5,5)
     2400            dataSizer = wx.FlexGridSizer(1,6,5,5)
    24012401            for Pa,val,ref,id in parms:
    24022402                sizeRef = wx.CheckBox(dataDisplay,-1,label=Pa)
  • trunk/GSASIIstruct.py

    r406 r407  
    6363    print ' Minimum delta-M/M for convergence: ','%.2g'%(Controls['min dM/M'])
    6464    print ' Initial shift factor: ','%.3f'%(Controls['shift factor'])
     65   
     66def GetConstraints(GPXfile):
     67    constrList = []
     68    file = open(GPXfile,'rb')
     69    while True:
     70        try:
     71            data = cPickle.load(file)
     72        except EOFError:
     73            break
     74        datum = data[0]
     75        if datum[0] == 'Constraints':
     76            constDict = datum[1]
     77            for item in constDict:
     78                constrList += constDict[item]
     79    file.close()
     80    return constrList
    6581   
    6682def GetPhaseNames(GPXfile):
     
    23482364    Controls = GetControls(GPXfile)
    23492365    ShowControls(Controls)           
     2366    constrList = GetConstraints(GPXfile)
    23502367    Histograms,Phases = GetUsedHistogramsAndPhases(GPXfile)
    23512368    if not Phases:
     
    23692386    parmDict.update(histDict)
    23702387    GetFprime(calcControls,Histograms)
     2388    for item in constrList: print item
    23712389    constrDict,constrFlag,fixedList = G2mv.InputParse([])        #constraints go here?
    23722390    groups,parmlist = G2mv.GroupConstraints(constrDict)
     
    24562474    Controls = GetControls(GPXfile)
    24572475    ShowControls(Controls)           
     2476    constrList = GetConstraints(GPXfile)
    24582477    Histograms,Phases = GetUsedHistogramsAndPhases(GPXfile)
    24592478    if not Phases:
Note: See TracChangeset for help on using the changeset viewer.