<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# DANA - Distributed (Asynchronous) Numerical Adaptive computing framework
# Copyright (C) 2009-2010  <NAME>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
'''
DenseConnection

'''
import numpy as np
from functions import extract
from connection import Connection, ConnectionError


class DenseConnection(Connection):
    ''' '''

    def __init__(self, source=None, target=None, weights=None, equation = ''):
        ''' '''

        Connection.__init__(self, source, target)
        self.setup_weights(weights)
        self.setup_equation(equation)


    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'

        Ks = np.array(list(weights.shape), dtype=int)//2
        Ss = np.array(list(self.source.shape), dtype=int)//2
        K = np.zeros((self.target.size,self.source.size), dtype=weights.dtype)
        for i in range(K.shape[0]):
            index = np.array(list(np.unravel_index(i, self.target.shape)))
            index = np.fix((index/np.array(self.target.shape, dtype=weights.dtype) \
                             * np.array(self.source.shape))).astype(int)
            K[i,:] = extract(weights,
                             self.source.shape, Ks+Ss-index, np.NaN).flatten()
        self._mask = 1 - np.isnan(K).astype(int)
        if self._mask.all():
            self._mask = 1
        self._weights = np.nan_to_num(K)


    def output(self):
        ''' '''
        #if not hasattr(self._source,'mask'):
        R = np.dot(self._weights, self._actual_source.flatten()) 
        #else:
        #mask = self._source.mask
        #R = np.dot(self._weights, (self._actual_source*mask).flatten()) 
        return R.reshape(self._target.shape)


    def __getitem__(self, key):
        ''' '''

        key = np.array(key) % self.target.shape
        K = np.zeros((len(key)+1,),dtype=np.int32)
        K[1:] = key
        S = np.ones((len(self.target.shape)+1),dtype=np.int32)
        S[:-1] = self.target.shape
        index = (S*K).sum()
#        key = np.array(key) % self.target.shape
#        s = np.ones((len(self.target.shape),))
#        for i in range(0,len(self.target.shape)-1):
#            s[i] = self.target.shape[i]*s[i+1]
#        index = int((s*key).sum())
        if type(self._mask) is np.ndarray:
            K = self.weights[index]*np.where(self._mask[index], 1, np.NaN)<commit_msg>Modify matrix building code to use the new convolution matrix method<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# DANA - Distributed (Asynchronous) Numerical Adaptive computing framework
# Copyright (C) 2009-2010  <NAME>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
'''
DenseConnection

'''
import numpy as np
from functions import extract
from functions import extract, convolution_matrix
from connection import Connection, ConnectionError


class DenseConnection(Connection):
    ''' '''

    def __init__(self, source=None, target=None, weights=None, equation = ''):
        ''' '''

        Connection.__init__(self, source, target)
        self.setup_weights(weights)
        self.setup_equation(equation)


    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'

        K = convolution_matrix(self.source, self.target, weights)
        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

#        Ks = np.array(list(weights.shape), dtype=int)//2
#        Ss = np.array(list(self.source.shape), dtype=int)//2
#        K = np.zeros((self.target.size,self.source.size), dtype=weights.dtype)
#        for i in range(K.shape[0]):
#            index = np.array(list(np.unravel_index(i, self.target.shape)))
#            index = np.fix((index/np.array(self.target.shape, dtype=weights.dtype) \
#                             * np.array(self.source.shape))).astype(int)
#            K[i,:] = extract(weights,
#                             self.source.shape, Ks+Ss-index, np.NaN).flatten()
#        self._mask = 1 - np.isnan(K).astype(int)
        if self._mask.all():
            self._mask = 1
        self._weights = np.array(K.todense()) #np.nan_to_num(K)


    def output(self):
        ''' '''
        #if not hasattr(self._source,'mask'):
        R = np.dot(self._weights, self._actual_source.flatten()) 
        #else:
        #mask = self._source.mask
        #R = np.dot(self._weights, (self._actual_source*mask).flatten()) 
        return R.reshape(self._target.shape)


    def __getitem__(self, key):
        ''' '''

        key = np.array(key) % self.target.shape
        K = np.zeros((len(key)+1,),dtype=np.int32)
        K[1:] = key
        S = np.ones((len(self.target.shape)+1),dtype=np.int32)
        S[:-1] = self.target.shape
        index = (S*K).sum()
#        key = np.array(key) % self.target.shape
#        s = np.ones((len(self.target.shape),))
#        for i in range(0,len(self.target.shape)-1):
#            s[i] = self.target.shape[i]*s[i+1]
#        index = int((s*key).sum())
        if type(self._mask) is np.ndarray:
            K = self.weights[index]*np.where(self._mask[index], 1, np.NaN)
{'fraction_non_alphanumeric': 0.18931821572430255, 'fraction_numerical': 0.009398776667163957, 'mean_word_length': 1.9835336003560302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4298621', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2001, 'n_words': 410}