Skip to content
Snippets Groups Projects
Commit eea7fc81 authored by Harriet Brown's avatar Harriet Brown
Browse files

Add workflow algorithm for merging workspaces with limits

This commit creates a workflow algorithm named MergeWorkspacesWithLimits
that handles the merging of the focused workspaces in the polaris scripts.

This commit removes a duplicated _load_qlims function in polaris_algs mistakenly added in commit 4c588e.

This commit adds a black line at the end of the CalculateSelfScatteringCorrection algorithm.

Re #27445
parent c39025eb
No related branches found
No related tags found
No related merge requests found
...@@ -100,4 +100,4 @@ class CalculateSelfScatteringCorrection(DataProcessorAlgorithm): ...@@ -100,4 +100,4 @@ class CalculateSelfScatteringCorrection(DataProcessorAlgorithm):
# Register algorithm with Mantid # Register algorithm with Mantid
AlgorithmFactory.subscribe(CalculateSelfScatteringCorrection) AlgorithmFactory.subscribe(CalculateSelfScatteringCorrection)
\ No newline at end of file
# 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)
...@@ -104,7 +104,9 @@ def generate_ts_pdf(run_number, focus_file_path, merge_banks=False, q_lims=None, ...@@ -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) focused_ws = mantid.Subtract(LHSWorkspace=focused_ws, RHSWorkspace=self_scattering_correction)
if merge_banks: 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)", pdf_output = mantid.PDFFourierTransform(Inputworkspace=merged_ws, InputSofQType="S(Q)-1", PDFType="G(r)",
Filter=True) Filter=True)
else: else:
...@@ -142,7 +144,7 @@ def _obtain_focused_run(run_number, focus_file_path): ...@@ -142,7 +144,7 @@ def _obtain_focused_run(run_number, focus_file_path):
return focused_ws 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 min_x = np.inf
max_x = -np.inf max_x = -np.inf
num_x = -np.inf num_x = -np.inf
...@@ -167,7 +169,6 @@ def _merge_workspace_with_limits(focused_ws, q_lims): ...@@ -167,7 +169,6 @@ def _merge_workspace_with_limits(focused_ws, q_lims):
mantid.MatchSpectra(InputWorkspace=focused_ws_conjoined, OutputWorkspace=focused_ws_conjoined, mantid.MatchSpectra(InputWorkspace=focused_ws_conjoined, OutputWorkspace=focused_ws_conjoined,
ReferenceSpectrum=largest_range_spectrum) ReferenceSpectrum=largest_range_spectrum)
q_min, q_max = _load_qlims(q_lims)
bin_width = np.inf bin_width = np.inf
for i in range(q_min.size): for i in range(q_min.size):
pdf_x_array = focused_ws_conjoined.readX(i) pdf_x_array = focused_ws_conjoined.readX(i)
...@@ -211,29 +212,6 @@ def _load_qlims(q_lims): ...@@ -211,29 +212,6 @@ def _load_qlims(q_lims):
return q_min, q_max 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): def _calculate_self_scattering_correction(run_number, cal_file_name, sample_details):
raw_ws = mantid.Load(Filename='POLARIS'+str(run_number)+'.nxs') raw_ws = mantid.Load(Filename='POLARIS'+str(run_number)+'.nxs')
mantid.SetSample(InputWorkspace=raw_ws, mantid.SetSample(InputWorkspace=raw_ws,
......
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