<commit_before>#!/usr/bin/env python
"""Provides vectorization of graphs."""

import joblib
import networkx as nx
import multiprocessing
import math
import numpy as np
from scipy import stats
from scipy.sparse import csr_matrix
from scipy.sparse import vstack
from collections import defaultdict, deque
from eden import apply_async
from eden import chunks
from eden import fast_hash, fast_hash_vec
from eden import fast_hash_2, fast_hash_3, fast_hash_4
from eden import AbstractVectorizer
from eden.util import serialize_dict

import logging
logger = logging.getLogger(__name__)


def vectorize(graphs, **opts):
    """Transform real vector labeled, weighted graphs in sparse vectors."""
    return Vectorizer(**opts).transform(graphs)


def vertex_vectorize(graphs, **opts):
    """Transform a list of networkx graphs into a list of sparse matrices."""
    return Vectorizer(**opts).vertex_transform(graphs)


def annotate(graphs,
             estimator=None,
             reweight=1.0,
             vertex_features=False, **opts):
    """Return graphs with extra node attributes: importance and features."""
    result = Vectorizer(**opts).annotate(graphs,
                                         estimator=estimator,
                                         reweight=reweight,
                                         vertex_features=vertex_features)
    return list(result)


# --------------------------------------------------------------------------

class Vectorizer(AbstractVectorizer):
    """Transform real vector labeled, weighted graphs in sparse vectors."""

    def __init__(self,
                 complexity=3,
                 r=None,
                 d=None,
                 min_r=0,
                 min_d=0,
                 weights_dict=None,
                 auto_weights=False,
                 nbits=16,
                 normalization=True,
                 inner_normalization=True,
                 positional=False,
                 discrete=True,
                 block_size=100,
                 n_jobs=-1,
                 key_label='label',
                 key_weight='weight',
                 key_nesting='nesting',
                 key_importance='importance',
                 key_class='class',
                 key_vec='vec',
                 key_svec='svec'):
        """Constructor.

        Parameters
        ----------<commit_msg>added kernel matrix method<commit_after>#!/usr/bin/env python
"""Provides vectorization of graphs."""

import joblib
import networkx as nx
import multiprocessing
import math
import numpy as np
from sklearn import metrics
from scipy import stats
from scipy.sparse import csr_matrix
from scipy.sparse import vstack
from collections import defaultdict, deque
from eden import apply_async
from eden import chunks
from eden import fast_hash, fast_hash_vec
from eden import fast_hash_2, fast_hash_3, fast_hash_4
from eden import AbstractVectorizer
from eden.util import serialize_dict

import logging
logger = logging.getLogger(__name__)


def vectorize(graphs, **opts):
    """Transform real vector labeled, weighted graphs in sparse vectors."""
    return Vectorizer(**opts).transform(graphs)


def vertex_vectorize(graphs, **opts):
    """Transform a list of networkx graphs into a list of sparse matrices."""
    return Vectorizer(**opts).vertex_transform(graphs)


def annotate(graphs,
             estimator=None,
             reweight=1.0,
             vertex_features=False, **opts):
    """Return graphs with extra node attributes: importance and features."""
    result = Vectorizer(**opts).annotate(graphs,
                                         estimator=estimator,
                                         reweight=reweight,
                                         vertex_features=vertex_features)
    return list(result)


def kernel_matrix(graphs, **opts):
    """Return the kernel matrix."""
    data_matrix = vectorize(graphs, opts)
    return metrics.pairwise.pairwise_kernels(data_matrix, metric='linear')

# --------------------------------------------------------------------------


class Vectorizer(AbstractVectorizer):
    """Transform real vector labeled, weighted graphs in sparse vectors."""

    def __init__(self,
                 complexity=3,
                 r=None,
                 d=None,
                 min_r=0,
                 min_d=0,
                 weights_dict=None,
                 auto_weights=False,
                 nbits=16,
                 normalization=True,
                 inner_normalization=True,
                 positional=False,
                 discrete=True,
                 block_size=100,
                 n_jobs=-1,
                 key_label='label',
                 key_weight='weight',
                 key_nesting='nesting',
                 key_importance='importance',
                 key_class='class',
                 key_vec='vec',
                 key_svec='svec'):
        """Constructor.

        Parameters
        ----------
{'fraction_non_alphanumeric': 0.11102188315599278, 'fraction_numerical': 0.0056213611724553305, 'mean_word_length': 1.8484848484848484, 'pattern_counts': {'":': 0, '<': 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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5644899', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1188, 'n_words': 334}