Exemplo n.º 1
0
    def setup_weights(self, weights):
        """ Setup weights """

        if type(weights) in [int,float]:
            weights = np.ones((1,)*len(self.source.shape))*weights
        dtype = weights.dtype

        # Is kernel already a sparse array ?
        if sparse.issparse(weights):
            if weights.shape != (self.target.size, self.source.size):
                raise ConnectionError, \
                    'weights matrix shape is wrong relative to source and target'
            else:
                W = weights.tocoo()
                data, row, col  = W.data,W.row,W.col
                i = (1 - np.isnan(data)).nonzero()
                data, row, col = data[i], row[i], col[i]
                data = np.where(data, data, np.NaN)
                weights = sparse.coo_matrix((data,(row,col)), shape=W.shape)
                weights.data = np.nan_to_num(data)
        # Else, we need to build it
        elif weights.shape != (self.target.size,self.source.size):
            if len(weights.shape) == len(self.source.shape):
                # If we have a toric connection, weights cannot be greater than source
                # in any dimension
                if self._toric:
                    s = np.array(self.source.shape)
                    w = np.array(weights.shape)
                    weights = extract(weights, np.minimum(s,w), w//2)
                weights = convolution_matrix(self.source, self.target, weights, self._toric)
            else:
                raise ConnectionError, \
                    'weights matrix shape is wrong relative to source and target'
        self._weights = csr_array(weights, dtype=dtype)
Exemplo n.º 2
0
    def setup_weights(self, weights):
        """ Setup weights """


        if type(weights) in [int,float]:
            weights = np.ones((1,)*len(self.source.shape))*weights

        if weights.shape == (self.target.size, self.source.size):
            self._weights = weights
            self._mask = 1-np.isnan(self._weights).astype(np.int32)
            if self._mask.all():
                self._mask = 1
            return

        if len(weights.shape) != len(weights.shape):
            raise ConnectionError, \
                'Weights matrix shape is wrong relative to source and target'

        # If we have a toric connection, weights cannot be greater than source
        # in any dimension
        if self._toric:
            s = np.array(self.source.shape)
            w = np.array(weights.shape)
            weights = extract(weights, np.minimum(s,w), w//2)

        K = convolution_matrix(self.source, self.target, weights, self._toric)
        nz_rows = K.row
        nz_cols = K.col
        self._weights = K.todense()
        self._mask = np.zeros(K.shape)
        self._mask[nz_rows, nz_cols] = 1
        if self._mask.all(): self._mask = 1
        self._weights = np.array(K.todense())
Exemplo n.º 3
0
    def setup_weights(self, weights):
        """ Setup weights """

        if type(weights) in [int, float]:
            weights = np.ones((1, ) * len(self.source.shape)) * weights

        if weights.shape == (self.target.size, self.source.size):
            self._weights = weights
            self._mask = 1 - np.isnan(self._weights).astype(np.int32)
            if self._mask.all():
                self._mask = 1
            return

        if len(weights.shape) != len(weights.shape):
            raise ConnectionError, \
                'Weights matrix shape is wrong relative to source and target'

        # If we have a toric connection, weights cannot be greater than source
        # in any dimension
        if self._toric:
            s = np.array(self.source.shape)
            w = np.array(weights.shape)
            weights = extract(weights, np.minimum(s, w), w // 2)

        K = convolution_matrix(self.source, self.target, weights, self._toric)
        nz_rows = K.row
        nz_cols = K.col
        self._weights = K.todense()
        self._mask = np.zeros(K.shape)
        self._mask[nz_rows, nz_cols] = 1
        if self._mask.all(): self._mask = 1
        self._weights = np.array(K.todense())
Exemplo n.º 4
0
    def setup_weights(self, weights):
        """ Setup weights """

        if type(weights) in [int, float]:
            weights = np.ones((1, ) * len(self.source.shape)) * weights
        dtype = weights.dtype

        # Is kernel already a sparse array ?
        if sparse.issparse(weights):
            if weights.shape != (self.target.size, self.source.size):
                raise ConnectionError, \
                    'weights matrix shape is wrong relative to source and target'
            else:
                W = weights.tocoo()
                data, row, col = W.data, W.row, W.col
                i = (1 - np.isnan(data)).nonzero()
                data, row, col = data[i], row[i], col[i]
                data = np.where(data, data, np.NaN)
                weights = sparse.coo_matrix((data, (row, col)), shape=W.shape)
                weights.data = np.nan_to_num(data)
        # Else, we need to build it
        elif weights.shape != (self.target.size, self.source.size):
            if len(weights.shape) == len(self.source.shape):
                # If we have a toric connection, weights cannot be greater than source
                # in any dimension
                if self._toric:
                    s = np.array(self.source.shape)
                    w = np.array(weights.shape)
                    weights = extract(weights, np.minimum(s, w), w // 2)
                weights = convolution_matrix(self.source, self.target, weights,
                                             self._toric)
            else:
                raise ConnectionError, \
                    'weights matrix shape is wrong relative to source and target'
        self._weights = csr_array(weights, dtype=dtype)