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

r"""
Module documentation.
"""

import os
import os.path
import numpy as np
import random as rd
from math import ceil, sqrt, log, exp, floor
from copy import deepcopy
from scipy import sparse
from scipy import io

from pygsp import utils


class Graph(object):
    r"""
    parameters:
        - W: Weights matrix
        - A: Adjacency matrix
        - N: Number of nodes
        - d: Degree vector
        - Ne: Egde number
        - gtype: Graph type
        - directed: If the graph is directed
        - lap_type: Laplacian type
        - L: Laplacian
    """

    # All the parameters that needs calculation to be set
    # or not needed are set to None
    def __init__(self, W=None, A=None, N=None, d=None, Ne=None,
                 gtype='unknown', directed=None,
                 lap_type='combinatorial', L=None, **kwargs):

        self.gtype = gtype
        self.lap_type = lap_type

        if W:
            self.W = sparse.lil_matrix(W)
        else:
            self.W = sparse.lil_matrix(0)
        if A:
            self.A = A
        else:
            self.A = sparse.lil_matrix(W > 0)
        if N:
            self.N = N
        else:
            self.N_init_default = True
            self.N = np.shape(self.W)[0]
        if d:
            self.d = d
        else:
            self.d = self.W.sum()
        if Ne:
            self.Ne = Ne
        else:
            self.Ne = np.zeros((self.N), float)
        if directed:
            self.directed = directed
        else:
            self.directed = utils.is_directed(self)
            pass
        if L:
            self.L = L
        else:
            self.L = utils.create_laplacian(self)

    def copy_graph_attr(self, gtype, Gn):
        r"""
        TODO write doc
        """
        return deepcopy(self)

    def separate_graph(self):
        r"""
        TODO write func & doc
        """
        raise NotImplementedError("Not implemented yet")

    def subgraph(self, c):
        r"""
        TODO better doc
        This function create a subgraph from G, keeping only the node(s) in c
        """

        sub_G = self
        sub_G.W = [c, c]
        try:
            sub_G.N = len(c)
        except TypeError:
            sub_G.N = 1

        sub_G.gtype = "sub-" + self.gtype

        return sub_G


class NNGraph(Graph):
    r"""
    Creates a graph from a pointcloud
    parameters:
        - Xin : Input Points
    """

    def __init__(self, Xin, gtype='knn', use_flann=False, center=True, rescale=True, k=10, sigma=0.1, epsilon=0.01, **kwargs):
        self.Xin = Xin
        self.gtype = gtype
        self.use_flann = use_flann
        self.center = center
        self.rescale = rescale
        self.k = k
        self.sigma = sigma
        self.epsilon = epsilon
        param = kwargs

        N, d = np.shape(self.Xin)
        Xout = self.Xin

        if self.center:
            Xout = self.Xin - np.kron(Xin.mean(), N)
            Xout = self.Xin - np.kron(np.ones((N, 1)),
                                      np.mean(self.Xin, axis=0))

        if self.rescale:
            bounding_radius = 0.5*np.linalg.norm(np.amax(Xout, axis=0) - np.amin(Xout, axis=0), 2)
            scale = N**min(d, 3)/10.
            Xout *= scale/bounding_radius

        if self.gtype == "knn":
            spi = np.zeros((N*self.k, 1))
            spj = np.zeros((N*self.k, 1))
            spv = np.zeros((N*self.k, 1))

            # since we did not find yet a goold python flann library, we wont implement it yet
            if self.use_flann:
                pass
            else:
                pass

            for i in xrange(N):
                spi[ii*k:(ii+1)*k] = np.kron(np.ones((k, 1)), i)
                spj[ii*k:(ii+1)*k] = NN[ii, 1:]
                spv[ii*k:(ii+1)*k] = np.exp(-np.power(D[ii, 1:], 2)/self.sigma)

            self.W = sparse.csc_matrix((spv, (spi, spj)),
                                       shape=(np.shape(self.Xin)[0],
                                              np.shape(self.Xin)[0]))

        elif self.gtype == "radius":

            for i in xrange(N):
                spi[ii*k:(ii+1)*k] = np.kron(np.ones((k, 1)), i)
                spj[ii*k:(ii+1)*k] = NN[ii, 1:]
                spv[ii*k:(ii+1)*k] = np.exp(-np.power(D[ii, 1:], 2)/self.sigma)

            self.W = sparse.csc_matrix((spv, (spi, spj)),
                                       shape=(np.shape(self.Xin)[0],
                                              np.shape(self.Xin)[0]))

        else:
            raise ValueError("Unknown type : allowed values are knn, radius")

        # Sanity check
        if np.shape(self.Xin)[0] != np.shape(self.Xin)[1]:
            raise ValueError("Weight matrix W is not square")

        self.coords = Xout
        self.gtype = "nearest neighbors"

        super(NNGraph, self).__init__(Xin=pts, **kwargs)


class Bunny(NNGraph):

    def __init__(self, **kwargs):
        self.type = "radius"
        self.rescale = 1
        self.center = 1
        self.epsilon = 0.2
        # TODO do the ritgh way when point cloud is merged
        self.Xin = Pointcloud(name="bunny").P
        super(Bunny, self).__init__(self.Xin, **kwargs)


class Sphere(NNGraph):

    def __init__(self, radius=1, nb_pts=300, nb_dim=3, sampling="random", **kwargs):
        self.radius = radius
        self.nb_pts = nb_pts
        self.nb_dim = nb_dim
        self.sampling = sampling

        if self.sampling == "random":
            pts = np.random.normal(0, 1, (self.nb_pts, self.nb_dim))
            for i in xrange(self.nb_pts):
                pts[i] /= np.linalg.norm(pts[i])
        else:
            raise ValueError("Unknow sampling!")

        self.gtype = "knn"
        self.k = 10

        super(Sphere, self).__init__(Xin=pts, gtype=self.gtype, k=self.k, **kwargs)

<commit_msg>adding bunny graph, changin' few things<commit_after># -*- coding: utf-8 -*-

r"""
Module documentation.
"""

import os
import os.path
import numpy as np
import random as rd
from math import ceil, sqrt, log, exp, floor
from copy import deepcopy
from scipy import sparse
from scipy import io

from pygsp import utils


class Graph(object):
    r"""
    parameters:
        - W: Weights matrix
        - A: Adjacency matrix
        - N: Number of nodes
        - d: Degree vector
        - Ne: Egde number
        - gtype: Graph type
        - directed: If the graph is directed
        - lap_type: Laplacian type
        - L: Laplacian
    """

    # All the parameters that needs calculation to be set
    # or not needed are set to None
    def __init__(self, W=None, A=None, N=None, d=None, Ne=None,
                 gtype='unknown', directed=None,
                 lap_type='combinatorial', L=None, **kwargs):

        self.gtype = gtype
        self.lap_type = lap_type

        if W:
            self.W = sparse.lil_matrix(W)
        else:
            self.W = sparse.lil_matrix(0)
        if A:
            self.A = A
        else:
            self.A = sparse.lil_matrix(W > 0)
        if N:
            self.N = N
        else:
            self.N_init_default = True
            self.N = np.shape(self.W)[0]
        if d:
            self.d = d
        else:
            self.d = self.W.sum()
        if Ne:
            self.Ne = Ne
        else:
            self.Ne = np.zeros((self.N), float)
        if directed:
            self.directed = directed
        else:
            self.directed = utils.is_directed(self)
            pass
        if L:
            self.L = L
        else:
            self.L = utils.create_laplacian(self)

    def copy_graph_attr(self, gtype, Gn):
        r"""
        TODO write doc
        """
        return deepcopy(self)

    def separate_graph(self):
        r"""
        TODO write func & doc
        """
        raise NotImplementedError("Not implemented yet")

    def subgraph(self, c):
        r"""
        TODO better doc
        This function create a subgraph from G, keeping only the node(s) in c
        """

        sub_G = self
        sub_G.W = [c, c]
        try:
            sub_G.N = len(c)
        except TypeError:
            sub_G.N = 1

        sub_G.gtype = "sub-" + self.gtype

        return sub_G


class NNGraph(Graph):
    r"""
    Creates a graph from a pointcloud
    parameters:
        - Xin : Input Points
    """

    def __init__(self, Xin, gtype='knn', use_flann=False, center=True, rescale=True, k=10, sigma=0.1, epsilon=0.01, **kwargs):
        self.Xin = Xin
        self.gtype = gtype
        self.use_flann = use_flann
        self.center = center
        self.rescale = rescale
        self.k = k
        self.sigma = sigma
        self.epsilon = epsilon
        param = kwargs

        N, d = np.shape(self.Xin)
        Xout = self.Xin

        if self.center:
            Xout = self.Xin - np.kron(Xin.mean(), N)
            Xout = self.Xin - np.kron(np.ones((N, 1)),
                                      np.mean(self.Xin, axis=0))

        if self.rescale:
            bounding_radius = 0.5*np.linalg.norm(np.amax(Xout, axis=0) - np.amin(Xout, axis=0), 2)
            scale = N**min(d, 3)/10.
            Xout *= scale/bounding_radius

        if self.gtype == "knn":
            spi = np.zeros((N*self.k, 1))
            spj = np.zeros((N*self.k, 1))
            spv = np.zeros((N*self.k, 1))

            # since we did not find yet a goold python flann library, we wont implement it yet
            if self.use_flann:
                pass
            else:
                pass

            for i in xrange(N):
                spi[ii*k:(ii+1)*k] = np.kron(np.ones((k, 1)), i)
                spj[ii*k:(ii+1)*k] = NN[ii, 1:]
                spv[ii*k:(ii+1)*k] = np.exp(-np.power(D[ii, 1:], 2)/self.sigma)

            self.W = sparse.csc_matrix((spv, (spi, spj)),
                                       shape=(np.shape(self.Xin)[0],
                                              np.shape(self.Xin)[0]))

        elif self.gtype == "radius":

            for i in xrange(N):
                spi[ii*k:(ii+1)*k] = np.kron(np.ones((k, 1)), i)
                spj[ii*k:(ii+1)*k] = NN[ii, 1:]
                spv[ii*k:(ii+1)*k] = np.exp(-np.power(D[ii, 1:], 2)/self.sigma)

            self.W = sparse.csc_matrix((spv, (spi, spj)),
                                       shape=(np.shape(self.Xin)[0],
                                              np.shape(self.Xin)[0]))

        else:
            raise ValueError("Unknown type : allowed values are knn, radius")

        # Sanity check
        if np.shape(self.Xin)[0] != np.shape(self.Xin)[1]:
            raise ValueError("Weight matrix W is not square")

        self.coords = Xout
        self.gtype = "nearest neighbors"

        super(NNGraph, self).__init__(**kwargs)


class Bunny(NNGraph):

    def __init__(self, **kwargs):
        self.type = "radius"
        self.rescale = True
        self.center = True
        self.epsilon = 0.2
        # TODO do the ritgh way when point cloud is merged
        self.Xin = Pointcloud(name="bunny").Pointcloud

        super(Bunny, self).__init__(Xin=self.Xin, center=self.center, rescale=self.rescale, epsilon=self.epsilon, **kwargs)


class Sphere(NNGraph):

    def __init__(self, radius=1, nb_pts=300, nb_dim=3, sampling="random", **kwargs):
        self.radius = radius
        self.nb_pts = nb_pts
        self.nb_dim = nb_dim
        self.sampling = sampling

        if self.sampling == "random":
            pts = np.random.normal(0, 1, (self.nb_pts, self.nb_dim))
            for i in xrange(self.nb_pts):
                pts[i] /= np.linalg.norm(pts[i])
        else:
            raise ValueError("Unknow sampling!")

        self.gtype = "knn"
        self.k = 10

        super(Sphere, self).__init__(Xin=pts, gtype=self.gtype, k=self.k, **kwargs)


{'fraction_non_alphanumeric': 0.1252860896838179, 'fraction_numerical': 0.009663473764516402, 'mean_word_length': 1.5372043010752687, 'pattern_counts': {'":': 6, '<': 3, '<?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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1596992', 'n_tokens_mistral': 4028, 'n_tokens_neox': 3804, 'n_words': 949}