Skip to content

Commit

Permalink
Update SNSPowderReduction to be able to use new compression options
Browse files Browse the repository at this point in the history
  • Loading branch information
rosswhitfield committed Jun 14, 2024
1 parent 2e5c282 commit 29e7353
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
37 changes: 27 additions & 10 deletions Framework/PythonInterface/plugins/algorithms/SNSPowderReduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def getBasename(filename):

class SNSPowderReduction(DataProcessorAlgorithm):
COMPRESS_TOL_TOF = 0.01
_compressBinningMode = None
_resampleX = None
_binning = None
_bin_in_dspace = None
Expand Down Expand Up @@ -325,6 +326,7 @@ def PyInit(self):
self.declareProperty("NormalizeByCurrent", True, "Normalize by current")

self.declareProperty("CompressTOFTolerance", 0.01, "Tolerance to compress events in TOF.")
self.copyProperties("AlignAndFocusPowderFromFiles", ["CompressBinningMode"])

# reduce logs being loaded
self.declareProperty(
Expand Down Expand Up @@ -455,10 +457,9 @@ def PyExec(self): # noqa

self._normalisebycurrent = self.getProperty("NormalizeByCurrent").value

# Tolerance for compress TOF event. If given a negative value, then use default 0.01
# Tolerance for compress TOF event.
self.COMPRESS_TOL_TOF = float(self.getProperty("CompressTOFTolerance").value)
if self.COMPRESS_TOL_TOF < -0.0:
self.COMPRESS_TOL_TOF = 0.01
self._compressBinningMode = self.getProperty("CompressBinningMode").value

# Clean the cache directory if so requested
if self._clean_cache:
Expand Down Expand Up @@ -637,8 +638,13 @@ def PyExec(self): # noqa
api.Scale(InputWorkspace=can_run_ws_name, OutputWorkspace=can_run_ws_name, Factor=self._containerScaleFactor)
api.Minus(LHSWorkspace=sam_ws_name, RHSWorkspace=can_run_ws_name, OutputWorkspace=sam_ws_name)
# compress event if the sample run workspace is EventWorkspace
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF > 0.0:
api.CompressEvents(InputWorkspace=sam_ws_name, OutputWorkspace=sam_ws_name, Tolerance=self.COMPRESS_TOL_TOF) # 10ns
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF != 0.0:
api.CompressEvents(
InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name,
Tolerance=self.COMPRESS_TOL_TOF,
BinningMode=self._compressBinningMode,
) # 10ns
# canRun = str(canRun)

if van_run_ws_name is not None:
Expand All @@ -659,8 +665,13 @@ def PyExec(self): # noqa
normalized = False

# Compress the event again
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF > 0.0:
api.CompressEvents(InputWorkspace=sam_ws_name, OutputWorkspace=sam_ws_name, Tolerance=self.COMPRESS_TOL_TOF) # 5ns/
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF != 0.0:
api.CompressEvents(
InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name,
Tolerance=self.COMPRESS_TOL_TOF,
BinningMode=self._compressBinningMode,
) # 5ns

if self._scaleFactor != 1.0:
api.Scale(sam_ws_name, Factor=self._scaleFactor, OutputWorkspace=sam_ws_name)
Expand Down Expand Up @@ -905,6 +916,7 @@ def _focusAndSum(self, filenames, preserveEvents=True, final_name=None, absorpti
PreserveEvents=preserveEvents,
RemovePromptPulseWidth=self._removePromptPulseWidth,
CompressTolerance=self.COMPRESS_TOL_TOF,
CompressBinningMode=self._compressBinningMode,
LorentzCorrection=self._lorentz,
UnwrapRef=self._LRef,
LowResRef=self._DIFCref,
Expand Down Expand Up @@ -1019,6 +1031,7 @@ def _focusChunks(self, filename, filter_wall=(0.0, 0.0), splitwksp=None, preserv
PreserveEvents=preserveEvents,
RemovePromptPulseWidth=self._removePromptPulseWidth,
CompressTolerance=self.COMPRESS_TOL_TOF,
CompressBinningMode=self._compressBinningMode,
LorentzCorrection=self._lorentz,
UnwrapRef=self._LRef,
LowResRef=self._DIFCref,
Expand Down Expand Up @@ -1081,11 +1094,12 @@ def _focusChunks(self, filename, filter_wall=(0.0, 0.0), splitwksp=None, preserv

# Compress events
for split_index in range(num_out_wksp):
if is_event_workspace(output_wksp_list[split_index]) and self.COMPRESS_TOL_TOF > 0.0:
if is_event_workspace(output_wksp_list[split_index]) and self.COMPRESS_TOL_TOF != 0.0:
api.CompressEvents(
InputWorkspace=output_wksp_list[split_index],
OutputWorkspace=output_wksp_list[split_index],
Tolerance=self.COMPRESS_TOL_TOF,
BinningMode=self._compressBinningMode,
) # 100ns
try:
if self._normalisebycurrent is True:
Expand Down Expand Up @@ -1580,9 +1594,12 @@ def _process_vanadium_runs(self, van_run_number_list, samRunIndex, **dummy_focus
)

# compress events
if is_event_workspace(van_run_ws_name) and self.COMPRESS_TOL_TOF > 0.0:
if is_event_workspace(van_run_ws_name) and self.COMPRESS_TOL_TOF != 0.0:
api.CompressEvents(
InputWorkspace=van_run_ws_name, OutputWorkspace=van_run_ws_name, Tolerance=self.COMPRESS_TOL_TOF
InputWorkspace=van_run_ws_name,
OutputWorkspace=van_run_ws_name,
Tolerance=self.COMPRESS_TOL_TOF,
BinningMode=self._compressBinningMode,
) # 10ns
except RuntimeError as e:
self.log().warning("Failed to process vanadium background. Skipping: {}".format(e))
Expand Down
63 changes: 63 additions & 0 deletions Testing/SystemTests/tests/framework/SNSPowderRedux.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,66 @@ def runTest(self):

# Check volume using height value from log - pi*(r^2)*h, r and h in meters
assert mtd["PG3_46577"].sample().getShape().volume() == np.pi * np.square(0.00295) * 0.020


class LogarithmicTest(systemtesting.MantidSystemTest):
cal_file = "PG3_FERNS_d4832_2011_08_24.cal"
char_file = "PG3_characterization_2012_02_23-HR-ILL.txt"
ref_file = "PG3_9829_sum_reference.gsa"
data_file = "PG3_9829_event.nxs"
expected_ws_name = "sns_powder_expected"

def cleanup(self):
return True

def requiredMemoryMB(self):
"""Requires 2Gb"""
return 2000

def requiredFiles(self):
files = [self.cal_file, self.char_file, self.ref_file, self.data_file]
return files

def runTest(self):
savedir = getSaveDir()

results = PDLoadCharacterizations(Filename=self.char_file, OutputWorkspace="characterizations")

AlignAndFocusPowderFromFiles(
Filename=self.data_file,
FilterBadPulses=95.0,
OutputWorkspace=self.expected_ws_name,
Characterizations="characterizations",
CalFileName=self.cal_file,
Params=0.01,
CompressTolerance=-0.001,
CompressBinningMode="Logarithmic",
PrimaryFlightPath=results.PrimaryFlightPath,
SpectrumIDs=results.SpectrumIDs,
L2=results.L2,
Polar=results.Polar,
Azimuthal=results.Azimuthal,
)
CompressEvents(
InputWorkspace=self.expected_ws_name, OutputWorkspace=self.expected_ws_name, Tolerance=-0.001, BinningMode="Logarithmic"
)
ConvertUnits(InputWorkspace=self.expected_ws_name, OutputWorkspace=self.expected_ws_name, Target="dSpacing")

SNSPowderReduction(
Filename="PG3_9829",
VanadiumNumber=-1,
CalibrationFile=self.cal_file,
CharacterizationRunsFile=self.char_file,
Binning=0.01,
CompressTOFTolerance=-0.001,
CompressBinningMode="Logarithmic",
OutputDirectory=savedir,
NormalizeByCurrent=False,
)

def validateMethod(self):
self.tolerance = 1.0e-5
return "ValidateWorkspaceToWorkspace"

def validate(self):
return ("PG3_9829", self.expected_ws_name)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- :ref:`SNSPowderReduction <algm-SNSPowderReduction>` has been updated to take advantage of new capabilities in :ref:`AlignAndFocusPowderFromFiles <algm-AlignAndFocusPowderFromFiles>` and :ref:`AlignAndFocusPowder <algm-AlignAndFocusPowder>`

0 comments on commit 29e7353

Please sign in to comment.