| 1 | ###################################################################### |
|---|
| 2 | # |
|---|
| 3 | # This is a partial copy from LeBailFitScript.py |
|---|
| 4 | # |
|---|
| 5 | # Python Script as Step 1 of Le Bail Fitting to |
|---|
| 6 | # 1. Load file |
|---|
| 7 | # 2. Create LeBailFitInput |
|---|
| 8 | # 3. Fit Peaks |
|---|
| 9 | # |
|---|
| 10 | # Step 1: Load data, model (HKL list) and starting instrument parameters values, |
|---|
| 11 | # and do an initial LeBailFit/calculation to see how much the starting |
|---|
| 12 | # values are off; |
|---|
| 13 | |
|---|
| 14 | #|------------------------------------------------------------------------------------------- |
|---|
| 15 | #|Step 2.1: Fit single peaks for TOF_h, Alpha, Beta, and Sigma; |
|---|
| 16 | #| |
|---|
| 17 | #|Step 2.2: Plot parameters (TOF_H, Alpha, Beta and Sigma) against d-spacing; |
|---|
| 18 | #| |
|---|
| 19 | #|Step 2.3: Remove the peaks with bad fit; |
|---|
| 20 | #| |
|---|
| 21 | #|Step 2.4: Refine instrument geometry parameters; |
|---|
| 22 | #| |
|---|
| 23 | #| It is possible to loop back to Step 2 to include more peaks with single peaks |
|---|
| 24 | #|------------------------------------------------------------------------------------------- |
|---|
| 25 | # |
|---|
| 26 | # Step 5: Do Le Bail Fit from previous result to see whether the peak parameters are |
|---|
| 27 | # close enough for Le Bail Fit |
|---|
| 28 | # |
|---|
| 29 | # !Step 6: Fit Alpha, Beta and apply the change to |
|---|
| 30 | # |
|---|
| 31 | # !Step 7: Save the result files to HDD for Step 6, LeBailFit in random walk; |
|---|
| 32 | # |
|---|
| 33 | # Step 8: Save the result files to HDD for Step 6, LeBailFit in random walk; |
|---|
| 34 | # |
|---|
| 35 | # Step 7: Not in this script; |
|---|
| 36 | # |
|---|
| 37 | # Step 8: Process Monte Carlo results from Step 6 |
|---|
| 38 | # |
|---|
| 39 | # |
|---|
| 40 | ###################################################################### |
|---|
| 41 | from Calibration_ImportInformation import * |
|---|
| 42 | |
|---|
| 43 | #-------------- Definition of Global Variables --------------------- |
|---|
| 44 | bankid = 0 |
|---|
| 45 | |
|---|
| 46 | datafilename = "" |
|---|
| 47 | hklfilename = "" |
|---|
| 48 | irffilename = "" |
|---|
| 49 | |
|---|
| 50 | # montecarlofilename = "" |
|---|
| 51 | # expirffilename = "" |
|---|
| 52 | |
|---|
| 53 | datawsname = "" |
|---|
| 54 | instrparamwsname = "" |
|---|
| 55 | braggpeakparamwsname = "" |
|---|
| 56 | |
|---|
| 57 | # outdataws1name = "" |
|---|
| 58 | |
|---|
| 59 | minpeakheight = 0.001 |
|---|
| 60 | |
|---|
| 61 | # Range for Le Bail Fit of all peaks |
|---|
| 62 | startx = -1 |
|---|
| 63 | endx = -1 |
|---|
| 64 | # Range for fitting single peaks for step 1~3 |
|---|
| 65 | tofmin_singlepeaks = -1 |
|---|
| 66 | tofmax_singlepeaks = -1 |
|---|
| 67 | |
|---|
| 68 | backgroundtype = "Polynomial" |
|---|
| 69 | backgroundorder = 6 |
|---|
| 70 | bkgdtablewsname = "" |
|---|
| 71 | bkgdwsname = "" |
|---|
| 72 | bkgdfilename = "" |
|---|
| 73 | usrbkgdpoints = '' |
|---|
| 74 | |
|---|
| 75 | latticesize = 4.1568899999999998 |
|---|
| 76 | #-------------------------------------------------------------------- |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | def setupGlobals(infofilename): |
|---|
| 80 | """ Set up globals values |
|---|
| 81 | """ |
|---|
| 82 | global datafilename, hklfilename, irffilename |
|---|
| 83 | global datawsname, instrparamwsname, braggpeakparamwsname |
|---|
| 84 | global bkgdtablewsname, bkgdwsname, bkgdfilename, backgroundorder |
|---|
| 85 | global tofmin_singlepeaks, tofmax_singlepeaks, startx, endx |
|---|
| 86 | global bankid, latticesize |
|---|
| 87 | global usrbkgdpoints |
|---|
| 88 | |
|---|
| 89 | bankid, calibDict = importCalibrationInformation(infofilename) |
|---|
| 90 | bankid = int(bankid) |
|---|
| 91 | |
|---|
| 92 | datafilename = calibDict["DataFileDir"] + calibDict[bankid]["DataFileName"] |
|---|
| 93 | hklfilename = calibDict["HKLFileDir"] + calibDict[bankid]["HKLFileName"] |
|---|
| 94 | irffilename = calibDict["IrfFileDir"] + calibDict[bankid]["IrfFileName"] |
|---|
| 95 | |
|---|
| 96 | startx = float(calibDict[bankid]["LeBailFitMinTOF"]) |
|---|
| 97 | endx = float(calibDict[bankid]["LeBailFitMaxTOF"]) |
|---|
| 98 | |
|---|
| 99 | # Name of workspaces |
|---|
| 100 | datawsname = calibDict[bankid]["DataWorkspace"] |
|---|
| 101 | instrparamwsname = "Bank%sInstrumentParameterTable" % (bankid) |
|---|
| 102 | braggpeakparamwsname = 'BraggPeakParameterTable' |
|---|
| 103 | |
|---|
| 104 | # Background related |
|---|
| 105 | usrbkgdpoints = calibDict[bankid]["UserSpecifiedBkgdPts"] |
|---|
| 106 | bkgdwsname = datawsname+"_Background" |
|---|
| 107 | backgroundtype = calibDict["BackgroundType"] |
|---|
| 108 | backgroundorder = int(calibDict["BackgroundOrder"]) |
|---|
| 109 | bkgdfilename = calibDict["WorkingDir"] + datawsname + "_Parameters.bak'" |
|---|
| 110 | bkgdwsname = datawsname + "_Background" |
|---|
| 111 | bkgdtablewsname = datawsname + "_Background_Parameters" |
|---|
| 112 | |
|---|
| 113 | # Other constants |
|---|
| 114 | latticesize = calibDict[bankid]["LatticeSize"] |
|---|
| 115 | |
|---|
| 116 | return |
|---|
| 117 | |
|---|
| 118 | #------------------------------------------------------------------------------ |
|---|
| 119 | # TableWorkspace Processing |
|---|
| 120 | #------------------------------------------------------------------------------ |
|---|
| 121 | def parseRefineGeometryMCResultWorkspace(tablews): |
|---|
| 122 | """ Parse the refinement result workspac |
|---|
| 123 | """ |
|---|
| 124 | numrows = tablews.rowCount() |
|---|
| 125 | numcols = tablews.columnCount() |
|---|
| 126 | colnames = tablews.getColumnNames() |
|---|
| 127 | |
|---|
| 128 | parameterdict = {} |
|---|
| 129 | |
|---|
| 130 | for irow in xrange(numrows): |
|---|
| 131 | ppdict = {} |
|---|
| 132 | for icol in xrange(numcols): |
|---|
| 133 | colname = colnames[icol] |
|---|
| 134 | value = tablews.cell(irow, icol) |
|---|
| 135 | ppdict[colname] = value |
|---|
| 136 | # ENDFOR Column |
|---|
| 137 | chi2 = ppdict["Chi2"] |
|---|
| 138 | |
|---|
| 139 | parameterdict[chi2] = ppdict |
|---|
| 140 | # ENDFOR |
|---|
| 141 | |
|---|
| 142 | return parameterdict |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | #def updateInstrumentParameterValue(tablews, paramdict): |
|---|
| 146 | # """ Update the value to an instrument parameter table |
|---|
| 147 | # """ |
|---|
| 148 | # paramnames = paramdict.keys() |
|---|
| 149 | # for parname in paramnames: |
|---|
| 150 | # parvalue = paramdict[parname] |
|---|
| 151 | # # print "%s = %f" % (parname, parvalue) |
|---|
| 152 | # if parname.count("Chi2") == 0: |
|---|
| 153 | # # Only update parameters nothing to do with chi2 |
|---|
| 154 | # UpdatePeakParameterTableValue(InputWorkspace=tablews, |
|---|
| 155 | # Column='Value', |
|---|
| 156 | # ParameterNames=[parname], |
|---|
| 157 | # NewFloatValue=parvalue) |
|---|
| 158 | # |
|---|
| 159 | # return |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | def doStep3(): |
|---|
| 163 | """ Step 3: to check whether the refinement result is good for low-d region |
|---|
| 164 | """ |
|---|
| 165 | global startx, endx |
|---|
| 166 | global datawsname, instrparamwsname, braggpeakparamwsname |
|---|
| 167 | global bkgdtablewsname, bkgdwsname, backgroundtype, backgroundorder |
|---|
| 168 | global bankid |
|---|
| 169 | global usrbkgdpoints |
|---|
| 170 | |
|---|
| 171 | # 1. Process background |
|---|
| 172 | bkgdwsname = datawsname+"_Background" |
|---|
| 173 | ProcessBackground(InputWorkspace=datawsname, |
|---|
| 174 | OutputWorkspace=bkgdwsname, |
|---|
| 175 | Options='SelectBackgroundPoints', |
|---|
| 176 | LowerBound=startx, |
|---|
| 177 | UpperBound=endx, |
|---|
| 178 | BackgroundType=backgroundtype, |
|---|
| 179 | BackgroundPoints=usrbkgdpoints, |
|---|
| 180 | NoiseTolerance='0.10000000000000001') |
|---|
| 181 | |
|---|
| 182 | functionstr = "name=%s,n=%d" % (backgroundtype, backgroundorder) |
|---|
| 183 | for iborder in xrange(backgroundorder+1): |
|---|
| 184 | functionstr = "%s,A%d=%.5f" % (functionstr, iborder, 0.0) |
|---|
| 185 | print "Background function: %s" % (functionstr) |
|---|
| 186 | |
|---|
| 187 | Fit(Function='name=Polynomial,n=6,A0=0.657699,A1=3.68433e-05,A2=-1.29638e-08,A3=1.16778e-12,A4=-4.71439e-17,A5=8.8499e-22,A6=-6.26573e-27', |
|---|
| 188 | InputWorkspace=bkgdwsname, |
|---|
| 189 | MaxIterations='1000', |
|---|
| 190 | Minimizer='Levenberg-MarquardtMD', |
|---|
| 191 | CreateOutput='1', |
|---|
| 192 | Output=bkgdwsname, |
|---|
| 193 | StartX=startx, |
|---|
| 194 | EndX=endx) |
|---|
| 195 | |
|---|
| 196 | # 2. Do calculation |
|---|
| 197 | usemcresult = True |
|---|
| 198 | try: |
|---|
| 199 | bestmdws = mtd["BestMCResult1"] |
|---|
| 200 | except KeyError: |
|---|
| 201 | usemcresult = False |
|---|
| 202 | |
|---|
| 203 | outwsname = datawsname + "_Calculated" |
|---|
| 204 | if usemcresult is True: |
|---|
| 205 | # Use MC N results |
|---|
| 206 | paramdict = parseRefineGeometryMCResultWorkspace(bestmtdws) |
|---|
| 207 | |
|---|
| 208 | raise NotImplementedError("Case 1") |
|---|
| 209 | |
|---|
| 210 | index = 0 |
|---|
| 211 | maxoutput = 1 |
|---|
| 212 | for chi2 in sorted(paramdict.keys()): |
|---|
| 213 | # 1. Update parameter values |
|---|
| 214 | updateInstrumentParameterValue(mtd["Bank%sInstrumentParameterTable1"%(bankid)], paramdict[chi2]) |
|---|
| 215 | |
|---|
| 216 | print "Fit range: %f , %f" % (startx, endx) |
|---|
| 217 | |
|---|
| 218 | LeBailFit(InputWorkspace=datawsname, |
|---|
| 219 | OutputWorkspace=outwsname, |
|---|
| 220 | InputParameterWorkspace='Bank%dInstrumentParameterTable1'%(bankid), |
|---|
| 221 | OutputParameterWorkspace='Bank%dInstrumentParameterTable1_%d'%(bankid, index), |
|---|
| 222 | InputHKLWorkspace='BraggPeakParameterTable1', |
|---|
| 223 | OutputPeaksWorkspace='BraggPeakParameterTable2_%d'%(index), |
|---|
| 224 | Function='Calculation', |
|---|
| 225 | FitRegion = '%f, %f' % (startx, endx), |
|---|
| 226 | BackgroundType='Polynomial', |
|---|
| 227 | BackgroundParametersWorkspace=bkgdtablewsname, |
|---|
| 228 | UseInputPeakHeights='0', |
|---|
| 229 | PeakRadius='8', |
|---|
| 230 | Minimizer='Levenberg-Marquardt') |
|---|
| 231 | |
|---|
| 232 | print "Pattern %s: Instrumental Geometry Chi^2 = %.5f" % ('CalculatedPattern%d'%(index), chi2) |
|---|
| 233 | index += 1 |
|---|
| 234 | if index >= maxoutput: |
|---|
| 235 | break |
|---|
| 236 | # ENDFOR |
|---|
| 237 | else: |
|---|
| 238 | # Use Version 2 result |
|---|
| 239 | |
|---|
| 240 | # data from manually refinement |
|---|
| 241 | # for pair in [("Dtt1", 22529.600), ("Dtt1t", 22747.400), ("Zerot", 15.792320), ("Width", 1.1072)]: |
|---|
| 242 | |
|---|
| 243 | # data from Jason's |
|---|
| 244 | # for pair in [("Dtt1", 22584.512), ("Dtt1t", 22604.852), ("Zerot", 11.317320), ("Width", 1.0521)]: |
|---|
| 245 | |
|---|
| 246 | index = 0 |
|---|
| 247 | print "Fit range: %f , %f" % (startx, endx) |
|---|
| 248 | LeBailFit(InputWorkspace=datawsname, |
|---|
| 249 | OutputWorkspace=outwsname, |
|---|
| 250 | InputParameterWorkspace='Bank1InstrumentParametersTable', |
|---|
| 251 | OutputParameterWorkspace='Bank%dInstrumentParameterTable1_%d'%(bankid, index), |
|---|
| 252 | InputHKLWorkspace='BraggPeakParameterTable1', |
|---|
| 253 | OutputPeaksWorkspace='BraggPeakParameterTable2_%d'%(index), |
|---|
| 254 | Function='Calculation', |
|---|
| 255 | FitRegion = '%f, %f' % (startx, endx), |
|---|
| 256 | BackgroundType='Polynomial', |
|---|
| 257 | BackgroundParametersWorkspace=bkgdtablewsname, |
|---|
| 258 | UseInputPeakHeights='0', |
|---|
| 259 | PeakRadius='8', |
|---|
| 260 | Minimizer='Levenberg-Marquardt') |
|---|
| 261 | |
|---|
| 262 | return |
|---|
| 263 | |
|---|
| 264 | def main(argv): |
|---|
| 265 | """ Main |
|---|
| 266 | """ |
|---|
| 267 | setupGlobals("Calibration_Information.config") |
|---|
| 268 | |
|---|
| 269 | print "Le Bail Fit Calibration Instrument... Step 3" |
|---|
| 270 | doStep3() |
|---|
| 271 | |
|---|
| 272 | return |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | if __name__=="__main__": |
|---|
| 276 | main(["LeBailFitScript"]) |
|---|