From 6eda086754f37eb18dc966934bb1103d79450eff Mon Sep 17 00:00:00 2001 From: ragibson Date: Fri, 22 Apr 2022 20:14:03 -0400 Subject: [PATCH] Make tests more robust and increment version --- setup.py | 2 +- tests/shared_testing_functions.py | 10 ++++++++++ tests/test_monolayer_parameter_estimation.py | 4 ++-- tests/test_multiplex_parameter_estimation.py | 7 ++++--- .../test_temporal_multilevel_parameter_estimation.py | 11 ++++++----- utilities/champ_utilities.py | 1 - 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index c290d9f..b3c0161 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name='modularitypruning', - version='1.1.3', + version='1.2.0', package_dir={'modularitypruning': 'utilities'}, packages=['modularitypruning'], url='https://github.com/ragibson/ModularityPruning', diff --git a/tests/shared_testing_functions.py b/tests/shared_testing_functions.py index e250502..ee8007d 100644 --- a/tests/shared_testing_functions.py +++ b/tests/shared_testing_functions.py @@ -1,4 +1,5 @@ import igraph as ig +from math import isnan from modularitypruning.partition_utilities import num_communities from random import randint, random, uniform @@ -87,3 +88,12 @@ def generate_multilayer_intralayer_SBM(copying_probability, p_in, p_out, first_l G_intralayer = ig.Graph(intralayer_edges, directed=False) return G_intralayer, layer_membership + + +def assert_almost_equal_or_both_none_or_nan(self, first, second, places=10): + if first is None: + self.assertIsNone(second) # isnan only accepts floats + elif isnan(first): + self.assertTrue(isnan(second)) # NaN is != NaN according to IEEE-754 + else: + self.assertAlmostEqual(first, second, places=places) diff --git a/tests/test_monolayer_parameter_estimation.py b/tests/test_monolayer_parameter_estimation.py index 32cadd1..c7fcae5 100644 --- a/tests/test_monolayer_parameter_estimation.py +++ b/tests/test_monolayer_parameter_estimation.py @@ -1,4 +1,4 @@ -from .shared_testing_functions import generate_igraph_famous, generate_random_partition +from .shared_testing_functions import assert_almost_equal_or_both_none_or_nan, generate_igraph_famous, generate_random_partition import igraph as ig from math import log from numpy import mean @@ -45,7 +45,7 @@ def test_directed_consistency_igraph_famous(self): G.to_directed() gamma_directed = gamma_estimate(G, random_membership) - self.assertAlmostEqual(gamma_undirected, gamma_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, gamma_undirected, gamma_directed, places=10) if __name__ == "__main__": diff --git a/tests/test_multiplex_parameter_estimation.py b/tests/test_multiplex_parameter_estimation.py index 2536871..1457706 100644 --- a/tests/test_multiplex_parameter_estimation.py +++ b/tests/test_multiplex_parameter_estimation.py @@ -1,4 +1,5 @@ -from .shared_testing_functions import generate_random_partition, generate_multilayer_intralayer_SBM +from .shared_testing_functions import assert_almost_equal_or_both_none_or_nan, generate_random_partition, \ + generate_multilayer_intralayer_SBM import igraph as ig from math import log from numpy import mean @@ -109,8 +110,8 @@ def test_directed_consistency_multiplex_SBM_louvain(self): gamma_directed, omega_directed = gamma_omega_estimate(G_intralayer, G_interlayer, layer_membership, partition, model="multiplex") - self.assertAlmostEqual(gamma_undirected, gamma_directed, places=10) - self.assertAlmostEqual(omega_undirected, omega_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, gamma_undirected, gamma_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, omega_undirected, omega_directed, places=10) if __name__ == "__main__": diff --git a/tests/test_temporal_multilevel_parameter_estimation.py b/tests/test_temporal_multilevel_parameter_estimation.py index 1a33ac6..905b5e9 100644 --- a/tests/test_temporal_multilevel_parameter_estimation.py +++ b/tests/test_temporal_multilevel_parameter_estimation.py @@ -1,4 +1,5 @@ -from .shared_testing_functions import generate_random_partition, generate_multilayer_intralayer_SBM +from .shared_testing_functions import assert_almost_equal_or_both_none_or_nan, generate_random_partition, \ + generate_multilayer_intralayer_SBM import igraph as ig from math import log from numpy import mean @@ -112,8 +113,8 @@ def test_directed_consistency_temporal_SBM_louvain(self): gamma_directed, omega_directed = gamma_omega_estimate(G_intralayer, G_interlayer, layer_membership, partition, model="temporal") - self.assertAlmostEqual(gamma_undirected, gamma_directed, places=10) - self.assertAlmostEqual(omega_undirected, omega_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, gamma_undirected, gamma_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, omega_undirected, omega_directed, places=10) # check multilevel parameter estimation as well gamma_undirected, omega_undirected = gamma_omega_estimate(G_intralayer, G_interlayer, layer_membership, @@ -123,8 +124,8 @@ def test_directed_consistency_temporal_SBM_louvain(self): gamma_directed, omega_directed = gamma_omega_estimate(G_intralayer, G_interlayer, layer_membership, partition, model="multilevel") - self.assertAlmostEqual(gamma_undirected, gamma_directed, places=10) - self.assertAlmostEqual(omega_undirected, omega_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, gamma_undirected, gamma_directed, places=10) + assert_almost_equal_or_both_none_or_nan(self, omega_undirected, omega_directed, places=10) if __name__ == "__main__": diff --git a/utilities/champ_utilities.py b/utilities/champ_utilities.py index f1d88e3..3844f63 100644 --- a/utilities/champ_utilities.py +++ b/utilities/champ_utilities.py @@ -2,7 +2,6 @@ membership_to_layered_communities from collections import defaultdict import numpy as np -from numpy import VisibleDeprecationWarning from numpy.random import choice from math import floor from multiprocessing import Pool, cpu_count