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

27 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.py 

5* Description: Unittests for the `sampling.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 pytest 

16 

17from cognitivefactory.interactive_clustering.sampling.clusters_based import ClustersBasedConstraintsSampling 

18from cognitivefactory.interactive_clustering.sampling.factory import sampling_factory 

19 

20 

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

22# test_sampling_factory_for_not_implemented_clustering 

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

24def test_sampling_factory_for_not_implemented_clustering(): 

25 """ 

26 Test that the `sampling.sampling_factory` method raises an `ValueError` for not implemented clustering. 

27 """ 

28 

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

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

31 sampling_factory( 

32 algorithm="unknown", 

33 ) 

34 

35 

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

37# test_sampling_factory_for_random_sampling 

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

39def test_sampling_factory_for_random_sampling(): 

40 """ 

41 Test that the `sampling.sampling_factory` can initialize an instance of `ClustersBasedConstraintsSampling` to sample random pairs of data IDs. 

42 """ 

43 

44 # Check average `random` sampling. 

45 sampling_model = sampling_factory( 

46 algorithm="random", 

47 ) 

48 assert isinstance(sampling_model, ClustersBasedConstraintsSampling) 

49 assert sampling_model.clusters_restriction is None 

50 assert sampling_model.distance_restriction is None 

51 

52 

53# ============================================================================== 

54# test_sampling_factory_for_random_in_same_cluster_sampling 

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

56def test_sampling_factory_for_random_in_same_cluster_sampling(): 

57 """ 

58 Test that the `sampling.sampling_factory` can initialize an instance of `ClustersBasedConstraintsSampling` to sample random pairs of data IDs in same clusters. 

59 """ 

60 

61 # Check average `random_in_same_cluster` sampling. 

62 sampling_model = sampling_factory( 

63 algorithm="random_in_same_cluster", 

64 ) 

65 assert isinstance(sampling_model, ClustersBasedConstraintsSampling) 

66 assert sampling_model.clusters_restriction == "same_cluster" 

67 assert sampling_model.distance_restriction is None 

68 

69 

70# ============================================================================== 

71# test_sampling_factory_for_farthest_in_same_cluster_sampling 

72# ============================================================================== 

73def test_sampling_factory_for_farthest_in_same_cluster_sampling(): 

74 """ 

75 Test that the `sampling.sampling_factory` can initialize an instance of `ClustersBasedConstraintsSampling` to sample farthest pairs of data IDs in same clusters. 

76 """ 

77 

78 # Check average `farthest_in_same_cluster` sampling. 

79 sampling_model = sampling_factory( 

80 algorithm="farthest_in_same_cluster", 

81 ) 

82 assert isinstance(sampling_model, ClustersBasedConstraintsSampling) 

83 assert sampling_model.clusters_restriction == "same_cluster" 

84 assert sampling_model.distance_restriction == "farthest_neighbors" 

85 

86 

87# ============================================================================== 

88# test_sampling_factory_for_closest_in_different_clusters_sampling 

89# ============================================================================== 

90def test_sampling_factory_for_closest_in_different_clusters_sampling(): 

91 """ 

92 Test that the `sampling.sampling_factory` can initialize an instance of `ClustersBasedConstraintsSampling` to sample closest pairs of data IDs in different clusters. 

93 """ 

94 

95 # Check average `closest_in_different_clusters` sampling. 

96 sampling_model = sampling_factory( 

97 algorithm="closest_in_different_clusters", 

98 ) 

99 assert isinstance(sampling_model, ClustersBasedConstraintsSampling) 

100 assert sampling_model.clusters_restriction == "different_clusters" 

101 assert sampling_model.distance_restriction == "closest_neighbors"