Skip to content

Commit

Permalink
Move more common code to IndirectReductionCommon.py
Browse files Browse the repository at this point in the history
Refs #10855
  • Loading branch information
DanNixon committed May 18, 2015
1 parent fb4df43 commit f2bbec8
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 81 deletions.
Expand Up @@ -71,10 +71,13 @@ def validateInputs(self):

def PyExec(self):
from IndirectReductionCommon import (load_files,
get_multi_frame_rebin,
identify_bad_detectors,
unwrap_monitor,
process_monitor_efficiency,
scale_monitor,
scale_detectors,
rebin_reduction,
group_spectra,
fold_chopped,
rename_reduction)
Expand All @@ -96,15 +99,8 @@ def PyExec(self):
workspaces = [c_ws_name]

# Process rebinning for framed data
if self._rebin_string is not None and is_multi_frame:
rebin_string_comp = self._rebin_string.split(',')
if len(rebin_string_comp) >= 5:
rebin_string_2 = ','.join(rebin_string_comp[2:])
else:
rebin_string_2 = self._rebin_string

bin_counts = [mtd[ws].blocksize() for ws in mtd[c_ws_name].getNames()]
num_bins = np.amax(bin_counts)
rebin_string_2, num_bins = get_multi_frame_rebin(c_ws_name,
self._rebin_string)

masked_detectors = identify_bad_detectors(workspaces[0])

Expand All @@ -123,16 +119,7 @@ def PyExec(self):
scale_monitor(ws_name)

# Scale detector data by monitor intensities
ConvertUnits(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
Target='Wavelength',
EMode='Elastic')
RebinToWorkspace(WorkspaceToRebin=ws_name,
WorkspaceToMatch=monitor_ws_name,
OutputWorkspace=ws_name)
Divide(LHSWorkspace=ws_name,
RHSWorkspace=monitor_ws_name,
OutputWorkspace=ws_name)
scale_detectors(ws_name, 'Elastic')

# Remove the no longer needed monitor workspace
DeleteWorkspace(monitor_ws_name)
Expand All @@ -144,30 +131,10 @@ def PyExec(self):
EMode='Elastic')

# Handle rebinning
if self._rebin_string is not None:
if is_multi_frame:
# Mulit frame data
if mtd[ws_name].blocksize() == num_bins:
Rebin(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
Params=self._rebin_string)
else:
Rebin(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
Params=rebin_string_2)
else:
# Regular data
Rebin(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
Params=self._rebin_string)
else:
try:
# If user does not want to rebin then just ensure uniform binning across spectra
RebinToWorkspace(WorkspaceToRebin=ws_name,
WorkspaceToMatch=ws_name,
OutputWorkspace=ws_name)
except RuntimeError:
logger.warning('Rebinning failed, will try to continue anyway.')
rebin_reduction(ws_name,
self._rebin_string,
rebin_string_2,
num_bins)

# Group spectra
group_spectra(ws_name,
Expand Down
Expand Up @@ -5,7 +5,6 @@
from mantid import config

import os
import numpy as np


_str_or_none = lambda s: s if s != '' else None
Expand Down Expand Up @@ -73,10 +72,13 @@ def PyInit(self):

def PyExec(self):
from IndirectReductionCommon import (load_files,
get_multi_frame_rebin,
identify_bad_detectors,
unwrap_monitor,
process_monitor_efficiency,
scale_monitor,
scale_detectors,
rebin_reduction,
group_spectra,
fold_chopped,
rename_reduction,
Expand All @@ -100,15 +102,8 @@ def PyExec(self):
workspaces = [c_ws_name]

# Process rebinning for framed data
if self._rebin_string is not None and is_multi_frame:
rebin_string_comp = self._rebin_string.split(',')
if len(rebin_string_comp) >= 5:
rebin_string_2 = ','.join(rebin_string_comp[2:])
else:
rebin_string_2 = self._rebin_string

bin_counts = [mtd[ws].blocksize() for ws in mtd[c_ws_name].getNames()]
num_bins = np.amax(bin_counts)
rebin_string_2, num_bins = get_multi_frame_rebin(c_ws_name,
self._rebin_string)

masked_detectors = identify_bad_detectors(workspaces[0])

Expand All @@ -118,16 +113,19 @@ def PyExec(self):

# Process monitor
if not unwrap_monitor(ws_name):
ConvertUnits(InputWorkspace=monitor_ws_name, OutputWorkspace=monitor_ws_name, Target='Wavelength', EMode='Elastic')
ConvertUnits(InputWorkspace=monitor_ws_name,
OutputWorkspace=monitor_ws_name,
Target='Wavelength',
EMode='Elastic')

process_monitor_efficiency(ws_name)
scale_monitor(ws_name)


# Do background removal if a range was provided
if self._background_range is not None:
ConvertToDistribution(Workspace=ws_name)
CalculateFlatBackground(InputWorkspace=ws_name, OutputWorkspace=ws_name,
CalculateFlatBackground(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
StartX=self._background_range[0],
EndX=self._background_range[1],
Mode='Mean')
Expand All @@ -140,45 +138,41 @@ def PyExec(self):
OutputWorkspace=ws_name)

# Scale detector data by monitor intensities
ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='Wavelength', EMode='Indirect')
RebinToWorkspace(WorkspaceToRebin=ws_name, WorkspaceToMatch=monitor_ws_name, OutputWorkspace=ws_name)
Divide(LHSWorkspace=ws_name, RHSWorkspace=monitor_ws_name, OutputWorkspace=ws_name)
scale_detectors(ws_name, 'Indirect')

# Remove the no longer needed monitor workspace
DeleteWorkspace(monitor_ws_name)

# Convert to energy
ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='DeltaE', EMode='Indirect')
CorrectKiKf(InputWorkspace=ws_name, OutputWorkspace=ws_name, EMode='Indirect')
ConvertUnits(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
Target='DeltaE',
EMode='Indirect')
CorrectKiKf(InputWorkspace=ws_name,
OutputWorkspace=ws_name,
EMode='Indirect')

# Handle rebinning
if self._rebin_string is not None:
if is_multi_frame:
# Mulit frame data
if mtd[ws_name].blocksize() == num_bins:
Rebin(InputWorkspace=ws_name, OutputWorkspace=ws_name, Params=self._rebin_string)
else:
Rebin(InputWorkspace=ws_name, OutputWorkspace=ws_name, Params=rebin_string_2)
else:
# Regular data
Rebin(InputWorkspace=ws_name, OutputWorkspace=ws_name, Params=self._rebin_string)
else:
try:
# If user does not want to rebin then just ensure uniform binning across spectra
RebinToWorkspace(WorkspaceToRebin=ws_name, WorkspaceToMatch=ws_name, OutputWorkspace=ws_name)
except RuntimeError:
logger.warning('Rebinning failed, will try to continue anyway.')
rebin_reduction(ws_name,
self._rebin_string,
rebin_string_2,
num_bins)

# Detailed balance
if self._detailed_balance is not None:
corr_factor = 11.606 / (2 * self._detailed_balance)
ExponentialCorrection(InputWorkspaces=ws_name, OutputWorkspace=ws_name,
C0=1.0, C1=corr_factor, Operation='Multiply')
ExponentialCorrection(InputWorkspaces=ws_name,
OutputWorkspace=ws_name,
C0=1.0,
C1=corr_factor,
Operation='Multiply')

# Scale
if self._scale_factor != 1.0:
Scale(InputWorkspaces=ws_name, OutputWorkspace=ws_name,
Factor=self._scale_factor, Operation='Multiply')
Scale(InputWorkspaces=ws_name,
OutputWorkspace=ws_name,
Factor=self._scale_factor,
Operation='Multiply')

# Group spectra
group_spectra(ws_name,
Expand Down
92 changes: 92 additions & 0 deletions Code/Mantid/scripts/Inelastic/IndirectReductionCommon.py
Expand Up @@ -3,6 +3,7 @@
from mantid import mtd, logger, config

import os
import numpy as np

#-------------------------------------------------------------------------------

Expand Down Expand Up @@ -348,6 +349,32 @@ def scale_monitor(workspace_name):

#-------------------------------------------------------------------------------

def scale_detectors(workspace_name, e_mode='Indirect'):
"""
Scales detectors by monitor intnesity.
@param workspace_name Name of detector workspace
@param e_mode Energy mode (Indirect for spectroscopy, Elastic for diffraction)
"""
from mantid.simpleapi import (ConvertUnits, RebinToWorkspace, Divide)

monitor_workspace_name = workspace_name + '_mon'

ConvertUnits(InputWorkspace=workspace_name,
OutputWorkspace=workspace_name,
Target='Wavelength',
EMode=e_mode)

RebinToWorkspace(WorkspaceToRebin=workspace_name,
WorkspaceToMatch=monitor_workspace_name,
OutputWorkspace=workspace_name)

Divide(LHSWorkspace=workspace_name,
RHSWorkspace=monitor_workspace_name,
OutputWorkspace=workspace_name)

#-------------------------------------------------------------------------------

def group_spectra(workspace_name, masked_detectors, method, group_file=None, group_ws=None):
"""
Groups spectra in a given workspace according to the Workflow.GroupingMethod and
Expand Down Expand Up @@ -648,3 +675,68 @@ def save_reduction(worksspace_names, formats, x_units='DeltaE'):
DeleteWorkspace(Workspace=workspace_name + '_davegrp_save_temp')

#-------------------------------------------------------------------------------

def get_multi_frame_rebin(workspace_name, rebin_string):
"""
Creates a rebin string for rebinning multiple frames data.
@param workspace_name Name of multiple frame workspace group
@param rebin_string Original rebin string
@return New rebin string
@return Maximum number of bins in input workspaces
"""

multi_frame = isinstance(mtd[workspace_name], WorkspaceGroup)

if rebin_string is not None and multi_frame:
rebin_string_comp = rebin_string.split(',')
if len(rebin_string_comp) >= 5:
rebin_string_2 = ','.join(rebin_string_comp[2:])
else:
rebin_string_2 = rebin_string

bin_counts = [mtd[ws].blocksize() for ws in mtd[workspace_name].getNames()]
num_bins = np.amax(bin_counts)

return rebin_string_2, num_bins

return None, None

#-------------------------------------------------------------------------------

def rebin_reduction(workspace_name, rebin_string, multi_frame_rebin_string, num_bins):
"""
@param workspace_name Name of workspace to rebin
@param rebin_string Rebin parameters
@param multi_frame_rebin_string Rebin string for multiple frame rebinning
@param num_bins Max number of bins in input frames
"""
from mantid.simpleapi import (Rebin, RebinToWorkspace)

if rebin_string is not None:
if multi_frame_rebin_string is not None and num_bins is not None:
# Multi frame data
if mtd[workspace_name].blocksize() == num_bins:
Rebin(InputWorkspace=workspace_name,
OutputWorkspace=workspace_name,
Params=rebin_string)
else:
Rebin(InputWorkspace=workspace_name,
OutputWorkspace=workspace_name,
Params=multi_frame_rebin_string)
else:
# Regular data
Rebin(InputWorkspace=workspace_name,
OutputWorkspace=workspace_name,
Params=rebin_string)
else:
try:
# If user does not want to rebin then just ensure uniform binning across spectra
RebinToWorkspace(WorkspaceToRebin=workspace_name,
WorkspaceToMatch=workspace_name,
OutputWorkspace=workspace_name)
except RuntimeError:
logger.warning('Rebinning failed, will try to continue anyway.')

#-------------------------------------------------------------------------------

0 comments on commit f2bbec8

Please sign in to comment.