1+ '''A module containing the main implementation logic for the Mapper algorithm.'''
2+
13import networkx as nx
24
35from tdamapper .utils .unionfind import UnionFind
@@ -11,18 +13,18 @@ def mapper_labels(X, y, cover, clustering):
1113 '''
1214 Computes the open cover, then perform local clustering on each open set from the cover.
1315
14- :param X: A dataset
15- :type X: numpy.ndarray or list-like
16- :param y: lens values
17- :type y: numpy.ndarray or list-like
18- :param cover: A cover algorithm
19- :type cover: A class from tdamapper.cover
20- :param clustering: A clustering algorithm
21- :type clustering: A class from tdamapper.clustering or a class from sklearn.cluster
16+ :param X: A dataset.
17+ :type X: ` numpy.ndarray` or list-like.
18+ :param y: Lens values.
19+ :type y: ` numpy.ndarray` or list-like.
20+ :param cover: A cover algorithm.
21+ :type cover: A class from ` tdamapper.cover`.
22+ :param clustering: A clustering algorithm.
23+ :type clustering: A class from ` tdamapper.clustering` or a class from ` sklearn.cluster`.
2224 :return: A list where each item is a sorted list of ints with no duplicate.
23- The list at position i contains the cluster labels to which the point at position i in X
24- belongs to. If i < j, the labels at position i are strictly less then those at position j .
25- :rtype: list[list[int]]
25+ The list at position `i` contains the cluster labels to which the point at position `i` in `X`
26+ belongs to. If ` i < j` , the labels at position `i` are strictly less then those at position `j` .
27+ :rtype: ` list[list[int]]`.
2628 '''
2729 itm_lbls = [[] for _ in X ]
2830 max_lbl = 0
@@ -39,12 +41,30 @@ def mapper_labels(X, y, cover, clustering):
3941
4042
4143def mapper_connected_components (X , y , cover , clustering ):
44+ '''
45+ Computes the connected components of the Mapper graph.
46+ The algorithm computes the connected components using a union-find data structure.
47+ This approach should be faster than computing the Mapper graph by first calling
48+ `tdamapper.core.mapper_graph` and then calling `networkx.connected_components` on it.
49+
50+ :param X: A dataset.
51+ :type X: `numpy.ndarray` or list-like.
52+ :param y: Lens values.
53+ :type y: `numpy.ndarray` or list-like.
54+ :param cover: A cover algorithm.
55+ :type cover: A class from `tdamapper.cover`.
56+ :param clustering: A clustering algorithm.
57+ :type clustering: A class from `tdamapper.clustering` or a class from `sklearn.cluster`.
58+ :return: A list of labels, where the value at position `i` identifies
59+ the connected component of the point `X[i]`.
60+ :rtype: `list[int]`.
61+ '''
4262 itm_lbls = mapper_labels (X , y , cover , clustering )
4363 label_values = set ()
4464 for lbls in itm_lbls :
4565 label_values .update (lbls )
4666 uf = UnionFind (label_values )
47- labels = []
67+ labels = [- 1 for _ in X ]
4868 for lbls in itm_lbls :
4969 len_lbls = len (lbls )
5070 # noise points
@@ -61,18 +81,18 @@ def mapper_connected_components(X, y, cover, clustering):
6181
6282def mapper_graph (X , y , cover , clustering ):
6383 '''
64- Computes the Mapper graph
65-
66- :param X: A dataset
67- :type X: numpy.ndarray or list-like
68- :param y: Lens values
69- :type y: numpy.ndarray or list-like
70- :param cover: A cover algorithm
71- :type cover: A class from tdamapper.cover
72- :param clustering: A clustering algorithm
73- :type clustering: A class from tdamapper.clustering or a class from sklearn.cluster
74- :return: The Mapper graph
75- :rtype: networkx.Graph
84+ Computes the Mapper graph.
85+
86+ :param X: A dataset.
87+ :type X: ` numpy.ndarray` or list-like.
88+ :param y: Lens values.
89+ :type y: ` numpy.ndarray` or list-like.
90+ :param cover: A cover algorithm.
91+ :type cover: A class from ` tdamapper.cover`.
92+ :param clustering: A clustering algorithm.
93+ :type clustering: A class from ` tdamapper.clustering` or a class from ` sklearn.cluster`.
94+ :return: The Mapper graph.
95+ :rtype: ` networkx.Graph`.
7696 '''
7797 itm_lbls = mapper_labels (X , y , cover , clustering )
7898 graph = nx .Graph ()
@@ -95,6 +115,18 @@ def mapper_graph(X, y, cover, clustering):
95115
96116
97117def aggregate_graph (y , graph , agg ):
118+ '''
119+ Computes an aggregation on the nodes of a graph.
120+
121+ :param y: A dataset.
122+ :type y: `numpy.ndarray` or list-like.
123+ :param graph: A graph.
124+ :type graph: `networkx.Graph`.
125+ :param agg: An aggregation function.
126+ :type agg: Callable.
127+ :return: A dict of values, where each node is mapped to its aggregation.
128+ :rtype: `dict`.
129+ '''
98130 agg_values = {}
99131 nodes = graph .nodes ()
100132 for node_id in nodes :
@@ -108,10 +140,10 @@ class MapperAlgorithm:
108140 '''
109141 Main class for performing the Mapper Algorithm.
110142
111- :param cover: A cover algorithm
112- :type cover: A class from tdamapper.cover
113- :param clustering: A clustering algorithm
114- :type clustering: A class from tdamapper.clustering or a class from sklearn.cluster
143+ :param cover: A cover algorithm.
144+ :type cover: A class from ` tdamapper.cover`.
145+ :param clustering: A clustering algorithm.
146+ :type clustering: A class from ` tdamapper.clustering` or a class from ` sklearn.cluster`.
115147 '''
116148
117149 def __init__ (self , cover , clustering ):
@@ -123,24 +155,24 @@ def fit(self, X, y=None):
123155 '''
124156 Computes the Mapper Graph
125157
126- :param X: A dataset
127- :type X: numpy.ndarray or list-like
128- :param y: Lens values
129- :type y: numpy.ndarray or list-like
130- :return: self
158+ :param X: A dataset.
159+ :type X: ` numpy.ndarray` or list-like.
160+ :param y: Lens values.
161+ :type y: ` numpy.ndarray` or list-like.
162+ :return: ` self`.
131163 '''
132164 self .graph_ = self .fit_transform (X , y )
133165 return self
134166
135167 def fit_transform (self , X , y ):
136168 '''
137- Computes the Mapper Graph
138-
139- :param X: A dataset
140- :type X: numpy.ndarray or list-like
141- :param y: Lens values
142- :type y: numpy.ndarray or list-like
143- :return: The Mapper Graph
144- :rtype: networkx.Graph
169+ Computes the Mapper Graph.
170+
171+ :param X: A dataset.
172+ :type X: ` numpy.ndarray` or list-like.
173+ :param y: Lens values.
174+ :type y: ` numpy.ndarray` or list-like.
175+ :return: The Mapper graph.
176+ :rtype: ` networkx.Graph`
145177 '''
146178 return mapper_graph (X , y , self .__cover , self .__clustering )
0 commit comments