Skip to content
Snippets Groups Projects
Commit 20a8c39f authored by David Fairbrother's avatar David Fairbrother
Browse files

Re #17949 Combine common elements of focus processing

parent 70ff568d
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ from isis_powder import calibrate
@add_metaclass(ABCMeta)
class AbstractInst(object):
def __init__(self, user_name=None, calibration_dir=None, raw_data_dir=None, output_dir=None,
default_input_ext=".raw", tt_mode=""):
default_input_ext=".raw"):
# ----- Properties common to ALL instruments -------- #
if user_name is None:
raise ValueError("A user name must be specified")
......@@ -24,7 +24,6 @@ class AbstractInst(object):
self._raw_data_dir = raw_data_dir
self._output_dir = output_dir
self._default_input_ext = _append_dot_to_ext(default_input_ext)
self._tt_mode = tt_mode
self._focus_mode = None
@property
......@@ -47,10 +46,6 @@ class AbstractInst(object):
def default_input_ext(self, new_ext):
self._default_input_ext = _append_dot_to_ext(new_ext)
@property
def tt_mode(self):
return self._tt_mode
@property
def focus_mode(self):
return self._focus_mode
......@@ -145,15 +140,6 @@ class AbstractInst(object):
"""
pass
@abstractmethod
def _get_focus_tof_binning(self):
"""
Returns the TOF binning values
@param self: The instrument to get TOF binning values for
@return: TOF binning Values
"""
pass
@abstractmethod
def _get_create_van_tof_binning(self):
"""
......@@ -273,16 +259,13 @@ class AbstractInst(object):
def _do_tof_rebinning_focus(self, input_workspace):
return input_workspace
def _focus_processing(self, run_number, input_workspace, perform_vanadium_norm):
return _empty_hook_return_none()
def _process_focus_output(self, processed_spectra, run_number, attenuate=False):
return _empty_hook_return_none()
def _subtract_sample_empty(self, input_sample):
return input_sample
def _apply_solid_angle_efficiency_corr(self, ws_to_correct, vanadium_number):
def _apply_solid_angle_efficiency_corr(self, ws_to_correct, vanadium_number=None, vanadium_path=None):
return ws_to_correct
def _load_monitor(self, number, cycle):
......@@ -297,9 +280,14 @@ class AbstractInst(object):
def _calibration_rebin_to_workspace(self, ws_to_rebin, ws_to_match):
return ws_to_rebin
def correct_sample_vanadium(self, focused_ws, index, vanadium_ws=None):
raise NotImplementedError("Cannot process the sample with a vanadium run for this instrument")
# ----- Private Implementation ----- #
# These should only be called by the abstract instrument class
def _append_dot_to_ext(ext):
if not ext.startswith('.'):
return '.' + ext
......
......@@ -68,7 +68,7 @@ def _apply_absorb_corrections(instrument, calibration_full_paths, corrected_van_
else:
absorb_ws = mantid.LoadNexus(Filename=calibration_full_paths["vanadium_absorption"])
# PEARL rebins whilst POLARIS does not
# PEARL rebins whilst POLARIS does not as some of the older absorption files have different number of bins
corrected_van_ws = instrument._calibration_rebin_to_workspace(ws_to_rebin=corrected_van_ws, ws_to_match=absorb_ws)
corrected_van_ws = mantid.Divide(LHSWorkspace=corrected_van_ws, RHSWorkspace=absorb_ws)
corrected_van_ws = mantid.ConvertUnits(InputWorkspace=corrected_van_ws, Target="dSpacing")
......
......@@ -14,26 +14,27 @@ def _run_focus(instrument, run_number, perform_attenuation, perform_vanadium_nor
read_ws = common._load_current_normalised_ws(number=run_number, instrument=instrument)
input_workspace = instrument._do_tof_rebinning_focus(read_ws) # Rebins for PEARL
# Align / Focus
cycle_information = instrument._get_cycle_information(run_number=run_number)
calibration_file_paths = instrument._get_calibration_full_paths(cycle=cycle_information["cycle"])
# Compensate for empty sample if specified
input_workspace = instrument._subtract_sample_empty(input_workspace)
# TODO this prints a warnings about detectors being incorrect on POLARIS
# Align / Focus
input_workspace = mantid.AlignDetectors(InputWorkspace=input_workspace,
CalibrationFile=calibration_file_paths["calibration"])
# TODO fix this - maybe have it save solid angle corrections and just load/apply
input_workspace = instrument._apply_solid_angle_efficiency_corr(ws_to_correct=input_workspace,
vanadium_number=calibration_file_paths["vanadium"])
vanadium_path=calibration_file_paths["vanadium"])
input_workspace = mantid.DiffractionFocussing(InputWorkspace=input_workspace,
GroupingFileName=calibration_file_paths["grouping"])
# Process
calibrated_spectra = instrument._focus_processing(run_number, input_workspace, perform_vanadium_norm)
calibrated_spectra = _divide_sample_by_vanadium(instrument=instrument, run_number=run_number,
input_workspace=input_workspace,
perform_vanadium_norm=perform_vanadium_norm)
common.remove_intermediate_workspace(read_ws)
common.remove_intermediate_workspace(input_workspace)
......@@ -45,3 +46,26 @@ def _run_focus(instrument, run_number, perform_attenuation, perform_vanadium_nor
common.remove_intermediate_workspace(ws)
return processed_nexus_files
def _divide_sample_by_vanadium(instrument, run_number, input_workspace, perform_vanadium_norm):
processed_spectra = []
cycle_information = instrument._get_cycle_information(run_number=run_number)
input_file_paths = instrument._get_calibration_full_paths(cycle=cycle_information["cycle"])
alg_range, save_range = instrument._get_instrument_alg_save_ranges(cycle_information["instrument_version"])
for index in range(0, alg_range):
if perform_vanadium_norm:
vanadium_ws = mantid.LoadNexus(Filename=input_file_paths["vanadium"], EntryNumber=index + 1)
processed_spectra.append(
instrument.correct_sample_vanadium(focused_ws=input_workspace, index=index, vanadium_ws=vanadium_ws))
common.remove_intermediate_workspace(vanadium_ws)
else:
processed_spectra.append(
instrument.calc_calibration_without_vanadium(focused_ws=input_workspace, index=index))
return processed_spectra
\ No newline at end of file
......@@ -31,7 +31,9 @@ class Pearl(AbstractInst):
input_file_ext=".raw", tt_mode="TT88"):
super(Pearl, self).__init__(user_name=user_name, calibration_dir=calibration_dir, raw_data_dir=raw_data_dir,
output_dir=output_dir, default_input_ext=input_file_ext, tt_mode=tt_mode)
output_dir=output_dir, default_input_ext=input_file_ext)
self._tt_mode = tt_mode
# This advanced option disables appending the current cycle to the
# path given for raw files.
......@@ -56,9 +58,6 @@ class Pearl(AbstractInst):
def _get_lambda_range(self):
return self._lambda_lower, self._lambda_upper
def _get_focus_tof_binning(self):
return self._focus_tof_binning
def _get_create_van_tof_binning(self):
return_dict = {"1": self._create_van_first_tof_binning,
"2": self._create_van_second_tof_binning}
......@@ -69,7 +68,7 @@ class Pearl(AbstractInst):
def _get_calibration_full_paths(self, cycle):
calibration_file, grouping_file, van_absorb, van_file =\
pearl_calib_factory.get_calibration_filename(cycle=cycle, tt_mode=self.tt_mode)
pearl_calib_factory.get_calibration_filename(cycle=cycle, tt_mode=self._tt_mode)
calibration_dir = self.calibration_dir
......@@ -234,6 +233,27 @@ class Pearl(AbstractInst):
ws_to_rebin = rebinned_ws
return ws_to_rebin
def correct_sample_vanadium(self, focused_ws, index, vanadium_ws=None):
data_ws = mantid.ExtractSingleSpectrum(InputWorkspace=focused_ws, WorkspaceIndex=index)
data_ws = mantid.ConvertUnits(InputWorkspace=data_ws, Target="TOF")
data_ws = mantid.Rebin(InputWorkspace=data_ws, Params=self._focus_tof_binning)
if vanadium_ws:
data_processed = "van_processed" + str(index) # Workaround for Mantid overwriting the WS in a loop
vanadium_ws = mantid.Rebin(InputWorkspace=vanadium_ws, Params=self._focus_tof_binning)
data_ws = mantid.Divide(LHSWorkspace=data_ws, RHSWorkspace=vanadium_ws, OutputWorkspace=data_processed)
else:
data_processed = "processed-" + str(index)
mantid.CropWorkspace(InputWorkspace=data_ws, XMin=0.1, OutputWorkspace=data_processed)
if vanadium_ws:
mantid.Scale(InputWorkspace=data_processed, Factor=10, OutputWorkspace=data_processed)
common.remove_intermediate_workspace(data_ws)
return data_processed
# Implementation of instrument specific steps
def _run_attenuate_workspace(self, input_workspace):
......@@ -334,29 +354,6 @@ class Pearl(AbstractInst):
mspectra = 1
return mspectra
def _perform_focus_loading(self, run_number, input_workspace, perform_vanadium_norm):
processed_spectra = []
cycle_information = self._get_cycle_information(run_number=run_number)
input_file_paths = self._get_calibration_full_paths(cycle=cycle_information["cycle"])
alg_range, save_range = self._get_instrument_alg_save_ranges(cycle_information["instrument_version"])
for index in range(0, alg_range):
if perform_vanadium_norm:
vanadium_ws = mantid.LoadNexus(Filename=input_file_paths["vanadium"], EntryNumber=index + 1)
van_rebinned = mantid.Rebin(InputWorkspace=vanadium_ws, Params=self._get_focus_tof_binning())
processed_spectra.append(calc_calibration_with_vanadium(input_workspace, index, van_rebinned,
tof_binning=self._focus_tof_binning))
common.remove_intermediate_workspace(vanadium_ws)
common.remove_intermediate_workspace(van_rebinned)
else:
processed_spectra.append(calc_calibration_without_vanadium(input_workspace, index))
return processed_spectra
# Support for old API - This can be removed when PEARL_routines is removed
def _old_api_constructor_set(self, user_name=None, calibration_dir=None, raw_data_dir=None, output_dir=None,
input_file_ext=None, tt_mode=None):
......@@ -399,6 +396,9 @@ class Pearl(AbstractInst):
def _PEARL_filename_is_full_path(self):
return self._old_api_uses_full_paths
def _get_focus_tof_binning(self):
return self._focus_tof_binning
def _old_api_get_file_name(in_path):
# Gets the filename from a full path
......@@ -519,29 +519,3 @@ def _spline_old_instrument_background(in_workspace):
WorkspaceIndex=i, NCoeff=coeff))
common.remove_intermediate_workspace(van_stripped)
return splined_ws_list
def calc_calibration_without_vanadium(focused_ws, index, instrument):
focus_spectrum = mantid.ExtractSingleSpectrum(InputWorkspace=focused_ws, WorkspaceIndex=index)
focus_spectrum = mantid.ConvertUnits(InputWorkspace=focus_spectrum, Target="TOF")
focus_spectrum = mantid.Rebin(InputWorkspace=focus_spectrum, Params=instrument.tof_binning)
focus_calibrated = mantid.CropWorkspace(InputWorkspace=focus_spectrum, XMin=0.1)
return focus_calibrated
def calc_calibration_with_vanadium(focused_ws, index, vanadium_ws, tof_binning):
data_ws = mantid.ExtractSingleSpectrum(InputWorkspace=focused_ws, WorkspaceIndex=index)
data_ws = mantid.ConvertUnits(InputWorkspace=data_ws, Target="TOF")
data_ws = mantid.Rebin(InputWorkspace=data_ws, Params=tof_binning)
data_processed = "van_processed" + str(index) # Workaround for Mantid overwriting the WS in a loop
mantid.Divide(LHSWorkspace=data_ws, RHSWorkspace=vanadium_ws, OutputWorkspace=data_processed)
mantid.CropWorkspace(InputWorkspace=data_processed, XMin=0.1, OutputWorkspace=data_processed)
mantid.Scale(InputWorkspace=data_processed, Factor=10, OutputWorkspace=data_processed)
common.remove_intermediate_workspace(data_ws)
return data_processed
from __future__ import (absolute_import, division, print_function)
import mantid.simpleapi as mantid
import os
from isis_powder.abstract_inst import AbstractInst
from isis_powder.polaris_routines import polaris_calib_factory
......@@ -24,7 +23,7 @@ class Polaris(AbstractInst):
input_file_ext=".raw", sample_empty_name=None): # TODO move TT_mode PEARL specific
super(Polaris, self).__init__(user_name=user_name, calibration_dir=calibration_dir, raw_data_dir=raw_data_dir,
output_dir=output_dir, default_input_ext=input_file_ext, tt_mode=None)
output_dir=output_dir, default_input_ext=input_file_ext)
self._masking_file_name = "VanaPeaks.dat"
self._sample_empty = sample_empty_name
......@@ -116,8 +115,14 @@ class Polaris(AbstractInst):
common.remove_intermediate_workspace(empty_sample)
return input_sample
def _apply_solid_angle_efficiency_corr(self, ws_to_correct, vanadium_number):
solid_angle_vanadium_ws = common._load_raw_files(run_number=vanadium_number, instrument=self)
def _apply_solid_angle_efficiency_corr(self, ws_to_correct, vanadium_number=None, vanadium_path=None):
assert(vanadium_number or vanadium_path)
if vanadium_number:
solid_angle_vanadium_ws = common._load_raw_files(run_number=vanadium_number, instrument=self)
else:
solid_angle_vanadium_ws = mantid.Load(Filename=vanadium_path)
solid_angle_vanadium_ws = self._normalise_ws(solid_angle_vanadium_ws)
corrections = self._calculate_solid_angle_efficiency_corrections(solid_angle_vanadium_ws)
......@@ -129,7 +134,9 @@ class Polaris(AbstractInst):
return ws_to_correct
def _focus_processing(self, run_number, input_workspace, perform_vanadium_norm):
raise NotImplementedError("Need to do focus processing")
self._get_cycle_information(run_number=run_number)
def _spline_background(self, focused_vanadium_ws, spline_number, instrument_version=''):
......@@ -245,3 +252,6 @@ def _apply_masking(workspaces_to_mask, mask_list):
index += 1
return output_workspaces
def _divide_sample_vanadium_splines(sample_ws, vanadium_splines_ws)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment