Skip to content

Commit

Permalink
Re #10836 Descriptor for properties with range of values
Browse files Browse the repository at this point in the history
Class modified to use descriptors for properties with range of values
  • Loading branch information
abuts committed Jan 12, 2015
1 parent 37cdebb commit 76f16eb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
1 change: 0 additions & 1 deletion Code/Mantid/scripts/Inelastic/Direct/NonIDF_Properties.py
Expand Up @@ -78,7 +78,6 @@ def getDefaultParameterValue(self,par_name):
facility = InstrumentDependentProp('_facility')
#
van_rmm = VanadiumRMM()

#-----------------------------------------------------------------------------------
@property
def instrument(self):
Expand Down
35 changes: 27 additions & 8 deletions Code/Mantid/scripts/Inelastic/Direct/PropertiesDescriptors.py
Expand Up @@ -162,13 +162,6 @@ def check_ei_bin_consistent(ei,binning_range):

return (True,'')

#-----------------------------------------------------------------------------------------
# END Descriptors for NonIDF_Properties class
#-----------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------
# Descriptors, providing overloads for some complex properties in PropertyManager
#-----------------------------------------------------------------------------------------
class VanadiumRMM(object):
""" define constant static rmm for vanadium """
def __get__(self,instance,owner=None):
Expand All @@ -178,7 +171,33 @@ def __get__(self,instance,owner=None):
def __set__(self,instance,value):
raise AttributeError("Can not change vanadium rmm");
#end VanadiumRMM
#
#-----------------------------------------------------------------------------------------
# END Descriptors for NonIDF_Properties class
#-----------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------
# Descriptors, providing overloads for some complex properties in PropertyManager
#-----------------------------------------------------------------------------------------
class PropertyFromRange(object):
""" Descriptor for property, which can have one value from a list of values """
def __init__(self,availible_values,default_value):
self._availible_values = availible_values
self.__set__(None,default_value)

def __get__(self,instance,owner):
""" Return current value for the property with range of values. """
if instance is None:
return self
return self._current_value

def __set__(self,instance,val):
""" set detector calibration file using various formats """
if val in self._availible_values:
self._current_value = val
else:
raise KeyError(' Property can not have value {0}'.format(val))


class DetCalFile(object):
""" property describes various sources for the detector calibration file """
def __init__(self):
Expand Down
24 changes: 7 additions & 17 deletions Code/Mantid/scripts/Inelastic/Direct/PropertyManager.py
Expand Up @@ -28,7 +28,7 @@ class PropertyManager(NonIDF_Properties):
3) Attempt to create property not present in this file throws.
4) A standard behavior is defined for the most of the properties (get/set appropriate value) when there is number of
overloaded properties, which support more complex behavior using specially written Descriptors
5) Changes to the properties are recorded and list of changed properties is available on request
5) Changes to the properties are recorded and the list of changed properties is available on request
########
design remarks:
Expand Down Expand Up @@ -82,8 +82,8 @@ def __init__(self,Instrument,instr_run=None):
# Overloaded parameters, defined through properties rather then descriptors
object.__setattr__(self,'_mask_run',None)

# define private properties served the class
private_properties = {'descriptors':[],'subst_dict':{},'prop_allowed_values':{},'changed_properties':set(),
# define private properties served the class (Accessible through __Property_name call
private_properties = {'descriptors':[],'subst_dict':{},'changed_properties':set(),
'file_properties':[],'abs_norm_file_properties':[]}
# place these properties to __dict__ with proper decoration
self._init_private_properties(private_properties)
Expand All @@ -110,10 +110,6 @@ def __init__(self,Instrument,instr_run=None):
object.__setattr__(self,class_dec+'abs_norm_file_properties',['monovan_mapfile'])

# properties with allowed values
self.__prop_allowed_values['normalise_method']=[None,'monitor-1','monitor-2','current'] # 'uamph', peak -- disabled/unknown at the moment
self.__prop_allowed_values['deltaE_mode']=['direct'] # I do not think we should try other modes here



def _convert_params_to_properties(self,param_list,detine_subst_dict=True,descr_list=[]):
""" method processes parameters obtained from IDF and modifies the IDF properties
Expand Down Expand Up @@ -184,15 +180,7 @@ def __setattr__(self,name0,val):

if type(val) is list and len(val) == 0:
val = None;


# Check allowed values property if value allowed TODO: re-implement through descriptors
if name in self.__prop_allowed_values:
allowed_values = self.__prop_allowed_values[name];
if not(val in allowed_values):
raise KeyError(" Property {0} can not have value: {1}".format(name,val));
#end


# set property value:
if name in self.__descriptors:
super(PropertyManager,self).__setattr__(name,val)
Expand Down Expand Up @@ -244,7 +232,9 @@ def __getattr__(self,name):
diag_spectra = DiagSpectra()
#
background_test_range = BackbgroundTestRange()

# Properties with allowed value
normalise_method= PropertyFromRange([None,'monitor-1','monitor-2','current'],'current')
deltaE_mode = PropertyFromRange(['direct'],'direct') # other modes should not be considered here
#----------------------------------------------------------------------------------------------------------------
def getChangedProperties(self):
""" method returns set of the properties changed from defaults """
Expand Down
6 changes: 5 additions & 1 deletion Code/Mantid/scripts/test/DirectPropertyManagerTest.py
@@ -1,5 +1,5 @@
import os
#os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
from mantid.simpleapi import *
from mantid import api
import unittest
Expand Down Expand Up @@ -307,6 +307,10 @@ def test_allowed_values(self):

self.assertRaises(KeyError,setattr,propman,'normalise_method','unsupported');

# Only direct method is supported
self.assertEqual(propman.deltaE_mode, 'direct')
self.assertRaises(KeyError,setattr,propman,'deltaE_mode','unsupported');

def test_ki_kf(self):
propman = self.prop_man

Expand Down

0 comments on commit 76f16eb

Please sign in to comment.