Skip to content

Commit

Permalink
Refs #18877 Add correct workspace name for batch reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonPiccardoSelg committed Feb 16, 2017
1 parent 6977e9e commit 78de78b
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 26 deletions.
56 changes: 31 additions & 25 deletions scripts/SANS/SANSBatchMode.py
Expand Up @@ -200,14 +200,12 @@ def BatchReduce(filename, format, plotresults=False, saveAlgs={'SaveRKH':'txt'},
# When we set a new user file, that means that the combineDet feature could be invalid,
# ie if the detector under investigation changed in the user file. We need to change this
# here too. But only if it is not None.
#if combinDet is not None:
# new_combineDet = ReductionSingleton().instrument.get_detector_selection()
# combineDet = su.get_correct_combinDet_setting(new_combineDet, instrument_name)


except (RunTimeError, ValueError) as e:
if combineDet is not None:
new_combineDet = ReductionSingleton().instrument.get_detector_selection()
combineDet = su.get_correct_combinDet_setting(ins_name, new_combineDet)
except (RuntimeError, ValueError) as e:
sanslog.warning("Error in Batchmode user files: Could not reset the specified user file %s. More info: %s" %(
str(run['user_file']),str(e)))
str(run['user_file']), str(e)))

local_settings = copy.deepcopy(ReductionSingleton().reference())
local_prop_man_settings = ReductionSingleton().settings.clone("TEMP_SETTINGS")
Expand Down Expand Up @@ -262,34 +260,42 @@ def BatchReduce(filename, format, plotresults=False, saveAlgs={'SaveRKH':'txt'},
final_name = sanitize_name(final_name)

#convert the names from the default one, to the agreement
# This caused a renaming with the following logic
# | combinDet | Name HAB | Name LAB | Name Merged |
# | rear | +_rear | - | - |
# | front | - | +_front | - |
# | both | +_rear | +_front | - |
# | merged | +_rear | +_front | +_merged |
# This is not great since it uses SANS2D terminology for all instruments

names = [final_name]
if combineDet == 'rear':
names = [final_name+'_rear']
RenameWorkspace(InputWorkspace=reduced,OutputWorkspace= final_name+'_rear')
new_name = su.rename_workspace_correctly(ins_name, su.ReducedType.LAB, final_name, reduced)
names = [new_name]
elif combineDet == 'front':
names = [final_name+'_front']
RenameWorkspace(InputWorkspace=reduced,OutputWorkspace= final_name+'_front')
new_name = su.rename_workspace_correctly(ins_name, su.ReducedType.HAB, final_name, reduced)
names = [new_name]
elif combineDet == 'both':
names = [final_name+'_front', final_name+'_rear']
if ins_name == 'SANS2D':
rear_reduced = reduced.replace('front','rear')
rear_reduced = reduced.replace('front', 'rear')
else: #if ins_name == 'lOQ':
rear_reduced = reduced.replace('HAB','main')
RenameWorkspace(InputWorkspace=reduced,OutputWorkspace=final_name+'_front')
RenameWorkspace(InputWorkspace=rear_reduced,OutputWorkspace=final_name+'_rear')
rear_reduced = reduced.replace('HAB', 'main')
new_name_HAB = su.rename_workspace_correctly(ins_name, su.ReducedType.HAB, final_name, reduced)
new_name_LAB = su.rename_workspace_correctly(ins_name, su.ReducedType.LAB, final_name, rear_reduced)
names = [new_name_HAB, new_name_LAB]
elif combineDet == 'merged':
names = [final_name + '_merged', final_name + '_rear', final_name+'_front']
if ins_name == 'SANS2D':
rear_reduced = reduced.replace('merged','rear')
front_reduced = reduced.replace('merged','front')
rear_reduced = reduced.replace('merged', 'rear')
front_reduced = reduced.replace('merged', 'front')
else:
rear_reduced = reduced.replace('_merged','')
front_reduced = rear_reduced.replace('main','HAB')
RenameWorkspace(InputWorkspace=reduced,OutputWorkspace= final_name + '_merged')
RenameWorkspace(InputWorkspace=rear_reduced,OutputWorkspace= final_name + '_rear')
RenameWorkspace(InputWorkspace=front_reduced,OutputWorkspace= final_name+'_front')
rear_reduced = reduced.replace('_merged', '')
front_reduced = rear_reduced.replace('main', 'HAB')
new_name_Merged = su.rename_workspace_correctly(ins_name, su.ReducedType.Merged, final_name, reduced)
new_name_LAB = su.rename_workspace_correctly(ins_name, su.ReducedType.LAB, final_name, rear_reduced)
new_name_HAB = su.rename_workspace_correctly(ins_name, su.ReducedType.HAB, final_name, front_reduced)
names = [new_name_Merged, new_name_LAB, new_name_HAB]
else:
RenameWorkspace(InputWorkspace=reduced,OutputWorkspace=final_name)
RenameWorkspace(InputWorkspace=reduced, OutputWorkspace=final_name)

file = run['output_as']
#saving if optional and doesn't happen if the result workspace is left blank. Is this feature used?
Expand Down
90 changes: 90 additions & 0 deletions scripts/SANS/SANSUtility.py
Expand Up @@ -1978,6 +1978,96 @@ def get_user_file_name_options_with_txt_extension(user_file_name):
return user_file_with_extension


def get_correct_combinDet_setting(instrument_name, detector_selection):
"""
We want to get the correct combinDet variable for batch reductions from a new detector selection.
@param instrument_name: the name of the intrument
@param detector_selection: a detector selection comes directly from the reducer
@return: a combinedet option
"""
if detector_selection is None:
return None

instrument_name = instrument_name.upper()
# If we are dealing with LARMOR, then the correct combineDet selection is None
if instrument_name == "LARMOR":
return None

detector_selection = detector_selection.upper()
# If we are dealing with LOQ, then the correct combineDet selection is
if instrument_name == "LOQ":
if detector_selection == "MAIN":
new_combine_detector_selection = 'rear'
elif detector_selection == "HAB":
new_combine_detector_selection = 'front'
elif detector_selection == "MERGED":
new_combine_detector_selection = 'merged'
elif detector_selection == "BOTH":
new_combine_detector_selection = 'both'
else:
raise RuntimeError("SANSBatchReduce: Unknown detector {0} for conversion "
"to combinDet.".format(detector_selection))
return new_combine_detector_selection

# If we are dealing with SANS2D, then the correct combineDet selection is
if instrument_name == "SANS2D":
if detector_selection == "REAR":
new_combine_detector_selection = 'rear'
elif detector_selection == "FRONT":
new_combine_detector_selection = 'front'
elif detector_selection == "MERGED":
new_combine_detector_selection = 'merged'
elif detector_selection == "BOTH":
new_combine_detector_selection = 'both'
else:
raise RuntimeError("SANSBatchReduce: Unknown detector {0} for conversion "
"to combinDet.".format(detector_selection))
return new_combine_detector_selection
raise RuntimeError("SANSBatchReduce: Unknown instrument {0}.".format(instrument_name))


class ReducedType(object):
class LAB(object):
pass

class HAB(object):
pass

class Merged(object):
pass


def rename_workspace_correctly(instrument_name, reduced_type, final_name, workspace):
def get_suffix(inst_name, red_type):
if inst_name == "SANS2D":
if red_type is ReducedType.LAB:
suffix = "_rear"
elif red_type is ReducedType.HAB:
suffix = "_front"
elif red_type is ReducedType.Merged:
suffix = "_merged"
else:
raise RuntimeError("Unknown reduction type {0}.".format(red_type))
return suffix
elif inst_name == "LOQ":
if red_type is ReducedType.LAB:
suffix = "_main"
elif red_type is ReducedType.HAB:
suffix = "_hab"
elif red_type is ReducedType.Merged:
suffix = "_merged"
else:
raise RuntimeError("Unknown reduction type {0}.".format(red_type))
return suffix
else:
return ""
final_suffix = get_suffix(instrument_name, reduced_type)
complete_name = final_name + final_suffix
RenameWorkspace(InputWorkspace=workspace, OutputWorkspace=complete_name)
return complete_name


###############################################################################
######################### Start of Deprecated Code ############################
###############################################################################
Expand Down
83 changes: 82 additions & 1 deletion scripts/test/SANSUtilityTest.py
Expand Up @@ -4,7 +4,7 @@
# Need to import mantid before we import SANSUtility
import mantid
from mantid.simpleapi import *
from mantid.api import (mtd, WorkspaceGroup, AlgorithmManager)
from mantid.api import (mtd, WorkspaceGroup, AlgorithmManager, AnalysisDataService)
from mantid.kernel import (DateAndTime, time_duration, FloatTimeSeriesProperty,
BoolTimeSeriesProperty,StringTimeSeriesProperty,
StringPropertyWithValue, V3D, Quat)
Expand Down Expand Up @@ -1598,5 +1598,86 @@ def test_that_does_alters_user_file_name_when_does_contain_txt_ending(self):
self.assertTrue(su.get_user_file_name_options_with_txt_extension("test.tt") == ["test.tt.txt", "test.tt.TXT"])
self.assertTrue(su.get_user_file_name_options_with_txt_extension("test") == ["test.txt", "test.TXT"])


class TestSelectNewDetector(unittest.TestCase):
def test_that_for_SANS2D_correct_settings_are_selected(self):
self.assertTrue(su.get_correct_combinDet_setting("SANS2d", "rear") == "rear")
self.assertTrue(su.get_correct_combinDet_setting("SANS2D", "FRONT") == "front")
self.assertTrue(su.get_correct_combinDet_setting("sAnS2d", "boTH") == "both")
self.assertTrue(su.get_correct_combinDet_setting("sans2d", "merged") == "merged")

def test_that_for_LOQ_correct_settings_are_selected(self):
self.assertTrue(su.get_correct_combinDet_setting("Loq", "main") == "rear")
self.assertTrue(su.get_correct_combinDet_setting("LOQ", "Hab") == "front")
self.assertTrue(su.get_correct_combinDet_setting("lOQ", "boTH") == "both")
self.assertTrue(su.get_correct_combinDet_setting("loq", "merged") == "merged")

def test_that_for_LARMOR_correct_settings_are_selected(self):
self.assertTrue(su.get_correct_combinDet_setting("larmor", "main") is None)
self.assertTrue(su.get_correct_combinDet_setting("LARMOR", "DetectorBench") is None)

def test_that_for_unknown_instrument_raises(self):
args = ["unknown_instrument", "main"]
self.assertRaises(RuntimeError, su.get_correct_combinDet_setting, *args)

def test_that_for_unknown_detector_command_raises(self):
args = ["sans2d", "main"]
self.assertRaises(RuntimeError, su.get_correct_combinDet_setting, *args)
args = ["loq", "front"]
self.assertRaises(RuntimeError, su.get_correct_combinDet_setting, *args)


class TestRenamingOfBatchModeWorkspaces(unittest.TestCase):
def _create_sample_workspace(self):
ws = CreateSampleWorkspace(Function='Flat background', NumBanks=1, BankPixelWidth=1, NumEvents=1,
XMin=1, XMax=14, BinWidth=2)
return ws

def test_that_SANS2D_workspace_is_renamed_correctly(self):
workspace = self._create_sample_workspace()
out_name = su.rename_workspace_correctly("SANS2D", su.ReducedType.LAB, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_rear"))
self.assertTrue(out_name == "test_rear")
out_name = su.rename_workspace_correctly("SANS2D", su.ReducedType.HAB, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_front"))
self.assertTrue(out_name == "test_front")
out_name = su.rename_workspace_correctly("SANS2D", su.ReducedType.Merged, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_merged"))
self.assertTrue(out_name == "test_merged")

if AnalysisDataService.doesExist("test_merged"):
AnalysisDataService.remove("test_merged")

def test_that_LOQ_workspace_is_renamed_correctly(self):
workspace = self._create_sample_workspace()
out_name = su.rename_workspace_correctly("LOQ", su.ReducedType.LAB, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_main"))
self.assertTrue(out_name == "test_main")
out_name = su.rename_workspace_correctly("LOQ", su.ReducedType.HAB, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_hab"))
self.assertTrue(out_name == "test_hab")
out_name = su.rename_workspace_correctly("LOQ", su.ReducedType.Merged, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test_merged"))
self.assertTrue(out_name == "test_merged")

if AnalysisDataService.doesExist("test_merged"):
AnalysisDataService.remove("test_merged")

def test_that_LAMROR_workspace_is_not_renamed(self):
workspace = self._create_sample_workspace()
out_name = su.rename_workspace_correctly("LARMOR", su.ReducedType.LAB, "test", workspace)
self.assertTrue(AnalysisDataService.doesExist("test"))
self.assertTrue(out_name == "test")

if AnalysisDataService.doesExist("test"):
AnalysisDataService.remove("test")

def test_that_raies_for_unkown_reduction_type(self):
workspace = self._create_sample_workspace()
args = ["SANS2D", "jsdlkfsldkfj", "test", workspace]
self.assertRaises(RuntimeError, su.rename_workspace_correctly, *args)
AnalysisDataService.remove("ws")


if __name__ == "__main__":
unittest.main()

0 comments on commit 78de78b

Please sign in to comment.