parameter adding and removing now fully functional according to tests, including fixes

This commit is contained in:
Max Zwiessele 2014-02-13 16:44:45 +00:00
parent f71af38505
commit 13212abd0b
4 changed files with 123 additions and 8 deletions

View file

@ -57,6 +57,7 @@ class ParameterIndexOperations(object):
You can give an offset to set an offset for the given indices in the
index array, for multi-param handling.
'''
_offset = 0
def __init__(self, constraints=None):
self._properties = IntArrayDict()
if constraints is not None:
@ -120,6 +121,14 @@ class ParameterIndexOperations(object):
return removed.astype(int)
return numpy.array([]).astype(int)
def update(self, parameter_index_view, offset=0):
for i, v in parameter_index_view.iteritems():
self.add(i, v+offset)
def copy(self):
return ParameterIndexOperations(dict(self.iteritems()))
def __getitem__(self, prop):
return self._properties[prop]
@ -223,9 +232,9 @@ class ParameterIndexOperationsView(object):
import pprint
return pprint.pformat(dict(self.iteritems()))
def update(self, parameter_index_view):
def update(self, parameter_index_view, offset=0):
for i, v in parameter_index_view.iteritems():
self.add(i, v)
self.add(i, v+offset)
def copy(self):

View file

@ -72,6 +72,13 @@ class Parentable(object):
def has_parent(self):
return self._direct_parent_ is not None
def _notify_parent_change(self):
for p in self._parameters_:
p._parent_changed(self)
def _parent_changed(self):
raise NotImplementedError, "shouldnt happen, Parentable objects need to be able to change their parent"
@property
def _highest_parent_(self):
if self._direct_parent_ is None:
@ -182,11 +189,9 @@ class Constrainable(Nameable, Indexable, Parameterizable):
# Constrain operations -> done
#===========================================================================
def _parent_changed(self, parent):
c = self.constraints
from index_operations import ParameterIndexOperationsView
self.constraints = ParameterIndexOperationsView(parent.constraints, parent._offset_for(self), self.size)
self.constraints.update(c)
del c
self._fixes_ = None
for p in self._parameters_:
p._parent_changed(parent)

View file

@ -87,17 +87,20 @@ class Parameterized(Constrainable, Pickleable, Observable, Gradcheckable):
elif param not in self._parameters_:
# make sure the size is set
if index is None:
self.constraints.update(param.constraints, self.size)
self._parameters_.append(param)
else:
start = sum(p.size for p in self._parameters_[:index])
self.constraints.shift(start, param.size)
self.constraints.update(param.constraints, start)
self._parameters_.insert(index, param)
self.size += param.size
else:
raise RuntimeError, """Parameter exists already added and no copy made"""
self._connect_parameters()
for p in self._parameters_:
p._parent_changed(self)
self._notify_parent_change()
self._connect_fixes()
def add_parameters(self, *parameters):
"""
@ -120,7 +123,13 @@ class Parameterized(Constrainable, Pickleable, Observable, Gradcheckable):
param._direct_parent_ = None
param._parent_index_ = None
param._connect_fixes()
param._notify_parent_change()
pname = adjust_name_for_printing(param.name)
if pname in self._added_names_:
del self.__dict__[pname]
self._connect_parameters()
#self._notify_parent_change()
self._connect_fixes()
def _connect_parameters(self):
# connect parameterlist to this parameterized object
@ -149,7 +158,6 @@ class Parameterized(Constrainable, Pickleable, Observable, Gradcheckable):
elif not (pname in not_unique):
self.__dict__[pname] = p
self._added_names_.add(pname)
self._connect_fixes()
#===========================================================================
# Pickling operations