/usr/share/pyshared/mvpa2/base/param.py is in python-mvpa2 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##g
"""Parameter representation"""
__docformat__ = 'restructuredtext'
import re
import textwrap
import numpy as np
from mvpa2.base.state import IndexedCollectable
if __debug__:
from mvpa2.base import debug
_whitespace_re = re.compile('\n\s+|^\s+')
__all__ = [ 'Parameter', 'KernelParameter' ]
class Parameter(IndexedCollectable):
"""This class shall serve as a representation of a parameter.
It might be useful if a little more information than the pure parameter
value is required (or even only useful).
Each parameter must have a value. However additional attributes can be
passed to the constructor and will be stored in the object.
Notes
-----
BIG ASSUMPTION: stored values are not mutable, ie nobody should do
cls.parameter1[:] = ...
or we wouldn't know that it was changed
Here is a list of possible additional attributes:
allowedtype : str
Description of what types are allowed
min
Minimum value
max
Maximum value
step
Increment/decrement step size hint for optimization
"""
def __init__(self, default, ro=False, index=None, value=None,
name=None, doc=None, **kwargs):
"""Specify a Parameter with a default value and arbitrary
number of additional attributes.
Parameters
----------
name : str
Name of the parameter under which it should be available in its
respective collection.
doc : str
Documentation about the purpose of this parameter.
index : int or None
Index of parameter among the others. Determines order of listing
in help. If None, order of instantiation determines the index.
ro : bool
Either value which will be assigned in the constructor is read-only and
cannot be changed
value
Actual value of the parameter to be assigned
"""
# XXX probably is too generic...
# and potentially dangerous...
# let's at least keep track of what is passed
self._additional_props = []
for k, v in kwargs.iteritems():
self.__setattr__(k, v)
self._additional_props.append(k)
self.__default = default
self._ro = ro
# needs to come after kwargs processing, since some debug statements
# rely on working repr()
# value is not passed since we need to invoke _set with init=True
# below
IndexedCollectable.__init__(self, index=index, # value=value,
name=name, doc=doc)
self._isset = False
if value is None:
self._set(self.__default, init=True)
else:
self._set(value, init=True)
if __debug__:
if kwargs.has_key('val'):
raise ValueError, "'val' property name is illegal."
def __reduce__(self):
icr = IndexedCollectable.__reduce__(self)
# Collect all possible additional properties which were passed
# to the constructor
state = dict([(k, getattr(self, k)) for k in self._additional_props])
state['_additional_props'] = self._additional_props
state.update(icr[2])
res = (self.__class__, (self.__default, self._ro) + icr[1], state)
#if __debug__ and 'COL_RED' in debug.active:
# debug('COL_RED', 'Returning %s for %s' % (res, self))
return res
def __str__(self):
res = IndexedCollectable.__str__(self)
# it is enabled but no value is assigned yet
res += '=%s' % (self.value,)
return res
def __repr__(self):
# cannot use IndexedCollectable's repr(), since the contructor
# needs to handle the mandatory 'default' argument
# TODO: so what? just tune it up ;)
# TODO: think what to do with index parameter...
s = "%s(%s, name=%s, doc=%s" % (self.__class__.__name__,
self.__default,
repr(self.name),
repr(self.__doc__))
plist = ["%s=%s" % (p, self.__getattribute__(p))
for p in self._additional_props]
if len(plist):
s += ', ' + ', '.join(plist)
if self._ro:
s += ', ro=True'
if not self.is_default:
s += ', value=%r' % self.value
s += ')'
return s
def _paramdoc(self, indent=" ", width=70):
"""Docstring for the parameter to be used in lists of parameters
Returns
-------
string or list of strings (if indent is None)
"""
paramsdoc = '%s' % self.name
if hasattr(paramsdoc, 'allowedtype'):
paramsdoc += " : %s" % self.allowedtype
paramsdoc = [paramsdoc]
try:
doc = self.__doc__.strip()
if not doc.endswith('.'): doc += '.'
try:
doc += " (Default: %r)" % (self.default,)
except:
pass
# Explicitly deal with multiple spaces, for some reason
# replace_whitespace is non-effective
doc = _whitespace_re.sub(' ', doc)
paramsdoc += [indent + x
for x in textwrap.wrap(doc, width=width-len(indent),
replace_whitespace=True)]
except Exception, e:
pass
return '\n'.join(paramsdoc)
# XXX should be named reset2default? correspondingly in
# ParameterCollection as well
def reset_value(self):
"""Reset value to the default"""
#IndexedCollectable.reset(self)
if not self.is_default and not self._ro:
self._isset = True
self.value = self.__default
def _set(self, val, init=False):
different_value = self._value != val
isarray = isinstance(different_value, np.ndarray)
if self._ro and not init:
raise RuntimeError, \
"Attempt to set read-only parameter %s to %s" \
% (self.name, val)
if (isarray and np.any(different_value)) or \
((not isarray) and different_value):
if __debug__:
debug("COL",
"Parameter: setting %s to %s " % (str(self), val))
if not isarray:
if hasattr(self, 'min') and val < self.min:
raise ValueError, \
"Minimal value for parameter %s is %s. Got %s" % \
(self.name, self.min, val)
if hasattr(self, 'max') and val > self.max:
raise ValueError, \
"Maximal value for parameter %s is %s. Got %s" % \
(self.name, self.max, val)
if hasattr(self, 'choices') and (not val in self.choices):
raise ValueError, \
"Valid choices for parameter %s are %s. Got %s" % \
(self.name, self.choices, val)
self._value = val
# Set 'isset' only if not called from initialization routine
self._isset = not init #True
elif __debug__:
debug("COL",
"Parameter: not setting %s since value is the same" \
% (str(self)))
@property
def is_default(self):
"""Returns True if current value is bound to default one"""
return self._value is self.default
@property
def equal_default(self):
"""Returns True if current value is equal to default one"""
return self._value == self.__default
def _set_default(self, value):
wasdefault = self.is_default
self.__default = value
if wasdefault:
self.reset_value()
self._isset = False
# incorrect behavior
#def reset(self):
# """Override reset so we don't clean the flag"""
# pass
default = property(fget=lambda x:x.__default, fset=_set_default)
value = property(fget=lambda x:x._value, fset=_set)
class KernelParameter(Parameter):
"""Just that it is different beast"""
pass
|