From f7f62ec605f1f60e46be6384114da069fd0530c4 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Mon, 7 Oct 2013 07:41:23 +0100 Subject: [PATCH] index operations now work on flattened indices --- GPy/core/index_operations.py | 37 ++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/GPy/core/index_operations.py b/GPy/core/index_operations.py index 1527c989..1e8c86a3 100644 --- a/GPy/core/index_operations.py +++ b/GPy/core/index_operations.py @@ -18,15 +18,37 @@ class ParameterIndexOperations(object): self._properties = {} self._shape = param.shape +# def iteritems(self): +# for prop, indices in self._properties.iteritems(): +# yield prop, self.unravel_indices(indices) + def iteritems(self): - for prop, indices in self._properties.iteritems(): - yield prop, numpy.unravel_index(indices, self._shape) + return self._properties.iteritems() - def keys(self): + def properties(self): return self._properties.keys() - def items(self): - return self._properties.items() + def iterproperties(self): + return self._properties.iterkeys() + + def values(self): + return self._properties.values() + + def itervalues(self): + return self._properties.itervalues() + + def clear(self): + self._properties.clear() + + def size(self): + return reduce(lambda a,b: a+b.size, self.itervalues(), 0) + +# def iterindices(self): +# for indices in self.itervalues(): +# yield self.unravel_indices(indices) + + def iterindices(self): + return self.itervalues() def indices(self, prop): """ @@ -34,7 +56,7 @@ class ParameterIndexOperations(object): these indices can be used as X[indices], which will be a flattened array of all restricted elements """ - return numpy.unravel_index(self._properties[prop], self._shape) + return self.unravel_indices(self._properties[prop]) def add(self, prop, indices): ind = self.create_raveled_indices(indices) @@ -58,3 +80,6 @@ class ParameterIndexOperations(object): else: i = [slice(None), indices] return numpy.array(numpy.ravel_multi_index(numpy.indices(self._shape)[i], self._shape)).flatten() + + def unravel_indices(self, raveled_indices): + return numpy.unravel_index(raveled_indices, self._shape) \ No newline at end of file