Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NeXus files in LoadVesuvio #18884

Merged
merged 9 commits into from Feb 23, 2017
85 changes: 48 additions & 37 deletions Framework/PythonInterface/plugins/algorithms/LoadVesuvio.py
Expand Up @@ -7,6 +7,9 @@

import copy
import numpy as np
import os
import re
import six

RUN_PROP = "Filename"
WKSP_PROP = "OutputWorkspace"
Expand All @@ -18,6 +21,8 @@
LOAD_MON = "LoadMonitors"
WKSP_PROP_LOAD_MON= "OutputMonitorWorkspace"

FILENAME_RE = re.compile(r'^([0-9]+)(\.[a-zA-z]+)?$')

# Raw workspace names which are necessary at the moment
SUMMED_WS = "__loadraw_evs"
# Enumerate the indexes for the different foil state sums
Expand All @@ -37,7 +42,6 @@ class LoadVesuvio(LoadEmptyVesuvio):
_ws_index = None
_spectrum_no = None
foil_map = None
_inst_prefix = None
_back_scattering = None
_load_common_called = False
_load_monitors = False
Expand Down Expand Up @@ -154,7 +158,9 @@ def validateInputs(self):
issues = {}

# Validate run number ranges
run_str = self.getProperty(RUN_PROP).value
user_input = self.getProperty(RUN_PROP).value
run_str = os.path.basename(user_input)
# String could be a full file path
if "-" in run_str:
lower, upper = run_str.split("-")
issues = self._validate_range_formatting(lower, upper, RUN_PROP, issues)
Expand Down Expand Up @@ -316,25 +322,13 @@ def _exec_single_foil_state_mode(self):
"""

runs = self._get_runs()

all_spectra = [item for sublist in self._spectra for item in sublist]

if len(runs) > 1:
self._set_spectra_type(all_spectra[0])
self._setup_raw(all_spectra)
else:
isis = config.getFacility("ISIS")
inst_prefix = isis.instrument("VESUVIO").shortName()

try:
run_str = inst_prefix + runs[0] +'.raw'
except ValueError:
run_str = runs[0]

self._raise_error_period_scatter(run_str, self._back_scattering)
all_spectra = [item for sublist in self._spectra for item in sublist]

self._load_single_run_spec_and_mon(all_spectra, run_str)
self._load_single_run_spec_and_mon(all_spectra, self._get_filename(runs[0]))

raw_group = mtd[SUMMED_WS]
self._nperiods = raw_group.size()
Expand Down Expand Up @@ -406,13 +400,33 @@ def _exec_single_foil_state_mode(self):
if self._sumspectra:
self._sum_all_spectra()

ms.DeleteWorkspace(Workspace=SUMMED_WS)
ms.DeleteWorkspace(Workspace=SUMMED_WS, EnableLogging=_LOGGING_)
self._store_results()
self._cleanup_raw()

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

def _get_filename(self, run_or_filename):
"""Given a string containing either a filename/partial filename or run number find the correct
file prefix"""
isis = config.getFacility("ISIS")
vesuvio = isis.instrument("VESUVIO")
if isinstance(run_or_filename, six.integer_types):
run_no = run_or_filename
return vesuvio.filePrefix(int(run_no)) + str(run_or_filename)
else:
match = FILENAME_RE.match(run_or_filename)
if match:
run_no = match.group(1)
return vesuvio.filePrefix(int(run_no)) + str(run_or_filename)
else:
# Assume file is okay and give it a go with Load
return run_or_filename

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

def _load_single_run_spec_and_mon(self, all_spectra, run_str):
self._raise_error_period_scatter(run_str, self._back_scattering)
# check if the monitor spectra are already in the spectra list
filtered_spectra = sorted([i for i in all_spectra if i <= self._mon_spectra[-1]])
mons_in_ws = False
Expand All @@ -423,13 +437,13 @@ def _load_single_run_spec_and_mon(self, all_spectra, run_str):
logger.warning("LoadMonitors is true while monitor spectra are defined in the spectra list.")
logger.warning("Monitors have been loaded into the data workspace not separately.")
if mons_in_ws:
ms.LoadRaw(Filename=run_str, OutputWorkspace=SUMMED_WS, SpectrumList=all_spectra,
EnableLogging=_LOGGING_)
ms.Load(Filename=run_str, OutputWorkspace=SUMMED_WS, SpectrumList=all_spectra,
EnableLogging=_LOGGING_)
else:
all_spec_inc_mon = self._mon_spectra
all_spec_inc_mon.extend(all_spectra)
ms.LoadRaw(Filename=run_str, OutputWorkspace=SUMMED_WS, SpectrumList=all_spec_inc_mon,
LoadMonitors='Separate', EnableLogging=_LOGGING_)
ms.Load(Filename=run_str, OutputWorkspace=SUMMED_WS, SpectrumList=all_spec_inc_mon,
LoadMonitors='Separate', EnableLogging=_LOGGING_)
if self._load_monitors:
monitor_group = mtd[SUMMED_WS +'_monitors']
mon_out_name = self.getPropertyValue(WKSP_PROP) + "_monitors"
Expand All @@ -441,6 +455,7 @@ def _load_single_run_spec_and_mon(self, all_spectra, run_str):
self._load_monitors_workspace = self._sum_monitors_in_group(monitor_group,
self._load_monitors_workspace)
self._raw_monitors = mtd[SUMMED_WS +'_monitors']

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

def _load_common_inst_parameters(self):
Expand All @@ -451,7 +466,6 @@ def _load_common_inst_parameters(self):
if self._load_common_called:
return

isis = config.getFacility("ISIS")
empty_vesuvio_ws = self._load_empty_evs()
empty_vesuvio = empty_vesuvio_ws.getInstrument()

Expand All @@ -461,7 +475,6 @@ def to_int_list(str_param):
return list(range(int(elements[0]),int(elements[1]) + 1)) # range goes x_l,x_h-1

# Attach parameters as attributes
self._inst_prefix = isis.instrument("VESUVIO").shortName()
parnames = empty_vesuvio.getParameterNames(False)
for name in parnames:
# Irritating parameter access doesn't let you query the type
Expand Down Expand Up @@ -584,29 +597,25 @@ def _load_and_sum_runs(self, spectra):
@param spectra :: The list of spectra to load
@returns a tuple of length 2 containing (main_detector_ws, monitor_ws)
"""
isis = config.getFacility("ISIS")
inst_prefix = isis.instrument("VESUVIO").shortName()

runs = self._get_runs()

self.summed_ws, self.summed_mon = "__loadraw_evs", "__loadraw_evs_monitors"
spec_inc_mon = self._mon_spectra
spec_inc_mon.extend(spectra)
for index, run in enumerate(runs):
run = inst_prefix + str(run)
self._raise_error_period_scatter(run, self._back_scattering)
filename = self._get_filename(run)
self._raise_error_period_scatter(filename, self._back_scattering)
if index == 0:
out_name, out_mon = SUMMED_WS, SUMMED_WS + '_monitors'
else:
out_name, out_mon = SUMMED_WS + 'tmp', SUMMED_WS + 'tmp_monitors'

# Load data
raw_filepath = FileFinder.findRuns(run)[0]
ms.LoadRaw(Filename=raw_filepath,
SpectrumList=spec_inc_mon,
OutputWorkspace=out_name,
LoadMonitors='Separate',
EnableLogging=_LOGGING_)
ms.Load(Filename=filename,
SpectrumList=spec_inc_mon,
OutputWorkspace=out_name,
LoadMonitors='Separate',
EnableLogging=_LOGGING_)

# Sum
if index > 0:
Expand Down Expand Up @@ -677,17 +686,19 @@ def _get_runs(self):
"""
Returns the runs as a list of strings
"""
run_str = self.getProperty(RUN_PROP).value
# String could be a full file path
user_input = self.getProperty(RUN_PROP).value
run_str = os.path.basename(user_input)
# Load is not doing the right thing when summing. The numbers don't look correct
if "-" in run_str:
lower, upper = run_str.split("-")
# Range goes lower to up-1 but we want to include the last number
runs = list(range(int(lower), int(upper)+1))

elif "," in run_str:
runs = run_str.split(",")
runs = run_str.split(",")
else:
runs = [run_str]
# Leave it as it is
runs = [user_input]

return runs

Expand Down
21 changes: 20 additions & 1 deletion Testing/SystemTests/tests/analysis/LoadVesuvioTest.py
@@ -1,7 +1,7 @@
#pylint: disable=invalid-name,no-init,too-many-public-methods,too-many-arguments
import stresstesting

from mantid.api import MatrixWorkspace, mtd
from mantid.api import FileFinder, MatrixWorkspace, mtd
import mantid.simpleapi as ms

import math
Expand Down Expand Up @@ -46,6 +46,25 @@ def test_spectrum_list_comma_separated_ranges(self):

#================== Success cases ================================

def test_filename_accepts_full_filepath(self):
diff_mode = "FoilOut"
rawfile = FileFinder.getFullPath("EVS14188.raw")
self._run_load(rawfile, "3", diff_mode)
self.assertTrue(mtd.doesExist('evs_raw'))
self.assertEquals(mtd['evs_raw'].getNumberHistograms(), 1)

def test_filename_accepts_filename_no_path(self):
diff_mode = "FoilOut"
self._run_load("EVS14188.raw", "3", diff_mode)
self.assertTrue(mtd.doesExist('evs_raw'))
self.assertEquals(mtd['evs_raw'].getNumberHistograms(), 1)

def test_filename_accepts_run_and_ext(self):
diff_mode = "FoilOut"
self._run_load("14188.raw", "3", diff_mode)
self.assertTrue(mtd.doesExist('evs_raw'))
self.assertEquals(mtd['evs_raw'].getNumberHistograms(), 1)

def test_load_with_back_scattering_spectra_produces_correct_workspace_using_double_difference(self):
diff_mode = "DoubleDifference"
self._run_load("14188", "3-134", diff_mode)
Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py.in
Expand Up @@ -92,7 +92,9 @@ except NameError:
# Run this after each test group has executed
doctest_global_cleanup = """
from mantid.api import FrameworkManager
from mantid.kernel import ConfigService
FrameworkManager.Instance().clear()
ConfigService.Instance().reset()
"""

# -- Options for pngmath --------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/release/v3.10.0/indirect_inelastic.rst
Expand Up @@ -12,6 +12,7 @@ Algorithms
##########

- A new input property *RebinCanToSample* was added to :ref:`ApplyPaalmanPingsCorrection <algm-ApplyPaalmanPingsCorrection>` which enables or disables the rebinning of the empty container workspace.
- :ref:`LoadVesuvio <algm-LoadVesuvio> can now load NeXus files as well as raw files

Data Analysis
#############
Expand Down
4 changes: 2 additions & 2 deletions scripts/Inelastic/IndirectReductionCommon.py
Expand Up @@ -37,8 +37,8 @@ def load_files(data_files, ipf_filename, spec_min, spec_max, sum_files=False, lo
logger.debug('Loading file %s as workspace %s' % (filename, ws_name))

if 'VESUVIO' in ipf_filename:
evs_filename = os.path.basename(str(filename)).replace('EVS', '')
LoadVesuvio(Filename=evs_filename,
# Load all spectra. They are cropped later
LoadVesuvio(Filename=str(filename),
OutputWorkspace=ws_name,
SpectrumList='1-198',
**load_opts)
Expand Down