Skip to content

Commit

Permalink
refs #10532 Defined generic operations over reduction parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Nov 14, 2014
1 parent 7fce5d2 commit 7782b68
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
Expand Up @@ -993,7 +993,7 @@ def initialise(self, instr_name,reload_instrument=False):
'diag_van_out_lo', 'diag_van_out_hi', 'diag_van_lo', 'diag_van_hi', 'diag_van_sig', 'diag_variation',\
'diag_bleed_test','diag_bleed_pixels','diag_bleed_maxrate','diag_hard_mask_file','diag_use_hard_mask_only','diag_background_test_range']

# before starting long run, makes sence to verify if all files requested for the run are in fact available. Here we specify the properties which describe these files
# before starting long run, makes sense to verify if all files requested for the run are in fact available. Here we specify the properties which describe these files
self.__file_properties = ['det_cal_file','map_file','hard_mask_file']
self.__abs_norm_file_properties = ['monovan_mapfile']

Expand Down
104 changes: 0 additions & 104 deletions Code/Mantid/scripts/Inelastic/DirectEnergyConversionHelpers.py

This file was deleted.

180 changes: 180 additions & 0 deletions Code/Mantid/scripts/Inelastic/DirectReductionHelpers.py
@@ -0,0 +1,180 @@
"""
Set of functions to assist with processing instrument parameters relevant to reduction.
"""

def get_default_parameter(instrument, name):
""" Function gets the value of a default instrument parameter and
assign proper(the one defined in IPF ) type to this parameter
@param instrument --
"""

if instrument is None:
raise ValueError("Cannot initiate default parameter, instrument has not been properly defined.")

type_name = instrument.getParameterType(name)
if type_name == "double":
val = instrument.getNumberParameter(name)
elif type_name == "bool":
val = instrument.getBoolParameter(name)
elif type_name == "string":
val = instrument.getStringParameter(name)
if val[0] == "None" :
return None
elif type_name == "int" :
val = instrument.getIntParameter(name)
else :
raise KeyError(" Instrument: {0} does not have parameter with name: {1}".format(instrument.getName(),name))

return val[0]

def get_default_idf_param_list(pInstrument,synonims_list=None):
""" Obtain default reduction parameters list from the instrument """

params = pInstrument.getParameterNames();
par_list = {};
for name in params:
par_list[name] = get_default_parameter(pInstrument,name);


return par_list;



def build_coupled_keys_dict(param_map,synonims) :
"""function to build the dictionary of the keys which are expressed through other keys
e.g. builds dictionary from strings in a form key1 = key2:key3
in the form key1 = ['key2','key3']
"""

# dictionary used for substituting composite keys.
composite_keys = dict();

for name in param_map:
if name in synonims:
final_name = synonims[name];
else:
final_name = name
composite_keys[final_name]=None;

param_keys = composite_keys.keys();

for name,val in param_map.items() :
if name in synonims:
final_name = synonims[name];
else:
final_name = name

if isinstance(val,str):
val = val.strip()
keys_candidates = val.split(":")
n_keys = len(keys_candidates)
#
if n_keys>1 : # this is the property we want to modify
composite_keys[final_name]= list();
for key in keys_candidates :
if key in synonims:
rkey = synonims[key];
else:
rkey = key;
if rkey in param_keys:
composite_keys[final_name].append(rkey);
else:
raise KeyError('Substitution key : {0} is not in the list of allowed keys'.format(rkey));
else:
composite_keys[final_name] = keys_candidates[0];
else:
composite_keys[final_name]=val;

return composite_keys



def build_subst_dictionary(synonims_list=None) :
"""Function to process "synonims_list" in the instrument parameters string, used to support synonyms in the reduction script
it takes string of synonyms in the form key1=subs1=subst2=subts3;key2=subst4 and returns the dictionary
in the form dict[subs1]=key1 ; dict[subst2] = key1 ... dict[subst4]=key2
e.g. if one wants to use the IDF key word my_detector instead of e.g. norm-mon1-spec, he has to type
norm-mon1-spec=my_detector in the synonyms field of the IDF parameters file.
"""
if not synonims_list : # nothing to do
return dict();
if type(synonims_list) == dict : # all done
return synonims_list
if type(synonims_list) != str :
raise AttributeError("The synonyms field of Reducer object has to be special format string or the dictionary")
# we are in the right place and going to transform string into dictionary

subst_lines = synonims_list.split(";")
rez = dict()
for lin in subst_lines :
lin=lin.strip()
keys = lin.split("=")
if len(keys) < 2 :
raise AttributeError("The pairs in the synonyms fields have to have form key1=key2=key3 with at least two values present")
if len(keys[0]) == 0:
raise AttributeError("The pairs in the synonyms fields have to have form key1=key2=key3 with at least two values present, but the first key is empty")
for i in xrange(1,len(keys)) :
if len(keys[i]) == 0 :
raise AttributeError("The pairs in the synonyms fields have to have form key1=key2=key3 with at least two values present, but the key"+str(i)+" is empty")
kkk = keys[i].strip();
rez[kkk]=keys[0].strip()

return rez;

def gen_getter(keyval_dict,key):
""" function returns value from dictionary with substitution
e.g. if keyval_dict[A] = 10, keyval_dict[B] = 20 and key_val[C] = [A,B]
gen_getter(keyval_dict,A) == 10; gen_getter(keyval_dict,B) == 20;
and gen_getter(keyval_dict,C) == [10,20];
"""

if key in keyval_dict:
test_val = keyval_dict[key];


if not isinstance(test_val,list):
return test_val;

fin_val = list();
for ik in test_val:
fin_val.append(keyval_dict[ik]);
return fin_val;

else:
raise KeyError(' key with name: {0} is not in the data dictionary'.format(key));

def gen_setter(keyval_dict,key,val):
""" function sets value to dictionary with substitution
e.g. if keyval_dict[A] = 10, keyval_dict[B] = 20 and key_val[C] = [A,B]
gen_setter(keyval_dict,A,20) causes keyval_dict[A] == 20
gen_setter(keyval_dict,B,30) causes keyval_dict[B] == 30
and gen_getter(keyval_dict,C,[1,2]) causes keyval_dict[A] == 1 and keyval_dict[B] == 2
"""

if key in keyval_dict:
test_key = keyval_dict[key];
if not isinstance(test_key,list):
if isinstance(val,list):
raise KeyError(' Key {0} can not assigned a list value'.format(key));
else:
keyval_dict[key] = val;
return;

if isinstance(val,list):
if len(val) != len(test_key):
raise KeyError('Property: {0} needs list of the length {1} to be assigned to it'.format(key,len(test_key)))
else:
raise KeyError(' You can not assign non-list value to list property {0}'.format(key));
pass

for i,key in enumerate(test_key):
keyval_dict[key] = val[i];
return;
else:
raise KeyError(' key with name: {0} is not in the data dictionary'.format(key));
28 changes: 0 additions & 28 deletions Code/Mantid/scripts/test/DirectEnergyConversionTest.py
Expand Up @@ -21,34 +21,6 @@ def setUp(self):
def tearDown(self):
pass

def test_build_subst_dictionary(self):
self.assertEqual(dict(), DirectEnergyConversion.build_subst_dictionary(""))
self.assertEqual(dict(),DirectEnergyConversion.build_subst_dictionary())

self.assertRaises(AttributeError,DirectEnergyConversion.build_subst_dictionary,10)
self.assertRaises(AttributeError,DirectEnergyConversion.build_subst_dictionary,"A=")
self.assertRaises(AttributeError,DirectEnergyConversion.build_subst_dictionary,"B=C;A=")

rez=dict();
rez['A']='B';
self.assertEqual(rez, DirectEnergyConversion.build_subst_dictionary(rez))

myDict = DirectEnergyConversion.build_subst_dictionary("A=B")
self.assertEqual(myDict['B'],'A')

myDict = DirectEnergyConversion.build_subst_dictionary("A=B;C=DD")
self.assertEqual(myDict['B'],'A')
self.assertEqual(myDict['DD'],'C')
myDict = DirectEnergyConversion.build_subst_dictionary("A=B=C=DD")
self.assertEqual(myDict['B'],'A')
self.assertEqual(myDict['DD'],'A')
self.assertEqual(myDict['C'],'A')

myDict = DirectEnergyConversion.build_subst_dictionary("A = B = C=DD")
self.assertEqual(myDict['B'],'A')
self.assertEqual(myDict['DD'],'A')
self.assertEqual(myDict['C'],'A')

#def test_build_coupled_keys_dict_simple(self):
# params = ["];

Expand Down

0 comments on commit 7782b68

Please sign in to comment.