<commit_before>"""Kernelized Incremental Feature Dependency Discovery"""

import numpy as np
from Representation import Representation
from itertools import combinations
from Tools import addNewElementForAllActions, PriorityQueueWithNovelty
import matplotlib.pyplot as plt

__copyright__ = "Copyright 2013, RLPy http://www.acl.mit.edu/RLPy"
__credits__ = ["<NAME>", "<NAME>", "<NAME>",
               "<NAME>", "<NAME>"]
__license__ = "BSD 3-Clause"
__author__ = "<NAME> <<EMAIL>>"


class KernelizedFeature(object):
    # feature index, -1 for non-discovered ones
    index = -1
    # relevance used to decide when to discover
    relevance = 0.
    # list of dimensions that are regarded by this feature
    dim = []
    # center = data point used to generate the feature
    # center gives the highest output of this feature
    center = None

    def __init__(self, center, dim, kernel, index=-1,
                 base_ids=None, kernel_args=[]):
        self.index = index
        self.kernel_args = kernel_args
        self.center = center
        self.dim = dim
        self.kernel = kernel
        if base_ids is None:
            self.base_ids = frozenset([self.index])
        else:
            self.base_ids = base_ids

    def __str__(self):
        res = "{" + ", ".join(sorted([str(i) for i in self.base_ids])) + "}  "
        res += ", ".join(["s{}={:.3g}".format(d + 1, self.center[d])
                         for d in self.dim])
        return res

    def output(self, s):
        return self.kernel(s, self.center, self.dim, *self.kernel_args)


class Candidate(object):

    """
    candidate feature as a combination of two existing features
    """
    activation_count = 0.
    td_error_sum = 0.
    relevance = 0.
    idx1 = -1
    idx2 = -1

    def __init__(self, idx1, idx2):
        self.idx1 = idx1
        self.idx2 = idx2


class KernelizediFDD(Representation):

    """
    Kernelized version of iFDD
    """
    features = []
    candidates = {}
    # contains a set for each feature indicating the ids of
    base_id_sets = set()
                       # 1-dim features it refines
    base_feature_ids = []
    max_relevance = 0.

    def __init__(self, domain, kernel, active_threshold, discover_threshold,
                 logger=None, kernel_args=[], normalization=True, sparsify=True,
                 max_active_base_feat=2, max_base_feat_sim=0.7):
        super(KernelizediFDD, self).__init__(domain, logger)
        self.kernel = kernel
        self.kernel_args = kernel_args
        self.active_threshold = active_threshold
        self.discover_threshold = discover_threshold
        self.normalization = normalization
        self.sparsify = sparsify
        self.sorted_ids = PriorityQueueWithNovelty()
        self.max_active_base_feat = max_active_base_feat
        self.max_base_feat_sim = max_base_feat_sim

    def show_features(self):
        l = self.sorted_ids.toList()[:]
        key = lambda x: (
            len(self.features[x].base_ids),
            tuple(self.features[x].dim),
            tuple(self.features[x].center[self.features[x].dim]))
        l.sort(key=key)<commit_msg>fixed broken test cases (missing init in KiFDD)<commit_after>"""Kernelized Incremental Feature Dependency Discovery"""

import numpy as np
from Representation import Representation
from itertools import combinations
from Tools import addNewElementForAllActions, PriorityQueueWithNovelty
import matplotlib.pyplot as plt

__copyright__ = "Copyright 2013, RLPy http://www.acl.mit.edu/RLPy"
__credits__ = ["<NAME>", "<NAME>", "<NAME>",
               "<NAME>", "<NAME>"]
__license__ = "BSD 3-Clause"
__author__ = "<NAME> <<EMAIL>>"


class KernelizedFeature(object):
    # feature index, -1 for non-discovered ones
    index = -1
    # relevance used to decide when to discover
    relevance = 0.
    # list of dimensions that are regarded by this feature
    dim = []
    # center = data point used to generate the feature
    # center gives the highest output of this feature
    center = None

    def __init__(self, center, dim, kernel, index=-1,
                 base_ids=None, kernel_args=[]):
        self.index = index
        self.kernel_args = kernel_args
        self.center = center
        self.dim = dim
        self.kernel = kernel
        if base_ids is None:
            self.base_ids = frozenset([self.index])
        else:
            self.base_ids = base_ids

    def __str__(self):
        res = "{" + ", ".join(sorted([str(i) for i in self.base_ids])) + "}  "
        res += ", ".join(["s{}={:.3g}".format(d + 1, self.center[d])
                         for d in self.dim])
        return res

    def output(self, s):
        return self.kernel(s, self.center, self.dim, *self.kernel_args)


class Candidate(object):

    """
    candidate feature as a combination of two existing features
    """
    activation_count = 0.
    td_error_sum = 0.
    relevance = 0.
    idx1 = -1
    idx2 = -1

    def __init__(self, idx1, idx2):
        self.idx1 = idx1
        self.idx2 = idx2


class KernelizediFDD(Representation):

    """
    Kernelized version of iFDD
    """
    features = []
    candidates = {}
    # contains a set for each feature indicating the ids of
    base_id_sets = set()
                       # 1-dim features it refines
    base_feature_ids = []
    max_relevance = 0.

    def __init__(self, domain, kernel, active_threshold, discover_threshold,
                 logger=None, kernel_args=[], normalization=True, sparsify=True,
                 max_active_base_feat=2, max_base_feat_sim=0.7):
        super(KernelizediFDD, self).__init__(domain, logger)
        self.kernel = kernel
        self.kernel_args = kernel_args
        self.active_threshold = active_threshold
        self.discover_threshold = discover_threshold
        self.normalization = normalization
        self.sparsify = sparsify
        self.sorted_ids = PriorityQueueWithNovelty()
        self.max_active_base_feat = max_active_base_feat
        self.max_base_feat_sim = max_base_feat_sim
        self.candidates = {}
        self.features = []
        self.base_features_ids = []
        self.max_relevance = 0.
    def show_features(self):
        l = self.sorted_ids.toList()[:]
        key = lambda x: (
            len(self.features[x].base_ids),
            tuple(self.features[x].dim),
            tuple(self.features[x].center[self.features[x].dim]))
        l.sort(key=key)
{'fraction_non_alphanumeric': 0.1061713042126535, 'fraction_numerical': 0.009171459661122337, 'mean_word_length': 2.477837837837838, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5648666', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1914, 'n_words': 497}