1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2014-03-12 20:46:47 +0000 (Wed, 12 Mar 2014) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 1247 $ |
---|
6 | # $URL: trunk/GSASIImapvars.py $ |
---|
7 | # $Id: GSASIImapvars.py 1247 2014-03-12 20:46:47Z toby $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | """ |
---|
10 | *GSASIImapvars: Parameter constraints* |
---|
11 | ====================================== |
---|
12 | |
---|
13 | Module to implements algebraic contraints, parameter redefinition |
---|
14 | and parameter simplification contraints. |
---|
15 | |
---|
16 | Parameter redefinition (new vars) is done by creating one or more relationships |
---|
17 | between a set of parameters |
---|
18 | |
---|
19 | :: |
---|
20 | |
---|
21 | Mx1 * Px + My1 * Py +... |
---|
22 | Mx2 * Px + Mz2 * Pz + ... |
---|
23 | |
---|
24 | where Pj is a parameter name and Mjk is a constant. |
---|
25 | |
---|
26 | Constant constraint Relations can also be supplied in the form of an equation: |
---|
27 | |
---|
28 | :: |
---|
29 | |
---|
30 | nx1 * Px + ny1 * Py +... = C1 |
---|
31 | |
---|
32 | where Cn is a constant. These equations define an algebraic |
---|
33 | constrant. |
---|
34 | |
---|
35 | Parameters can also be "fixed" (held), which prevents them from being refined. |
---|
36 | |
---|
37 | All of the above three cases are input using routines |
---|
38 | GroupConstraints and GenerateConstraints. The input consists of a list of |
---|
39 | relationship dictionaries: |
---|
40 | |
---|
41 | .. code-block:: python |
---|
42 | |
---|
43 | constrDict = [ |
---|
44 | {'0:12:Scale': 2.0, '0:14:Scale': 4.0, '0:13:Scale': 3.0, '0:0:Scale': 0.5}, |
---|
45 | {'2::C(10,6,1)': 1.0, '1::C(10,6,1)': 1.0}, |
---|
46 | {'0::A0': 0.0}] |
---|
47 | fixedList = ['5.0', None, '0'] |
---|
48 | |
---|
49 | Where the dictionary defines the first part of an expression and the corresponding fixedList |
---|
50 | item is either None (for parameter redefinition) or the constant value, for a constant |
---|
51 | constraint equation. A dictionary that contains a single term defines a variable that |
---|
52 | will be fixed (held). The multiplier and the fixedList value in this case are ignored. |
---|
53 | |
---|
54 | Parameters can also be equivalenced or "slaved" to another parameter, such that one |
---|
55 | (independent) parameter is equated to several (now dependent) parameters. In |
---|
56 | algebraic form this is: |
---|
57 | |
---|
58 | :: |
---|
59 | |
---|
60 | P0 = M1 * P1 = M2 * P2 = ... |
---|
61 | |
---|
62 | Thus parameters P0, P1 and P2,... are linearly equivalent. Routine StoreEquivalence is |
---|
63 | used to specify these equivalences. |
---|
64 | |
---|
65 | Parameter redefinition (new vars) describes a new, independent, parameter, which |
---|
66 | is defined in terms of dependent parameters that are defined in the |
---|
67 | Model, while fixed constrained relations effectively reduce the complexity |
---|
68 | of the Model by removing a degree of freedom. It is possible for a parameter to appear |
---|
69 | in both a parameter redefinition expression and a fixed constraint equation, but a |
---|
70 | parameter cannot be used a parameter equivalance cannot be used elsewhere (not fixed, |
---|
71 | constrained or redefined). Likewise a fixed parameter cannot be used elsewhere (not |
---|
72 | equivalanced, constrained or redefined). |
---|
73 | |
---|
74 | Relationships are grouped so that a set of dependent parameters appear |
---|
75 | in only one group (done in routine GroupConstraints.) Note that if a |
---|
76 | group contains relationships/equations that involve N dependent |
---|
77 | parameters, there must exist N-C new parameters, where C is the number |
---|
78 | of contraint equations in the group. Routine GenerateConstraints takes |
---|
79 | the output from GroupConstraints and generates the |
---|
80 | "missing" relationships and saves that information in the module's |
---|
81 | global variables. Each generated parameter is named sequentially using paramPrefix. |
---|
82 | |
---|
83 | A list of parameters that will be varied is specified as input to GenerateConstraints |
---|
84 | (varyList). A fixed parameter will simply be removed from this list preventing that |
---|
85 | parameter from being varied. Note that all parameters in a constraint relationship |
---|
86 | must specified as varied (appear in varyList) or none can be varied. This is |
---|
87 | checked in GenerateConstraints. Likewise, if all parameters in a constraint are |
---|
88 | not referenced in a refinement, the constraint is ignored, but if some parameters |
---|
89 | in a constraint group are not referenced in a refinement, but others are this |
---|
90 | constitutes and error. |
---|
91 | |
---|
92 | * When a new variable is created, the variable is assigned the name associated |
---|
93 | in the constraint definition or it is assigned a default name of form |
---|
94 | ``::constr<n>`` (see paramPrefix). The vary setting for variables used in the |
---|
95 | constraint are ignored. |
---|
96 | Note that any generated "missing" relations are not varied. Only |
---|
97 | the input relations can be are varied. |
---|
98 | |
---|
99 | * If all parameters in a fixed constraint equation are varied, the generated "missing" |
---|
100 | relations in the group are all varied. This provides the N-C degrees of freedom. |
---|
101 | |
---|
102 | *External Routines* |
---|
103 | ------------------- |
---|
104 | |
---|
105 | To define a set of constrained and unconstrained relations, one |
---|
106 | defines a list of dictionary defining constraint parameters and their |
---|
107 | values, a list of fixed values for each constraint and a list of |
---|
108 | parameters to be varied. In addition, one uses |
---|
109 | :func:`StoreEquivalence` to define parameters that are equivalent. One |
---|
110 | can then use :func:`CheckConstraints` to check that the input is |
---|
111 | internally consistent and finally :func:`GroupConstraints` and |
---|
112 | :func:`GenerateConstraints` to generate the internally used |
---|
113 | tables. Routines :func:`Map2Dict` is used to initialize the parameter |
---|
114 | dictionary and :func:`Dict2Map`, :func:`Dict2Deriv`, and |
---|
115 | :func:`ComputeDepESD` are used to apply constraints. Routine |
---|
116 | :func:`VarRemapShow` is used to print out the constraint information, |
---|
117 | as stored by :func:`GenerateConstraints`. |
---|
118 | |
---|
119 | :func:`InitVars` |
---|
120 | This is optionally used to clear out all defined previously defined constraint information |
---|
121 | |
---|
122 | :func:`StoreEquivalence` |
---|
123 | To implement parameter redefinition, one calls StoreEquivalence. This should be called for every set of |
---|
124 | equivalence relationships. There is no harm in using StoreEquivalence with the same independent variable: |
---|
125 | |
---|
126 | .. code-block:: python |
---|
127 | |
---|
128 | StoreEquivalence('x',('y',)) |
---|
129 | StoreEquivalence('x',('z',)) |
---|
130 | |
---|
131 | or equivalently |
---|
132 | |
---|
133 | .. code-block:: python |
---|
134 | |
---|
135 | StoreEquivalence('x',('y','z')) |
---|
136 | |
---|
137 | The latter will run more efficiently. Note that mixing independent and dependent variables is |
---|
138 | problematic. This is not allowed: |
---|
139 | |
---|
140 | .. code-block:: python |
---|
141 | |
---|
142 | StoreEquivalence('x',('y',)) |
---|
143 | StoreEquivalence('y',('z',)) |
---|
144 | |
---|
145 | Use StoreEquivalence before calling GenerateConstraints or CheckConstraints |
---|
146 | |
---|
147 | :func:`CheckConstraints` |
---|
148 | To check that input in internally consistent, use CheckConstraints |
---|
149 | |
---|
150 | :func:`Map2Dict` |
---|
151 | To determine values for the parameters created in this module, one |
---|
152 | calls Map2Dict. This will not apply contraints. |
---|
153 | |
---|
154 | :func:`Dict2Map` |
---|
155 | To take values from the new independent parameters and constraints, |
---|
156 | one calls Dict2Map. This will apply contraints. |
---|
157 | |
---|
158 | :func:`Dict2Deriv` |
---|
159 | Use Dict2Deriv to determine derivatives on independent parameters |
---|
160 | from those on dependent ones |
---|
161 | |
---|
162 | :func:`ComputeDepESD` |
---|
163 | Use ComputeDepESD to compute uncertainties on dependent variables |
---|
164 | |
---|
165 | :func:`VarRemapShow` |
---|
166 | To show a summary of the parameter remapping, one calls VarRemapShow |
---|
167 | |
---|
168 | *Global Variables* |
---|
169 | ------------------ |
---|
170 | |
---|
171 | dependentParmList: |
---|
172 | contains a list by group of lists of |
---|
173 | parameters used in the group. Note that parameters listed in |
---|
174 | dependentParmList should not be refined as they will not affect |
---|
175 | the model |
---|
176 | |
---|
177 | indParmList: |
---|
178 | a list of groups of Independent parameters defined in |
---|
179 | each group. This contains both parameters used in parameter |
---|
180 | redefinitions as well as names of generated new parameters. |
---|
181 | |
---|
182 | fixedVarList: |
---|
183 | a list of variables that have been 'fixed' |
---|
184 | by defining them as equal to a constant (::var: = 0). Note that |
---|
185 | the constant value is ignored at present. These variables are |
---|
186 | later removed from varyList which prevents them from being refined. |
---|
187 | Unlikely to be used externally. |
---|
188 | |
---|
189 | arrayList: |
---|
190 | a list by group of relationship matrices to relate |
---|
191 | parameters in dependentParmList to those in indParmList. Unlikely |
---|
192 | to be used externally. |
---|
193 | |
---|
194 | invarrayList: |
---|
195 | a list by group of relationship matrices to relate |
---|
196 | parameters in indParmList to those in dependentParmList. Unlikely |
---|
197 | to be used externally. |
---|
198 | |
---|
199 | fixedDict: |
---|
200 | a dictionary containing the fixed values corresponding |
---|
201 | to parameter equations. The dict key is an ascii string, but the |
---|
202 | dict value is a float. Unlikely to be used externally. |
---|
203 | |
---|
204 | *Routines* |
---|
205 | ---------- |
---|
206 | |
---|
207 | Note that parameter names in GSAS-II are strings of form ``<ph>:<hst>:<nam>`` |
---|
208 | |
---|
209 | """ |
---|
210 | |
---|
211 | import numpy as np |
---|
212 | import GSASIIpath |
---|
213 | GSASIIpath.SetVersionNumber("$Revision: 1247 $") |
---|
214 | # data used for constraints; |
---|
215 | debug = False # turns on printing as constraint input is processed |
---|
216 | # note that constraints are stored listed by contraint groups, where each constraint |
---|
217 | # group contains those parameters that must be handled together |
---|
218 | dependentParmList = [] # contains a list of parameters in each group |
---|
219 | # note that parameters listed in dependentParmList should not be refined |
---|
220 | arrayList = [] # a list of of relationship matrices |
---|
221 | invarrayList = [] # a list of inverse relationship matrices |
---|
222 | indParmList = [] # a list of names for the new parameters |
---|
223 | fixedDict = {} # a dictionary containing the fixed values corresponding to defined parameter equations |
---|
224 | # key is original ascii string, value is float |
---|
225 | fixedVarList = [] # List of variables that should not be refined |
---|
226 | |
---|
227 | # prefix for parameter names |
---|
228 | paramPrefix = "::constr" |
---|
229 | consNum = 0 # number of the next constraint to be created |
---|
230 | |
---|
231 | def InitVars(): |
---|
232 | '''Initializes all constraint information''' |
---|
233 | global dependentParmList,arrayList,invarrayList,indParmList,fixedDict,consNum |
---|
234 | dependentParmList = [] # contains a list of parameters in each group |
---|
235 | arrayList = [] # a list of of relationship matrices |
---|
236 | invarrayList = [] # a list of inverse relationship matrices |
---|
237 | indParmList = [] # a list of names for the new parameters |
---|
238 | fixedDict = {} # a dictionary containing the fixed values corresponding to defined parameter equations |
---|
239 | consNum = 0 # number of the next constraint to be created |
---|
240 | fixedVarList = [] |
---|
241 | |
---|
242 | def VarKeys(constr): |
---|
243 | """Finds the keys in a constraint that represent variables |
---|
244 | e.g. eliminates any that start with '_' |
---|
245 | |
---|
246 | :param dict constr: a single constraint entry of form:: |
---|
247 | |
---|
248 | {'var1': mult1, 'var2': mult2,... '_notVar': val,...} |
---|
249 | |
---|
250 | (see :func:`GroupConstraints`) |
---|
251 | :returns: a list of keys where any keys beginning with '_' are |
---|
252 | removed. |
---|
253 | """ |
---|
254 | return [i for i in constr.keys() if not i.startswith('_')] |
---|
255 | |
---|
256 | |
---|
257 | def GroupConstraints(constrDict): |
---|
258 | """divide the constraints into groups that share no parameters. |
---|
259 | |
---|
260 | :param dict constrDict: a list of dicts defining relationships/constraints |
---|
261 | |
---|
262 | :: |
---|
263 | |
---|
264 | constrDict = [{<constr1>}, {<constr2>}, ...] |
---|
265 | |
---|
266 | where {<constr1>} is {'var1': mult1, 'var2': mult2,... } |
---|
267 | |
---|
268 | :returns: two lists of lists: |
---|
269 | |
---|
270 | * a list of grouped contraints where each constraint grouped containts a list |
---|
271 | of indices for constraint constrDict entries |
---|
272 | * a list containing lists of parameter names contained in each group |
---|
273 | |
---|
274 | """ |
---|
275 | assignedlist = [] # relationships that have been used |
---|
276 | groups = [] # contains a list of grouplists |
---|
277 | ParmList = [] |
---|
278 | for i,consi in enumerate(constrDict): |
---|
279 | if i in assignedlist: continue # already in a group, skip |
---|
280 | # starting a new group |
---|
281 | grouplist = [i,] |
---|
282 | assignedlist.append(i) |
---|
283 | groupset = set(VarKeys(consi)) |
---|
284 | changes = True # always loop at least once |
---|
285 | while(changes): # loop until we can't find anything to add to the current group |
---|
286 | changes = False # but don't loop again unless we find something |
---|
287 | for j,consj in enumerate(constrDict): |
---|
288 | if j in assignedlist: continue # already in a group, skip |
---|
289 | if len(set(VarKeys(consj)) & groupset) > 0: # true if this needs to be added |
---|
290 | changes = True |
---|
291 | grouplist.append(j) |
---|
292 | assignedlist.append(j) |
---|
293 | groupset = groupset | set(VarKeys(consj)) |
---|
294 | group = sorted(grouplist) |
---|
295 | varlist = sorted(list(groupset)) |
---|
296 | groups.append(group) |
---|
297 | ParmList.append(varlist) |
---|
298 | return groups,ParmList |
---|
299 | |
---|
300 | def CheckConstraints(varyList,constrDict,fixedList): |
---|
301 | '''Takes a list of relationship entries comprising a group of |
---|
302 | constraints and checks for inconsistencies such as conflicts in |
---|
303 | parameter/variable definitions and or inconsistently varied parameters. |
---|
304 | |
---|
305 | :param list varyList: a list of parameters names that will be varied |
---|
306 | |
---|
307 | :param dict constrDict: a list of dicts defining relationships/constraints |
---|
308 | (as created in :func:`GSASIIstrIO.ProcessConstraints` and |
---|
309 | documented in :func:`GroupConstraints`) |
---|
310 | |
---|
311 | :param list fixedList: a list of values specifying a fixed value for each |
---|
312 | dict in constrDict. Values are either strings that can be converted to |
---|
313 | floats or ``None`` if the constraint defines a new parameter rather |
---|
314 | than a constant. |
---|
315 | |
---|
316 | :returns: two strings: |
---|
317 | |
---|
318 | * the first lists conflicts internal to the specified constraints |
---|
319 | * the second lists conflicts where the varyList specifies some |
---|
320 | parameters in a constraint, but not all |
---|
321 | |
---|
322 | If there are no errors, both strings will be empty |
---|
323 | ''' |
---|
324 | import re |
---|
325 | global dependentParmList,arrayList,invarrayList,indParmList,consNum |
---|
326 | errmsg = '' |
---|
327 | warnmsg = '' |
---|
328 | fixVlist = [] |
---|
329 | # process fixed variables (holds) |
---|
330 | for cdict in constrDict: |
---|
331 | # N.B. No "_" names in holds |
---|
332 | if len(cdict) == 1: |
---|
333 | fixVlist.append(cdict.keys()[0]) |
---|
334 | |
---|
335 | # process equivalences: make a list of dependent and independent vars |
---|
336 | # and check for repeated uses (repetition of a parameter as an |
---|
337 | # independent var is OK) |
---|
338 | indepVarList = [] |
---|
339 | depVarList = [] |
---|
340 | multdepVarList = [] |
---|
341 | for varlist,mapvars,multarr,invmultarr in zip( |
---|
342 | dependentParmList,indParmList,arrayList,invarrayList): |
---|
343 | if multarr is None: # an equivalence |
---|
344 | zeromult = False |
---|
345 | for mv in mapvars: |
---|
346 | varied = 0 |
---|
347 | notvaried = '' |
---|
348 | if mv in varyList: |
---|
349 | varied += 1 |
---|
350 | else: |
---|
351 | if notvaried: notvaried += ', ' |
---|
352 | notvaried += mv |
---|
353 | if mv not in indepVarList: indepVarList.append(mv) |
---|
354 | for v,m in zip(varlist,invmultarr): |
---|
355 | if m == 0: zeromult = True |
---|
356 | if v in varyList: |
---|
357 | varied += 1 |
---|
358 | else: |
---|
359 | if notvaried: notvaried += ', ' |
---|
360 | notvaried += v |
---|
361 | if v in depVarList: |
---|
362 | multdepVarList.append(v) |
---|
363 | else: |
---|
364 | depVarList.append(v) |
---|
365 | if varied > 0 and varied != len(varlist)+1: |
---|
366 | warnmsg += "\nNot all variables refined in equivalence:\n\t" |
---|
367 | s = "" |
---|
368 | for v in varlist: |
---|
369 | if s != "": s+= " & " |
---|
370 | s += str(v) |
---|
371 | warnmsg += str(mv) + " => " + s |
---|
372 | warnmsg += '\nNot refined: ' + notvaried + '\n' |
---|
373 | if zeromult: |
---|
374 | errmsg += "\nZero multiplier is invalid in equivalence:\n\t" |
---|
375 | s = "" |
---|
376 | for v in varlist: |
---|
377 | if s != "": s+= " & " |
---|
378 | s += str(v) |
---|
379 | errmsg += str(mv) + " => " + s + '\n' |
---|
380 | |
---|
381 | # check for errors: |
---|
382 | inboth = set(indepVarList).intersection(set(depVarList)) |
---|
383 | if len(inboth) > 0: |
---|
384 | errmsg += "\nThe following parameters(s) are used as both dependent and independent variables in Equivalence relations:\n" |
---|
385 | s = '' |
---|
386 | for var in sorted(inboth): |
---|
387 | if s != "": s+= ", " |
---|
388 | s += str(var) |
---|
389 | errmsg += '\t'+ s + '\n' |
---|
390 | if len(multdepVarList) > 0: |
---|
391 | errmsg += "\nThe following parameters(s) are used in multiple Equivalence relations as dependent variables:\n" |
---|
392 | s = '' |
---|
393 | for var in sorted(set(multdepVarList)): |
---|
394 | if s != "": s+= ", " |
---|
395 | s += str(var) |
---|
396 | errmsg += '\t'+ s + '\n' |
---|
397 | equivVarList = list(set(indepVarList).union(set(depVarList))) |
---|
398 | #print 'equivVarList',equivVarList |
---|
399 | inboth = set(fixVlist).intersection(set(equivVarList)) |
---|
400 | if len(inboth) > 0: |
---|
401 | errmsg += "\nThe following parameter(s) are used in both Equivalence and Fixed constraints:\n" |
---|
402 | s = '' |
---|
403 | for var in sorted(inboth): |
---|
404 | if s != "": s+= ", " |
---|
405 | s += str(var) |
---|
406 | errmsg += '\t'+ s + '\n' |
---|
407 | |
---|
408 | groups,parmlist = GroupConstraints(constrDict) |
---|
409 | # scan through parameters in each relationship. Are all varied? If only some are |
---|
410 | # varied, create a warning message. |
---|
411 | for group,varlist in zip(groups,parmlist): |
---|
412 | if len(varlist) == 1: continue |
---|
413 | for rel in group: |
---|
414 | varied = 0 |
---|
415 | notvaried = '' |
---|
416 | for var in constrDict[rel]: |
---|
417 | if var.startswith('_'): continue |
---|
418 | if not re.match('[0-9]*:[0-9\*]*:',var): |
---|
419 | warnmsg += "\nVariable "+str(var)+" does not begin with a ':'" |
---|
420 | if var in varyList: |
---|
421 | varied += 1 |
---|
422 | else: |
---|
423 | if notvaried: notvaried += ', ' |
---|
424 | notvaried += var |
---|
425 | if var in fixVlist: |
---|
426 | errmsg += '\nParameter '+var+" is Fixed and used in a constraint:\n\t" |
---|
427 | errmsg += _FormatConstraint(constrDict[rel],fixedList[rel])+"\n" |
---|
428 | if varied > 0 and varied != len(VarKeys(constrDict[rel])): |
---|
429 | warnmsg += "\nNot all variables refined in constraint:\n\t" |
---|
430 | warnmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
431 | warnmsg += '\nNot refined: ' + notvaried + '\n' |
---|
432 | if errmsg or warnmsg: |
---|
433 | return errmsg,warnmsg |
---|
434 | |
---|
435 | # now look for process each group and create the relations that are needed to form |
---|
436 | # non-singular square matrix |
---|
437 | for group,varlist in zip(groups,parmlist): |
---|
438 | if len(varlist) == 1: continue # a constraint group with a single variable can be ignored |
---|
439 | if len(varlist) < len(group): # too many relationships -- no can do |
---|
440 | errmsg += "\nOver-constrained input. " |
---|
441 | errmsg += "There are more constraints " + str(len(group)) |
---|
442 | errmsg += "\n\tthan variables " + str(len(varlist)) + "\n" |
---|
443 | for rel in group: |
---|
444 | errmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
445 | errmsg += "\n" |
---|
446 | continue |
---|
447 | try: |
---|
448 | multarr = _FillArray(group,constrDict,varlist) |
---|
449 | _RowEchelon(len(group),multarr,varlist) |
---|
450 | except: |
---|
451 | errmsg += "\nSingular input. " |
---|
452 | errmsg += "There are internal inconsistencies in these constraints\n" |
---|
453 | for rel in group: |
---|
454 | errmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
455 | errmsg += "\n" |
---|
456 | continue |
---|
457 | try: |
---|
458 | multarr = _FillArray(group,constrDict,varlist,FillDiagonals=True) |
---|
459 | GramSchmidtOrtho(multarr,len(group)) |
---|
460 | except: |
---|
461 | errmsg += "\nUnexpected singularity with constraints (in Gram-Schmidt)\n" |
---|
462 | for rel in group: |
---|
463 | errmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
464 | errmsg += "\n" |
---|
465 | continue |
---|
466 | mapvar = [] |
---|
467 | group = group[:] |
---|
468 | # scan through all generated and input variables |
---|
469 | # Check again for inconsistent variable use |
---|
470 | # for new variables -- where varied and unvaried parameters get grouped |
---|
471 | # together. I don't think this can happen when not flagged before, but |
---|
472 | # it does not hurt to check again. |
---|
473 | for i in range(len(varlist)): |
---|
474 | varied = 0 |
---|
475 | notvaried = '' |
---|
476 | if len(group) > 0: |
---|
477 | rel = group.pop(0) |
---|
478 | fixedval = fixedList[rel] |
---|
479 | for var in VarKeys(constrDict[rel]): |
---|
480 | if var in varyList: |
---|
481 | varied += 1 |
---|
482 | else: |
---|
483 | if notvaried: notvaried += ', ' |
---|
484 | notvaried += var |
---|
485 | else: |
---|
486 | fixedval = None |
---|
487 | if fixedval is None: |
---|
488 | varname = paramPrefix + str(consNum) # assign a name to a variable |
---|
489 | mapvar.append(varname) |
---|
490 | consNum += 1 |
---|
491 | else: |
---|
492 | mapvar.append(fixedval) |
---|
493 | if varied > 0 and notvaried != '': |
---|
494 | warnmsg += "\nNot all variables refined in generated constraint" |
---|
495 | warnmsg += '\nPlease report this unexpected error\n' |
---|
496 | for rel in group: |
---|
497 | warnmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
498 | warnmsg += "\n" |
---|
499 | warnmsg += '\n\tNot refined: ' + notvaried + '\n' |
---|
500 | try: |
---|
501 | np.linalg.inv(multarr) |
---|
502 | except: |
---|
503 | errmsg += "\nSingular input. " |
---|
504 | errmsg += "The following constraints are not " |
---|
505 | errmsg += "linearly independent\n\tor do not " |
---|
506 | errmsg += "allow for generation of a non-singular set\n" |
---|
507 | errmsg += 'Please report this unexpected error\n' |
---|
508 | for rel in group: |
---|
509 | errmsg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
510 | errmsg += "\n" |
---|
511 | return errmsg,warnmsg |
---|
512 | |
---|
513 | def GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList,parmDict=None,SeqHist=None): |
---|
514 | '''Takes a list of relationship entries comprising a group of |
---|
515 | constraints and builds the relationship lists and their inverse |
---|
516 | and stores them in global variables Also checks for internal |
---|
517 | conflicts or inconsistencies in parameter/variable definitions. |
---|
518 | |
---|
519 | :param list groups: a list of grouped contraints where each constraint |
---|
520 | grouped containts a list of indices for constraint constrDict entries, |
---|
521 | created in :func:`GroupConstraints` (returned as 1st value) |
---|
522 | |
---|
523 | :param list parmlist: a list containing lists of parameter names |
---|
524 | contained in each group, created in :func:`GroupConstraints` |
---|
525 | (returned as 2nd value) |
---|
526 | |
---|
527 | :param list varyList: a list of parameters names (strings of form |
---|
528 | ``<ph>:<hst>:<nam>``) that will be varied |
---|
529 | |
---|
530 | :param dict constrDict: a list of dicts defining relationships/constraints |
---|
531 | (as defined in :func:`GroupConstraints`) |
---|
532 | |
---|
533 | :param list fixedList: a list of values specifying a fixed value for each |
---|
534 | dict in constrDict. Values are either strings that can be converted to |
---|
535 | floats, float values or None if the constraint defines a new parameter. |
---|
536 | |
---|
537 | :param dict parmDict: a dict containing all parameters defined in current |
---|
538 | refinement. |
---|
539 | |
---|
540 | :param int SeqHist: number of current histogram, when used in a sequential |
---|
541 | refinement. None (default) otherwise. Wildcard variable names are |
---|
542 | set to the current histogram, when found if not None. |
---|
543 | ''' |
---|
544 | global dependentParmList,arrayList,invarrayList,indParmList,consNum |
---|
545 | msg = '' |
---|
546 | |
---|
547 | # process fixed (held) variables |
---|
548 | for cdict in constrDict: |
---|
549 | if len(cdict) == 1: |
---|
550 | fixedVarList.append(cdict.keys()[0]) |
---|
551 | |
---|
552 | # process equivalences: make a list of dependent and independent vars |
---|
553 | # and check for repeated uses (repetition of a parameter as an |
---|
554 | # independent var is OK [A=B; A=C], but chaining: [A=B; B=C] is not good) |
---|
555 | indepVarList = [] |
---|
556 | depVarList = [] |
---|
557 | multdepVarList = [] |
---|
558 | for varlist,mapvars,multarr,invmultarr in zip( # process equivalences |
---|
559 | dependentParmList,indParmList,arrayList,invarrayList): |
---|
560 | if multarr is None: # true only if an equivalence |
---|
561 | zeromult = False |
---|
562 | for mv in mapvars: |
---|
563 | #s = '' |
---|
564 | varied = 0 |
---|
565 | notvaried = '' |
---|
566 | if mv in varyList: |
---|
567 | varied += 1 |
---|
568 | else: |
---|
569 | if notvaried: notvaried += ', ' |
---|
570 | notvaried += mv |
---|
571 | if mv not in indepVarList: indepVarList.append(mv) |
---|
572 | if parmDict is not None and mv not in parmDict: |
---|
573 | msg += "\nCannot equivalence to variable "+str(mv)+". Not defined in this refinement" |
---|
574 | continue |
---|
575 | for v,m in zip(varlist,invmultarr): |
---|
576 | if parmDict is not None and v not in parmDict: |
---|
577 | print "Dropping equivalence for variable "+str(v)+". Not defined in this refinement" |
---|
578 | continue |
---|
579 | if m == 0: zeromult = True |
---|
580 | if v in varyList: |
---|
581 | varied += 1 |
---|
582 | else: |
---|
583 | if notvaried: notvaried += ', ' |
---|
584 | notvaried += v |
---|
585 | if v in depVarList: |
---|
586 | multdepVarList.append(v) |
---|
587 | else: |
---|
588 | depVarList.append(v) |
---|
589 | if varied > 0 and varied != len(varlist)+1: |
---|
590 | msg += "\nNot all variables refined in equivalence:\n\t" |
---|
591 | s = "" |
---|
592 | for v in varlist: |
---|
593 | if s != "": s+= " & " |
---|
594 | s += str(v) |
---|
595 | msg += str(mv) + " => " + s |
---|
596 | msg += '\nNot refined: ' + notvaried + '\n' |
---|
597 | if zeromult: |
---|
598 | msg += "\nZero multiplier is invalid in equivalence:\n\t" |
---|
599 | s = "" |
---|
600 | for v in varlist: |
---|
601 | if s != "": s+= " & " |
---|
602 | s += str(v) |
---|
603 | msg += str(mv) + " => " + s + '\n' |
---|
604 | |
---|
605 | #print 'indepVarList',indepVarList |
---|
606 | #print 'depVarList',depVarList |
---|
607 | # check for errors: |
---|
608 | inboth = set(indepVarList).intersection(set(depVarList)) |
---|
609 | if len(inboth) > 0: |
---|
610 | msg += "\nThe following parameters(s) are used as both dependent and independent variables in Equivalence relations:\n" |
---|
611 | s = '' |
---|
612 | for var in sorted(inboth): |
---|
613 | if s != "": s+= ", " |
---|
614 | s += str(var) |
---|
615 | msg += '\t'+ s + '\n' |
---|
616 | if len(multdepVarList) > 0: |
---|
617 | msg += "\nThe following parameters(s) are used in multiple Equivalence relations as dependent variables:\n" |
---|
618 | s = '' |
---|
619 | for var in sorted(set(multdepVarList)): |
---|
620 | if s != "": s+= ", " |
---|
621 | s += str(var) |
---|
622 | msg += '\t'+ s + '\n' |
---|
623 | equivVarList = list(set(indepVarList).union(set(depVarList))) |
---|
624 | |
---|
625 | # scan through parameters in each relationship. Are all varied? If only some are |
---|
626 | # varied, create an error message. |
---|
627 | for group,varlist in zip(groups,parmlist): |
---|
628 | if len(varlist) == 1: continue |
---|
629 | for rel in group: |
---|
630 | varied = 0 |
---|
631 | notvaried = '' |
---|
632 | unused = 0 |
---|
633 | notused = '' |
---|
634 | for var in constrDict[rel]: |
---|
635 | if var.startswith('_'): continue |
---|
636 | if var.split(':')[1] == '*' and SeqHist is not None: |
---|
637 | # convert wildcard var to current histogram |
---|
638 | sv = var.split(':') |
---|
639 | sv[1] = str(SeqHist) |
---|
640 | var = ':'.join(sv) |
---|
641 | if parmDict is not None and var not in parmDict: |
---|
642 | unused += 1 |
---|
643 | if notvaried: notused += ', ' |
---|
644 | notused += var |
---|
645 | if var in varyList: |
---|
646 | varied += 1 |
---|
647 | else: |
---|
648 | if notvaried: notvaried += ', ' |
---|
649 | notvaried += var |
---|
650 | if var in fixedVarList: |
---|
651 | msg += '\nError: parameter '+var+" is Fixed and used in a constraint:\n\t" |
---|
652 | msg += _FormatConstraint(constrDict[rel],fixedList[rel])+"\n" |
---|
653 | #if unused > 0:# and unused != len(VarKeys(constrDict[rel])): |
---|
654 | if unused > 0 and unused != len(VarKeys(constrDict[rel])): |
---|
655 | msg += "\nSome (but not all) variables in constraint are not defined:\n\t" |
---|
656 | msg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
657 | msg += '\nNot used: ' + notused + '\n' |
---|
658 | if varied > 0 and varied != len(VarKeys(constrDict[rel])): |
---|
659 | msg += "\nNot all variables refined in constraint:\n\t" |
---|
660 | msg += _FormatConstraint(constrDict[rel],fixedList[rel]) |
---|
661 | msg += '\nNot refined: ' + notvaried + '\n' |
---|
662 | # if there were errors found, go no farther |
---|
663 | if msg: |
---|
664 | print ' *** ERROR in constraint definitions! ***' |
---|
665 | print msg |
---|
666 | raise Exception |
---|
667 | |
---|
668 | # now process each group and create the relations that are needed to form |
---|
669 | # a non-singular square matrix |
---|
670 | # If all are varied and this is a constraint equation, then set VaryFree flag |
---|
671 | # so that the newly created relationships will be varied |
---|
672 | for group,varlist in zip(groups,parmlist): |
---|
673 | if len(varlist) == 1: continue |
---|
674 | # for constraints, if all included variables are refined, |
---|
675 | # set the VaryFree flag, and remaining degrees of freedom will be |
---|
676 | # varied (since consistency was checked, if any one variable is |
---|
677 | # refined, then assume that all are) |
---|
678 | varsList = [] # make a list of all the referenced variables as well |
---|
679 | VaryFree = False |
---|
680 | for rel in group: |
---|
681 | varied = 0 |
---|
682 | unused = 0 |
---|
683 | for var in VarKeys(constrDict[rel]): |
---|
684 | if var.split(':')[1] == '*' and SeqHist is not None: |
---|
685 | # convert wildcard var to current histogram |
---|
686 | sv = var.split(':') |
---|
687 | sv[1] = str(SeqHist) |
---|
688 | var = ':'.join(sv) |
---|
689 | if parmDict is not None and var not in parmDict: |
---|
690 | unused += 1 |
---|
691 | if var not in varsList: varsList.append(var) |
---|
692 | if var in varyList: varied += 1 |
---|
693 | if fixedList[rel] is not None and varied > 0: |
---|
694 | VaryFree = True |
---|
695 | if len(varlist) < len(group): # too many relationships -- no can do |
---|
696 | msg = 'too many relationships' |
---|
697 | break |
---|
698 | # Since we checked before, if any variables are unused, then all must be. |
---|
699 | # If so, this set of relationships can be ignored |
---|
700 | if unused: |
---|
701 | if debug: print('Constraint ignored (all variables undefined)') |
---|
702 | if debug: print (' '+_FormatConstraint(constrDict[rel],fixedList[rel])) |
---|
703 | continue |
---|
704 | # fill in additional degrees of freedom |
---|
705 | try: |
---|
706 | arr = _FillArray(group,constrDict,varlist) |
---|
707 | _RowEchelon(len(group),arr,varlist) |
---|
708 | constrArr = _FillArray(group,constrDict,varlist,FillDiagonals=True) |
---|
709 | GramSchmidtOrtho(constrArr,len(group)) |
---|
710 | except: |
---|
711 | msg = 'Singular relationships' |
---|
712 | break |
---|
713 | mapvar = [] |
---|
714 | group = group[:] |
---|
715 | # scan through all generated and input relationships, we need to add to the varied list |
---|
716 | # all the new parameters where VaryFree has been set or where a New Var is varied. |
---|
717 | # |
---|
718 | # If a group does not contain any fixed values (constraint equations) |
---|
719 | # and nothing in the group is varied, drop this group, so that the |
---|
720 | # dependent parameters can be refined individually. |
---|
721 | unused = True |
---|
722 | for i in range(len(varlist)): |
---|
723 | if len(group) > 0: # get the original equation reference |
---|
724 | rel = group.pop(0) |
---|
725 | fixedval = fixedList[rel] |
---|
726 | varyflag = constrDict[rel].get('_vary',False) |
---|
727 | varname = constrDict[rel].get('_name','') |
---|
728 | else: # this relationship has been generated |
---|
729 | varyflag = False |
---|
730 | varname = '' |
---|
731 | fixedval = None |
---|
732 | if fixedval is None: # this is a new variable, not a constraint |
---|
733 | if not varname: |
---|
734 | varname = paramPrefix + str(consNum) # no assigned name, create one |
---|
735 | consNum += 1 |
---|
736 | mapvar.append(varname) |
---|
737 | # vary the new relationship if it is a degree of freedom in |
---|
738 | # a set of contraint equations or if a New Var is flagged to be varied. |
---|
739 | if VaryFree or varyflag: |
---|
740 | unused = False |
---|
741 | varyList.append(varname) |
---|
742 | # fix (prevent varying) of all the variables inside the constraint group |
---|
743 | # (dependent vars) |
---|
744 | for var in varsList: |
---|
745 | if var in varyList: varyList.remove(var) |
---|
746 | else: |
---|
747 | unused = False |
---|
748 | mapvar.append(fixedval) |
---|
749 | if unused: # skip over constraints that don't matter (w/o fixed value or any refined variables) |
---|
750 | if debug: print('Constraint ignored (all variables unrefined)') |
---|
751 | if debug: print (' '+_FormatConstraint(constrDict[rel],fixedList[rel])) |
---|
752 | continue |
---|
753 | dependentParmList.append(varlist) |
---|
754 | arrayList.append(constrArr) |
---|
755 | invarrayList.append(np.linalg.inv(constrArr)) |
---|
756 | indParmList.append(mapvar) |
---|
757 | if msg: |
---|
758 | print ' *** ERROR in constraint definitions! ***' |
---|
759 | print msg |
---|
760 | print VarRemapShow(varyList) |
---|
761 | raise Exception |
---|
762 | # setup dictionary containing the fixed values |
---|
763 | global fixedDict |
---|
764 | # key is original ascii string, value is float |
---|
765 | for fixedval in fixedList: |
---|
766 | if fixedval: |
---|
767 | fixedDict[fixedval] = float(fixedval) |
---|
768 | |
---|
769 | if debug: # on debug, show what is parsed & generated, semi-readable |
---|
770 | print 50*'-' |
---|
771 | print VarRemapShow(varyList) |
---|
772 | print 'Varied: ',varyList |
---|
773 | print 'Not Varied: ',fixedVarList |
---|
774 | |
---|
775 | def StoreEquivalence(independentVar,dependentList): |
---|
776 | '''Takes a list of dependent parameter(s) and stores their |
---|
777 | relationship to a single independent parameter (independentVar) |
---|
778 | |
---|
779 | :param str independentVar: name of master parameter that will be used to determine the value |
---|
780 | to set the dependent variables |
---|
781 | |
---|
782 | :param list dependentList: a list of parameters that will set from |
---|
783 | independentVar. Each item in the list can be a string with the parameter |
---|
784 | name or a tuple containing a name and multiplier: |
---|
785 | ``['parm1',('parm2',.5),]`` |
---|
786 | |
---|
787 | ''' |
---|
788 | |
---|
789 | global dependentParmList,arrayList,invarrayList,indParmList |
---|
790 | mapList = [] |
---|
791 | multlist = [] |
---|
792 | for var in dependentList: |
---|
793 | if isinstance(var, basestring): |
---|
794 | mult = 1.0 |
---|
795 | elif len(var) == 2: |
---|
796 | var,mult = var |
---|
797 | else: |
---|
798 | raise Exception, "Cannot parse "+repr(var) + " as var or (var,multiplier)" |
---|
799 | mapList.append(var) |
---|
800 | multlist.append(tuple((mult,))) |
---|
801 | # added relationships to stored values |
---|
802 | arrayList.append(None) |
---|
803 | invarrayList.append(np.array(multlist)) |
---|
804 | indParmList.append(tuple((independentVar,))) |
---|
805 | dependentParmList.append(mapList) |
---|
806 | return |
---|
807 | |
---|
808 | def GetDependentVars(): |
---|
809 | '''Return a list of dependent variables: e.g. variables that are |
---|
810 | constrained in terms of other variables |
---|
811 | |
---|
812 | :returns: a list of variable names |
---|
813 | |
---|
814 | ''' |
---|
815 | dependentVars = [] |
---|
816 | global dependentParmList |
---|
817 | for lst in dependentParmList: |
---|
818 | for itm in lst: dependentVars.append(itm) |
---|
819 | return dependentVars |
---|
820 | |
---|
821 | def GetIndependentVars(): |
---|
822 | '''Return a list of independent variables: e.g. variables that are |
---|
823 | created by constraints of other variables |
---|
824 | |
---|
825 | :returns: a list of variable names |
---|
826 | |
---|
827 | ''' |
---|
828 | independentVars = [] |
---|
829 | global indParmList,fixedDict |
---|
830 | for lst in indParmList: |
---|
831 | for name in lst: |
---|
832 | if name in fixedDict: continue |
---|
833 | independentVars.append(name) |
---|
834 | return independentVars |
---|
835 | |
---|
836 | def PrintIndependentVars(parmDict,varyList,sigDict,PrintAll=False,pFile=None): |
---|
837 | '''Print the values and uncertainties on the independent variables''' |
---|
838 | global dependentParmList,arrayList,invarrayList,indParmList,fixedDict |
---|
839 | printlist = [] |
---|
840 | mapvars = GetIndependentVars() |
---|
841 | for i,name in enumerate(mapvars): |
---|
842 | if name in fixedDict: continue |
---|
843 | if PrintAll or name in varyList: |
---|
844 | sig = sigDict.get(name) |
---|
845 | printlist.append([name,parmDict[name],sig]) |
---|
846 | if len(printlist) == 0: return |
---|
847 | s1 = '' |
---|
848 | print >>pFile,130*'-' |
---|
849 | print >>pFile,"Variables generated by constraints" |
---|
850 | printlist.append(3*[None]) |
---|
851 | for name,val,esd in printlist: |
---|
852 | if len(s1) > 120 or name is None: |
---|
853 | print >>pFile,'' |
---|
854 | print >>pFile,s1 |
---|
855 | print >>pFile,s2 |
---|
856 | print >>pFile,s3 |
---|
857 | s1 = '' |
---|
858 | if name is None: break |
---|
859 | if s1 == "": |
---|
860 | s1 = ' name :' |
---|
861 | s2 = ' value :' |
---|
862 | s3 = ' sig :' |
---|
863 | s1 += '%15s' % (name) |
---|
864 | s2 += '%15.4f' % (val) |
---|
865 | if esd is None: |
---|
866 | s3 += '%15s' % ('n/a') |
---|
867 | else: |
---|
868 | s3 += '%15.4f' % (esd) |
---|
869 | |
---|
870 | def ComputeDepESD(covMatrix,varyList,parmDict): |
---|
871 | '''Compute uncertainties for dependent parameters from independent ones |
---|
872 | returns a dictionary containing the esd values for dependent parameters |
---|
873 | ''' |
---|
874 | sigmaDict = {} |
---|
875 | for varlist,mapvars,invmultarr in zip(dependentParmList,indParmList,invarrayList): |
---|
876 | #if invmultarr is None: continue # probably not needed |
---|
877 | valuelist = [parmDict[var] for var in mapvars] |
---|
878 | # get the v-covar matrix for independent parameters |
---|
879 | vcov = np.zeros((len(mapvars),len(mapvars))) |
---|
880 | for i1,name1 in enumerate(mapvars): |
---|
881 | if name1 not in varyList: continue |
---|
882 | iv1 = varyList.index(name1) |
---|
883 | for i2,name2 in enumerate(mapvars): |
---|
884 | if name2 not in varyList: continue |
---|
885 | iv2 = varyList.index(name2) |
---|
886 | vcov[i1][i2] = covMatrix[iv1][iv2] |
---|
887 | # vec is the vector that multiplies each of the independent values |
---|
888 | for v,vec in zip(varlist,invmultarr): |
---|
889 | sigmaDict[v] = np.sqrt(np.inner(vec.T,np.inner(vcov,vec))) |
---|
890 | return sigmaDict |
---|
891 | |
---|
892 | def _FormatConstraint(RelDict,RelVal): |
---|
893 | '''Formats a Constraint or Function for use in a convenient way''' |
---|
894 | linelen = 45 |
---|
895 | s = [""] |
---|
896 | for var,val in RelDict.items(): |
---|
897 | if var.startswith('_'): continue |
---|
898 | if len(s[-1]) > linelen: s.append(' ') |
---|
899 | m = val |
---|
900 | if s[-1] != "" and m >= 0: |
---|
901 | s[-1] += ' + ' |
---|
902 | elif s[-1] != "": |
---|
903 | s[-1] += ' - ' |
---|
904 | m = abs(m) |
---|
905 | s[-1] += '%.3f*%s '%(m,var) |
---|
906 | if len(s[-1]) > linelen: s.append(' ') |
---|
907 | if RelVal is None: |
---|
908 | s[-1] += ' = New variable' |
---|
909 | else: |
---|
910 | s[-1] += ' = ' + RelVal |
---|
911 | s1 = '' |
---|
912 | for s2 in s: |
---|
913 | if s1 != '': s1 += '\n\t' |
---|
914 | s1 += s2 |
---|
915 | return s1 |
---|
916 | |
---|
917 | def VarRemapShow(varyList): |
---|
918 | '''List out the saved relationships. This should be done after the constraints have been |
---|
919 | defined using :func:`StoreEquivalence`, :func:`GroupConstraints` and :func:`GenerateConstraints`. |
---|
920 | |
---|
921 | :returns: a string containing the details of the contraint relationships |
---|
922 | ''' |
---|
923 | s = '' |
---|
924 | if len(fixedVarList) > 0: |
---|
925 | s += 'Fixed Variables:\n' |
---|
926 | for v in fixedVarList: |
---|
927 | s += ' ' + v + '\n' |
---|
928 | s += 'Variable mapping relations:\n' |
---|
929 | global dependentParmList,arrayList,invarrayList,indParmList,fixedDict |
---|
930 | for varlist,mapvars,multarr,invmultarr in zip( |
---|
931 | dependentParmList,indParmList,arrayList,invarrayList): |
---|
932 | for i,mv in enumerate(mapvars): |
---|
933 | if multarr is None: |
---|
934 | s += ' ' + str(mv) + ' is equivalent to parameter(s): ' |
---|
935 | j = 0 |
---|
936 | for v,m in zip(varlist,invmultarr): |
---|
937 | #print v,m[0] |
---|
938 | if j > 0: s += ' & ' |
---|
939 | j += 1 |
---|
940 | s += str(v) |
---|
941 | if m != 1: |
---|
942 | s += " / " + str(m[0]) |
---|
943 | s += '\n' |
---|
944 | continue |
---|
945 | s += ' %s = ' % mv |
---|
946 | j = 0 |
---|
947 | for m,v in zip(multarr[i,:],varlist): |
---|
948 | if m == 0: continue |
---|
949 | if j > 0: s += ' + ' |
---|
950 | j += 1 |
---|
951 | s += '(%s * %s)' % (m,v) |
---|
952 | if mv in varyList: s += ' VARY' |
---|
953 | s += '\n' |
---|
954 | s += 'Inverse variable mapping relations:\n' |
---|
955 | for varlist,mapvars,invmultarr in zip(dependentParmList,indParmList,invarrayList): |
---|
956 | for i,mv in enumerate(varlist): |
---|
957 | s += ' %s = ' % mv |
---|
958 | j = 0 |
---|
959 | for m,v in zip(invmultarr[i,:],mapvars): |
---|
960 | if m == 0: continue |
---|
961 | if j > 0: s += ' + ' |
---|
962 | j += 1 |
---|
963 | s += '(%s * %s)' % (m,v) |
---|
964 | s += '\n' |
---|
965 | return s |
---|
966 | |
---|
967 | def Dict2Deriv(varyList,derivDict,dMdv): |
---|
968 | '''Compute derivatives for Independent Parameters from the |
---|
969 | derivatives for the original parameters |
---|
970 | |
---|
971 | :param list varyList: a list of parameters names that will be varied |
---|
972 | |
---|
973 | :param dict derivDict: a dict containing derivatives for parameter values keyed by the |
---|
974 | parameter names. |
---|
975 | |
---|
976 | :param list dMdv: a Jacobian, as a list of np.array containing derivatives for dependent |
---|
977 | parameter computed from derivDict |
---|
978 | |
---|
979 | ''' |
---|
980 | global dependentParmList,arrayList,invarrayList,indParmList,invarrayList |
---|
981 | for varlist,mapvars,multarr,invmultarr in zip(dependentParmList,indParmList,arrayList,invarrayList): |
---|
982 | for i,name in enumerate(mapvars): |
---|
983 | # grouped variables: need to add in the derv. w/r |
---|
984 | # dependent variables to the independent ones |
---|
985 | if name not in varyList: continue # skip if independent var not varied |
---|
986 | if multarr is None: |
---|
987 | for v,m in zip(varlist,invmultarr): |
---|
988 | #print 'add derv',v,'/',m[0],'to derv',name |
---|
989 | if m == 0: continue |
---|
990 | dMdv[varyList.index(name)] += derivDict[v] / m[0] |
---|
991 | else: |
---|
992 | for v,m in zip(varlist,multarr[i,:]): |
---|
993 | #print 'add derv',v,'*',m,'to derv',name |
---|
994 | if m == 0: continue |
---|
995 | dMdv[varyList.index(name)] += m * derivDict[v] |
---|
996 | |
---|
997 | def Map2Dict(parmDict,varyList): |
---|
998 | '''Create (or update) the Independent Parameters from the original |
---|
999 | set of Parameters |
---|
1000 | |
---|
1001 | Removes dependent variables from the varyList |
---|
1002 | |
---|
1003 | This should be done once, after the constraints have been |
---|
1004 | defined using :func:`StoreEquivalence`, |
---|
1005 | :func:`GroupConstraints` and :func:`GenerateConstraints` and |
---|
1006 | before any variable refinement is done |
---|
1007 | to complete the parameter dictionary by defining independent |
---|
1008 | parameters and satisfying the constraint equations. |
---|
1009 | |
---|
1010 | :param dict parmDict: a dict containing parameter values keyed by the |
---|
1011 | parameter names. |
---|
1012 | This will contain updated values for both dependent and independent |
---|
1013 | parameters after Dict2Map is called. It will also contain some |
---|
1014 | unexpected entries of every constant value {'0':0.0} & {'1.0':1.0}, |
---|
1015 | which do not cause any problems. |
---|
1016 | |
---|
1017 | :param list varyList: a list of parameters names that will be varied |
---|
1018 | |
---|
1019 | |
---|
1020 | ''' |
---|
1021 | # process the Independent vars: remove dependent ones from varylist |
---|
1022 | # and then compute values for the Independent ones from their dependents |
---|
1023 | global dependentParmList,arrayList,invarrayList,indParmList,fixedDict |
---|
1024 | for varlist,mapvars,multarr in zip(dependentParmList,indParmList,arrayList): |
---|
1025 | for item in varlist: |
---|
1026 | try: |
---|
1027 | varyList.remove(item) |
---|
1028 | except ValueError: |
---|
1029 | pass |
---|
1030 | if multarr is None: continue |
---|
1031 | valuelist = [parmDict[var] for var in varlist] |
---|
1032 | parmDict.update(zip(mapvars, |
---|
1033 | np.dot(multarr,np.array(valuelist))) |
---|
1034 | ) |
---|
1035 | # now remove fixed variables from the varyList |
---|
1036 | global fixedVarList |
---|
1037 | for item in fixedVarList: |
---|
1038 | try: |
---|
1039 | varyList.remove(item) |
---|
1040 | except ValueError: |
---|
1041 | pass |
---|
1042 | # Set constrained parameters to their fixed values |
---|
1043 | parmDict.update(fixedDict) |
---|
1044 | |
---|
1045 | def Dict2Map(parmDict,varyList): |
---|
1046 | '''Applies the constraints defined using :func:`StoreEquivalence`, |
---|
1047 | :func:`GroupConstraints` and :func:`GenerateConstraints` by changing |
---|
1048 | values in a dict containing the parameters. This should be |
---|
1049 | done before the parameters are used for any computations |
---|
1050 | |
---|
1051 | :param dict parmDict: a dict containing parameter values keyed by the |
---|
1052 | parameter names. |
---|
1053 | This will contain updated values for both dependent and independent |
---|
1054 | parameters after Dict2Map is called. It will also contain some |
---|
1055 | unexpected entries of every constant value {'0':0.0} & {'1.0':1.0}, |
---|
1056 | which do not cause any problems. |
---|
1057 | |
---|
1058 | :param list varyList: a list of parameters names that will be varied |
---|
1059 | |
---|
1060 | ''' |
---|
1061 | global dependentParmList,arrayList,invarrayList,indParmList,fixedDict |
---|
1062 | # reset fixed values (should not be needed, but very quick) |
---|
1063 | # - this seems to update parmDict with {'0':0.0} & {'1.0':1.0} - probably not what was intended |
---|
1064 | # not needed, but also not a problem - BHT |
---|
1065 | parmDict.update(fixedDict) |
---|
1066 | for varlist,mapvars,invmultarr in zip(dependentParmList,indParmList,invarrayList): |
---|
1067 | #if invmultarr is None: continue |
---|
1068 | valuelist = [parmDict[var] for var in mapvars] |
---|
1069 | parmDict.update(zip(varlist, |
---|
1070 | np.dot(invmultarr,np.array(valuelist))) |
---|
1071 | ) |
---|
1072 | |
---|
1073 | #====================================================================== |
---|
1074 | # internal routines follow (these routines are unlikely to be called |
---|
1075 | # from outside the module) |
---|
1076 | |
---|
1077 | def GramSchmidtOrtho(a,nkeep=0): |
---|
1078 | '''Use the Gram-Schmidt process (http://en.wikipedia.org/wiki/Gram-Schmidt) to |
---|
1079 | find orthonormal unit vectors relative to first row. |
---|
1080 | |
---|
1081 | If nkeep is non-zero, the first nkeep rows in the array are not changed |
---|
1082 | |
---|
1083 | input: |
---|
1084 | arrayin: a 2-D non-singular square array |
---|
1085 | returns: |
---|
1086 | a orthonormal set of unit vectors as a square array |
---|
1087 | ''' |
---|
1088 | def proj(a,b): |
---|
1089 | 'Projection operator' |
---|
1090 | return a*(np.dot(a,b)/np.dot(a,a)) |
---|
1091 | for j in range(nkeep,len(a)): |
---|
1092 | for i in range(j): |
---|
1093 | a[j] -= proj(a[i],a[j]) |
---|
1094 | if np.allclose(np.linalg.norm(a[j]),0.0): |
---|
1095 | raise Exception,"Singular input to GramSchmidtOrtho" |
---|
1096 | a[j] /= np.linalg.norm(a[j]) |
---|
1097 | return a |
---|
1098 | |
---|
1099 | def _FillArray(sel,dict,collist,FillDiagonals=False): |
---|
1100 | '''Construct a n by n matrix (n = len(collist) |
---|
1101 | filling in the rows using the relationships defined |
---|
1102 | in the dictionaries selected by sel |
---|
1103 | |
---|
1104 | If FillDiagonals is True, diagonal elements in the |
---|
1105 | array are set to 1.0 |
---|
1106 | ''' |
---|
1107 | n = len(collist) |
---|
1108 | if FillDiagonals: |
---|
1109 | arr = np.eye(n) |
---|
1110 | else: |
---|
1111 | arr = np.zeros(2*[n,]) |
---|
1112 | # fill the top rows |
---|
1113 | for i,cnum in enumerate(sel): |
---|
1114 | for j,var in enumerate(collist): |
---|
1115 | arr[i,j] = dict[cnum].get(var,0) |
---|
1116 | return arr |
---|
1117 | |
---|
1118 | def _SwapColumns(i,m,v): |
---|
1119 | '''Swap columns in matrix m as well as the labels in v |
---|
1120 | so that element (i,i) is replaced by the first non-zero element in row i after that element |
---|
1121 | |
---|
1122 | Throws an exception if there are no non-zero elements in that row |
---|
1123 | ''' |
---|
1124 | for j in range(i+1,len(v)): |
---|
1125 | if not np.allclose(m[i,j],0): |
---|
1126 | m[:,(i,j)] = m[:,(j,i)] |
---|
1127 | v[i],v[j] = v[j],v[i] |
---|
1128 | return |
---|
1129 | else: |
---|
1130 | raise Exception,'Singular input' |
---|
1131 | |
---|
1132 | def _RowEchelon(m,arr,collist): |
---|
1133 | '''Convert the first m rows in Matrix arr to row-echelon form |
---|
1134 | exchanging columns in the matrix and collist as needed. |
---|
1135 | |
---|
1136 | throws an exception if the matrix is singular because |
---|
1137 | the first m rows are not linearly independent |
---|
1138 | ''' |
---|
1139 | n = len(collist) |
---|
1140 | for i in range(m): |
---|
1141 | if np.allclose(arr[i,i],0): |
---|
1142 | _SwapColumns(i,arr,collist) |
---|
1143 | arr[i,:] /= arr[i,i] # normalize row |
---|
1144 | # subtract current row from subsequent rows to set values to left of diagonal to 0 |
---|
1145 | for j in range(i+1,m): |
---|
1146 | arr[j,:] -= arr[i,:] * arr[j,i] |
---|
1147 | |
---|
1148 | if __name__ == "__main__": |
---|
1149 | parmdict = {} |
---|
1150 | constrDict = [ |
---|
1151 | {'0:12:Scale': 2.0, '0:11:Scale': 1.0, '0:14:Scale': 4.0, '0:13:Scale': 3.0, '0:0:Scale': 0.5}, |
---|
1152 | {'0:0:eA': 0.0}, |
---|
1153 | {'2::C(10,6,1)': 1.0, '1::C(10,6,1)': 1.0}, |
---|
1154 | {'1::C(10,0,1)': 1.0, '2::C(10,0,1)': 1.0}, |
---|
1155 | {'1::AUiso:0': 1.0, '0::AUiso:0': 1.0}, |
---|
1156 | {'0::A0': 0.0} |
---|
1157 | ] |
---|
1158 | fixedList = ['5.0', '0', None, None, '1.0', '0'] |
---|
1159 | StoreEquivalence('2::atomx:3',('2::atomy:3', ('2::atomz:3',2,), )) |
---|
1160 | #StoreEquivalence('1::atomx:3',('2::atomx:3', ('2::atomz:3',2,), )) # error: dependent & independent vars mixed |
---|
1161 | #StoreEquivalence('1::atomx:3',('2::atomy:3', ('2::atomz:3',2,), )) # error: dependent vars repeated |
---|
1162 | #StoreEquivalence('0:1:eA',('0:0:eA',)) # error: equiv & fixed |
---|
1163 | #StoreEquivalence('0:99:Scale',('0:12:Scale',)) # error: equiv & constrained |
---|
1164 | #StoreEquivalence('0:12:Scale',('0:99:Scale',)) # error: equiv & constrained |
---|
1165 | varylist = ['2::atomx:3', |
---|
1166 | '2::C(10,6,1)', '1::C(10,6,1)', |
---|
1167 | '2::atomy:3', '2::atomz:3', |
---|
1168 | '0:12:Scale', '0:11:Scale', '0:14:Scale', '0:13:Scale', '0:0:Scale'] |
---|
1169 | # e,w = CheckConstraints([, |
---|
1170 | # [{'2:0:Scale': 1.0, '5:0:Scale': 1.0, '10:0:Scale': 1.0, '6:0:Scale': 1.0, '9:0:Scale': 1.0, '8:0:Scale': 1.0,# '3:0:Scale': 1.0, '4:0:Scale': 1.0, '7:0:Scale': 1.0, '1:0:Scale': 1.0, '0:0:Scale': 1.0}], |
---|
1171 | # ['1.0']) |
---|
1172 | # if e: print 'error=',e |
---|
1173 | # if w: print 'error=',w |
---|
1174 | # varyList = ['0::A0', '0::AUiso:0', '0::Afrac:1', '0::Afrac:2', '0::Afrac:3', '0::Afrac:4', '0::dAx:5', '0::dAy:5', '0::dAz:5', '0::AUiso:5', ':0:Back:0', ':0:Back:1', ':0:Back:2', ':0:Back:3', ':0:Back:4', ':0:Back:5', ':0:Back:6', ':0:Back:7', ':0:Back:8', ':0:Back:9', ':0:Back:10', ':0:Back:11', ':0:U', ':0:V', ':0:W', ':0:X', ':0:Y', ':0:Scale', ':0:DisplaceX', ':0:DisplaceY'] |
---|
1175 | # constrDict = [ |
---|
1176 | # {'0::Afrac:4': 24.0, '0::Afrac:1': 16.0, '0::Afrac:3': 24.0, '0::Afrac:2': 16.0}, |
---|
1177 | # {'0::Afrac:1': 1.0, '0::Afrac:2': 1.0}, |
---|
1178 | # {'0::Afrac:4': 1.0, '0::Afrac:3': 1.0}] |
---|
1179 | # fixedList = ['40.0', '1.0', '1.0'] |
---|
1180 | |
---|
1181 | errmsg, warnmsg = CheckConstraints(varylist,constrDict,fixedList) |
---|
1182 | if errmsg: |
---|
1183 | print "*** Error ********************" |
---|
1184 | print errmsg |
---|
1185 | if warnmsg: |
---|
1186 | print "*** Warning ********************" |
---|
1187 | print warnmsg |
---|
1188 | if errmsg or warnmsg: |
---|
1189 | sys.exit() |
---|
1190 | groups,parmlist = GroupConstraints(constrDict) |
---|
1191 | GenerateConstraints(groups,parmlist,varylist,constrDict,fixedList) |
---|
1192 | print VarRemapShow(varylist) |
---|
1193 | parmdict.update( { |
---|
1194 | '0:12:Scale': 1.0, '0:11:Scale': 1.0, '0:14:Scale': 1.0, '0:13:Scale': 1.0, '0:0:Scale': 2.0, |
---|
1195 | '0:0:eA': 0.0, |
---|
1196 | '2::C(10,6,1)': 0.2, '1::C(10,6,1)': 0.3, |
---|
1197 | '1::C(10,0,1)': 0.2, '2::C(10,0,1)': 0.3, |
---|
1198 | '1::AUiso:0': 0.02, '0::AUiso:0': 0.03, |
---|
1199 | '0::A0': 0.0, |
---|
1200 | '2::atomx:3':0.23,'2::atomy:3':-.23, '2::atomz:3':-0.11, |
---|
1201 | }) |
---|
1202 | print 'parmdict start',parmdict |
---|
1203 | print 'varylist start',varylist |
---|
1204 | before = parmdict.copy() |
---|
1205 | Map2Dict(parmdict,varylist) |
---|
1206 | print 'parmdict before and after Map2Dict' |
---|
1207 | print ' key / before / after' |
---|
1208 | for key in sorted(parmdict.keys()): |
---|
1209 | print ' '+key,'\t',before.get(key),'\t',parmdict[key] |
---|
1210 | print 'varylist after',varylist |
---|
1211 | before = parmdict.copy() |
---|
1212 | Dict2Map(parmdict,varylist) |
---|
1213 | print 'after Dict2Map' |
---|
1214 | print ' key / before / after' |
---|
1215 | for key in sorted(parmdict.keys()): |
---|
1216 | print ' '+key,'\t',before.get(key),'\t',parmdict[key] |
---|
1217 | # dMdv = len(varylist)*[0] |
---|
1218 | # deriv = {} |
---|
1219 | # for i,v in enumerate(parmdict.keys()): deriv[v]=i |
---|
1220 | # Dict2Deriv(varylist,deriv,dMdv) |
---|