Coverage for tests\clustering\test_factory.py: 100.00%

47 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-17 13:31 +0100

1# -*- coding: utf-8 -*- 

2 

3""" 

4* Name: interactive-clustering/tests/clustering/test_factory.py 

5* Description: Unittests for the `clustering.factory` module. 

6* Author: Erwan SCHILD 

7* Created: 17/03/2021 

8* Licence: CeCILL (https://cecill.info/licences.fr.html) 

9""" 

10 

11# ============================================================================== 

12# IMPORT PYTHON DEPENDENCIES 

13# ============================================================================== 

14 

15import math 

16 

17import pytest 

18 

19from cognitivefactory.interactive_clustering.clustering.affinity_propagation import ( 

20 AffinityPropagationConstrainedClustering, 

21) 

22from cognitivefactory.interactive_clustering.clustering.dbscan import DBScanConstrainedClustering 

23from cognitivefactory.interactive_clustering.clustering.factory import clustering_factory 

24from cognitivefactory.interactive_clustering.clustering.hierarchical import HierarchicalConstrainedClustering 

25from cognitivefactory.interactive_clustering.clustering.kmeans import KMeansConstrainedClustering 

26from cognitivefactory.interactive_clustering.clustering.mpckmeans import MPCKMeansConstrainedClustering 

27from cognitivefactory.interactive_clustering.clustering.spectral import SpectralConstrainedClustering 

28 

29 

30# ============================================================================== 

31# test_clustering_factory_for_not_implemented_clustering 

32# ============================================================================== 

33def test_clustering_factory_for_not_implemented_clustering(): 

34 """ 

35 Test that the `clustering.factory.clustering_factory` method raises an `ValueError` for not implemented clustering. 

36 """ 

37 

38 # Check `ValueError` for bad string value for `algorithm`. 

39 with pytest.raises(ValueError, match="`algorithm`"): 

40 clustering_factory( 

41 algorithm="unknown", 

42 ) 

43 

44 

45# ============================================================================== 

46# test_clustering_factory_for_affinity_propagation_clustering 

47# ============================================================================== 

48def test_clustering_factory_for_affinity_propagation_clustering(): 

49 """ 

50 Test that the `clustering.factory.clustering_factory` can initialize an instance of `AffinityPropagationConstrainedClustering`. 

51 """ 

52 

53 # Check COP `clustering_factory` clustering. 

54 clustering_model = clustering_factory( 

55 algorithm="affinity_propagation", 

56 max_iteration=100, 

57 convergence_iteration=5, 

58 ) 

59 assert isinstance(clustering_model, AffinityPropagationConstrainedClustering) 

60 assert clustering_model.max_iteration == 100 

61 assert clustering_model.convergence_iteration == 5 

62 

63 

64# ============================================================================== 

65# test_clustering_factory_for_dbscan_clustering 

66# ============================================================================== 

67def test_clustering_factory_for_dbscan_clustering(): 

68 """ 

69 Test that the `clustering.factory.clustering_factory` can initialize an instance of `DBScanConstrainedClustering`. 

70 """ 

71 

72 # Check COP `dbscan` clustering. 

73 clustering_model = clustering_factory( 

74 algorithm="dbscan", 

75 eps=0.5, 

76 min_samples=3, 

77 ) 

78 assert isinstance(clustering_model, DBScanConstrainedClustering) 

79 assert math.isclose(clustering_model.eps, 0.5) 

80 assert clustering_model.min_samples == 3 

81 

82 

83# ============================================================================== 

84# test_clustering_factory_for_hierarchical_clustering 

85# ============================================================================== 

86def test_clustering_factory_for_hierarchical_clustering(): 

87 """ 

88 Test that the `clustering.factory.clustering_factory` can initialize an instance of `HierarchicalConstrainedClustering`. 

89 """ 

90 

91 # Check average `hierarchical` clustering. 

92 clustering_model = clustering_factory( 

93 algorithm="hierarchical", 

94 linkage="average", 

95 ) 

96 assert isinstance(clustering_model, HierarchicalConstrainedClustering) 

97 assert clustering_model.linkage == "average" 

98 

99 # Check single `hierarchical` clustering. 

100 clustering_model = clustering_factory( 

101 algorithm="hierarchical", 

102 linkage="single", 

103 ) 

104 assert isinstance(clustering_model, HierarchicalConstrainedClustering) 

105 assert clustering_model.linkage == "single" 

106 

107 

108# ============================================================================== 

109# test_clustering_factory_for_kmeans_clustering 

110# ============================================================================== 

111def test_clustering_factory_for_kmeans_clustering(): 

112 """ 

113 Test that the `clustering.factory.clustering_factory` can initialize an instance of `KMeansConstrainedClustering`. 

114 """ 

115 

116 # Check COP `kmeans` clustering. 

117 clustering_model = clustering_factory( 

118 algorithm="kmeans", 

119 model="COP", 

120 max_iteration=100, 

121 tolerance=1e-3, 

122 ) 

123 assert isinstance(clustering_model, KMeansConstrainedClustering) 

124 assert clustering_model.model == "COP" 

125 assert clustering_model.max_iteration == 100 

126 assert math.isclose(clustering_model.tolerance, 1e-3) 

127 

128 

129# ============================================================================== 

130# test_clustering_factory_for_mpckmeans_clustering 

131# ============================================================================== 

132def test_clustering_factory_for_mpckmeans_clustering(): 

133 """ 

134 Test that the `clustering.factory.clustering_factory` can initialize an instance of `MPCKMeansConstrainedClustering`. 

135 """ 

136 

137 # Check COP `mpckmeans` clustering. 

138 clustering_model = clustering_factory( 

139 algorithm="mpckmeans", 

140 model="MPC", 

141 max_iteration=100, 

142 w=0.5, 

143 ) 

144 assert isinstance(clustering_model, MPCKMeansConstrainedClustering) 

145 assert clustering_model.model == "MPC" 

146 assert clustering_model.max_iteration == 100 

147 assert math.isclose(clustering_model.w, 0.5) 

148 

149 

150# ============================================================================== 

151# test_clustering_factory_for_spectral_clustering 

152# ============================================================================== 

153def test_clustering_factory_for_spectral_clustering(): 

154 """ 

155 Test that the `clustering.factory.clustering_factory` can initialize an instance of `SpectralConstrainedClustering`. 

156 """ 

157 

158 # Check SPEC `spectral` clustering. 

159 clustering_model = clustering_factory( 

160 algorithm="spectral", 

161 model="SPEC", 

162 nb_components=10, 

163 ) 

164 assert isinstance(clustering_model, SpectralConstrainedClustering) 

165 assert clustering_model.model == "SPEC" 

166 assert clustering_model.nb_components == 10