Coverage for src\cognitivefactory\interactive_clustering\clustering\factory.py: 100.00%
23 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.clustering.factory
5* Description: The factory method used to easily initialize a constrained clustering algorithm.
6* Author: Erwan SCHILD
7* Created: 17/03/2021
8* Licence: CeCILL-C License v1.0 (https://cecill.info/licences.fr.html)
9"""
11# ==============================================================================
12# IMPORT PYTHON DEPENDENCIES
13# ==============================================================================
15from cognitivefactory.interactive_clustering.clustering.abstract import AbstractConstrainedClustering
16from cognitivefactory.interactive_clustering.clustering.affinity_propagation import (
17 AffinityPropagationConstrainedClustering,
18)
19from cognitivefactory.interactive_clustering.clustering.dbscan import DBScanConstrainedClustering
20from cognitivefactory.interactive_clustering.clustering.hierarchical import HierarchicalConstrainedClustering
21from cognitivefactory.interactive_clustering.clustering.kmeans import KMeansConstrainedClustering
22from cognitivefactory.interactive_clustering.clustering.mpckmeans import MPCKMeansConstrainedClustering
23from cognitivefactory.interactive_clustering.clustering.spectral import SpectralConstrainedClustering
26# ==============================================================================
27# CLUSTERING FACTORY
28# ==============================================================================
29def clustering_factory(algorithm: str = "kmeans", **kargs) -> "AbstractConstrainedClustering":
30 """
31 A factory to create a new instance of a constrained clustering model.
33 Args:
34 algorithm (str): The identification of model to instantiate. Can be `"affinity_propagation"`, `"dbscan"`, `"hierarchical"`, `"kmeans"`, `"mpckmeans"` or `"spectral"`. Defaults to `"kmeans"`.
35 **kargs (dict): Other parameters that can be used in the instantiation.
37 Warns:
38 FutureWarning: `clustering.affinity_propagation.AffinityPropagationConstrainedClustering`, `clustering.dbscan.DBScanConstrainedClustering` and `clustering.mpckmeans.MPCKMeansConstrainedClustering` are still in development and are not fully tested : it is not ready for production use.
40 Raises:
41 ValueError: if `algorithm` is not implemented.
43 Returns:
44 AbstractConstraintsClustering: An instance of constrained clustering model.
46 Example:
47 ```python
48 # Import.
49 from cognitivefactory.interactive_clustering.clustering.factory import clustering_factory
51 # Create an instance of kmeans.
52 clustering_model = clustering_factory(
53 algorithm="kmeans",
54 model="COP",
55 random_seed=42,
56 )
57 ```
58 """
60 # Check that the requested algorithm is implemented.
61 if algorithm not in {
62 "affinity_propagation",
63 "dbscan",
64 "hierarchical",
65 "kmeans",
66 "mpckmeans",
67 "spectral",
68 }:
69 raise ValueError("The `algorithm` '" + str(algorithm) + "' is not implemented.")
71 # Initialize
72 cluster_object: AbstractConstrainedClustering
74 # Case of Affinity Propagation Constrained Clustering.
75 if algorithm == "affinity_propagation":
76 cluster_object = AffinityPropagationConstrainedClustering(**kargs)
78 # Case of DBScan Constrained Clustering.
79 elif algorithm == "dbscan":
80 cluster_object = DBScanConstrainedClustering(**kargs)
82 # Case of Hierachical Constrained Clustering.
83 elif algorithm == "hierarchical":
84 cluster_object = HierarchicalConstrainedClustering(**kargs)
86 # Case of MPC KMmeans Constrained Clustering.
87 elif algorithm == "mpckmeans":
88 cluster_object = MPCKMeansConstrainedClustering(**kargs)
90 # Case of Spectral Constrained Clustering.
91 elif algorithm == "spectral":
92 cluster_object = SpectralConstrainedClustering(**kargs)
94 # Default case of KMeans Constrained Clustering (algorithm=="kmeans":).
95 else:
96 cluster_object = KMeansConstrainedClustering(**kargs)
98 # Return cluster object.
99 return cluster_object