Coverage for src\cognitivefactory\interactive_clustering\sampling\factory.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-17 13:31 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-17 13:31 +0100
1# -*- coding: utf-8 -*-
3"""
4* Name: cognitivefactory.interactive_clustering.sampling.factory
5* Description: The factory method used to easily initialize a constraints sampling algorithm.
6* Author: Erwan SCHILD
7* Created: 17/03/2021
8* Licence: CeCILL (https://cecill.info/licences.fr.html)
9"""
11# ==============================================================================
12# IMPORT PYTHON DEPENDENCIES
13# ==============================================================================
15from cognitivefactory.interactive_clustering.sampling.abstract import AbstractConstraintsSampling
16from cognitivefactory.interactive_clustering.sampling.clusters_based import ClustersBasedConstraintsSampling
19# ==============================================================================
20# SAMPLING FACTORY
21# ==============================================================================
22def sampling_factory(algorithm: str, **kargs) -> "AbstractConstraintsSampling":
23 """
24 A factory to create a new instance of a constraints sampling model.
26 Args:
27 algorithm (str): The identification of model to instantiate. Can be `"random"` or `"random_in_same_cluster"` or `"farthest_in_same_cluster"` or `"closest_in_different_clusters"`. Defaults to `"random"`
28 **kargs (dict): Other parameters that can be used in the instantiation.
30 Raises:
31 ValueError: if `algorithm` is not implemented.
33 Returns:
34 AbstractConstraintsSampling: An instance of constraints sampling model.
36 Example:
37 ```python
38 # Import.
39 from cognitivefactory.interactive_clustering.sampling.factory import sampling_factory
41 # Create an instance of random sampler.
42 sampler = sampling_factory(
43 algorithm="random",
44 )
45 ```
46 """
48 # Check that the requested algorithm is implemented.
49 if algorithm not in {
50 "random",
51 "random_in_same_cluster",
52 "farthest_in_same_cluster",
53 "closest_in_different_clusters",
54 }:
55 raise ValueError("The `algorithm` '" + str(algorithm) + "' is not implemented.")
57 # Case of Random In Same Cluster Constraints Sampling.
58 if algorithm == "random_in_same_cluster":
59 return ClustersBasedConstraintsSampling(clusters_restriction="same_cluster", **kargs)
61 # Case of Farthest In Same Cluster Constraints Sampling.
62 if algorithm == "farthest_in_same_cluster":
63 return ClustersBasedConstraintsSampling(
64 distance_restriction="farthest_neighbors", clusters_restriction="same_cluster", **kargs
65 )
67 # Case of Closest In Different Clusters Constraints Sampling.
68 if algorithm == "closest_in_different_clusters":
69 return ClustersBasedConstraintsSampling(
70 distance_restriction="closest_neighbors", clusters_restriction="different_clusters", **kargs
71 )
73 # Case of Random Constraints Sampling.
74 ##if algorithm == "random":
75 return ClustersBasedConstraintsSampling(**kargs)