Skip to content

Commit

Permalink
Re #11421 Enabled clearing old results in multirep mode
Browse files Browse the repository at this point in the history
and various unit tests for ReductionWrapper, verifying output file name procedure
  • Loading branch information
abuts committed Mar 25, 2015
1 parent d47b297 commit 26cddbd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 9 deletions.
21 changes: 18 additions & 3 deletions Code/Mantid/scripts/Inelastic/Direct/DirectEnergyConversion.py
Expand Up @@ -342,7 +342,9 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
# inform user on what parameters have changed from script or gui
# if monovan present, check if abs_norm_ parameters are set
self.prop_man.log_changed_values('notice')

# before trying to process new results, let's remove from memory old results
# if any present and they are not needed any more (user have not renamed them)
self._clear_old_results()

start_time = time.time()

Expand Down Expand Up @@ -479,6 +481,7 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
if out_ws_name:
if self._multirep_mode:
result.append(deltaE_ws_sample)
self._old_runs_list.append(deltaE_ws_sample.name())
else:
results_name = deltaE_ws_sample.name()
if results_name != out_ws_name:
Expand Down Expand Up @@ -1268,11 +1271,15 @@ def __init__(self, instr_name=None,reload_instrument=False):
# workspace
# processed
object.__setattr__(self,'_multirep_mode',False)
# list of workspace names, processed earlier
object.__setattr__(self,'_old_runs_list',[])


all_methods = dir(self)
# define list of all existing properties, which have descriptors
object.__setattr__(self,'_descriptors',extract_non_system_names(all_methods))


if instr_name:
self.initialise(instr_name,reload_instrument)
#end
Expand Down Expand Up @@ -1512,7 +1519,7 @@ def _do_mono(self, run, ei_guess,
#-------------------------------------------------------------------------------
def _get_wb_inegrals(self,run):
"""Obtain white bean vanadium integrals either by integrating
workspace in question or cashed value
workspace in question or using cashed value
"""
run = self.get_run_descriptor(run)
white_ws = run.get_workspace()
Expand Down Expand Up @@ -1575,7 +1582,15 @@ def _build_white_tag(self):
low,upp = self.wb_integr_range
white_tag = 'NormBy:{0}_IntergatedIn:{1:0>10.2f}:{2:0>10.2f}'.format(self.normalise_method,low,upp)
return white_tag

#
def _clear_old_results(self):
"""Remove workspaces, processed earlier and not used any more"""
ws_list = self._old_runs_list
for ws_name in ws_list:
if ws_name in mtd:
DeleteWorkspace(ws_name)
object.__setattr__(self,'_old_runs_list',[])
#
def get_failed_spectra_list_from_masks(masked_wksp,prop_man):
"""Compile a list of spectra numbers that are marked as
masked in the masking workspace
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/scripts/Inelastic/Direct/ReductionWrapper.py
Expand Up @@ -346,7 +346,9 @@ def run_reduction(self):
RenameWorkspace(InputWorkspace=red_ws,OutputWorkspace=out_ws_name)
results.append(mtd[out_ws_name])
else:
results.append(red_ws)
OutWSName = '{0}#{1}of{2}'.format(out_ws_name,num+1,nruns)
RenameWorkspace(InputWorkspace=red_ws,OutputWorkspace=OutWSName)
results.append(mtd[OutWSName])
#end
if len(results) == 1:
return results[0]
Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/scripts/Inelastic/Direct/RunDescriptor.py
Expand Up @@ -455,7 +455,10 @@ def _set_run_list(self,instance,run_list,file_path=None,fext=None):
self._run_list.set_last_ind2sum(ind)
self._run_number = run_num
self._run_file_path = file_path
self._fext = main_fext
if fext is None:
self._fext = None
else:
self._fext = main_fext
self._ws_name = self._build_ws_name()

def run_number(self):
Expand Down
93 changes: 89 additions & 4 deletions Code/Mantid/scripts/test/ReductionWrapperTest.py
Expand Up @@ -44,7 +44,32 @@ def custom_name(prop_man):
#return None
@iliad
def reduce(self, input_file = None, output_directory = None):
return ''

self.reducer._clear_old_results()
if input_file:
self.reducer.prop_man.sample_run = input_file
run = self.reducer.prop_man.sample_run

result = []
if PropertyManager.incident_energy.multirep_mode():
en_range = self.reducer.prop_man.incident_energy
for ind,en in enumerate(en_range):
ws=CreateSampleWorkspace()
AddSampleLog(ws,LogName = 'run_number',LogText=str(run))
PropertyManager.sample_run.set_action_suffix('#{0}_reduced'.format(ind+1))
PropertyManager.sample_run.synchronize_ws(ws)
result.append(ws)
self.reducer._old_runs_list.append(ws.name())
else:
ws=CreateSampleWorkspace()
AddSampleLog(ws,LogName = 'run_number',LogText=str(run))
PropertyManager.sample_run.set_action_suffix('_reduced')
PropertyManager.sample_run.synchronize_ws(ws)
result.append(ws)

if len(result) == 1:
result = result[0]
return result
#-----------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -82,6 +107,14 @@ def test_export_advanced_values(self):

test_dir = config['defaultsave.directory']
file = os.path.join(test_dir,'reduce_vars.py')
#clear up previous rubbish may be present from other runs
if os.path.isfile(file):
os.remove(file)
fbase,fext = os.path.splitext(file)
fcomp = fbase+'.pyc'
if os.path.isfile(fcomp):
os.remove(fcomp)
# save wen variables
red.save_web_variables(file)
self.assertTrue(os.path.isfile(file))

Expand Down Expand Up @@ -166,12 +199,64 @@ def test_custom_print_name(self):
th.reduce()

save_file = th.reducer.prop_man.save_file_name
self.assertEqual(save_file,'SOMETHING100_10.01meV_rings')
# such strange name because custom print function above access workspace,
# generated by reduction
self.assertEqual(save_file,'SOMETHINGSR_MAR000100#2_reduced_10.01meV_rings')

th.reducer.prop_man.sample_run = 200
PropertyManager.incident_energy.next()
save_file = th.reducer.prop_man.save_file_name
self.assertEqual(save_file,'SOMETHING200_20.00meV_rings')
# now reduction have not been run, and the name is generated from run number
self.assertEqual(save_file,'SOMETHINGSR_MAR000100#2_reduced_20.00meV_rings')

def test_return_run_list(self):
th=test_helper()

th.reducer.prop_man.sample_run=200
th.run_reduction()
# standard reduction would save and delete workspace but our simplified one
# will just keep it
name = 'SR_MAR000200_reduced'
self.assertTrue(name in mtd)

th.reducer.prop_man.sample_run=300
# new run deletes the old one
self.assertFalse(name in mtd)

rez = th.run_reduction()
self.assertTrue(isinstance(rez,api.Workspace))
self.assertTrue('rez' in mtd)
self.assertEqual(rez.name(),'rez')

th.reducer.prop_man.sample_run=[300,400]
th.run_reduction()
self.assertFalse('SR_MAR000300_reduced' in mtd)
self.assertTrue('SR_MAR000400_reduced' in mtd)

th.reducer.prop_man.sample_run=[500,600]
self.assertFalse('SR_MAR000400_reduced' in mtd)
th.run_reduction()
self.assertFalse('SR_MAR000500_reduced' in mtd)
self.assertTrue('SR_MAR000600_reduced' in mtd)

th.reducer.prop_man.sample_run=[300,400]
runs = th.run_reduction()
self.assertTrue('runs#1of2' in mtd)
self.assertTrue('runs#2of2' in mtd)
self.assertEqual(runs[0].name(),'runs#1of2')
self.assertEqual(runs[1].name(),'runs#2of2')

th.reducer.prop_man.incident_energy=[10,20]
th.reducer.prop_man.sample_run=300
th.run_reduction()
self.assertTrue('SR_MAR000300#1_reduced' in mtd)
self.assertTrue('SR_MAR000300#2_reduced' in mtd)
th.reducer.prop_man.sample_run=400
th.run_reduction()
self.assertFalse('SR_MAR000300#1_reduced' in mtd)
self.assertFalse('SR_MAR000300#2_reduced' in mtd)
self.assertTrue('SR_MAR000400#1_reduced' in mtd)
self.assertTrue('SR_MAR000400#2_reduced' in mtd)




Expand Down

0 comments on commit 26cddbd

Please sign in to comment.