Skip to content

Commit a573b7e

Browse files
committed
rework HDF5 comments; add comments search; more startup options for testing/devel; allow comments to have = or : (note #258); change .bat file name fir reset now that it is fixed.
1 parent 9141ebe commit a573b7e

File tree

7 files changed

+281
-31
lines changed

7 files changed

+281
-31
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
config.py
33

44
# local code for debugging
5-
debug_setup.py.example
6-
debug_setup.py
5+
debug_setup.py*
6+
debug_startup.py*
77

88
# created by install/saved-versions.py
99
GSASII/saved_version.py

GSASII/GSASIIctrlGUI.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,62 @@ def Filter(self,event):
22252225
ChoiceList = self.ChoiceList
22262226
self.clb.AppendItems(ChoiceList)
22272227
self._ShowSelections()
2228+
2229+
def SelectSearchVars(G2frame,labelLst,keyList):
2230+
'''Get a sample parameter and a comment label from the user
2231+
so we can search for that in the comments
2232+
'''
2233+
def OnChoice(event):
2234+
'Respond when a parameter is selected in the Choice box'
2235+
key = event.GetEventObject().key
2236+
result[key] = event.GetString()
2237+
if result.get('Selection') and result.get('Key'):
2238+
dlg.EndModal(wx.ID_OK)
2239+
2240+
result = {}
2241+
dlg = wx.Dialog(G2frame,wx.ID_ANY,'Select a parameter to set',
2242+
style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
2243+
mainSizer = wx.BoxSizer(wx.VERTICAL)
2244+
mainSizer.Add((5,5))
2245+
subSizer = wx.BoxSizer(wx.HORIZONTAL)
2246+
subSizer.Add((-1,-1),1,wx.EXPAND)
2247+
subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Select a parameter'))
2248+
subSizer.Add((-1,-1),1,wx.EXPAND)
2249+
mainSizer.Add(subSizer,0,wx.EXPAND,0)
2250+
mainSizer.Add((0,10))
2251+
2252+
subSizer = wx.FlexGridSizer(0,2,5,0)
2253+
subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Parameter: '),0,wx.BOTTOM,10)
2254+
ch = wx.Choice(dlg, wx.ID_ANY, choices = sorted(labelLst))
2255+
ch.key = 'Selection'
2256+
ch.SetSelection(-1)
2257+
ch.Bind(wx.EVT_CHOICE, OnChoice)
2258+
subSizer.Add(ch)
2259+
subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Key in comments: '),0,wx.TOP,10)
2260+
ch = wx.Choice(dlg, wx.ID_ANY, choices = keyList)
2261+
ch.key = 'Key'
2262+
ch.SetSelection(-1)
2263+
ch.Bind(wx.EVT_CHOICE, OnChoice)
2264+
subSizer.Add(ch)
2265+
mainSizer.Add(subSizer)
2266+
2267+
mainSizer.Add((-1,20))
2268+
btnsizer = wx.StdDialogButtonSizer()
2269+
btn = wx.Button(dlg, wx.ID_CANCEL)
2270+
btnsizer.AddButton(btn)
2271+
btnsizer.Realize()
2272+
mainSizer.Add((-1,5),1,wx.EXPAND,1)
2273+
mainSizer.Add(btnsizer,0,wx.ALIGN_CENTER,0)
2274+
mainSizer.Add((-1,10))
2275+
2276+
dlg.SetSizer(mainSizer)
2277+
mainSizer.Fit(dlg)
2278+
dlg.CenterOnParent()
2279+
if dlg.ShowModal() == wx.ID_OK:
2280+
dlg.Destroy()
2281+
return result.get('Selection'),result.get('Key')
2282+
dlg.Destroy()
2283+
return None,None
22282284

22292285
def SelectEdit1Var(G2frame,array,labelLst,elemKeysLst,dspLst,refFlgElem):
22302286
'''Select a variable from a list, then edit it and select histograms

GSASII/GSASIIdataGUI.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,15 @@ def GSASIImain(application):
770770
#application.GetTopWindow().SendSizeEvent()
771771
application.GetTopWindow().Show(True)
772772
application.main.UpdateTask = GSASIIpath.GetRepoUpdatesInBackground()
773+
if GSASIIpath.GetConfigValue('debug'):
774+
cmdfile = os.path.join(GSASIIpath.path2GSAS2,'debug_startup.py')
775+
if os.path.exists(cmdfile):
776+
print(f'executing debug commands from {cmdfile}')
777+
txt = open(cmdfile,'r').read()
778+
def exectxt():
779+
# print(txt)
780+
exec(txt)
781+
wx.CallLater(100,exectxt)
773782

774783
#### Create main frame (window) for GUI; main menu items here #######################################
775784
class GSASII(wx.Frame):
@@ -982,7 +991,7 @@ def PreviewFile(self,filename):
982991
return False
983992

984993
def OnImportGeneric(self,reader,readerlist,label,multiple=False,
985-
usedRanIdList=[],Preview=True,load2Tree=False):
994+
usedRanIdList=[],Preview=True,load2Tree=False,filename=None):
986995
'''Used for all imports, including Phases, datasets, images...
987996
988997
Called from :meth:`GSASII.OnImportPhase`, :meth:`GSASII.OnImportImage`,
@@ -1032,6 +1041,8 @@ def OnImportGeneric(self,reader,readerlist,label,multiple=False,
10321041
value to change to a list of True values rather than
10331042
reader objects.
10341043
1044+
:param str filename: a filename, used only for debugging
1045+
10351046
:returns: a list of reader objects (rd_list) that were able
10361047
to read the specified file(s). This list may be empty.
10371048
'''
@@ -1079,8 +1090,11 @@ def OnImportGeneric(self,reader,readerlist,label,multiple=False,
10791090
typ = ' (type to be guessed)'
10801091
else:
10811092
typ = ' (type '+readerlist[0].formatName+')'
1082-
filelist = G2G.GetImportFile(self,message="Choose "+label+" input file"+typ,
1083-
defaultFile="",wildcard=choices,style=mode)
1093+
if filename:
1094+
filelist = [filename,]
1095+
else:
1096+
filelist = G2G.GetImportFile(self,message="Choose "+label+" input file"+typ,
1097+
defaultFile="",wildcard=choices,style=mode)
10841098
rd_list = []
10851099
filelist1 = []
10861100
for filename in filelist:
@@ -6955,7 +6969,7 @@ def _makemenu(): # routine to create menu when first used
69556969

69566970
# PWDR / Sample Parameters
69576971
G2G.Define_wxId('wxID_SAMPLECOPY', 'wxID_SAMPLECOPYSOME', 'wxID_SAMPLEFLAGCOPY','wxID_SAMPLESAVE',
6958-
'wxID_SAMPLELOAD', 'wxID_SETSCALE', 'wxID_SAMPLE1VAL', 'wxID_ALLSAMPLELOAD',)
6972+
'wxID_SAMPLELOAD', 'wxID_SETSCALE', 'wxID_SAMPLE1VAL', 'wxID_ALLSAMPLELOAD','wxID_SEARCHVAL',)
69596973
def _makemenu(): # routine to create menu when first used
69606974
self.SampleMenu = wx.MenuBar()
69616975
self.PrefillDataMenu(self.SampleMenu)
@@ -6970,6 +6984,7 @@ def _makemenu(): # routine to create menu when first used
69706984
self.SampleEdit.Append(G2G.wxID_SAMPLE1VAL,'Set one value','Set one sample parameter value across multiple histograms')
69716985
self.SampleEdit.Append(G2G.wxID_ALLSAMPLELOAD,'Load all','Load sample parameters over multiple histograms')
69726986
self.SampleEdit.Append(G2G.wxID_RESCALEALL,'Rescale all','Rescale all data with selected range')
6987+
self.SampleEdit.Append(G2G.wxID_SEARCHVAL,'Set from comments','Search the comments to set a parameter value')
69736988
self.PostfillDataMenu()
69746989
self.SetScale.Enable(False)
69756990
SetDataMenuBar(G2frame,self.SampleMenu)

GSASII/GSASIIpwdGUI.py

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,27 +3849,113 @@ def OnCopy1Val(event):
38493849
# TODO: check if values need to be copied to editing widgets
38503850
wx.CallAfter(UpdateSampleGrid,G2frame,data)
38513851

3852+
def OnSearchComments(event):
3853+
'''Set a selected Sample parameter from a selected entry in the comments
3854+
for a selected set of histograms
3855+
'''
3856+
# get possible keys from current histogram's comments
3857+
Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(
3858+
G2frame, G2frame.PatternId, 'Comments'))
3859+
keyList = []
3860+
for line in Comments:
3861+
if ':' in line:
3862+
key = line.split(':')[0]
3863+
elif '=' in line:
3864+
key = line.split('=')[0]
3865+
else:
3866+
continue
3867+
keyList.append(key.strip())
3868+
sampleVar,commentKey = G2G.SelectSearchVars(G2frame,labelLst,keyList)
3869+
if sampleVar is None or commentKey is None: return
3870+
# find the array element tied to the sample var
3871+
if sampleVar not in labelLst:
3872+
print(f'OnSearchComments: how did we get {sampleVar} not in {labelLst}?')
3873+
return
3874+
i = labelLst.index(sampleVar)
3875+
sampleArrKeys = elemKeysLst[i]
3876+
# need to select histograms to search/set
3877+
hst = G2frame.GPXtree.GetItemText(G2frame.PatternId)
3878+
histList = GetHistsLikeSelected(G2frame)
3879+
histList.insert(0,hst)
3880+
if len(histList) == 1: # only current histogram present -- don't ask
3881+
selection = [0]
3882+
else:
3883+
dlg = G2G.G2MultiChoiceDialog(G2frame,f'Set "{sampleVar}" value in histograms...',
3884+
'Set value from comments', histList)
3885+
try:
3886+
if dlg.ShowModal() == wx.ID_OK:
3887+
selection = dlg.GetSelections()
3888+
finally:
3889+
dlg.Destroy()
3890+
# cycle through selected histograms, get comments & values
3891+
count = 0
3892+
for i in selection:
3893+
hist = histList[i]
3894+
hId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root, hist)
3895+
if not hId:
3896+
print(f'{hist} not found! strange')
3897+
continue
3898+
Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,hId,'Comments'))
3899+
Sample = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,hId, 'Sample Parameters'))
3900+
# search comments for value matching commentKey
3901+
for item in Comments:
3902+
if '=' in item:
3903+
itemSp = item.split('=')
3904+
elif ':' in item:
3905+
itemSp = item.split(':')
3906+
else:
3907+
continue
3908+
if commentKey == itemSp[0]:
3909+
try:
3910+
value = float(itemSp[1])
3911+
break
3912+
except:
3913+
print(f'"{item.strip()}" has an invalid value in Comments for {hist}')
3914+
else:
3915+
print(f'"{commentKey}" not in Comments for {hist}')
3916+
continue
3917+
# set the value in the selected entry in Sample Parameters
3918+
try:
3919+
d = Sample
3920+
for k in sampleArrKeys[:-1]:
3921+
d = d[k]
3922+
d[sampleArrKeys[-1]]
3923+
except KeyError:
3924+
print(f'Strange: "{commentKey}" not in Sample Parameters for {hist}')
3925+
continue
3926+
count += 1
3927+
d[sampleArrKeys[-1]] = value
3928+
print(f'Done. Set "{sampleVar}" in {count} histograms')
3929+
wx.CallAfter(UpdateSampleGrid,G2frame,data)
3930+
38523931
def SearchAllComments(value,tc,*args,**kwargs):
38533932
'''Called when the label for a FreePrm is changed: the comments for all PWDR
3854-
histograms are searched for a "label=value" pair that matches the label (case
3933+
histograms are searched for a "label=value" or "label:value" pair that matches the label (case
38553934
is ignored) and the values are then set to this value, if it can be converted
38563935
to a float.
38573936
'''
38583937
Id, cookie = G2frame.GPXtree.GetFirstChild(G2frame.root)
3938+
count = 0
38593939
while Id:
38603940
name = G2frame.GPXtree.GetItemText(Id)
38613941
if 'PWDR' in name:
38623942
Comments = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,Id,'Comments'))
38633943
Sample = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,Id, 'Sample Parameters'))
38643944
for i,item in enumerate(Comments):
3865-
itemSp = item.split('=')
3866-
if value.lower() == itemSp[0].lower():
3945+
if '=' in item:
3946+
itemSp = item.split('=')
3947+
elif ':' in item:
3948+
itemSp = item.split(':')
3949+
else:
3950+
continue
3951+
if value.lower() in itemSp[0].lower():
38673952
try:
38683953
Sample[tc.key] = float(itemSp[1])
38693954
except:
38703955
print('"{}" has an invalid value in Comments from {}'
38713956
.format(item.strip(),name))
38723957
Id, cookie = G2frame.GPXtree.GetNextChild(G2frame.root, cookie)
3958+
print(f'{count} values were found in the histogram comments')
38733959
wx.CallLater(100,UpdateSampleGrid,G2frame,data)
38743960

38753961
# start of UpdateSampleGrid
@@ -3886,6 +3972,7 @@ def SearchAllComments(value,tc,*args,**kwargs):
38863972
G2frame.Bind(wx.EVT_MENU, OnSampleSave, id=G2G.wxID_SAMPLESAVE)
38873973
G2frame.Bind(wx.EVT_MENU, OnSampleLoad, id=G2G.wxID_SAMPLELOAD)
38883974
G2frame.Bind(wx.EVT_MENU, OnCopy1Val, id=G2G.wxID_SAMPLE1VAL)
3975+
G2frame.Bind(wx.EVT_MENU, OnSearchComments, id=G2G.wxID_SEARCHVAL)
38893976
G2frame.Bind(wx.EVT_MENU, OnAllSampleLoad, id=G2G.wxID_ALLSAMPLELOAD)
38903977
G2frame.Bind(wx.EVT_MENU, OnRescaleAll, id=G2G.wxID_RESCALEALL)
38913978
if histName[:4] in ['SASD','REFD','PWDR']:

0 commit comments

Comments
 (0)