Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/8505_slicing'
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterParker committed Dec 4, 2013
2 parents 6857b84 + 74cadec commit 5935a22
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 48 deletions.
31 changes: 22 additions & 9 deletions Code/Mantid/scripts/SANS/SANSUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def getBinsBoundariesFromWorkspace(ws_reference):
return binning

def loadMonitorsFromFile(fileName, monitor_ws_name='monitor_ws'):
monitor = LoadNexus(fileName, SpectrumMax=8, OutputWorkspace=monitor_ws_name)
monitor = LoadNexusMonitors(fileName, OutputWorkspace=monitor_ws_name)
return monitor

def getFilePathFromWorkspace(ws):
Expand All @@ -432,19 +432,32 @@ def getFilePathFromWorkspace(ws):
raise RuntimeError("Can not find the file name for workspace " + str(ws))
return file_path

def getMonitor4event(ws_event):
file_path = getFilePathFromWorkspace(ws_event)
ws_monitor = loadMonitorsFromFile(file_path)
return ws_monitor

def fromEvent2Histogram(ws_event, ws_monitor = None):
if not ws_monitor:
file_path = getFilePathFromWorkspace(ws_event)
ws_monitor = loadMonitorsFromFile(file_path)
"""Transform an event mode workspace into a histogram workspace.
It does conjoin the monitor and the workspace as it is expected from the current
SANS data inside ISIS.
Finally, it copies the parameter map from the workspace to the resulting histogram
in order to preserve the positions of the detectors components inside the workspace.
bins_option = getBinsBoundariesFromWorkspace(ws_monitor)
It will finally, replace the input workspace with the histogram equivalent workspace.
"""
if not ws_monitor:
ws_monitor = getMonitor4event(ws_event)

aux_hist = Rebin(ws_event, bins_option, False)
aux_hist = RebinToWorkspace(ws_event, ws_monitor, False)

monitor_ws_name = ws_monitor.name()
ConjoinWorkspaces(ws_monitor, aux_hist, CheckOverlapping=True)
name = '__monitor_tmp'
ws_monitor.clone(OutputWorkspace=name)
ConjoinWorkspaces(name, aux_hist, CheckOverlapping=True)
CopyInstrumentParameters(ws_event, OutputWorkspace=name)

ws_hist = RenameWorkspace(monitor_ws_name, OutputWorkspace=str(ws_event))
ws_hist = RenameWorkspace(name, OutputWorkspace=str(ws_event))

return ws_hist

Expand Down
18 changes: 16 additions & 2 deletions Code/Mantid/scripts/SANS/isis_reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import reduction.instruments.sans.sans_reduction_steps as sans_reduction_steps
import isis_reduction_steps
from mantid.simpleapi import *
from mantid.api import IEventWorkspace
import os
import copy

Expand Down Expand Up @@ -109,7 +110,8 @@ def _to_steps(self):
"""
Defines the steps that are run and their order
"""
proc_TOF = [self.crop_detector]
proc_TOF = [self.event2hist]
proc_TOF.append(self.crop_detector)
proc_TOF.append(self.mask)
proc_TOF.append(self.to_wavelen)

Expand Down Expand Up @@ -138,7 +140,7 @@ def _init_steps(self):
self._out_name = isis_reduction_steps.GetOutputName()

#except self.prep_normalize all the steps below are used by the reducer
self.crop_detector = isis_reduction_steps.CropDetBank(crop_sample=True)
self.crop_detector = isis_reduction_steps.CropDetBank()
self.mask =self._mask= isis_reduction_steps.Mask_ISIS()
self.to_wavelen = isis_reduction_steps.UnitsConvert('Wavelength')
self.norm_mon = isis_reduction_steps.NormalizeToMonitor()
Expand Down Expand Up @@ -167,6 +169,7 @@ def _clean_loaded_data(self):
self._can_run = Can()
self.samp_trans_load = None
self.can_trans_load = None
self.event2hist = isis_reduction_steps.SliceEvent()

def __init__(self):
SANSReducer.__init__(self)
Expand Down Expand Up @@ -228,6 +231,13 @@ def get_sample(self):
else:
return self.get_can()

def get_monitor(self, index):
_ws = mtd[self.get_sample().wksp_name]
if isinstance(_ws, IEventWorkspace):
_ws = mtd[self.event2hist.monitor]
__MonitorWs = ExtractSingleSpectrum(_ws, index)
return __MonitorWs

def get_transmissions(self):
""" Get the transmission and direct workspace if they were given
for the can and for the sample"""
Expand Down Expand Up @@ -311,6 +321,10 @@ def _reduce(self, init=True, post=True, steps=None):

if not steps:
steps = self._reduction_steps

# create the workspace that will be used throughout the reduction
CloneWorkspace(self.get_sample().wksp_name, OutputWorkspace=self.output_wksp)

#the main part of the reduction is done here, go through and execute each step
for item in steps:
if item:
Expand Down
74 changes: 37 additions & 37 deletions Code/Mantid/scripts/SANS/isis_reduction_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
sanslog = sans_reduction_steps.sanslog

from mantid.simpleapi import *
from mantid.api import WorkspaceGroup, Workspace
from mantid.api import WorkspaceGroup, Workspace, IEventWorkspace
from SANSUtility import (GetInstrumentDetails, MaskByBinRange,
isEventWorkspace, fromEvent2Histogram,
getFilePathFromWorkspace, getWorkspaceReference)
getFilePathFromWorkspace, getWorkspaceReference,
getMonitor4event)
import isis_instrument
import os
import math
Expand Down Expand Up @@ -97,24 +98,20 @@ def _load(self, inst = None, is_can=False, extra_options=dict()):
"""
if self._period != self.UNSET_PERIOD:
workspace = self._get_workspace_name(self._period)
extra_options['EntryNumber'] = self._period
else:
workspace = self._get_workspace_name()

if os.path.splitext(self._data_file)[1].lower().startswith('.r') or os.path.splitext(self._data_file)[1].lower().startswith('.s'):
outWs = LoadRaw(Filename=self._data_file,
OutputWorkspace=workspace,
**extra_options)
if self._period != self.UNSET_PERIOD:
outWs = mtd[self._leaveSinglePeriod(str(outWs), self._period)]

# After loading raw, it is necessary to load Sample details
extra_options['OutputWorkspace'] = workspace
outWs = Load(self._data_file, **extra_options)

loader_name = outWs.getHistory().lastAlgorithm().getProperty('LoaderName').value

if loader_name == 'LoadRaw':
self._loadSampleDetails(workspace)
else:
if self._period != self.UNSET_PERIOD:
extra_options['EntryNumber']=self._period
outWs = LoadNexus(Filename=self._data_file,
OutputWorkspace=workspace,
**extra_options)

if self._period != self.UNSET_PERIOD and isinstance(outWs, WorkspaceGroup):
outWs = mtd[self._leaveSinglePeriod(outWs.name(), self._period)]

self.periods_in_file = self._find_workspace_num_periods(workspace)
self._wksp_name = workspace
Expand Down Expand Up @@ -190,9 +187,6 @@ def _loadFromWorkspace(self, reducer):
if 'Algorithm: Move' in hist_str or 'Algorithm: Rotate' in hist_str:
raise RuntimeError('Moving components needs to be made compatible with not reloading the sample')

if isEventWorkspace(ws_pointer):
ws_pointer = fromEvent2Histogram(ws_pointer)

return True


Expand Down Expand Up @@ -954,19 +948,14 @@ class CropDetBank(ReductionStep):
and crops the input workspace to just those spectra. Supports optionally
generating the output workspace from a different (sample) workspace
"""
def __init__(self, crop_sample=False):
def __init__(self):
"""
Sets up the object to either the output or sample workspace
@param crop_sample: if set to true the input workspace name is not the output but is taken from reducer.get_sample().wksp_name (default off)
"""
super(CropDetBank, self).__init__()
self._use_sample = crop_sample

def execute(self, reducer, workspace):
if self._use_sample:
in_wksp = reducer.get_sample().wksp_name
else:
in_wksp = workspace
in_wksp = workspace

# Get the detector bank that is to be used in this analysis leave the complete workspace
reducer.instrument.cur_detector().crop_to_detector(in_wksp, workspace)
Expand All @@ -979,13 +968,12 @@ class NormalizeToMonitor(sans_reduction_steps.Normalize):
"""
NORMALISATION_SPEC_NUMBER = 1
NORMALISATION_SPEC_INDEX = 0
def __init__(self, spectrum_number=None, raw_ws=None):
def __init__(self, spectrum_number=None):
if not spectrum_number is None:
index_num = spectrum_number
else:
index_num = None
super(NormalizeToMonitor, self).__init__(index_num)
self._raw_ws = raw_ws

#the result of this calculation that will be used by CalculateNorm() and the ConvertToQ
self.output_wksp = None
Expand All @@ -996,17 +984,13 @@ def execute(self, reducer, workspace):
#the -1 converts from spectrum number to spectrum index
normalization_spectrum = reducer.instrument.get_incident_mon()

raw_ws = self._raw_ws
if raw_ws is None:
raw_ws = reducer.get_sample().wksp_name

sanslog.notice('Normalizing to monitor ' + str(normalization_spectrum))

self.output_wksp = 'Monitor'
CropWorkspace(InputWorkspace=raw_ws,OutputWorkspace= self.output_wksp,
StartWorkspaceIndex = normalization_spectrum-1,
EndWorkspaceIndex = normalization_spectrum-1)

self.output_wksp = 'Monitor'
mon = reducer.get_monitor(normalization_spectrum-1)
if str(mon) != self.output_wksp:
RenameWorkspace(mon, OutputWorkspace=self.output_wksp)
if reducer.instrument.name() == 'LOQ':
RemoveBins(InputWorkspace=self.output_wksp,OutputWorkspace= self.output_wksp,XMin= reducer.transmission_calculator.loq_removePromptPeakMin,XMax=
reducer.transmission_calculator.loq_removePromptPeakMax, Interpolation="Linear")
Expand Down Expand Up @@ -1593,6 +1577,22 @@ def set_range(self, w_low = None, w_high = None):
def __str__(self):
return ' Wavelength range: ' + self.get_rebin()

class SliceEvent(ReductionStep):

def __init__(self):
super(SliceEvent, self).__init__()
self.monitor = ""

def execute(self, reducer, workspace):
ws_pointer = getWorkspaceReference(workspace)

# it applies only for event workspace
if not isinstance(ws_pointer, IEventWorkspace):
return
self.monitor = getMonitor4event(ws_pointer)
hist = fromEvent2Histogram(ws_pointer, self.monitor)
self.monitor = str(self.monitor)

class UserFile(ReductionStep):
"""
Reads an ISIS SANS mask file of the format described here mantidproject.org/SANS_User_File_Commands
Expand Down

0 comments on commit 5935a22

Please sign in to comment.