Coverage for tests\sampling\test_factory_random.py: 100.00%

38 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/sampling/test_factory_random.py 

5* Description: Unittests for the `sampling.cluster_based` module, `"random"` sampler. 

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 pytest 

16 

17from cognitivefactory.interactive_clustering.constraints.binary import BinaryConstraintsManager 

18from cognitivefactory.interactive_clustering.sampling.clusters_based import ClustersBasedConstraintsSampling 

19 

20 

21# ============================================================================== 

22# test_factory_random_sampler_for_correct_settings 

23# ============================================================================== 

24def test_factory_random_sampler_for_correct_settings(): 

25 """ 

26 Test that the `random sampler` works for correct settings. 

27 """ 

28 

29 # Check a correct initialization. 

30 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

31 

32 assert sampler 

33 assert sampler.random_seed == 1 

34 

35 

36# ============================================================================== 

37# test_factory_random_sampler_sample_for_incorrect_constraints_manager 

38# ============================================================================== 

39def test_factory_random_sampler_sample_for_incorrect_constraints_manager(): 

40 """ 

41 Test that the `random sampler` sampling raises `ValueError` for incorrect `constraints_manager`. 

42 """ 

43 

44 # Initialize a `random sampler` instance. 

45 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

46 

47 # Check sample with incorrect `constraints_manager`. 

48 with pytest.raises(ValueError, match="`constraints_manager`"): 

49 sampler.sample( 

50 constraints_manager=None, 

51 nb_to_select=None, 

52 ) 

53 

54 

55# ============================================================================== 

56# test_factory_random_sampler_sample_for_incorrect_nb_to_select 

57# ============================================================================== 

58def test_factory_random_sampler_sample_for_incorrect_nb_to_select(): 

59 """ 

60 Test that the `random sampler` sampling raises `ValueError` for incorrect `nb_to_select`. 

61 """ 

62 

63 # Initialize a `random sampler` instance. 

64 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

65 

66 # Check sample with incorrect `nb_to_select`. 

67 with pytest.raises(ValueError, match="`nb_to_select`"): 

68 sampler.sample( 

69 constraints_manager=BinaryConstraintsManager( 

70 list_of_data_IDs=[ 

71 "bonjour", 

72 "salut", 

73 "coucou", 

74 "au revoir", 

75 "a bientôt", 

76 ] 

77 ), 

78 nb_to_select=None, 

79 ) 

80 

81 # Check sample with incorrect `nb_to_select` 

82 with pytest.raises(ValueError, match="`nb_to_select`"): 

83 sampler.sample( 

84 constraints_manager=BinaryConstraintsManager( 

85 list_of_data_IDs=[ 

86 "bonjour", 

87 "salut", 

88 "coucou", 

89 "au revoir", 

90 "a bientôt", 

91 ], 

92 ), 

93 nb_to_select=-99, 

94 ) 

95 

96 

97# ============================================================================== 

98# test_factory_random_sampler_sample_for_zero_nb_to_select 

99# ============================================================================== 

100def test_factory_random_sampler_sample_for_zero_nb_to_select(): 

101 """ 

102 Test that the `random sampler` sampling works for zero `nb_to_select`. 

103 """ 

104 

105 # Initialize a `random sampler` instance. 

106 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

107 

108 # Check sample with zero `nb_to_select` 

109 assert not sampler.sample( 

110 constraints_manager=BinaryConstraintsManager( 

111 list_of_data_IDs=[ 

112 "bonjour", 

113 "salut", 

114 "coucou", 

115 "au revoir", 

116 "a bientôt", 

117 ], 

118 ), 

119 nb_to_select=0, 

120 ) 

121 

122 

123# ============================================================================== 

124# test_factory_random_sampler_sample_for_empty_constraints_manager 

125# ============================================================================== 

126def test_factory_random_sampler_sample_for_empty_constraints_manager(): 

127 """ 

128 Test that the `random sampler` sampling works for empty `constraints_manager`. 

129 """ 

130 

131 # Initialize a `random sampler` instance. 

132 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

133 

134 # Check sample with empty `constraints_manager` 

135 assert sampler.sample( 

136 constraints_manager=BinaryConstraintsManager( 

137 list_of_data_IDs=[ 

138 "bonjour", 

139 "salut", 

140 "coucou", 

141 "au revoir", 

142 "a bientôt", 

143 ], 

144 ), 

145 nb_to_select=3, 

146 ) == [("coucou", "salut"), ("bonjour", "coucou"), ("au revoir", "salut")] 

147 

148 

149# ============================================================================== 

150# test_factory_random_sampler_sample_for_correct_constraints_manager 

151# ============================================================================== 

152def test_factory_random_sampler_sample_for_correct_constraints_manager(): 

153 """ 

154 Test that the `random sampler` sampling works for correct `constraints_manager`. 

155 """ 

156 

157 # Initialize a `random sampler` instance. 

158 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

159 

160 # Initialize a `BinaryConstraintsManager` instance 

161 constraints_manager = BinaryConstraintsManager( 

162 list_of_data_IDs=[ 

163 "bonjour", 

164 "salut", 

165 "coucou", 

166 "au revoir", 

167 "a bientôt", 

168 ] 

169 ) 

170 constraints_manager.add_constraint(data_ID1="bonjour", data_ID2="salut", constraint_type="MUST_LINK") 

171 constraints_manager.add_constraint(data_ID1="au revoir", data_ID2="a bientôt", constraint_type="MUST_LINK") 

172 

173 # Check sample with correct `constraints_manager` 

174 assert sampler.sample( 

175 constraints_manager=constraints_manager, 

176 nb_to_select=3, 

177 ) == [("au revoir", "bonjour"), ("au revoir", "coucou"), ("bonjour", "coucou")] 

178 

179 

180# ============================================================================== 

181# test_factory_random_sampler_sample_for_full_annotated_constraints_manager 

182# ============================================================================== 

183def test_factory_random_sampler_sample_for_full_annotated_constraints_manager(): 

184 """ 

185 Test that the `random sampler` sampling works for full annotated `constraints_manager`. 

186 """ 

187 

188 # Initialize a `random sampler` instance. 

189 sampler = ClustersBasedConstraintsSampling(random_seed=1) 

190 

191 # Initialize a `BinaryConstraintsManager` instance 

192 constraints_manager = BinaryConstraintsManager( 

193 list_of_data_IDs=[ 

194 "bonjour", 

195 "salut", 

196 "coucou", 

197 "au revoir", 

198 "a bientôt", 

199 ] 

200 ) 

201 constraints_manager.add_constraint(data_ID1="bonjour", data_ID2="salut", constraint_type="MUST_LINK") 

202 constraints_manager.add_constraint(data_ID1="bonjour", data_ID2="coucou", constraint_type="MUST_LINK") 

203 constraints_manager.add_constraint(data_ID1="bonjour", data_ID2="au revoir", constraint_type="CANNOT_LINK") 

204 constraints_manager.add_constraint(data_ID1="au revoir", data_ID2="a bientôt", constraint_type="MUST_LINK") 

205 

206 # Check sample for full annotated `constraints_manager` 

207 assert not sampler.sample( 

208 constraints_manager=constraints_manager, 

209 nb_to_select=3, 

210 )