1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """ Parse different strategy file provided by users """
16
17 __id__ = "$Id: strategyfile.py 6671 2011-07-29 02:22:12Z yshang $"
18
19 import os
20 from diffpy.srrietveld.exceptions import SrrFileError
21
23 ''' Parse the strategy file into a list of dictionaries for later refinement
24 [{method: [[fullpath, value, constrain formula], ...]}, {...}, ...]
25 '''
26 - def __init__(self, fit, strategyfile=None, strategy=None):
48
50 """ Read file """
51 sifile = open(self.strategyfile, "rU")
52 self.inlines = sifile.readlines()
53 sifile.close()
54 return
55
57 """ Parse .txt format file
58 strateylist = [{method: [[fullpath, value, constrain formula], ...]}, {...}, ...]
59 method: 'Rietveld' or 'Lebail'
60 fullpath: full path of parameter;
61 value: initial value of refined parameter;
62 formula of constraint: 1.0*Var_paramname
63 """
64
65 self.readFile()
66
67
68 paramlist = []
69 method = ""
70 step = {}
71 stepn = -1
72 for ln, line in enumerate(self.inlines):
73 line = line.strip()
74 if not line:
75 continue
76 if line.startswith('#'):
77 continue
78 terms = line.split()
79
80
81
82 if len(terms) == 3:
83
84 if stepn >= 0:
85 step[method] = paramlist
86 self.strategy.append(step)
87 stepn += 1
88
89 step = {}
90 paramlist = []
91
92 method = terms[0]
93 generalpath = terms[1]
94 generalformula = terms[2]
95
96 elif len(terms) == 2:
97 generalpath = terms[0]
98 generalformula = terms[1]
99 else:
100 raise SrrFileError("The format of strategy file is wrong in line %d" % ln)
101
102
103
104
105
106
107
108
109
110
111
112
113 try:
114 pftuplelist = self.transRefinables(generalpath, generalformula)
115 except:
116 continue
117 for fullpath, formula in pftuplelist:
118 paramlist.append([fullpath, '', formula])
119
120
121 step[method] = paramlist
122 self.strategy.append(step)
123
124 return
125
127 """ When input strategylist, need to reset variableid.
128 """
129 variableids = []
130 for step in self.strategy:
131 for param in step.values()[0]:
132 variableid = param[2].split("_")[1]
133 if variableid not in variableids:
134 variableids.append(variableid)
135 self.variableid += 1
136 param[2].replace(variableid, str(self.variableid))
137 return
138
140 """ Translate the generalpath and generalformula in strategyfile
141 And return the pftuplelist containing the list of parameters for the project.
142 """
143 pftuplelist = []
144
145 if generalpath.endswith("Lattice"):
146 parpaths = []
147 for name in ["a", "b", "c", "alpha", "beta", "gamma"]:
148 parpaths.extend(self._parsePath(generalpath.replace("Lattice", name)))
149 elif generalpath.endswith("Background"):
150
151
152
153 backgroundobj = self.fit.getByPath(generalpath)
154 if isinstance(backgroundobj, list):
155 backgroundobj = backgroundobj[0]
156
157 if backgroundobj.get("BCK"):
158 generalpath += ".BCK[:]"
159 else:
160 generalpath += ".BACK[:]"
161 parpaths = self._parsePath(generalpath)
162 else:
163 parpaths = self._parsePath(generalpath)
164 for path in parpaths:
165 self.variableid += 1
166 formula = generalformula.split("_")[0] + "_" + str(self.variableid)
167 pftuplelist.append((path, formula))
168
169 return pftuplelist
170
171
173 """Parse a path having a form as ABC[1], without '.'
174
175 path -- the path to an parameter or parameter array
176 return: a list of translated paths.
177 """
178 import re
179 res = re.search(r'(.+)\[([0-9:]+)\]$', path)
180 if res:
181 parpath, index= res.groups()
182
183
184 if index.count(':') > 0:
185
186 index = slice(*[{True: lambda n: None, False: int}[x == ''](x)
187 for x in (index.split(':') + ['', '', ''])[:3]])
188 else:
189 index = int(index)
190
191 datasets = self.fit.getByPath(parpath)
192 if not isinstance(datasets, list):
193 datasets = [datasets, ]
194
195 paths = []
196 for dataset in datasets:
197 if isinstance(index, int):
198 paths.append(dataset.path + '[%i]'%index)
199 else:
200 n = dataset.shape[-1]
201 start, stop, step = index.indices(n)
202 parpath = dataset.path
203 for i in range(start, stop, step):
204 paths.append(parpath+'[%i]'%i)
205
206 return paths
207
208 datasets = self.fit.getByPath(path)
209 if not isinstance(datasets, list):
210
211 datasets = [datasets,]
212
213 return [dataset.path for dataset in datasets]
214
216 '''write the strateg list to a local file. The strategy is read from the
217 strategy member variable, no the lines
218 fullpath - the file path to write into
219 return'''
220 import diffpy.srrietveld.gui.srrguiglobals as GLOBALS
221 lines = []
222 lines.append("# SrRietveld project file: %s" % GLOBALS.project.basepath + GLOBALS.project.name)
223 lines.append("# Refinement name:" + self.fit.name)
224 for step in self.strategy:
225 method = step.keys()[0]
226 line = ""
227 for ln, param in enumerate(step[method]):
228 datapath = param[0].split('.', 1)[-1]
229 fomular = param[2]
230 if ln == 0:
231 line = "\t".join([method, datapath, fomular])
232 else:
233 line = "\t".join(["\t", datapath, fomular])
234
235 lines.append(line)
236
237 with open(fullpath, "w") as f:
238 f.write(os.linesep.join(lines))
239
240 return
241
243 """ Get the strategy list """
244 return self.strategy
245
247 """ Set new list to strategylist """
248 self.strategy = strategylist
249 return
250
252 """ Get the step dict """
253 return self.strategy[step-1]
254
256 """ Get strategy for certain step """
257 if step == 0:
258 return []
259 else:
260 return self.strategy[step-1].values()[0]
261
263 """ Get method for certain step """
264 if step == 0:
265 return "Rietveld"
266 else:
267 return self.strategy[step-1].keys()[0]
268
270 """ Get the order of the stepstrategy """
271 return self.strategy.index(stepstrategy) + 1
272
274 """ Get total number of steps """
275 return len(self.strategy)
276
278 """ Get new variable id """
279 self.variableid += 1
280 return self.variableid
281
283 """ Form a new paramlist """
284 currparamlist = []
285 currparamlist.append(newparam)
286 for param in paramlist:
287 currparamlist.append(param)
288 return currparamlist
289
291 """ Get Fullpath for id parameter in strategyeditor """
292 paramlist, stepparamid = self.getPosParamListId(id)
293 param = paramlist[stepparamid]
294 return param[0]
295
297 """ Remove the position in strategyeditor from parameter list """
298 paramlist, stepparamid = self.getPosParamListId(id)
299 paramlist.pop(stepparamid)
300 return
301
303 """ Add the parameter into paramlist """
304 paramlist, stepparamid = self.getPosParamListId(id)
305 paramlist.insert(stepparamid, param)
306 return
307
309 """ Get corresponding paramlist to id of strategyeditor """
310 paramid = 0
311 stepid = 0
312 for step in self.strategy:
313 stepid += 1
314 paramlist = step.values()[0]
315 stepparamid = -1
316 for param in paramlist:
317 paramid += 1
318 stepparamid += 1
319 if (paramid+stepid) == id:
320 return paramlist, stepparamid
321 return
322
324 """ Remove the step from list """
325 self.strategy.pop(step-1)
326 return
327
328 - def addParam(self, step, posid, fullpath, value='', formula=None):
329 """ Add the fullpath into parameter list """
330 strategystep = self.strategy[step-1]
331 paramlist = strategystep.values()[0]
332 if formula == None:
333 self.variableid += 1
334 formula = "1.0*Var_" + str(self.variableid)
335 paramlist.insert(posid, [fullpath, value, formula])
336 return
337
339 """ Addd new strategystep """
340 self.strategy.insert(step, {method: paramlist})
341 return
342
344 """ Clean the strategy list """
345 self.strategy = []
346 return
347