Skip to content

Commit 633f1bd

Browse files
committed
Add group copy/copy selected; load histogram & phase once; make prmArray (from getXXX routines) global; fix some display bugs
1 parent 784e3be commit 633f1bd

1 file changed

Lines changed: 77 additions & 68 deletions

File tree

GSASII/GSASIIgroupGUI.py

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,15 @@ def UpdateGroup(G2frame,item,plot=True):
181181
def onDisplaySel(event):
182182
G2frame.GroupInfo['displayMode'] = dsplType.GetValue()
183183
wx.CallAfter(UpdateGroup,G2frame,item,False)
184-
#UpdateGroup(G2frame,item)
185-
def OnCopyAll(event):
186-
G2G.G2MessageBox(G2frame,
187-
'Sorry, not fully implemented yet',
188-
'In progress')
189-
return
184+
185+
def copyPrep():
190186
Controls = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,G2frame.root, 'Controls'))
191187
groupDict = Controls.get('Groups',{}).get('groupDict',{})
192188
groupName = G2frame.GroupInfo['groupName']
193189
# make a list of groups of the same length as the current
194190
curLen = len(groupDict[groupName])
195191
matchGrps = []
192+
selList = []
196193
for g in groupDict:
197194
if g == groupName: continue
198195
if curLen != len(groupDict[g]): continue
@@ -212,38 +209,63 @@ def OnCopyAll(event):
212209
else:
213210
selList = matchGrps
214211
if len(selList) == 0: return
215-
Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
216-
prmArray = None
217-
if G2frame.GroupInfo['displayMode'].startswith('Sample'):
218-
prmArray = getSampleVals(G2frame,Histograms)
219-
elif G2frame.GroupInfo['displayMode'].startswith('Instrument'):
220-
prmArray = getInstVals(G2frame,Histograms)
221-
elif G2frame.GroupInfo['displayMode'].startswith('Limits'):
222-
prmArray = getLimitVals(G2frame,Histograms)
223-
elif G2frame.GroupInfo['displayMode'].startswith('Background'):
224-
prmArray = getBkgVals(G2frame,Histograms)
225-
else:
226-
print('Unexpected', G2frame.GroupInfo['displayMode'])
227-
return
228-
for h in selList: # group
229-
for src,dst in zip(groupDict[groupName],groupDict[h]):
230-
print('copy',src,'to',dst)
231-
# for i in prmTable[src]:
232-
# #if i not in prmTable[dst]:
233-
# # print
234-
# # continue
235-
# if 'val' in prmTable[src][i]:
236-
# breakpoint()
237-
# so what do we copy?
238-
#breakpoint()
239-
print('OnCopyAll')
212+
return selList,groupDict,groupName
213+
214+
def OnCopyAll(event):
215+
res = copyPrep()
216+
if res is None: return
217+
selList,groupDict,groupName = res
218+
dataSource = prmArray['_dataSource']
219+
for h in selList: # selected groups
220+
for src,dst in zip(groupDict[groupName],groupDict[h]): # histograms in groups (same length enforced)
221+
for i in prmArray[src]:
222+
for j in ('ref','val','str'):
223+
if j in prmArray[src][i]:
224+
if prmArray[src][i][j] is None: continue
225+
try:
226+
arr,indx = indexArrayRef(dataSource,dst,prmArray[src][i][j])
227+
arr[indx] = indexArrayVal(dataSource,src,prmArray[src][i][j])
228+
except Exception as msg: # could hit an error if an array element is not defined
229+
pass
230+
#print(msg)
231+
#print('error with',i,dst)
240232
def OnCopySel(event):
241-
print('OnCopySel')
242-
G2G.G2MessageBox(G2frame,
243-
f'Sorry, not fully implemented yet',
244-
'In progress')
245-
return
233+
res = copyPrep()
234+
if res is None: return
235+
selList,groupDict,groupName = res
236+
dataSource = prmArray['_dataSource']
237+
choices = []
238+
for src in groupDict[groupName]:
239+
for i in prmArray[src]:
240+
for j in ('ref','val','str'):
241+
if prmArray[src][i].get(j) is None:
242+
continue
243+
if i not in choices:
244+
choices.append(i)
245+
dlg = G2G.G2MultiChoiceDialog(G2frame, 'Copy which items?', 'Copy what?', choices)
246+
itemList = []
247+
try:
248+
if dlg.ShowModal() == wx.ID_OK:
249+
itemList = [choices[i] for i in dlg.GetSelections()]
250+
finally:
251+
dlg.Destroy()
252+
if len(itemList) == 0: return
253+
for h in selList: # selected groups
254+
for src,dst in zip(groupDict[groupName],groupDict[h]): # histograms in groups (same length enforced)
255+
for i in prmArray[src]:
256+
if i not in itemList: continue
257+
for j in ('ref','val','str'):
258+
if j in prmArray[src][i]:
259+
if prmArray[src][i][j] is None: continue
260+
try:
261+
arr,indx = indexArrayRef(dataSource,dst,prmArray[src][i][j])
262+
arr[indx] = indexArrayVal(dataSource,src,prmArray[src][i][j])
263+
except Exception as msg: # could hit an error if an array element is not defined
264+
pass
265+
#print(msg)
266+
#print('error with',i,dst)
246267

268+
Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
247269
if not hasattr(G2frame,'GroupInfo'):
248270
G2frame.GroupInfo = {}
249271
G2frame.GroupInfo['displayMode'] = G2frame.GroupInfo.get('displayMode','Sample')
@@ -271,9 +293,9 @@ def OnCopySel(event):
271293
topSizer.Add(G2G.HelpButton(topParent,helpIndex=G2frame.dataWindow.helpKey))
272294

273295
if G2frame.GroupInfo['displayMode'].startswith('Hist'):
274-
HAPframe(G2frame)
296+
HAPframe(G2frame,Histograms,Phases)
275297
else:
276-
HistFrame(G2frame)
298+
HistFrame(G2frame,Histograms)
277299
if plot: G2pwpl.PlotPatterns(G2frame,plotType='GROUP')
278300
G2frame.dataWindow.SetDataSize()
279301
#wx.CallLater(100,G2frame.SendSizeEvent)
@@ -509,13 +531,13 @@ def displayDataArray(rowLabels,DataArray,Sizer,Panel,lblRow=False,deltaMode=Fals
509531
print('Should not happen',DataArray[hist][row],hist,row)
510532
return firstentry,lblDict
511533

512-
def HistFrame(G2frame):
534+
def HistFrame(G2frame,Histograms):
513535
'''Put everything in a single FlexGridSizer.
514536
'''
515537
#---------------------------------------------------------------------
516538
# generate a dict with values for each histogram
517-
Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
518539
CopyCtrl = True
540+
global prmArray
519541
if G2frame.GroupInfo['displayMode'].startswith('Sample'):
520542
prmArray = getSampleVals(G2frame,Histograms)
521543
elif G2frame.GroupInfo['displayMode'].startswith('Instrument'):
@@ -527,6 +549,7 @@ def HistFrame(G2frame):
527549
prmArray = getBkgVals(G2frame,Histograms)
528550
CopyCtrl = False
529551
else:
552+
prmArray = None
530553
print('Unexpected', G2frame.GroupInfo['displayMode'])
531554
return
532555
rowLabels = []
@@ -560,7 +583,6 @@ def HistFrame(G2frame):
560583
mainSizer = wx.BoxSizer(wx.VERTICAL)
561584
G2G.HorizontalLine(mainSizer,panel)
562585
panel.SetSizer(mainSizer)
563-
Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
564586
deltaMode = "\u0394" in G2frame.GroupInfo['displayMode']
565587
n = 2
566588
if CopyCtrl and len(prmArray) > 2: n += 1 # add column for copy (when more than one histogram)
@@ -824,7 +846,7 @@ def getBkgVals(G2frame,Histograms):
824846
'txt' : val}
825847
return indexDict
826848

827-
def HAPframe(G2frame):
849+
def HAPframe(G2frame,Histograms,Phases):
828850
'''This creates two side-by-side scrolled panels, each containing
829851
a FlexGridSizer.
830852
The panel to the left contains the labels for the sizer to the right.
@@ -852,15 +874,16 @@ def OnScroll(event):
852874
page = 0
853875
#print('no page selected',phaseList[page])
854876
# generate a dict with HAP values for each phase (may not be the same)
855-
HAParray = getHAPvals(G2frame,phaseList[page])
877+
global prmArray
878+
prmArray = getHAPvals(G2frame,phaseList[page],Histograms,Phases)
856879
# construct a list of row labels, attempting to keep the
857880
# order they appear in the original array
858881
rowLabels = []
859882
lpos = 0
860-
for hist in HAParray:
883+
for hist in prmArray:
861884
if hist == '_dataSource': continue
862885
prevkey = None
863-
for key in HAParray[hist]:
886+
for key in prmArray[hist]:
864887
if key not in rowLabels:
865888
if prevkey is None:
866889
rowLabels.insert(lpos,key)
@@ -886,7 +909,7 @@ def OnScroll(event):
886909
# Create scrolled panel to display HAP data
887910
HAPScroll = wx.lib.scrolledpanel.ScrolledPanel(panel,
888911
style=wx.VSCROLL|wx.HSCROLL|wx.ALWAYS_SHOW_SB)
889-
HAPSizer = wx.FlexGridSizer(0,len(HAParray),hpad,10)
912+
HAPSizer = wx.FlexGridSizer(0,len(prmArray),hpad,10)
890913
HAPScroll.SetSizer(HAPSizer)
891914
bigSizer.Add(HAPScroll,1,wx.EXPAND)
892915

@@ -903,7 +926,7 @@ def OnScroll(event):
903926
w0 = wx.StaticText(lblScroll,label=' ')
904927
lblSizer.Add(w0)
905928
lblSizer.Add(wx.StaticText(lblScroll,label=' Ref '))
906-
firstentry,lblDict = displayDataArray(rowLabels,HAParray,HAPSizer,HAPScroll,
929+
firstentry,lblDict = displayDataArray(rowLabels,prmArray,HAPSizer,HAPScroll,
907930
lblRow=True,lblSizer=lblSizer,lblPanel=lblScroll)
908931
# get row sizes in data table
909932
HAPSizer.Layout()
@@ -944,7 +967,6 @@ def OnScroll(event):
944967

945968
G2G.HorizontalLine(mainSizer,midPanel)
946969
midPanel.SetSizer(mainSizer)
947-
Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
948970
if not Phases:
949971
mainSizer.Add(wx.StaticText(midPanel,
950972
label='There are no phases in use'))
@@ -966,26 +988,13 @@ def OnScroll(event):
966988
selectPhase(None)
967989
#G2frame.dataWindow.SetDataSize()
968990

969-
def getHAPvals(G2frame,phase):
991+
def getHAPvals(G2frame,phase,Histograms,Phases):
970992
'''Generate the Parameter Data Table (a dict of dicts) with
971993
all HAP values for the selected phase and all histograms in the
972994
selected histogram group (from G2frame.GroupInfo['groupName']).
973995
This will be used to generate the contents of the GUI for HAP values.
974996
'''
975-
sub = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,'Phases')
976-
item, cookie = G2frame.GPXtree.GetFirstChild(sub)
977-
PhaseData = None
978-
while item: # loop over phases
979-
phaseName = G2frame.GPXtree.GetItemText(item)
980-
if phase is None: phase = phaseName
981-
if phase == phaseName:
982-
PhaseData = G2frame.GPXtree.GetItemPyData(item)
983-
break
984-
item, cookie = G2frame.GPXtree.GetNextChild(sub, cookie)
985-
if PhaseData is None:
986-
print(f'Unexpected: Phase {phase!r} not found')
987-
return {}
988-
997+
PhaseData = Phases[phase]
989998
SGData = PhaseData['General']['SGData']
990999
cell = PhaseData['General']['Cell'][1:]
9911000
Amat,Bmat = G2lat.cell2AB(cell[:6])
@@ -1079,15 +1088,15 @@ def getHAPvals(G2frame,phase):
10791088
indexDict[hist]['Spherical harmonics'] = {
10801089
'ref' : ('Pref.Ori.',2),}
10811090
indexDict[hist]['SH order'] = {
1082-
'str' : str('Pref.Ori.',4)}
1083-
for lbl in arr[5]:
1091+
'str' : ('Pref.Ori.',4),
1092+
'fmt' : '.0f'}
1093+
for lbl in PhaseData['Histograms'][hist]['Pref.Ori.'][5]:
10841094
indexDict[hist][f'SP {lbl}']= {
10851095
'val' : ('Pref.Ori.',5,lbl),
10861096
}
1087-
indexDict[hist]['SH text indx'] = {
1097+
indexDict[hist]['SH txtr indx'] = {
10881098
'str' : None,
1089-
'txt' : f'{G2lat.textureIndex('Pref.Ori.'[5]):.3f}'}
1090-
1099+
'txt' : f'{G2lat.textureIndex(PhaseData['Histograms'][hist]['Pref.Ori.'][5]):.3f}'}
10911100
# misc: Layer Disp, Extinction
10921101
if 'Layer Disp' in PhaseData['Histograms'][hist]:
10931102
indexDict[hist]['Layer displ'] = {

0 commit comments

Comments
 (0)