<commit_before>from nose.tools import *

import networkx as nx
from networkx.exception import *
import networkx.algorithms.link_prediction as lp


class TestResourceAllocationIndex():
    def setUp(self):
        self.func = lp.resource_allocation_index
        def test_func(G, u, v, expected):
            tol = 1e-7
            result = self.func(G, u, v)
            assert_true(abs(result - expected) < tol)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        self.test(G, 0, 1, 0.75)

    def test_P3(self):
        G = nx.path_graph(3)
        self.test(G, 0, 2, 0.5)

    def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, 1, 2, 0.25)

    @raises(NetworkXNotImplemented)
    def test_digraph(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multigraph(self):
        G = nx.MultiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    def test_custom1(self):
        """Case of no common neighbors."""
        G = nx.Graph()
        G.add_nodes_from([0, 1])
        self.test(G, 0, 1, 0)

    def test_custom2(self):
        """Case of equal nodes."""
        G = nx.complete_graph(4)
        self.test(G, 0, 0, 1)


class TestJaccardCoefficient():
    def setUp(self):
        self.func = lp.jaccard_coefficient
        def test_func(G, u, v, expected):
            tol = 1e-7
            result = self.func(G, u, v)
            assert_true(abs(result - expected) < tol)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        self.test(G, 0, 1, 0.6)

    def test_P4(self):
        G = nx.path_graph(4)
        self.test(G, 0, 2, 0.5)

    @raises(NetworkXNotImplemented)
    def test_digraph(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multigraph(self):
        G = nx.MultiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXError)
    def test_nonexistent_node1(self):
        G = nx.complete_graph(5)
        self.func(G, 'A', 0)

    @raises(NetworkXError)
    def test_nonexistent_node2(self):
        G = nx.complete_graph(5)
        self.func(G, 0, 'A')

    def test_no_common_neighbor(self):
        G = nx.Graph()
        G.add_edges_from([(0, 1), (2, 3)])
        self.test(G, 0, 2, 0)

    def test_isolated_nodes(self):
        G = nx.Graph()
        G.add_nodes_from([0, 1])
        self.test(G, 0, 1, 0)


class TestCNSoundarajanHopcroft():
    def setUp(self):
        self.func = lp.cn_soundarajan_hopcroft
        def test_func(G, u, v, expected):
            result = self.func(G, u, v)
            assert_equal(result, expected)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        G.node[0]['community'] = 0
        G.node[1]['community'] = 0
        G.node[2]['community'] = 0
        G.node[3]['community'] = 0
        G.node[4]['community'] = 1
        self.test(G, 0, 1, 5)
<commit_msg>Added tests for Adamic-Adar index<commit_after>import math

from nose.tools import *

import networkx as nx
from networkx.exception import *
import networkx.algorithms.link_prediction as lp


class TestResourceAllocationIndex():
    def setUp(self):
        self.func = lp.resource_allocation_index
        def test_func(G, u, v, expected):
            tol = 1e-7
            result = self.func(G, u, v)
            assert_true(abs(result - expected) < tol)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        self.test(G, 0, 1, 0.75)

    def test_P3(self):
        G = nx.path_graph(3)
        self.test(G, 0, 2, 0.5)

    def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, 1, 2, 0.25)

    @raises(NetworkXNotImplemented)
    def test_digraph(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multigraph(self):
        G = nx.MultiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    def test_custom1(self):
        """Case of no common neighbors."""
        G = nx.Graph()
        G.add_nodes_from([0, 1])
        self.test(G, 0, 1, 0)

    def test_custom2(self):
        """Case of equal nodes."""
        G = nx.complete_graph(4)
        self.test(G, 0, 0, 1)


class TestJaccardCoefficient():
    def setUp(self):
        self.func = lp.jaccard_coefficient
        def test_func(G, u, v, expected):
            tol = 1e-7
            result = self.func(G, u, v)
            assert_true(abs(result - expected) < tol)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        self.test(G, 0, 1, 0.6)

    def test_P4(self):
        G = nx.path_graph(4)
        self.test(G, 0, 2, 0.5)

    @raises(NetworkXNotImplemented)
    def test_digraph(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multigraph(self):
        G = nx.MultiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXError)
    def test_nonexistent_node1(self):
        G = nx.complete_graph(5)
        self.func(G, 'A', 0)

    @raises(NetworkXError)
    def test_nonexistent_node2(self):
        G = nx.complete_graph(5)
        self.func(G, 0, 'A')

    def test_no_common_neighbor(self):
        G = nx.Graph()
        G.add_edges_from([(0, 1), (2, 3)])
        self.test(G, 0, 2, 0)

    def test_isolated_nodes(self):
        G = nx.Graph()
        G.add_nodes_from([0, 1])
        self.test(G, 0, 1, 0)


class TestAdamicAdarIndex():
    def setUp(self):
        self.func = lp.adamic_adar_index
        def test_func(G, u, v, expected):
            tol = 1e-7
            result = self.func(G, u, v)
            assert_true(abs(result - expected) < tol)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        self.test(G, 0, 1, 3 / math.log(4))

    def test_P3(self):
        G = nx.path_graph(3)
        self.test(G, 0, 2, 1 / math.log(2))

    def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, 1, 2, 1 / math.log(4))

    @raises(NetworkXNotImplemented)
    def test_digraph(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multigraph(self):
        G = nx.MultiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXNotImplemented)
    def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (1, 2)])
        self.func(G, 0, 2)

    @raises(NetworkXError)
    def test_nonexistent_node1(self):
        G = nx.complete_graph(5)
        self.func(G, 'A', 0)

    @raises(NetworkXError)
    def test_nonexistent_node2(self):
        G = nx.complete_graph(5)
        self.func(G, 0, 'A')

    def test_no_common_neighbor(self):
        G = nx.Graph()
        G.add_nodes_from([0, 1])
        self.test(G, 0, 1, 0)

    def test_equal_nodes(self):
        G = nx.complete_graph(4)
        self.test(G, 0, 0, 3 / math.log(3))


class TestCNSoundarajanHopcroft():
    def setUp(self):
        self.func = lp.cn_soundarajan_hopcroft
        def test_func(G, u, v, expected):
            result = self.func(G, u, v)
            assert_equal(result, expected)
        self.test = test_func

    def test_K5(self):
        G = nx.complete_graph(5)
        G.node[0]['community'] = 0
        G.node[1]['community'] = 0
        G.node[2]['community'] = 0
        G.node[3]['community'] = 0
        G.node[4]['community'] = 1
        self.test(G, 0, 1, 5)

{'fraction_non_alphanumeric': 0.14525530426672884, 'fraction_numerical': 0.03334110515271625, 'mean_word_length': 2.12759752096245, 'pattern_counts': {'":': 0, '<': 8, '<?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': '408589', 'n_tokens_mistral': 3512, 'n_tokens_neox': 3250, 'n_words': 670}