diff --git a/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CalculateSelfScatteringCorrection.py b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CalculateSelfScatteringCorrection.py index 31e53298b45ef672074f304ca9631cb8bfdc0855..739a9629bfa605e0179085bfc7aa83a30067d0f2 100644 --- a/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CalculateSelfScatteringCorrection.py +++ b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CalculateSelfScatteringCorrection.py @@ -100,4 +100,4 @@ class CalculateSelfScatteringCorrection(DataProcessorAlgorithm): # Register algorithm with Mantid -AlgorithmFactory.subscribe(CalculateSelfScatteringCorrection) \ No newline at end of file +AlgorithmFactory.subscribe(CalculateSelfScatteringCorrection) diff --git a/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py new file mode 100644 index 0000000000000000000000000000000000000000..796200fb5f75ff844139426795e6152a2d6655ea --- /dev/null +++ b/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/MergeWorkspacesWithLimits.py @@ -0,0 +1,81 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source +# & Institut Laue - Langevin +# SPDX - License - Identifier: GPL - 3.0 + +from __future__ import (absolute_import, division, print_function) + +from mantid.simpleapi import (ConjoinWorkspaces, CropWorkspaceRagged, DeleteWorkspace, MatchSpectra, Rebin, SumSpectra) +from mantid.api import (DataProcessorAlgorithm, AlgorithmFactory, WorkspaceProperty, ) +from mantid.kernel import (Direction, FloatArrayProperty) +import numpy as np + + +class MergeWorkspaceWithLimits(DataProcessorAlgorithm): + + def category(self): + return "Workflow\\Diffraction" + + def seeAlso(self): + return [""] + + def summary(self): + return "Merges a group workspace using weighting from a set of range limits for each workspace." + + def checkGroups(self): + return False + + def PyInit(self): + self.declareProperty(WorkspaceProperty('WorkspaceGroup', '', direction=Direction.Input), + doc='Workspace group for merging') + self.declareProperty(WorkspaceProperty('MergedWorkspace', '', direction=Direction.Output), + doc='The weighted merged workspace') + self.declareProperty(FloatArrayProperty('XMin', [],), + doc='Array of minimum X values for each workspace') + self.declareProperty(FloatArrayProperty('XMax', [],), + doc='Array of maximum X values for each workspace') + + def PyExec(self): + ws_group = self.getProperty('WorkspaceGroup').value + x_min = self.getProperty('XMin').value + x_max = self.getProperty('XMax').value + min_x = np.inf + max_x = -np.inf + num_x = -np.inf + ws_max_range = 0 + largest_range_spectrum = 0 + for i in range(ws_group.size()): + x_data = ws_group[i].dataX(0) + min_x = min(np.min(x_data), min_x) + max_x = max(np.max(x_data), max_x) + num_x = max(x_data.size, num_x) + ws_range = np.max(x_data) - np.min(x_data) + if ws_range > ws_max_range: + largest_range_spectrum = i + 1 + ws_max_range = ws_range + ws_group = Rebin(InputWorkspace=ws_group, + Params=[min_x, (max_x - min_x) / num_x, max_x], + StoreInADS=False) + while ws_group.size() > 1: + ConjoinWorkspaces(InputWorkspace1=ws_group[0], + InputWorkspace2=ws_group[1]) + + ws_conjoined, offset, scale, chisq = MatchSpectra(InputWorkspace=ws_group[0], + ReferenceSpectrum=largest_range_spectrum) + bin_width = np.inf + for i in range(x_min.size): + pdf_x_array = ws_conjoined.readX(i) + x_min[i] = pdf_x_array[np.amin(np.where(pdf_x_array >= x_min[i]))] + x_max[i] = pdf_x_array[np.amax(np.where(pdf_x_array <= x_max[i]))] + bin_width = min(pdf_x_array[1] - pdf_x_array[0], bin_width) + ws_conjoined = CropWorkspaceRagged(InputWorkspace=ws_conjoined, XMin=x_min, XMax=x_max) + ws_conjoined = Rebin(InputWorkspace=ws_conjoined, Params=[min(x_min), bin_width, max(x_max)]) + merged_ws = SumSpectra(InputWorkspace=ws_conjoined, WeightedSum=True, MultiplyBySpectra=False, StoreInADS=False) + DeleteWorkspace(ws_group) + DeleteWorkspace(ws_conjoined) + self.setProperty('MergedWorkspace', merged_ws) + + +# Register algorithm with Mantid +AlgorithmFactory.subscribe(MergeWorkspaceWithLimits) diff --git a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py index 205097c5d707c6eee519d4e907a369387102f727..a1c4e1aa75bb4d00104476004db06bd378b668bf 100644 --- a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py +++ b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py @@ -104,7 +104,9 @@ def generate_ts_pdf(run_number, focus_file_path, merge_banks=False, q_lims=None, focused_ws = mantid.Subtract(LHSWorkspace=focused_ws, RHSWorkspace=self_scattering_correction) if merge_banks: - merged_ws = _merge_workspace_with_limits(focused_ws, q_lims) + q_min, q_max = _load_qlims(q_lims) + merged_ws = mantid.MergeWorkspaceWithLimits(focused_ws, q_min, q_max) + #merged_ws = _merge_workspace_with_limits(focused_ws, q_min, q_max) pdf_output = mantid.PDFFourierTransform(Inputworkspace=merged_ws, InputSofQType="S(Q)-1", PDFType="G(r)", Filter=True) else: @@ -142,7 +144,7 @@ def _obtain_focused_run(run_number, focus_file_path): return focused_ws -def _merge_workspace_with_limits(focused_ws, q_lims): +def _merge_workspace_with_limits(focused_ws, q_min, q_max): min_x = np.inf max_x = -np.inf num_x = -np.inf @@ -167,7 +169,6 @@ def _merge_workspace_with_limits(focused_ws, q_lims): mantid.MatchSpectra(InputWorkspace=focused_ws_conjoined, OutputWorkspace=focused_ws_conjoined, ReferenceSpectrum=largest_range_spectrum) - q_min, q_max = _load_qlims(q_lims) bin_width = np.inf for i in range(q_min.size): pdf_x_array = focused_ws_conjoined.readX(i) @@ -211,29 +212,6 @@ def _load_qlims(q_lims): return q_min, q_max -def _load_qlims(q_lims): - if type(q_lims) == str or type(q_lims) == unicode: - q_min = [] - q_max = [] - try: - with open(q_lims, 'r') as f: - line_list = [line.rstrip('\n') for line in f] - for line in line_list[1:]: - value_list = line.split() - q_min.append(float(value_list[2])) - q_max.append(float(value_list[3])) - q_min = np.array(q_min) - q_max = np.array(q_max) - except IOError: - raise RuntimeError("q_lims directory is not valid") - elif type(q_lims) == list or type(q_lims) == np.ndarray: - q_min = q_lims[0, :] - q_max = q_lims[1, :] - else: - raise RuntimeError("q_lims type is not valid") - return q_min, q_max - - def _calculate_self_scattering_correction(run_number, cal_file_name, sample_details): raw_ws = mantid.Load(Filename='POLARIS'+str(run_number)+'.nxs') mantid.SetSample(InputWorkspace=raw_ws,