score:0

It's not clear to me what's not working, but I think maybe you're trying to do something like this:

import numpy

class X(object):

    def __init__(self, parent):
        self.parent = parent
        self.pid = [0, 1, 2]

    @property
    def values(self):
        tmp = self.parent.P[self.pid]
        return tmp
    @values.setter
    def values(self, input):
        self.parent.P[self.pid] = input

class Node(object):

    def __init__(self):
        self.P = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        self._values = X(self)

    @property
    def x(self):
        return self._values.values
    @x.setter
    def x(self, input):
        self._values.values = input

I hope that get's you started.

update

The reason that n.x[1:3] = [77, 88] doesn't work using this approach is because n.x and n.x[:] = ~ both call the get method of X.values which returns tmp. But tmp is a copy of part of P and after n.x[:] = ~ tmp is thrown away and P is not updated. tmp is a copy because when you index an array with another array you get a copy not a view. Here is an example to make that more clear, you can read more about numpy slicing/indexing here.

>>> P = np.arange(10)
>>> pid = np.array([1, 2, 3])
>>> Q = P[pid]
>>> Q[:] = 99
>>> P
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> R = P[1:4]
>>> R[:] = 99
>>> P
array([ 0, 99, 99, 99,  4,  5,  6,  7,  8,  9])
>>> P[[1,2]][:] = 88
>>> P
array([ 0, 99, 99, 99,  4,  5,  6,  7,  8,  9])

setitem won't help, because you're calling the setitem method of the tmp not X.

The easiest way to make it work is to replace the pid array with a slice, but I know that's kind of limiting. You could also keep track of the tmp array, have a self._tmp so you can move the values from _tmp to P later. I know neither of those are perfect, but maybe someone else here will come up with a better approach. Sorry I couldn't do more.


Related Query