From aee6eb74bb640b4c681cf5feacc3ddf6ab0e3598 Mon Sep 17 00:00:00 2001
From: Brendan Sullivan <sullivanbt@ornl.gov>
Date: Fri, 27 Jul 2018 15:27:06 -0400
Subject: [PATCH] Re #22811 move mask size to instrument param file

---
 .../IntegratePeaksProfileFitting.py           | 12 +++++----
 instrument/CORELLI_Parameters.xml             |  5 ++++
 instrument/MANDI_Parameters.xml               |  5 ++++
 instrument/MANDI_Parameters_2015_08_01.xml    |  4 +++
 instrument/MANDI_Parameters_2016_02_01.xml    |  5 ++++
 instrument/TOPAZ_Parameters.xml               |  5 ++++
 scripts/SCD_Reduction/BVGFitTools.py          | 14 +++++-----
 scripts/SCD_Reduction/ICCFitTools.py          | 27 ++++++++++---------
 8 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/Framework/PythonInterface/plugins/algorithms/IntegratePeaksProfileFitting.py b/Framework/PythonInterface/plugins/algorithms/IntegratePeaksProfileFitting.py
index c38a7219d37..d37667e0e1c 100644
--- a/Framework/PythonInterface/plugins/algorithms/IntegratePeaksProfileFitting.py
+++ b/Framework/PythonInterface/plugins/algorithms/IntegratePeaksProfileFitting.py
@@ -118,11 +118,12 @@ class IntegratePeaksProfileFitting(PythonAlgorithm):
         instrumentName = peaks_ws.getInstrument().getFullName()
         try:
             nTheta = peaks_ws.getInstrument().getIntParameter("numBinsTheta")[0]
-            nPhi = peaks_ws.getInstrument().getIntParameter("numBinsPhi")[0] 
+            nPhi = peaks_ws.getInstrument().getIntParameter("numBinsPhi")[0]
             mindtBinWidth = peaks_ws.getInstrument().getNumberParameter("mindtBinWidth")[0]
-            maxdtBinWidth = peaks_ws.getInstrument().getNumberParameter("maxdtBinWidth")[0] 
-            fracHKL = peaks_ws.getInstrument().getNumberParameter("fracHKL")[0] 
-            dQPixel = peaks_ws.getInstrument().getNumberParameter("dQPixel")[0] 
+            maxdtBinWidth = peaks_ws.getInstrument().getNumberParameter("maxdtBinWidth")[0]
+            fracHKL = peaks_ws.getInstrument().getNumberParameter("fracHKL")[0]
+            dQPixel = peaks_ws.getInstrument().getNumberParameter("dQPixel")[0]
+            peakMaskSize = peaks_ws.getInstrument().getIntParameter("peakMaskSize")[0]
 
         except:
             raise
@@ -170,7 +171,8 @@ class IntegratePeaksProfileFitting(PythonAlgorithm):
                                                                       maxdtBinWidth=maxdtBinWidth,
                                                                       pplmin_frac=pplmin_frac, pplmax_frac=pplmax_frac,
                                                                       forceCutoff=forceCutoff, edgeCutoff=edgeCutoff,
-                                                                      instrumentName=instrumentName)
+                                                                      instrumentName=instrumentName,
+                                                                      peakMaskSize=peakMaskSize)
 
                     # First we get the peak intensity
                     peakIDX = Y3D/Y3D.max() > fracStop
diff --git a/instrument/CORELLI_Parameters.xml b/instrument/CORELLI_Parameters.xml
index a8167e41032..d7421bdbedb 100644
--- a/instrument/CORELLI_Parameters.xml
+++ b/instrument/CORELLI_Parameters.xml
@@ -70,6 +70,11 @@
      <value val="60" />
     </parameter>
 
+    <!-- Size of peak mask for background calculation in units of dQPixel -->
+    <parameter name="peakMaskSize" type="int">
+     <value val="10" />
+    </parameter>
+
   </component-link>
 
 </parameter-file>
diff --git a/instrument/MANDI_Parameters.xml b/instrument/MANDI_Parameters.xml
index dcaa2ca55f3..9ac7e699e5c 100644
--- a/instrument/MANDI_Parameters.xml
+++ b/instrument/MANDI_Parameters.xml
@@ -63,6 +63,11 @@
  <value val="50" />
 </parameter>
 
+<!-- Size of peak mask for background calculation in units of dQPixel -->
+<parameter name="peakMaskSize" type="int">
+ <value val="5" />
+</parameter>
+
 </component-link>
 
 </parameter-file>
diff --git a/instrument/MANDI_Parameters_2015_08_01.xml b/instrument/MANDI_Parameters_2015_08_01.xml
index fef3a0458b8..2de3d3e5077 100644
--- a/instrument/MANDI_Parameters_2015_08_01.xml
+++ b/instrument/MANDI_Parameters_2015_08_01.xml
@@ -63,6 +63,10 @@
  <value val="50" />
 </parameter>
 
+<!-- Size of peak mask for background calculation in units of dQPixel -->
+<parameter name="peakMaskSize" type="int">
+ <value val="5" />
+</parameter>
 
 
 </parameter-file>
diff --git a/instrument/MANDI_Parameters_2016_02_01.xml b/instrument/MANDI_Parameters_2016_02_01.xml
index 9b8c188b408..7af08e9e0ac 100644
--- a/instrument/MANDI_Parameters_2016_02_01.xml
+++ b/instrument/MANDI_Parameters_2016_02_01.xml
@@ -63,4 +63,9 @@
  <value val="50" />
 </parameter>
 
+<!-- Size of peak mask for background calculation in units of dQPixel -->
+<parameter name="peakMaskSize" type="int">
+ <value val="5" />
+</parameter>
+
 </parameter-file>
diff --git a/instrument/TOPAZ_Parameters.xml b/instrument/TOPAZ_Parameters.xml
index ff52c5ebdd2..b4fddb7ddc5 100644
--- a/instrument/TOPAZ_Parameters.xml
+++ b/instrument/TOPAZ_Parameters.xml
@@ -139,6 +139,11 @@ detScale={13:1.046504,14:1.259293,16:1.02449,17:1.18898,18:0.88014,19:0.98665,\
  <value val="15" />
 </parameter>
 
+<!-- Size of peak mask for background calculation in units of dQPixel -->
+<parameter name="peakMaskSize" type="int">
+ <value val="15" />
+</parameter>
+
 </component-link>
 
 </parameter-file>
diff --git a/scripts/SCD_Reduction/BVGFitTools.py b/scripts/SCD_Reduction/BVGFitTools.py
index 72d9972b0cf..1897c816320 100644
--- a/scripts/SCD_Reduction/BVGFitTools.py
+++ b/scripts/SCD_Reduction/BVGFitTools.py
@@ -15,7 +15,7 @@ def get3DPeak(peak, peaks_ws, box, padeCoefficients, qMask, nTheta=150, nPhi=150
               plotResults=False, zBG=1.96, bgPolyOrder=1, fICCParams=None, oldICCFit=None,
               strongPeakParams=None, forceCutoff=250, edgeCutoff=15,
               neigh_length_m=3, q_frame='sample', dtSpread=0.03, pplmin_frac=0.8, pplmax_frac=1.5, mindtBinWidth=1,
-              maxdtBinWidth=50, figureNumber=2, instrumentName=None):
+              maxdtBinWidth=50, figureNumber=2, instrumentName=None, peakMaskSize=5):
     n_events = box.getNumEventsArray()
 
     if q_frame == 'lab':
@@ -31,14 +31,13 @@ def get3DPeak(peak, peaks_ws, box, padeCoefficients, qMask, nTheta=150, nPhi=150
                     n_events, peak=peak, box=box, qMask=qMask, calc_pp_lambda=True, padeCoefficients=padeCoefficients,
                     neigh_length_m=neigh_length_m, pp_lambda=None, pplmin_frac=pplmin_frac,
                     pplmax_frac=pplmax_frac, mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                    instrumentName=instrumentName)
+                    instrumentName=instrumentName, peakMaskSize=peakMaskSize)
         YTOF, fICC, x_lims = fitTOFCoordinate(
                     box, peak, padeCoefficients, dtSpread=dtSpread, qMask=qMask, bgPolyOrder=bgPolyOrder, zBG=zBG,
                     plotResults=plotResults, pp_lambda=pp_lambda, neigh_length_m=neigh_length_m, pplmin_frac=pplmin_frac,
                     pplmax_frac=pplmax_frac, mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                    instrumentName=instrumentName)
+                    instrumentName=instrumentName, peakMaskSize=peakMaskSize)
         chiSqTOF = mtd['fit_Parameters'].column(1)[-1]
-
     else:  # we already did I-C profile, so we'll just read the parameters
         pp_lambda = fICCParams[-1]
         fICC = ICC.IkedaCarpenterConvoluted()
@@ -51,7 +50,7 @@ def get3DPeak(peak, peaks_ws, box, padeCoefficients, qMask, nTheta=150, nPhi=150
         fICC['HatWidth'] = fICCParams[10]
         fICC['KConv'] = fICCParams[11]
         goodIDX, _ = ICCFT.getBGRemovedIndices(
-            n_events, pp_lambda=pp_lambda, qMask=qMask, instrumentName=instrumentName)
+            n_events, pp_lambda=pp_lambda, qMask=qMask, instrumentName=instrumentName, peakMaskSize=peakMaskSize)
         chiSqTOF = fICCParams[4] #Last entry
 
         # Get the 3D TOF component, YTOF
@@ -88,7 +87,6 @@ def get3DPeak(peak, peaks_ws, box, padeCoefficients, qMask, nTheta=150, nPhi=150
     useForceParams = peak.getIntensity() < forceCutoff or peak.getRow() <= dEdge or peak.getRow(
     ) >= nPixels[0] - dEdge or peak.getCol() <= dEdge or peak.getCol() >= nPixels[1] - dEdge
 
-
     #Here we retrieve some instrument specific parameters
     try:
         doPeakConvolution = peaks_ws.getInstrument().getBoolParameter("fitConvolvedPeak")[0]
@@ -253,7 +251,7 @@ def getXTOF(box, peak):
 def fitTOFCoordinate(box, peak, padeCoefficients, dtSpread=0.03, minFracPixels=0.01,
                      neigh_length_m=3, zBG=1.96, bgPolyOrder=1, qMask=None, plotResults=False,
                      fracStop=0.01, pp_lambda=None, pplmin_frac=0.8, pplmax_frac=1.5, mindtBinWidth=1,
-                     maxdtBinWidth=50, instrumentName=None):
+                     maxdtBinWidth=50, instrumentName=None, peakMaskSize=5):
 
     # Get info from the peak
     tof = peak.getTOF()  # in us
@@ -272,7 +270,7 @@ def fitTOFCoordinate(box, peak, padeCoefficients, dtSpread=0.03, minFracPixels=0
                                 neigh_length_m=neigh_length_m, zBG=zBG, pp_lambda=pp_lambda,
                                 pplmin_frac=pplmin_frac, pplmax_frac=pplmax_frac,
                                 mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                                instrumentName=instrumentName)
+                                instrumentName=instrumentName, peakMaskSize=peakMaskSize)
 
     fitResults, fICC = ICCFT.doICCFit(tofWS, energy, flightPath,
                                       padeCoefficients, fitOrder=bgPolyOrder, constraintScheme=1,
diff --git a/scripts/SCD_Reduction/ICCFitTools.py b/scripts/SCD_Reduction/ICCFitTools.py
index 9c378f849d4..9f0076de238 100644
--- a/scripts/SCD_Reduction/ICCFitTools.py
+++ b/scripts/SCD_Reduction/ICCFitTools.py
@@ -93,7 +93,7 @@ def getQXQYQZ(box):
 
 def getQuickTOFWS(box, peak, padeCoefficients, goodIDX=None, dtSpread=0.03, qMask=None,
                   pp_lambda=None, minppl_frac=0.8, maxppl_frac=1.5, mindtBinWidth=1, maxdtBinWidth=50,
-                  constraintScheme=1, instrumentName=None):
+                  constraintScheme=1, instrumentName=None, peakMaskSize=5):
     """
     getQuickTOFWS - generates a quick-and-dirty TOFWS.  Useful for determining the background.
     Input:
@@ -130,7 +130,8 @@ def getQuickTOFWS(box, peak, padeCoefficients, goodIDX=None, dtSpread=0.03, qMas
     tofWS, ppl = getTOFWS(box, flightPath, scatteringHalfAngle, tof, peak, qMask, dtSpread=dtSpread,
                           minFracPixels=0.01, neigh_length_m=3, zBG=1.96, pp_lambda=pp_lambda,
                           calc_pp_lambda=calc_pp_lambda, pplmin_frac=minppl_frac, pplmax_frac=minppl_frac,
-                          mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth, instrumentName=instrumentName)
+                          mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth, instrumentName=instrumentName,
+                          peakMaskSize=peakMaskSize)
     fitResults, fICC = doICCFit(
         tofWS, energy, flightPath, padeCoefficients, fitOrder=1, constraintScheme=constraintScheme,
         instrumentName=instrumentName)
@@ -246,10 +247,6 @@ def getOptimizedGoodIDX(n_events, padeCoefficients, zBG=1.96, neigh_length_m=3,
     cY = nY//2
     cZ = nZ//2
     dP = peakMaskSize
-    if instrumentName == 'TOPAZ':
-        dP = 15
-    elif instrumentName == 'CORELLI':
-        dP = 10
 
     peakMask = qMask.copy()
     peakMask[cX-dP:cX+dP, cY-dP:cY+dP, cZ-dP:cZ+dP] = 0
@@ -286,7 +283,7 @@ def getOptimizedGoodIDX(n_events, padeCoefficients, zBG=1.96, neigh_length_m=3,
                 chiSq, h, intens, sigma = getQuickTOFWS(box, peak, padeCoefficients, goodIDX=goodIDX, qMask=qMask, pp_lambda=pp_lambda,
                                                         minppl_frac=minppl_frac, maxppl_frac=maxppl_frac, mindtBinWidth=mindtBinWidth,
                                                         maxdtBinWidth=maxdtBinWidth, constraintScheme=constraintScheme,
-                                                        instrumentName=instrumentName)
+                                                        instrumentName=instrumentName, peakMaskSize=peakMaskSize)
             except:
                 # raise
                 break
@@ -300,6 +297,7 @@ def getOptimizedGoodIDX(n_events, padeCoefficients, zBG=1.96, neigh_length_m=3,
         except RuntimeError:
             # This is caused by there being fewer datapoints remaining than parameters.  For now, we just hope
             # we found a satisfactory answer.
+            raise
             break
         except KeyboardInterrupt:
             sys.exit()
@@ -309,7 +307,7 @@ def getOptimizedGoodIDX(n_events, padeCoefficients, zBG=1.96, neigh_length_m=3,
     chiSq, h, intens, sigma = getQuickTOFWS(box, peak, padeCoefficients, goodIDX=goodIDX, qMask=qMask,
                                             pp_lambda=pp_lambda, minppl_frac=minppl_frac, maxppl_frac=maxppl_frac,
                                             mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                                            instrumentName=instrumentName)
+                                            instrumentName=instrumentName, peakMaskSize=peakMaskSize)
     if qMask is not None:
         return goodIDX*qMask, pp_lambda
     return goodIDX, pp_lambda
@@ -375,7 +373,8 @@ def getBGRemovedIndices(n_events, zBG=1.96, calc_pp_lambda=False, neigh_length_m
                                            minppl_frac=pplmin_frac, maxppl_frac=pplmax_frac, qMask=qMask, peak=peak,
                                            box=box, pp_lambda=pp_lambda, peakNumber=peakNumber,
                                            mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                                           constraintScheme=constraintScheme, instrumentName=instrumentName)
+                                           constraintScheme=constraintScheme, instrumentName=instrumentName,
+                                           peakMaskSize=peakMaskSize)
             except KeyboardInterrupt:
                 sys.exit()
             except:
@@ -535,7 +534,7 @@ def get_pp_lambda(n_events, hasEventsIDX):
 
 def getTOFWS(box, flightPath, scatteringHalfAngle, tofPeak, peak, qMask, zBG=-1.0, dtSpread=0.02,
              minFracPixels=0.005, workspaceNumber=None, neigh_length_m=0, pp_lambda=None, calc_pp_lambda=False,
-             padeCoefficients=None, pplmin_frac=0.8, pplmax_frac=1.5,
+             padeCoefficients=None, pplmin_frac=0.8, pplmax_frac=1.5, peakMaskSize=5,
              mindtBinWidth=1, maxdtBinWidth=50, constraintScheme=1, instrumentName=None):
     """
     Builds a TOF profile from the data in box which is nominally centered around a peak.
@@ -580,7 +579,8 @@ def getTOFWS(box, flightPath, scatteringHalfAngle, tofPeak, peak, qMask, zBG=-1.
                                                  calc_pp_lambda=calc_pp_lambda, padeCoefficients=padeCoefficients,
                                                  pplmin_frac=pplmin_frac, pplmax_frac=pplmax_frac,
                                                  mindtBinWidth=mindtBinWidth, maxdtBinWidth=maxdtBinWidth,
-                                                 constraintScheme=constraintScheme, instrumentName=instrumentName)
+                                                 constraintScheme=constraintScheme, instrumentName=instrumentName,
+                                                 peakMaskSize=peakMaskSize)
         hasEventsIDX = np.logical_and(goodIDX, qMask)
         boxMeanIDX = np.where(hasEventsIDX)
     else:  # don't do background removal - just consider one pixel at a time
@@ -908,7 +908,7 @@ def integrateSample(run, MDdata, peaks_ws, paramList, UBMatrix, dQ, qMask, padeC
                     dQPixel=0.005, p=None, neigh_length_m=0, zBG=-1.0, bgPolyOrder=1,
                     doIterativeBackgroundFitting=False, q_frame='sample',
                     progressFile=None, minpplfrac=0.8, maxpplfrac=1.5, mindtBinWidth=1, maxdtBinWidth=50,
-                    keepFitDict=False, constraintScheme=1):
+                    keepFitDict=False, constraintScheme=1, peakMaskSize=5):
     """
     integrateSample contains the loop that integrates over all of the peaks in a run and saves the results.  Importantly, it also handles
     errors (mostly by passing and recording special values for failed fits.)
@@ -984,7 +984,8 @@ def integrateSample(run, MDdata, peaks_ws, paramList, UBMatrix, dQ, qMask, padeC
                                                          mindtBinWidth=mindtBinWidth,
                                                          maxdtBinWidth=maxdtBinWidth,
                                                          pplmin_frac=minpplfrac, pplmax_frac=maxpplfrac,
-                                                         constraintScheme=constraintScheme, instrumentName=instrumentName)
+                                                         constraintScheme=constraintScheme, instrumentName=instrumentName,
+                                                         peakMaskSize=peakMaskSize)
                 # --IN PRINCIPLE!!! WE CALCULATE THIS BEFORE GETTING HERE
                 tofWS = mtd['tofWS']
 
-- 
GitLab