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  import textwrap 
 14  from mvpa.misc.state import CollectableAttribute 
 15   
 16  if __debug__: 
 17      from mvpa.base import debug 
 18   
19 -class Parameter(CollectableAttribute):
20 """This class shall serve as a representation of a parameter. 21 22 It might be useful if a little more information than the pure parameter 23 value is required (or even only useful). 24 25 Each parameter must have a value. However additional property can be 26 passed to the constructor and will be stored in the object. 27 28 BIG ASSUMPTION: stored values are not mutable, ie nobody should do 29 30 cls.parameter1[:] = ... 31 32 or we wouldn't know that it was changed 33 34 Here is a list of possible property names: 35 36 min - minimum value 37 max - maximum value 38 step - increment/decrement stepsize 39 """ 40
41 - def __init__(self, default, name=None, doc=None, index=None, **kwargs):
42 """Specify a parameter by its default value and optionally an arbitrary 43 number of additional parameters. 44 45 TODO: :Parameters: for Parameter 46 """ 47 self.__default = default 48 49 CollectableAttribute.__init__(self, name=name, doc=doc, index=index) 50 51 self.resetvalue() 52 self._isset = False 53 54 if __debug__: 55 if kwargs.has_key('val'): 56 raise ValueError, "'val' property name is illegal." 57 58 # XXX probably is too generic... 59 for k, v in kwargs.iteritems(): 60 self.__setattr__(k, v)
61 62
63 - def __str__(self):
64 res = CollectableAttribute.__str__(self) 65 # it is enabled but no value is assigned yet 66 res += '=%s' % (self.value,) 67 return res
68 69
70 - def doc(self, indent=" ", width=70):
71 """Docstring for the parameter to be used in lists of parameters 72 73 :Returns: 74 string or list of strings (if indent is None) 75 """ 76 paramsdoc = " %s" % self.name 77 if hasattr(paramsdoc, 'allowedtype'): 78 paramsdoc += " : %s" % self.allowedtype 79 paramsdoc = [paramsdoc] 80 try: 81 doc = self.__doc__ 82 if not doc.endswith('.'): doc += '.' 83 try: 84 doc += " (Default: %s)" % self.default 85 except: 86 pass 87 paramsdoc += [' ' + x 88 for x in textwrap.wrap(doc, width=width-len(indent))] 89 except Exception, e: 90 pass 91 92 if indent is None: 93 return paramsdoc 94 else: 95 return ('\n' + indent).join(paramsdoc)
96 97 98 # XXX should be named reset2default? correspondingly in 99 # ParameterCollection as well
100 - def resetvalue(self):
101 """Reset value to the default""" 102 #CollectableAttribute.reset(self) 103 if not self.isDefault: 104 self._isset = True 105 self.value = self.__default
106
107 - def _set(self, val):
108 if self._value != val: 109 if __debug__: 110 debug("COL", 111 "Parameter: setting %s to %s " % (str(self), val)) 112 if hasattr(self, 'min') and val < self.min: 113 raise ValueError, \ 114 "Minimal value for parameter %s is %s. Got %s" % \ 115 (self.name, self.min, val) 116 if hasattr(self, 'max') and val > self.max: 117 raise ValueError, \ 118 "Maximal value for parameter %s is %s. Got %s" % \ 119 (self.name, self.max, val) 120 if hasattr(self, 'choices') and (not val in self.choices): 121 raise ValueError, \ 122 "Valid choices for parameter %s are %s. Got %s" % \ 123 (self.name, self.choices, val) 124 self._value = val 125 self._isset = True 126 elif __debug__: 127 debug("COL", 128 "Parameter: not setting %s since value is the same" \ 129 % (str(self)))
130 131 @property
132 - def isDefault(self):
133 """Returns True if current value is bound to default one""" 134 return self._value is self.default
135 136 @property
137 - def equalDefault(self):
138 """Returns True if current value is equal to default one""" 139 return self._value == self.__default
140
141 - def setDefault(self, value):
142 wasdefault = self.isDefault 143 self.__default = value 144 if wasdefault: 145 self.resetvalue() 146 self._isset = False
147 148 # incorrect behavior 149 #def reset(self): 150 # """Override reset so we don't clean the flag""" 151 # pass 152 153 default = property(fget=lambda x:x.__default, fset=setDefault) 154 value = property(fget=lambda x:x._value, fset=_set)
155
156 -class KernelParameter(Parameter):
157 """Just that it is different beast""" 158 pass
159