<commit_before>"""Tests for the weighted_modularity module"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import unittest


# Third party
import networkx as nx
import nose.tools as nt
import numpy as np
import numpy.testing as npt

# Our own
from .. import util
from .. import weighted_modularity as wm


def get_test_data():
    """ grabs local txt file with adj matrices
    Returns
    =======
    graph : networkx graph
    communities : list of sets
    """
    pth, _ = os.path.split(__file__)
    testdir = os.path.join(pth, 'tdata_corr_txt')
    data_file = os.path.join(testdir, '101_Block01.txt')
    mat = np.loadtxt(data_file)
    mat[mat<0] = 0
    graph = nx.from_numpy_matrix(mat)
    # graph has 85 nodes, make generic communities
    communities = [set(range(42)), set(range(42,86))]
    return graph, communities

class TestWeightedPartition(unittest.TestCase):

    def setUp(self):
        ## generate a default graph and communities
        graph, communities = get_test_data()
        self.graph = graph
        self.communities = communities

    def test_init(self):
        part = wm.WeightedPartition(self.graph)
        self.assertEqual(type(part.degrees), type({}))
        npt.assert_array_almost_equal(part.total_edge_weight, 1500.5653444)
        # generated communities
        comm = [set([node]) for node in self.graph.nodes()]
        self.assertEqual(part.communities, comm)
        # test communities cannot be replaced by garbage
        with self.assertRaises(TypeError):
            part.communities = 11
        # but we can pass a valid community partition
        part.communities = comm
        self.assertEqual(part.communities, comm)

    def test_communities_degree(self):
        ## if no community, method will raise error
        part = wm.WeightedPartition(self.graph)
        part = wm.WeightedPartition(self.graph, self.communities)
        cdegree = part.communities_degree()
        self.assertEqual(round(cdegree[0]), 1462.0)


    def test_set_communities(self):
        part = wm.WeightedPartition(self.graph, self.communities)
        self.assertEqual(part.communities, self.communities)
        with self.assertRaises(TypeError):
            # raise error if not list of sets
            part.set_communities(part.communities[0])
        with self.assertRaises(TypeError):
            part.set_communities('a')
        with self.assertRaises(ValueError):
            ## missing nodes
            comm = self.graph.nodes()[:-3]
            part.set_communities([set(comm)])

    def test_allnodes_in_communities(self):<commit_msg>TEST: added test for setter of WeightedPartition, catch partition with missing nodes<commit_after>"""Tests for the weighted_modularity module"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import unittest


# Third party
import networkx as nx
import nose.tools as nt
import numpy as np
import numpy.testing as npt

# Our own
from .. import util
from .. import weighted_modularity as wm


def get_test_data():
    """ grabs local txt file with adj matrices
    Returns
    =======
    graph : networkx graph
    communities : list of sets
    """
    pth, _ = os.path.split(__file__)
    testdir = os.path.join(pth, 'tdata_corr_txt')
    data_file = os.path.join(testdir, '101_Block01.txt')
    mat = np.loadtxt(data_file)
    mat[mat<0] = 0
    graph = nx.from_numpy_matrix(mat)
    # graph has 85 nodes, make generic communities
    communities = [set(range(42)), set(range(42,86))]
    return graph, communities

class TestWeightedPartition(unittest.TestCase):

    def setUp(self):
        ## generate a default graph and communities
        graph, communities = get_test_data()
        self.graph = graph
        self.communities = communities

    def test_init(self):
        part = wm.WeightedPartition(self.graph)
        self.assertEqual(type(part.degrees), type({}))
        npt.assert_array_almost_equal(part.total_edge_weight, 1500.5653444)
        # generated communities
        comm = [set([node]) for node in self.graph.nodes()]
        self.assertEqual(part.communities, comm)
        # test communities cannot be replaced by garbage
        with self.assertRaises(TypeError):
            part.communities = 11
        # doesnt work if nodes are missing from partition
        with self.assertRaises(ValueError):
            part.communities = [set([1,2,3])]
        # but we can pass a valid community partition
        part.communities = comm
        self.assertEqual(part.communities, comm)

    def test_communities_degree(self):
        ## if no community, method will raise error
        part = wm.WeightedPartition(self.graph)
        part = wm.WeightedPartition(self.graph, self.communities)
        cdegree = part.communities_degree()
        self.assertEqual(round(cdegree[0]), 1462.0)


    def test_set_communities(self):
        part = wm.WeightedPartition(self.graph, self.communities)
        self.assertEqual(part.communities, self.communities)
        with self.assertRaises(TypeError):
            # raise error if not list of sets
            part.set_communities(part.communities[0])
        with self.assertRaises(TypeError):
            part.set_communities('a')
        with self.assertRaises(ValueError):
            ## missing nodes
            comm = self.graph.nodes()[:-3]
            part.set_communities([set(comm)])

    def test_allnodes_in_communities(self):
{'fraction_non_alphanumeric': 0.14734427386624316, 'fraction_numerical': 0.013234515616728428, 'mean_word_length': 2.361803084223013, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7136795', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1558, 'n_words': 398}