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

"""
Node definition

Gate definition
Slot definition
Nodetype definition

default Nodetypes

"""

import logging

from micropsi_core.nodenet.node import Node, Gate, Nodetype, Slot
from .dict_link import DictLink
from micropsi_core.nodenet.dict_engine.dict_netentity import NetEntity

__author__ = 'joscha'
__date__ = '09.05.12'


emptySheafElement = dict(uid="default", name="default", activation=0)


class DictNode(NetEntity, Node):
    """A net entity with slots and gates and a node function.

    Node functions are called alternating with the link functions. They process the information in the slots
    and usually call all the gate functions to transmit the activation towards the links.

    Attributes:
        activation: a numeric value (usually between -1 and 1) to indicate its activation. Activation is determined
            by the node function, usually depending on the value of the slots.
        slots: a list of slots (activation inlets)
        gates: a list of gates (activation outlets)
        node_function: a function to be executed whenever the node receives activation
    """

    @property
    def activation(self):
        return self.sheaves['default']['activation']

    @property
    def activations(self):
        return dict((k, v['activation']) for k,v in self.sheaves.items())

    @activation.setter
    def activation(self, activation):
        self.set_sheaf_activation(activation)

    def set_sheaf_activation(self, activation, sheaf="default"):
        sheaves_to_calculate = self.get_sheaves_to_calculate()
        if sheaf not in sheaves_to_calculate:
            raise "Sheaf " + sheaf + " can not be set as it hasn't been propagated to any slot"

        if activation is None:
            activation = 0

        self.sheaves[sheaf]['activation'] = float(activation)
        if len(self.nodetype.gatetypes):
            self.set_gate_activation(self.nodetype.gatetypes[0], activation, sheaf)

    def __init__(self, nodenet, parent_nodespace, position, state=None, activation=0,
                 name="", type="Concept", uid=None, index=None, parameters=None, gate_parameters=None, gate_activations=None, **_):
        if not gate_parameters:
            gate_parameters = {}

        if nodenet.is_node(uid):
            raise KeyError("Node already exists")

        Node.__init__(self, type, nodenet.get_nodetype(type))

        NetEntity.__init__(self, nodenet, parent_nodespace, position,
            name=name, entitytype="nodes", uid=uid, index=index)

        self.__non_default_gate_parameters = {}

        self.__state = {}

        self.__gates = {}
        self.__slots = {}

        self.__parameters = dict((key, None) for key in self.nodetype.parameters)
        if parameters is not None:
            self.__parameters.update(parameters)

        for gate_name in gate_parameters:
            for key in gate_parameters[gate_name]:
                if gate_parameters[gate_name][key] != self.nodetype.gate_defaults.get(key, None):
                    if gate_name not in self.__non_default_gate_parameters:
                        self.__non_default_gate_parameters[gate_name] = {}
                    self.__non_default_gate_parameters[gate_name][key] = gate_parameters[gate_name][key]

        gate_parameters = self.nodetype.gate_defaults.copy()
        gate_parameters.update(self.__non_default_gate_parameters)

        for key in gate_parameters.copy():
            if key in self.nodetype.gate_defaults:
                try:
                    gate_parameters[key] = float(gate_parameters[key])
                except:
                    logging.getLogger('nodenet').warn('Invalid gate parameter value for gate %s, param %s, node %s' % (type, key, self.uid))
                    gate_parameters[key] = self.nodetype.gate_defaults.get(key, 0)
            else:
                gate_parameters[key] = float(gate_parameters[key])

        for gate in self.nodetype.gatetypes:
            if gate_activations is None or gate not in gate_activations:
                sheaves_to_use = None
            else:
                sheaves_to_use = gate_activations[gate]
            self.__gates[gate] = DictGate(gate, self, sheaves=sheaves_to_use, gate_function=None, parameters=gate_parameters.get(gate))
        for slot in self.nodetype.slottypes:
            self.__slots[slot] = DictSlot(slot, self)
        if state:
            self.__state = state
        nodenet._register_node(self)
        self.sheaves = {"default": emptySheafElement.copy()}

        self.activation = 0<commit_msg>Fixing gate parameter validation broken in fa35e55<commit_after># -*- coding: utf-8 -*-

"""
Node definition

Gate definition
Slot definition
Nodetype definition

default Nodetypes

"""

import logging

from micropsi_core.nodenet.node import Node, Gate, Nodetype, Slot
from .dict_link import DictLink
from micropsi_core.nodenet.dict_engine.dict_netentity import NetEntity

__author__ = 'joscha'
__date__ = '09.05.12'


emptySheafElement = dict(uid="default", name="default", activation=0)


class DictNode(NetEntity, Node):
    """A net entity with slots and gates and a node function.

    Node functions are called alternating with the link functions. They process the information in the slots
    and usually call all the gate functions to transmit the activation towards the links.

    Attributes:
        activation: a numeric value (usually between -1 and 1) to indicate its activation. Activation is determined
            by the node function, usually depending on the value of the slots.
        slots: a list of slots (activation inlets)
        gates: a list of gates (activation outlets)
        node_function: a function to be executed whenever the node receives activation
    """

    @property
    def activation(self):
        return self.sheaves['default']['activation']

    @property
    def activations(self):
        return dict((k, v['activation']) for k,v in self.sheaves.items())

    @activation.setter
    def activation(self, activation):
        self.set_sheaf_activation(activation)

    def set_sheaf_activation(self, activation, sheaf="default"):
        sheaves_to_calculate = self.get_sheaves_to_calculate()
        if sheaf not in sheaves_to_calculate:
            raise "Sheaf " + sheaf + " can not be set as it hasn't been propagated to any slot"

        if activation is None:
            activation = 0

        self.sheaves[sheaf]['activation'] = float(activation)
        if len(self.nodetype.gatetypes):
            self.set_gate_activation(self.nodetype.gatetypes[0], activation, sheaf)

    def __init__(self, nodenet, parent_nodespace, position, state=None, activation=0,
                 name="", type="Concept", uid=None, index=None, parameters=None, gate_parameters=None, gate_activations=None, **_):
        if not gate_parameters:
            gate_parameters = {}

        if nodenet.is_node(uid):
            raise KeyError("Node already exists")

        Node.__init__(self, type, nodenet.get_nodetype(type))

        NetEntity.__init__(self, nodenet, parent_nodespace, position,
            name=name, entitytype="nodes", uid=uid, index=index)

        self.__non_default_gate_parameters = {}

        self.__state = {}

        self.__gates = {}
        self.__slots = {}

        self.__parameters = dict((key, None) for key in self.nodetype.parameters)
        if parameters is not None:
            self.__parameters.update(parameters)

        for gate_name in gate_parameters:
            for key in gate_parameters[gate_name]:
                if gate_parameters[gate_name][key] != self.nodetype.gate_defaults.get(key, None):
                    if gate_name not in self.__non_default_gate_parameters:
                        self.__non_default_gate_parameters[gate_name] = {}
                    self.__non_default_gate_parameters[gate_name][key] = gate_parameters[gate_name][key]

        gate_parameters = self.nodetype.gate_defaults.copy()
        gate_parameters.update(self.__non_default_gate_parameters)

        gate_parameters_for_validation = gate_parameters.copy()
        for gate_name in gate_parameters_for_validation:
            for key in gate_parameters_for_validation[gate_name]:
                if key in self.nodetype.gate_defaults:
                    try:
                        gate_parameters[gate_name][key] = float(gate_parameters[gate_name][key])
                    except:
                        logging.getLogger('nodenet').warn('Invalid gate parameter value for gate %s, param %s, node %s' % (gate_name, key, self.uid))
                        gate_parameters[gate_name][key] = self.nodetype.gate_defaults[gate_name].get(key, 0)
                else:
                    gate_parameters[gate_name][key] = float(gate_parameters[gate_name][key])

        for gate in self.nodetype.gatetypes:
            if gate_activations is None or gate not in gate_activations:
                sheaves_to_use = None
            else:
                sheaves_to_use = gate_activations[gate]
            self.__gates[gate] = DictGate(gate, self, sheaves=sheaves_to_use, gate_function=None, parameters=gate_parameters.get(gate))
        for slot in self.nodetype.slottypes:
            self.__slots[slot] = DictSlot(slot, self)
        if state:
            self.__state = state
        nodenet._register_node(self)
        self.sheaves = {"default": emptySheafElement.copy()}

        self.activation = 0
{'fraction_non_alphanumeric': 0.08818565400843882, 'fraction_numerical': 0.003586497890295359, 'mean_word_length': 2.753365003958828, 'pattern_counts': {'":': 2, '<': 3, '<?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': '5620430', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2571, 'n_words': 697}