Package mvpa :: Package misc :: Module param
[hide private]
[frames] | no frames]

Source Code for Module mvpa.misc.param

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set sts=4 ts=4 sw=4 et: 
  3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  4  # 
  5  #   See COPYING file distributed along with the PyMVPA package for the 
  6  #   copyright and license terms. 
  7  # 
  8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##g 
  9  """Parameter representation""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  from mvpa.misc.state import CollectableAttribute 
 14   
 15  if __debug__: 
 16      from mvpa.misc import debug 
17 18 -class Parameter(CollectableAttribute):
19 """This class shall serve as a representation of a parameter. 20 21 It might be useful if a little more information than the pure parameter 22 value is required (or even only useful). 23 24 Each parameter must have a value. However additional property can be 25 passed to the constructor and will be stored in the object. 26 27 BIG ASSUMPTION: stored values are not mutable, ie nobody should do 28 29 cls.parameter1[:] = ... 30 31 or we wouldn't know that it was changed 32 33 Here is a list of possible property names: 34 35 min - minimum value 36 max - maximum value 37 step - increment/decrement stepsize 38 """ 39
40 - def __init__(self, default, name=None, doc=None, **kwargs):
41 """Specify a parameter by its default value and optionally an arbitrary number 42 of additional parameters. 43 44 TODO: :Parameters: for Parameter 45 """ 46 self.__default = default 47 48 CollectableAttribute.__init__(self, name, doc) 49 50 self.resetvalue() 51 self._isset = False 52 53 if __debug__: 54 if kwargs.has_key('val'): 55 raise ValueError, "'val' property name is illegal." 56 57 # XXX probably is too generic... 58 for k, v in kwargs.iteritems(): 59 self.__setattr__(k, v)
60 61
62 - def __str__(self):
63 res = CollectableAttribute.__str__(self) 64 res += '=%s' % self.value # it is enabled but no value is assigned yet 65 return res
66 67
68 - def resetvalue(self):
69 """Reset value to the default""" 70 #CollectableAttribute.reset(self) 71 if not self.isDefault: 72 self._isset = True 73 self.value = self.__default
74
75 - def _set(self, val):
76 if self._value != val: 77 if __debug__: 78 debug("COL", 79 "Parameter: setting %s to %s " % (str(self), val)) 80 if hasattr(self, 'min') and val < self.min: 81 raise ValueError, \ 82 "Minimal value for parameter %s is %s. Got %s" % \ 83 (self.name, self.min, val) 84 if hasattr(self, 'max') and val > self.max: 85 raise ValueError, \ 86 "Maximal value for parameter %s is %s. Got %s" % \ 87 (self.name, self.max, val) 88 if hasattr(self, 'choices') and (not val in self.choices): 89 raise ValueError, \ 90 "Valid choices for parameter %s are %s. Got %s" % \ 91 (self.name, self.choices, val) 92 self._value = val 93 self._isset = True 94 elif __debug__: 95 debug("COL", 96 "Parameter: not setting %s since value is the same" % (str(self)))
97 98 @property
99 - def isDefault(self):
100 """Returns True if current value is bound to default one""" 101 return self._value is self.default
102 103 @property
104 - def equalDefault(self):
105 """Returns True if current value is equal to default one""" 106 return self._value == self.__default
107
108 - def setDefault(self, value):
109 wasdefault = self.isDefault 110 self.__default = value 111 if wasdefault: 112 self.resetvalue() 113 self._isset = False
114 115 # incorrect behavior 116 #def reset(self): 117 # """Override reset so we don't clean the flag""" 118 # pass 119 120 default = property(fget=lambda x:x.__default, fset=setDefault) 121 value = property(fget=lambda x:x._value, fset=_set)
122
123 -class KernelParameter(Parameter):
124 """Just that it is different beast""" 125 pass
126