<commit_before># -*- coding: utf-8 -*-

"""
Nodenet definition
"""
from copy import deepcopy

import json
import os

import theano
from theano import tensor as T
import numpy as np

import micropsi_core.tools

from micropsi_core.nodenet.nodenet import Nodenet
from micropsi_core.nodenet.node import Node
from threading import Lock
import logging

from micropsi_core.nodenet.theano_engine.theano_node import TheanoNode

NODENET_VERSION = 1

NUMBER_OF_NODES = 1000
NUMBER_OF_ELEMENTS_PER_NODE = 7
NUMBER_OF_ELEMENTS = NUMBER_OF_NODES * NUMBER_OF_ELEMENTS_PER_NODE


class TheanoNodenet(Nodenet):
    """
        theano runtime engine implementation
    """

    # numpy data structures holding the actual data
    w_matrix = None
    a_array = None
    theta_array = None

    # theano tensors for performing operations
    w = None            # matrix of weights
    a = None            # vector of activations
    theta = None        # vector of thetas (i.e. biases)

    @property
    def engine(self):
        return "theano_engine"

    @property
    def current_step(self):
        pass

    @property
    def data(self):
        pass

    def __init__(self, filename, name="", worldadapter="Default", world=None, owner="", uid=None, nodetypes={}, native_modules={}):

        super(TheanoNodenet, self).__init__(name or os.path.basename(filename), worldadapter, world, owner, uid)

        self.w_matrix = np.zeros((NUMBER_OF_ELEMENTS, NUMBER_OF_ELEMENTS), dtype=np.float32)
        self.w = theano.shared(value=self.w_matrix.astype(T.config.floatX), name="w", borrow=True)

        self.a_array = np.zeros(NUMBER_OF_ELEMENTS, dtype=np.float32)
        self.a = theano.shared(value=self.a_array.astype(T.config.floatX), name="a", borrow=True)

        self.theta_array = np.zeros(NUMBER_OF_ELEMENTS, dtype=np.float32)
        self.theta = theano.shared(value=self.theta_array.astype(T.config.floatX), name="theta", borrow=True)

    def step(self):
        pass

    def get_node(self, uid):
        return TheanoNode(self, uid)
        pass

    def get_node_uids(self):
        pass

    def is_node(self, uid):
        pass

    def create_node(self, nodetype, nodespace_uid, position, name="", uid=None, parameters=None, gate_parameters=None):
        pass

    def delete_node(self, uid):
        pass

    def get_nodespace(self, uid):
        pass

    def get_nodespace_uids(self):
        pass

    def is_nodespace(self, uid):
        pass

    def create_nodespace(self, parent_uid, position, name="", uid=None, gatefunction_strings=None):
        pass

    def delete_nodespace(self, uid):
        pass

    def create_link(self, source_node_uid, gate_type, target_node_uid, slot_type, weight=1, certainty=1):
        pass

    def set_link_weight(self, source_node_uid, gate_type, target_node_uid, slot_type, weight=1, certainty=1):
        pass

    def delete_link(self, source_node_uid, gate_type, target_node_uid, slot_type):
        pass

    def reload_native_modules(self, native_modules):
        pass

    def get_nodespace_area_data(self, nodespace_uid, x1, x2, y1, y2):<commit_msg>Node creation and deletion<commit_after># -*- coding: utf-8 -*-

"""
Nodenet definition
"""
from copy import deepcopy

import json
import os

import theano
from theano import tensor as T
import numpy as np

import micropsi_core.tools

from micropsi_core.nodenet.nodenet import Nodenet
from micropsi_core.nodenet.node import Node
from threading import Lock
import logging

from micropsi_core.nodenet.theano_engine.theano_node import TheanoNode

NODENET_VERSION = 1

NUMBER_OF_NODES = 1000
NUMBER_OF_ELEMENTS_PER_NODE = 7
NUMBER_OF_ELEMENTS = NUMBER_OF_NODES * NUMBER_OF_ELEMENTS_PER_NODE

REGISTER = 1
SENSOR = 2
ACTUATOR = 3
ACTIVATOR = 4
CONCEPT = 5
SCRIPT = 6
PIPE = 7
TRIGGER = 8


class TheanoNodenet(Nodenet):
    """
        theano runtime engine implementation
    """

    allocated_nodes = None
    last_allocated_node = -1

    # numpy data structures holding the actual data
    w_matrix = None
    a_array = None
    theta_array = None

    # theano tensors for performing operations
    w = None            # matrix of weights
    a = None            # vector of activations
    theta = None        # vector of thetas (i.e. biases)

    @property
    def engine(self):
        return "theano_engine"

    @property
    def current_step(self):
        pass

    @property
    def data(self):
        pass

    def __init__(self, filename, name="", worldadapter="Default", world=None, owner="", uid=None, nodetypes={}, native_modules={}):

        super(TheanoNodenet, self).__init__(name or os.path.basename(filename), worldadapter, world, owner, uid)

        self.allocated_nodes = np.zeros(NUMBER_OF_NODES, dtype=np.int32)

        self.w_matrix = np.zeros((NUMBER_OF_ELEMENTS, NUMBER_OF_ELEMENTS), dtype=np.float32)
        self.w = theano.shared(value=self.w_matrix.astype(T.config.floatX), name="w", borrow=True)

        self.a_array = np.zeros(NUMBER_OF_ELEMENTS, dtype=np.float32)
        self.a = theano.shared(value=self.a_array.astype(T.config.floatX), name="a", borrow=True)

        self.theta_array = np.zeros(NUMBER_OF_ELEMENTS, dtype=np.float32)
        self.theta = theano.shared(value=self.theta_array.astype(T.config.floatX), name="theta", borrow=True)

    def step(self):
        pass

    def get_node(self, uid):
        if int(uid) in self.get_node_uids():
            return TheanoNode(self, int(uid))
        else:
            return None

    def get_node_uids(self):
        return np.nonzero(self.allocated_nodes)[0]

    def is_node(self, uid):
        return int(uid) in self.get_node_uids()

    def create_node(self, nodetype, nodespace_uid, position, name="", uid=None, parameters=None, gate_parameters=None):

        uid = -1
        while uid < 0:
            for i in range((self.last_allocated_node+1), NUMBER_OF_NODES):
                if self.allocated_nodes[i] == 0:
                    uid = i
                    break

        if uid < 0:
            for i in range(self.last_allocated_node-1):
                if self.allocated_nodes[i] == 0:
                    uid = i
                    break

        if uid < 0:
            self.logger.warning("Cannot find free id, all "+NUMBER_OF_NODES+" node entries already in use.")
            return None

        self.last_allocated_node = uid

        numerictype = 0
        if nodetype == "Register":
            numerictype = REGISTER
        elif nodetype == "Actuator":
            numerictype = ACTUATOR
        elif nodetype == "Sensor":
            numerictype = SENSOR
        elif nodetype == "Activator":
            numerictype = ACTIVATOR
        elif nodetype == "Concept":
            numerictype = CONCEPT
        elif nodetype == "Script":
            numerictype = SCRIPT
        elif nodetype == "Pipe":
            numerictype = PIPE
        elif nodetype == "Trigger":
            numerictype = TRIGGER
        self.allocated_nodes[uid] = numerictype
        return str(uid)

    def delete_node(self, uid):
        self.allocated_nodes[int(uid)] = 0
        self.last_allocated_node = int(uid)-1

    def get_nodespace(self, uid):
        pass

    def get_nodespace_uids(self):
        pass

    def is_nodespace(self, uid):
        pass

    def create_nodespace(self, parent_uid, position, name="", uid=None, gatefunction_strings=None):
        pass

    def delete_nodespace(self, uid):
        pass

    def create_link(self, source_node_uid, gate_type, target_node_uid, slot_type, weight=1, certainty=1):
        pass

    def set_link_weight(self, source_node_uid, gate_type, target_node_uid, slot_type, weight=1, certainty=1):
        pass

    def delete_link(self, source_node_uid, gate_type, target_node_uid, slot_type):
        pass

    def reload_native_modules(self, native_modules):
        pass

    def get_nodespace_area_data(self, nodespace_uid, x1, x2, y1, y2):
{'fraction_non_alphanumeric': 0.09468501451836889, 'fraction_numerical': 0.00820603459159197, 'mean_word_length': 2.8233590733590734, 'pattern_counts': {'":': 8, '<': 6, '<?xml version=': 0, '>': 3, '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': '4622514', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2501, 'n_words': 606}