Skip to content

factory

  • Name: cognitivefactory.interactive_clustering.sampling.factory
  • Description: The factory method used to easily initialize a constraints sampling algorithm.
  • Author: Erwan SCHILD
  • Created: 17/03/2021
  • Licence: CeCILL (https://cecill.info/licences.fr.html)

sampling_factory(algorithm, **kargs)

A factory to create a new instance of a constraints sampling model.

Parameters:

Name Type Description Default
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"

required
**kargs dict

Other parameters that can be used in the instantiation.

{}

Raises:

Type Description
ValueError

if algorithm is not implemented.

Returns:

Name Type Description
AbstractConstraintsSampling AbstractConstraintsSampling

An instance of constraints sampling model.

Example
# Import.
from cognitivefactory.interactive_clustering.sampling.factory import sampling_factory

# Create an instance of random sampler.
sampler = sampling_factory(
    algorithm="random",
)
Source code in src\cognitivefactory\interactive_clustering\sampling\factory.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def sampling_factory(algorithm: str, **kargs) -> "AbstractConstraintsSampling":
    """
    A factory to create a new instance of a constraints sampling model.

    Args:
        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"`
        **kargs (dict): Other parameters that can be used in the instantiation.

    Raises:
        ValueError: if `algorithm` is not implemented.

    Returns:
        AbstractConstraintsSampling: An instance of constraints sampling model.

    Example:
        ```python
        # Import.
        from cognitivefactory.interactive_clustering.sampling.factory import sampling_factory

        # Create an instance of random sampler.
        sampler = sampling_factory(
            algorithm="random",
        )
        ```
    """

    # Check that the requested algorithm is implemented.
    if algorithm not in {
        "random",
        "random_in_same_cluster",
        "farthest_in_same_cluster",
        "closest_in_different_clusters",
    }:
        raise ValueError("The `algorithm` '" + str(algorithm) + "' is not implemented.")

    # Case of Random In Same Cluster Constraints Sampling.
    if algorithm == "random_in_same_cluster":
        return ClustersBasedConstraintsSampling(clusters_restriction="same_cluster", **kargs)

    # Case of Farthest In Same Cluster Constraints Sampling.
    if algorithm == "farthest_in_same_cluster":
        return ClustersBasedConstraintsSampling(
            distance_restriction="farthest_neighbors", clusters_restriction="same_cluster", **kargs
        )

    # Case of Closest In Different Clusters Constraints Sampling.
    if algorithm == "closest_in_different_clusters":
        return ClustersBasedConstraintsSampling(
            distance_restriction="closest_neighbors", clusters_restriction="different_clusters", **kargs
        )

    # Case of Random Constraints Sampling.
    ##if algorithm == "random":
    return ClustersBasedConstraintsSampling(**kargs)