Skip to content

Commit

Permalink
refs #6667 Better defined overloaded properties
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Jul 5, 2013
1 parent 596aa80 commit 3711e07
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 62 deletions.
108 changes: 73 additions & 35 deletions Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def do_white(self, white_run, spectra_masks, map_file,mon_number=None):

# White beam scale factor
white_ws *= self.wb_scale_factor
self.workspaces_list['white_ws'] = white_ws
self.workspace_list['white_ws'] = white_ws
return white_ws

def mono_van(self, mono_van, ei_guess, white_run=None, map_file=None,
Expand All @@ -242,7 +242,7 @@ def mono_van(self, mono_van, ei_guess, white_run=None, map_file=None,
white_run, map_file, spectra_masks, Tzero)
# Normalize by vanadium sample weight
monovan /= float(self.van_mass)/float(self.__van_rmm)
self.workspaces_list['monovan_ws'] = monovan
self.workspace_list['monovan_ws'] = monovan
return monovan

def mono_sample(self, mono_run, ei_guess, white_run=None, map_file=None,
Expand Down Expand Up @@ -507,33 +507,6 @@ def convert_to_energy(self, mono_run, ei, white_run=None, mono_van=None,\
common.clear_loaded_data()

return sample_wkspace
#----------------------------------------------------------------------------------
# Complex setters/getters
#----------------------------------------------------------------------------------
def __getattribute__(self, name):
if name == "van_rmm":
return self.__van_rmm
else:
return object.__getattribute__(self,name);

def __setattr__(self, name,value):
if name == 'energy_bins':
if value != None:
if isinstance(value,str):
list = str.split(value,',');
value = [float(list[0]),float(list[1]),float(list[2])]
if len(value) != 3:
raise KeyError("Energy_bin value has to be either list of 3 numbers or string, representing these three number separated by commas")

if name == 'map_file' or name == 'monovan_mapfile':
if value != None:
fileName, fileExtension = os.path.splitext(value)
if (not fileExtension):
value=value+'.map'


object.__setattr__(self, name, value)


#----------------------------------------------------------------------------------
# Reduction steps
Expand Down Expand Up @@ -756,8 +729,71 @@ def __init__(self, instr_name=None):
self.instr_name=None
if not (instr_name is None or len(instr_name)==0) :
# Instrument and default parameter setup
# fomats availible for saving. As the reducer has to have a method to process one of this, it is private property
self.__save_formats = {}
self.__save_formats['.spe'] = lambda workspace,filename : SaveSPE(InputWorkspace=workspace,Filename= filename)
self.__save_formats['.nxspe'] = lambda workspace,filename : SaveNXSPE(InputWorkspace=workspace,Filename= filename, KiOverKfScaling=self.apply_kikf_correction,Psi=self.psi)
self.__save_formats['.nxs'] = lambda workspace,filename : SaveNexus(InputWorkspace=workspace,Filename= filename)

self.initialise(instr_name)

#----------------------------------------------------------------------------------
# Complex setters/getters
#----------------------------------------------------------------------------------
@property
def van_rmm(self):
return self.__van_rmm

@property
def save_format(self):
return self._save_format

@save_format.setter
def save_format(self, value):
if value not in self.__save_formats :
self.log("Trying to set unknown format: \""+str(value)+"\" No saving will occur")
value = None
self._save_format = value

@property
def energy_bins(self):
return self._energy_bins;
@energy_bins.setter
def energy_bins(self,value):
if value != None:
if isinstance(value,str):
list = str.split(value,',');
value = [float(list[0]),float(list[1]),float(list[2])]
if len(value) != 3:
raise KeyError("Energy_bin value has to be either list of 3 numbers or string, representing these three number separated by commas")

self._energy_bins= value;
@property
def map_file(self):
return self._map_file;
@map_file.setter
def map_file(self,value):
if value != None:
fileName, fileExtension = os.path.splitext(value)
if (not fileExtension):
value=value+'.map'

self._map_file = value

@property
def monovan_mapfile(self):
return self._monovan_mapfile;
@monovan_mapfile.setter
def monovan_mapfile(self,value):
if value != None:
fileName, fileExtension = os.path.splitext(value)
if (not fileExtension):
value=value+'.map'

self._monovan_mapfile = value



def initialise(self, instr_name,reload_instrument=False):
"""
Initialise the attributes of the class
Expand Down Expand Up @@ -805,12 +841,6 @@ def init_idf_params(self, reload_instrument=False):
specify some parameters which may be not in IDF Parameters file
"""
# fomats availible for saving. As the reducer has to have a method to process one of this, it is private property
self.__save_formats = {}
self.__save_formats['.spe'] = lambda workspace,filename : SaveSPE(InputWorkspace=workspace,Filename= filename)
self.__save_formats['.nxspe'] = lambda workspace,filename : SaveNXSPE(InputWorkspace=workspace,Filename= filename, KiOverKfScaling=self.apply_kikf_correction,Psi=self.psi)
self.__save_formats['.nxs'] = lambda workspace,filename : SaveNexus(InputWorkspace=workspace,Filename= filename)



## Detector diagnosis
Expand Down Expand Up @@ -1303,6 +1333,14 @@ def test_comlex_set(self):
tReducer.monovan_mapfile = "other_map.myExt"
self.assertEqual("other_map.myExt",tReducer.monovan_mapfile)

tReducer.save_format = 'unknown'
self.assertTrue(tReducer.save_format is None)

tReducer.save_format = '.spe'
self.assertEqual('.spe',tReducer.save_format)



#self.assertRaises(KeyError,tReducer.energy_bins=20,None)
def test_default_warnings(self):
tReducer = self.reducer
Expand Down
20 changes: 4 additions & 16 deletions Code/Mantid/scripts/Inelastic/dgreduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file=None,monovan_run=None,**
# diag the sample and detector vanadium. It will deal with hard mask only if it is set that way
masking = Reducer.diagnose(wb_run,sample = mask_run,
second_white = None,variation=1.1,print_results=True)
if(mask_run!=sample_run) :
copy_masks(mask_run,samle_run)


# Calculate absolute units:
Expand All @@ -266,14 +264,15 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file=None,monovan_run=None,**
# MaskDetectors(Workspace=sample_run,MaskedWorkspace=masking2)
else: # masks have already been combined through common wb_run
pass

masking += masking2


else: # if Reducer.mono_correction_factor != None :
pass


Reducer.spectra_masks=masking
# estimate and report the number of failing detectors
failed_sp_list,nSpectra = get_failed_spectra_list_from_ws(masking)
failed_sp_list,nSpectra = get_failed_spectra_list(masking)
nMaskedSpectra = len(failed_sp_list)
print 'Diag processed workspace with {0:d} spectra and found {1:d} bad spectra'.format(nSpectra,nMaskedSpectra)
#Run the conversion first on the sample
Expand Down Expand Up @@ -303,17 +302,6 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file=None,monovan_run=None,**
return deltaE_wkspace_sample


def mask_workspace(run_number,mask_ws) :
"""
mask workspace defined by the run number with masks defined in masking workspace
"""
run_ws=common.load_run(run_number)

#if Reducer.map_file != None:
# run_ws=GroupDetectors(InputWorkspace=run_ws,OutputWorkspace=run_ws,
# MapFile= Reducer.map_file, KeepUngroupedSpectra=0, Behaviour='Average')

MaskDetectors(Workspace=run_ws, MaskedWorkspace=mask_ws)

def abs_units(wb_for_run,sample_run,monovan_run,wb_for_monovanadium,samp_rmm,samp_mass,ei_guess,rebin,map_file,monovan_mapfile,**kwargs):
"""
Expand Down
23 changes: 12 additions & 11 deletions Code/Mantid/scripts/Inelastic/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,19 @@ def diagnose(white_int, **kwargs):
test_results[4] = [str(__bleed_masks), failures]
add_masking(white_int, __bleed_masks)
DeleteWorkspace(__bleed_masks)

if hasattr(parser, 'print_results') and parser.print_results:
start_index_name = "from: start"
default=True
if 'start_index' in kwargs:
default = False
start_index_name = "from: "+str(kwargs['start_index'])
end_index_name=" to: end"
if 'end_index' in kwargs :
default = False
end_index_name = " to: "+str(kwargs['end_index'])
# endif not hard_mask_only
start_index_name = "from: start"
end_index_name=" to: end"
default = True
if hasattr(parser, 'print_results') and parser.print_results:
default=True
if 'start_index' in kwargs:
default = False
start_index_name = "from: "+str(kwargs['start_index'])
if 'end_index' in kwargs :
default = False
end_index_name = " to: "+str(kwargs['end_index'])


testName=start_index_name+end_index_name
if not default :
Expand Down

0 comments on commit 3711e07

Please sign in to comment.