Skip to content

Commit

Permalink
Re #11445 ReductionWrapper validate_run_number property
Browse files Browse the repository at this point in the history
instead if validate_result method. validate_result is still possible but may be used internally.
  • Loading branch information
abuts committed Mar 31, 2015
1 parent ae520a8 commit 4c646aa
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 51 deletions.
Expand Up @@ -81,13 +81,40 @@ def __init__(self):

def runTest(self):
# prepare reduction variable
# At the moment MARI reduction differs from it original by
# less then 1% due to changes in the procedure. At the moment
# we have to account for this but when we make it the same,
# the code below should be commented. Meanwhile it tests workspace
# workflow
#------------------------------------------------------
#ref_file = 'MARIReduction.nxs'
#file = FileFinder.getFullPath(ref_file)
#etalon_ws = Load(file)
#etalon_ws/=0.997979227566217
#------------------------------------------------------
rd = mr.ReduceMARIFromFile()
rd.def_main_properties()
rd.def_advanced_properties()
# this is correct workflow for the ref file
#rd.reducer.prop_man.save_file_name = ref_file
# temporary workflow, until we fix workspace adjustment
rd._tolerr =3.e-3
rd.reducer.prop_man.save_file_name = 'MARIReduction.nxs'
rd.validate_run_number=11001
try:
rez,mess = rd.run_reduction()
self.result=rez
if not rez:
print "*** Validation failed: {0}".format(mess)
if mess.find('Created')>-1: # validation still failed due to missing validation file
print "*** Validation failed: {0}".format(mess)
self.result=False
except RuntimeError as err:
print "*** Validation failed with error: {0}".format(err.message)
self.result=False
rd.reducer.prop_man.save_file_name = None


self.result,message = rd.validate_result()
if not self.result:
print "*** Validation failed: {0}".format(message)



Expand Down
Expand Up @@ -47,15 +47,9 @@ def reduce(self,input_file=None,output_directory=None):
"""Method executes reduction over single file
Overload only if custom reduction is needed
"""
outWS = ReductionWrapper.reduce(self,input_file,output_directory)
converted_to_energy_transfer_ws = ReductionWrapper.reduce(self,input_file,output_directory)
#SaveNexus(outWS,Filename = 'MARNewReduction.nxs')
return outWS

def validate_result(self,build_validation=False):
"""Change this method to verify different results """
# build_validation -- if true, build and save new workspace rather then validating the old one
rez,message = ReductionWrapper.build_or_validate_result(self,11001,"MARIReduction.nxs",build_validation,1.e-2)
return rez,message
return converted_to_energy_transfer_ws

def set_custom_output_filename(self):
""" define custom name of output files if standard one is not satisfactory
Expand Down
12 changes: 8 additions & 4 deletions Code/Mantid/scripts/Inelastic/Direct/DirectEnergyConversion.py
Expand Up @@ -971,14 +971,18 @@ def save_results(self, workspace, save_file=None, formats=None):
formats = self.prop_man.save_format

if save_file:
save_file,ext = os.path.splitext(save_file)
if len(ext) > 1:
save_file,ext = os.path.splitext(save_file)
if len(ext) > 1:
formats.add(ext[1:])
else:
save_file = self.prop_man.save_file_name
save_file = self.prop_man.save_file_name

if save_file is None:
save_file = workspace.getName()
if workspace is None:
prop_man.log("DirectEnergyConversion:save_results: Nothing to do",'warning')
return
else:
save_file = workspace.getName()
elif os.path.isdir(save_file):
save_file = os.path.join(save_file, workspace.getName())
elif save_file == '':
Expand Down
Expand Up @@ -342,7 +342,12 @@ def __get__(self,instance,owner=None):
return name

def __set__(self,instance,value):
self._file_name = value

if value is None:
self._file_name = None
else:
self._file_name = str(value)

def set_custom_print(self,routine):
self._custom_print = routine
#end SaveFileName
Expand Down
103 changes: 68 additions & 35 deletions Code/Mantid/scripts/Inelastic/Direct/ReductionWrapper.py
Expand Up @@ -27,12 +27,14 @@ def __init__(self,Web_vars=None):
self.advanced_vars = None
#
def get_all_vars(self):
"""Return dictionary with all defined variables"""
web_vars = None
"""Return dictionary with all defined variables
combined together
"""
web_vars = {}
if self.advanced_vars:
web_vars = self.advanced_vars
if self.standard_vars:
if web_vars:
if len(web_vars)>0:
web_vars.update(self.standard_vars)
else:
web_vars = self.standard_vars
Expand All @@ -53,6 +55,8 @@ def __init__(self,instrumentName,web_var=None):
# used during debugging "wait for files" workflow
# instead of Pause algorithm
self._debug_wait_for_files_operation = None
# tolerance to change in some tests if default is not working well
self._tolerr=None

# The variables which are set up from web interface or to be exported to
# web interface
Expand Down Expand Up @@ -153,17 +157,21 @@ def validation_file_name(self):
""" the name of the file, used as reference to
validate the run, specified as the class property
It can also be overloaded to return a workspace
or workspace name to validate results against
The method can be overloaded to return a workspace
or workspace name to validate results against.
"""

instr = self.reducer.prop_man.instr_name
run_n = self.validate_run_number
ei = PropertyManager.incident_energy.get_current()
file_name = '{0}{1}_{2:<3.2f}meV_VALIDATION_file.nxs'.format(instr,run_n,ei)
if self.reducer.prop_man.save_file_name:
file_name = self.reducer.prop_man.save_file_name
if isinstance(file_name,api.Workspace):
return file_name
else:
instr = self.reducer.prop_man.instr_name
run_n = self.validate_run_number
ei = PropertyManager.incident_energy.get_current()
file_name = '{0}{1}_{2:<3.2f}meV_VALIDATION_file.nxs'.format(instr,run_n,ei)
run_dir = self.validation_file_place()
full_name = os.path.join(run_dir,file_name)
return full_name;
return full_name

def validation_file_place(self):
"""Redefine this to the place, where validation file, used in conjunction with
Expand All @@ -176,7 +184,7 @@ def validate_result(self,Error=1.e-6,ToleranceRelErr=True):
"""Method to validate result against existing validation file
or workspace
Change this method to verify different results"""
Change this method to verify different results or validate results differently"""
rez,message = ReductionWrapper.build_or_validate_result(self,
Error,ToleranceRelErr)
return rez,message
Expand Down Expand Up @@ -206,32 +214,46 @@ def build_or_validate_result(self,Error=1.e-6,ToleranceRelErr=True):
file and returns True if the reduction and saving of this file is successful
"""
# this row defines location of the validation file in this script folder
# this row defines location of the validation file
validation_file = self.validation_file_name()
sample_run = self.validate_run_number
if os.path.isfile(validation_file):
build_validation = False
try:
sample = Load(validation_file)
except:
build_validation = True
elif isinstance(vaildation_file,api.Workspace) or (isinstance(vaildation_file,str) and vaildation_file in mtd):
# its workspace:
if isinstance(vaildation_file,api.Workspace):
sample_run = vaildation_file
if isinstance(validation_file,str):
path,name = os.path.split(validation_file)
if name in mtd:
reference_ws = mtd[name]
build_validation = False
fileName = "workspace:"+reference_ws.name()
else:
sample_run = mtd[vaildation_file]
if len(path)>0:
config.appendDataSearchDir(path)
# it there bug in getFullPath? It returns the same string if given full path
# but file has not been found
fileName = FileFinder.getFullPath(name)
if len(fileName)>0:
build_validation = False
try:
reference_ws = Load(fileName)
except:
build_validation = True
else:
build_validation = True
elif isinstance(validation_file,api.Workspace):
# its workspace:
reference_ws = validation_file
build_validation = False
fileName = "workspace:"+reference_ws.name()
else:
build_validation = True
#--------------------------------------------------------
if build_validation:
self.reducer.prop_man.save_file_name = validation_file
self.reducer.prop_man.log\
("*** FOUND VALIDATION FILE: {0}\n"\
" Validating run {1} against this file".format(validation_file,sample_run),'warning')
("*** WARNING:can not find or load validation file {0}\n"\
" Building validation file for run N:{1}".format(validation_file,sample_run),'warning')
else:
self.reducer.prop_man.log\
("*** WARNING:can not find (load?) validation file {0}\n"\
" Building validation file for run{1}".format(validation_file,sample_run),'warning')
("*** FOUND VALIDATION FILE: {0}\n"\
" Validating run {1} against this file".format(fileName,sample_run),'warning')

# just in case, to be sure
current_web_state = self._run_from_web
Expand All @@ -249,23 +271,33 @@ def build_or_validate_result(self,Error=1.e-6,ToleranceRelErr=True):
reduced = self.reduce()

if build_validation:
self.reducer.prop_man.save_file_name = None
result_name = os.path.splitext(validation_file)[0]
self.reducer.prop_man.log("*** Saving validation file with name: {0}.nxs".format(result_name),'notice')
SaveNexus(reduced,Filename=result_name + '.nxs')
self.reducer.prop_man.save_file_name = None
return True,'Created validation file {0}.nxs'.format(result_name)
else:
if isinstance(reduced,list): # check only first result in multirep
reduced = reduced[0]
result = CheckWorkspacesMatch(Workspace1=sample,Workspace2=reduced,\
Tolerance=Error,CheckSample=False,\
# Cheat! Counterintuitive!
if self._tolerr:
TOLL=self._tolerr
else:
TOLL = Error
result = CheckWorkspacesMatch(Workspace1=reference_ws,Workspace2=reduced,\
Tolerance=TOLL,CheckSample=False,\
CheckInstrument=False,ToleranceRelErr=ToleranceRelErr)

self.wait_for_file = current_wait_state
self._run_from_web = current_web_state
if result == 'Success!':
return True,'Reference file and reduced workspace are equivalent'
else:
fname,ext = os.path.splitext(fileName)
filename = fname+'-mismatch.nxs'
self.reducer.prop_man.log("***WARNING: can not get results matching the reference file.\n"\
" Saving new results to file {0}".format(filename),'warning')
SaveNexus(reduced,Filename=filename)
return False,result

@abstractmethod
Expand Down Expand Up @@ -322,12 +354,12 @@ def reduce(self,input_file=None,output_directory=None):

self._run_pause(timeToWait)
Found,input_file = PropertyManager.sample_run.find_file(file_hint=file_hint,be_quet=True)
ws = self.reducer.convert_to_energy(None,input_file)
converted_to_energy_transfer_ws = self.reducer.convert_to_energy(None,input_file)

else:
ws = self.reducer.convert_to_energy(None,input_file)
converted_to_energy_transfer_ws = self.reducer.convert_to_energy(None,input_file)

return ws
return converted_to_energy_transfer_ws
#
def sum_and_reduce(self):
""" procedure used to sum and reduce runs in case when not all files
Expand Down Expand Up @@ -404,7 +436,8 @@ def run_reduction(self):
self.reducer.prop_man.log\
("**************************************************************************************",'warning')
raise RuntimeError("Validation against old data file failed")
return
self.validate_run_number=None
return rez,mess

if self.reducer.sum_runs:
# --------### sum runs provided ------------------------------------###
Expand Down

0 comments on commit 4c646aa

Please sign in to comment.