<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2018-2020 Data61, CSIROß
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Utils tests:

"""
import pytest
import scipy as sp

from stellargraph.core.utils import *
from ..test_utils.graphs import example_graph_random


@pytest.fixture
def example_graph():
    return example_graph_random()


def test_normalize_adj(example_graph):
    node_list = list(example_graph.nodes())
    Aadj = example_graph.to_adjacency_matrix()
    csr = normalize_adj(Aadj)
    dense = csr.todense()
    assert 5 == pytest.approx(dense.sum(), 0.1)
    assert csr.get_shape() == Aadj.get_shape()

    csr = normalize_adj(Aadj, symmetric=False)
    dense = csr.todense()
    assert 5 == pytest.approx(dense.sum(), 0.1)
    assert csr.get_shape() == Aadj.get_shape()


def test_normalized_laplacian(example_graph):
    Aadj = example_graph.to_adjacency_matrix()
    laplacian = normalized_laplacian(Aadj).todense()
    eigenvalues, _ = np.linalg.eig(laplacian)

    # min eigenvalue of normalized laplacian is 0
    # max eigenvalue of normalized laplacian is <= 2
    assert eigenvalues.min() == pytest.approx(0, abs=1e-7)
    assert eigenvalues.max() <= (2 + 1e-7)
    assert laplacian.shape == Aadj.get_shape()

    laplacian = normalized_laplacian(Aadj, symmetric=False)
    assert 1 == pytest.approx(laplacian.sum(), abs=1e-7)
    assert laplacian.get_shape() == Aadj.get_shape()


def test_rescale_laplacian(example_graph):
    node_list = list(example_graph.nodes())
    Aadj = example_graph.to_adjacency_matrix()<commit_msg>replaced check of symetrically normalized adj sum with eigenvalue check (#997)<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2018-2020 Data61, CSIROß
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Utils tests:

"""
import pytest
import scipy as sp

from stellargraph.core.utils import *
from ..test_utils.graphs import example_graph_random


@pytest.fixture
def example_graph():
    return example_graph_random()


def test_normalize_adj(example_graph):
    node_list = list(example_graph.nodes())
    Aadj = example_graph.to_adjacency_matrix()
    csr = normalize_adj(Aadj, symmetric=True)
    dense = csr.todense()
    eigen_vals, _ = np.linalg.eig(dense)
    assert eigen_vals.max() == pytest.approx(1, abs=1e-7)
    assert csr.get_shape() == Aadj.get_shape()

    csr = normalize_adj(Aadj, symmetric=False)
    dense = csr.todense()
    assert 5 == pytest.approx(dense.sum(), 0.1)
    assert csr.get_shape() == Aadj.get_shape()


def test_normalized_laplacian(example_graph):
    Aadj = example_graph.to_adjacency_matrix()
    laplacian = normalized_laplacian(Aadj).todense()
    eigenvalues, _ = np.linalg.eig(laplacian)

    # min eigenvalue of normalized laplacian is 0
    # max eigenvalue of normalized laplacian is <= 2
    assert eigenvalues.min() == pytest.approx(0, abs=1e-7)
    assert eigenvalues.max() <= (2 + 1e-7)
    assert laplacian.shape == Aadj.get_shape()

    laplacian = normalized_laplacian(Aadj, symmetric=False)
    assert 1 == pytest.approx(laplacian.sum(), abs=1e-7)
    assert laplacian.get_shape() == Aadj.get_shape()


def test_rescale_laplacian(example_graph):
    node_list = list(example_graph.nodes())
    Aadj = example_graph.to_adjacency_matrix()
{'fraction_non_alphanumeric': 0.1076633756464504, 'fraction_numerical': 0.015749882463563705, 'mean_word_length': 4.2272727272727275, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1747241', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1346, 'n_words': 378}