Package diffpy :: Package srrietveld :: Module buildfit
[frames] | no frames]

Source Code for Module diffpy.srrietveld.buildfit

  1  ############################################################################## 
  2  # 
  3  # diffpy.srrietveld by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2009 Trustees of the Columbia University 
  6  #                   in the City of New York.  All rights reserved. 
  7  # 
  8  # File coded by:    Peng Tian 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 12  # 
 13  ############################################################################## 
 14   
 15  """The file contains functions for building a Fit object. 
 16  """ 
 17  __id__ = "$Id: buildfit.py 6300 2011-03-10 21:34:27Z yshang $" 
 18  import diffpy.srrietveld.utility as UT 
 19  from diffpy.srrietveld.exceptions import SrrConfigError 
 20   
21 -class BuildFit:
22 """ BuildFit class is to build fit object based on the GUI inputs. """
23 - def __init__(self, enginename, strudict, instdict, datadict):
24 """ Initialization """ 25 self.engine = "diffpy.py" + enginename 26 self.strudict = strudict 27 self.instdict = instdict 28 self.datadict = datadict 29 return
30
31 - def buildFit(self):
32 """ Build fit object """ 33 # import engine fit 34 fit = UT.importClass(self.engine+".fit", "fit", "Fit", None) 35 self.buildPhase(fit) 36 self.buildPattern(fit) 37 self.buildContribution(fit) 38 self.buildPeakProfile(fit) 39 return fit
40
41 - def buildPhase(self, fit):
42 """ Build phase """ 43 for phasekey, phasevalue in self.strudict.items(): 44 if phasekey.count("PHASE") == 1: 45 # set engine phase 46 phase = UT.importClass(self.engine+".phase", "phase", "Phase", fit) 47 atomdict = {} 48 for key, value in phasevalue[0].items(): 49 if key.startswith("ATOM"): 50 # set engine atom 51 atom = UT.importClass(self.engine+".atom", "atom", 52 "AtomCrystal", None) 53 for akey, avalue in value[0].items(): 54 if akey in ["Uiso", "Biso"]: 55 atomadp = UT.importClass(self.engine+".atom", "atom", 56 "AtomicDisplacementFactorIsotropic", None) 57 elif akey in ["U11", "B11"]: 58 atomadp = UT.importClass(self.engine+".atom", "atom", 59 "AtomicDisplacementFactorAnisotropic", None) 60 if akey.startswith("U") or akey.startswith("B"): 61 atomadp.set(akey, avalue) 62 else: 63 atom.set(akey, avalue) 64 atom.set("AtomicDisplacementFactor", atomadp) 65 atomdict[key] = atom 66 elif key in phase.ParamDict: 67 phase.set(key, value) 68 for key in sorted(atomdict.keys()): 69 phase.set("Atom", atomdict[key]) 70 # add into fit 71 fit.set("Phase", phase) 72 return
73
74 - def buildPattern(self, fit):
75 """ Build pattern 76 FIXME: only for one bank now 77 """ 78 instname = self.instdict["Name"] 79 if instname in ["NPDF", "POWGEN", "TOF"]: 80 patternname = "PatternTOF" 81 radiationtype = "NeutronTOF" 82 elif instname in ["RAPDF", "NeutronCW", "Xray"]: 83 patternname = "Pattern2Theta" 84 radiationtype = "NeutronCW" 85 if instname in ["RAPDF", "Xray"]: 86 radiationtype = "Xray" 87 else: 88 raise SrrConfigError("SrRietveld doesn't support %s!"%instname) 89 # set engine pattern 90 pattern = UT.importClass(self.engine+".pattern", "pattern", patternname, fit) 91 # add into fit 92 fit.set("Pattern", pattern) 93 pattern.setDataProperty([self.datadict.keys()[0], 94 self.datadict.values()[0]["Name"]], radiationtype) 95 for patternkey, patternvalue in self.instdict.items(): 96 # set the single parameters 97 if patternkey in pattern.ParamDict: 98 pattern.set(patternkey, patternvalue) 99 elif patternkey == "MEASUREMENT": 100 # set measurement range and scale factor 101 for mkey, mvalue in patternvalue[0].items(): 102 if mkey in pattern.ParamDict: 103 pattern.set(mkey, mvalue) 104 elif patternkey == "BACKGROUND": 105 # add background 106 bgtype = patternvalue[0]["Type"] 107 background = UT.importClass(self.engine+".pattern", 108 "pattern", bgtype, pattern) 109 pattern.set("Background",background) 110 for bgkey, bgvalue in patternvalue[0].items(): 111 if bgkey == "Type": 112 continue 113 else: 114 background.set(bgkey, bgvalue) 115 elif patternkey == "EXCLUDEDREGION": 116 # add excludedregion 117 exre = UT.importClass(self.engine+".pattern", "pattern", 118 "ExcludedRegion", pattern) 119 for key, value in patternvalue[0].items(): 120 exre.set(key, value) 121 pattern.set("ExcludedRegion", exre) 122 elif patternkey == "ENERGY": 123 for key, value in patternvalue[0].items(): 124 pattern.set(key, value) 125 elif patternkey == "LPFACTOR": 126 lpfactor = pattern.get("LPFactor") 127 for key, value in patternvalue[0].items(): 128 lpfactor.set(key, value) 129 130 return
131
132 - def buildContribution(self, fit):
133 """ Build contribution """ 134 phaselist = fit.get("Phase") 135 patternlist = fit.get("Pattern") 136 #print phaselist, patternlist 137 for phase in phaselist: 138 for pattern in patternlist: 139 classname = pattern.__class__.__name__ 140 contributionname = "Contribution" + classname.split("Pattern")[1] 141 contribution = UT.importClass(self.engine+".contribution", 142 "contribution", contributionname, fit, pattern, phase) 143 fit.set("Contribution", contribution) 144 145 for key, value in self.instdict["MEASUREMENT"][0].items(): 146 if key in contribution.ParamDict.keys(): 147 contribution.set(key, value) 148 149 if "EXPDECAYFUNCTION" in self.instdict.keys(): 150 expdecay = contribution.get("ExpDecayFunction") 151 for key, value in self.instdict["EXPDECAYFUNCTION"][0].items(): 152 expdecay.set(key, value) 153 else: 154 pass 155 return
156 157
158 - def buildPeakProfile(self, fit):
159 """ Build peakprofile """ 160 contributionlist = fit.get("Contribution") 161 profile_id = -1 162 for instkey, instvalue in self.instdict.items(): 163 if instkey.count("PEAKPROFILE") == 1: 164 profile = UT.importClass(self.engine+".contribution", 165 "contribution", instvalue[0]["Type"], None) 166 for key, value in instvalue[0].items(): 167 if key != "Type": 168 profile.set(key, value) 169 profile_id += 1 170 contribution = contributionlist[profile_id] 171 contribution.set("Profile", profile) 172 return
173
174 - def addBkgdPoints(self, fit, bkgdlist):
175 """ Add background points to fit """ 176 patterns = fit.get("Pattern") 177 for pattern in patterns: 178 pattern.addBackgroundPoints(bkgdlist) 179 return
180
181 - def addExre(self, fit, exredict):
182 """ Add excludedregion to fit """ 183 patterns = fit.get("Pattern") 184 for pattern in patterns: 185 pattern_id = patterns.index(pattern) + 1 186 bankterm = "BANK" + str(pattern_id) 187 if bankterm in exredict.keys(): 188 exre = UT.importClass(self.engine+".pattern", "pattern", 189 "ExcludedRegion", pattern) 190 for key, value in exredict[bankterm][0].items(): 191 exre.set(key, value) 192 pattern.set("ExcludedRegion", exre) 193 return
194