abd-clam 0.14.0

Clustered Learning of Approximate Manifolds
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""This module contains the `Edge` and `Graph` classes."""

import typing

import numpy

from ..utils import constants
from ..utils import helpers
from . import cluster
from . import space

logger = helpers.make_logger(__name__)

AdjacencyDict = dict[cluster.Cluster, set["Edge"]]


class Edge:
    """An `Edge` represents a relationship between two `Cluster`s in a `Graph`.

    Two clusters in a graph have an edge between them if there is any overlap
    between the volumes of the two clusters.

    In CLAM, we do not allow edges from a cluster to itself.
    In CLAM, edges are bidirectional.
    """

    __slots__ = [
        "__left",
        "__right",
        "__distance",
    ]

    def __init__(
        self,
        left: cluster.Cluster,
        right: cluster.Cluster,
        distance: float,
    ) -> None:
        """Create an edge using two clusters and the distance between their centers."""
        self.__left, self.__right = (left, right) if left < right else (right, left)
        self.__distance = distance

    def __eq__(self, other: "Edge") -> bool:  # type: ignore[override]
        """Two edges are equal if their clusters are equal."""
        return self.clusters == other.clusters

    def __str__(self) -> str:
        """Returns a string representation of the edge."""
        return f"{self.__left!s} -- {self.__right!s}"

    def __repr__(self) -> str:
        """Returns a string representation of the edge."""
        return f"{self.__left!s} -- {self.__right!s} -- {self.__distance:.6e}"

    def __hash__(self) -> int:
        """Returns a hash of the edge."""
        return hash(str(self))

    def __contains__(self, c: cluster.Cluster) -> bool:
        """Returns whether the given cluster is in the edge."""
        return (c == self.__left) or (c == self.__right)

    @property
    def clusters(self) -> tuple[cluster.Cluster, cluster.Cluster]:
        """Returns the two clusters in the edge."""
        return self.__left, self.__right

    @property
    def left(self) -> cluster.Cluster:
        """Returns the left cluster in the edge."""
        return self.__left

    @property
    def right(self) -> cluster.Cluster:
        """Returns the right cluster in the edge."""
        return self.__right

    @property
    def distance(self) -> float:
        """Returns the distance between the centers of the two clusters."""
        return self.__distance

    @property
    def to_self(self) -> bool:
        """Whether the `left` and `right` clusters are equal."""
        return self.left == self.right

    def neighbor(self, c: cluster.Cluster) -> cluster.Cluster:
        """Returns the neighbor of the given cluster, if the cluster is in the edge.

        Otherwise raises a ValueError.
        """
        if c == self.__right:
            return self.__left

        if c == self.__left:
            return self.__right

        msg = f"Cluster {c.name} is not in this edge {self!s}."
        raise ValueError(msg)


class Graph:
    """A `Graph` of `Cluster`s with `Edge`s between clusters with overlapping volumes.

    We can select clusters from a tree to build a graph.

    All clusters in a graph must come from the same tree and the same
    `MetricSpace`. If using any of the `SelectionCriteria` from CLAM, this
    property will hold. Otherwise, it is upon the user to ensure.

    In CLAM, a `Graph` has a useful invariant:
        - Every instance in the dataset is in exactly one cluster in the graph.
        - No cluster is an ancestor/descendent of another cluster in the graph.

    If clusters were selected using any of the `SelectionCriteria`, this
    invariant will hold. Otherwise, the user must verify this manually.
    This invariant does not hold for sub-graphs such as connected components.
    """

    __slots__ = [
        "__clusters",
        "__root",
        "__metric_space",
        "__population",
        "__min_depth",
        "__max_depth",
        "__edges",
        "__adjacency_dict",
        "__neighborhood_sizes",
        "__components",
        "__distance_matrix",
    ]

    def __init__(
        self,
        clusters: set[cluster.Cluster],
        edges: typing.Optional[set[Edge]] = None,
    ) -> None:
        """Creates a graph from the given clusters.

        The edges need not be provided because we have efficient internal algorithms
        for finding all edges.

        Args:
            clusters: The set of clusters with which to build the graph.
            edges: Optional. The set of edges to use.
        """
        if len(clusters) == 0:
            msg = "Cannot create a graph with an empty set of clusters."
            raise ValueError(msg)

        self.__clusters = clusters

        _c = next(iter(clusters))
        self.__root = _c if _c.depth == 0 else _c.ancestry[0]
        self.__metric_space = _c.metric_space

        self.__population = sum(c.cardinality for c in self.__clusters)
        self.__min_depth = min(c.depth for c in self.__clusters)
        self.__max_depth = max(c.depth for c in self.__clusters)

        self.__edges: typing.Union[set[Edge], constants.Unset] = (
            edges or constants.UNSET
        )
        self.__adjacency_dict: typing.Union[
            AdjacencyDict,
            constants.Unset,
        ] = constants.UNSET
        self.__neighborhood_sizes: dict[cluster.Cluster, list[int]] = {}
        self.__components: typing.Union[
            list["Graph"],
            constants.Unset,
        ] = constants.UNSET
        self.__distance_matrix: typing.Union[
            numpy.ndarray,
            constants.Unset,
        ] = constants.UNSET

    @property
    def root(self) -> cluster.Cluster:
        """The root of the tree from which the clusters in the graph were selected."""
        return self.__root

    @property
    def clusters(self) -> set[cluster.Cluster]:
        """The set of clusters in the graph."""
        return self.__clusters

    @property
    def edges(self) -> set[Edge]:
        """The set of edges in the graph."""
        if self.__edges is constants.UNSET:
            msg = "Please call `build` before accessing this property."
            raise ValueError(msg)
        return self.__edges  # type: ignore[return-value]

    @property
    def metric_space(self) -> space.Space:
        """The metric space in which the clusters in the graph were selected."""
        return self.__metric_space

    @property
    def vertex_cardinality(self) -> int:
        """The number of clusters in the graph."""
        return len(self.__clusters)

    @property
    def edge_cardinality(self) -> int:
        """The number of edges in the graph."""
        return len(self.edges)

    @property
    def population(self) -> int:
        """The total number of instances across all clusters in the graph."""
        return self.__population

    @property
    def min_depth(self) -> int:
        """The minimum depth of any cluster in the graph."""
        return self.__min_depth

    @property
    def max_depth(self) -> int:
        """The maximum depth of any cluster in the graph."""
        return self.__max_depth

    @property
    def depth_range(self) -> tuple[int, int]:
        """The range of depths of the clusters in the graph."""
        return self.__min_depth, self.__max_depth

    @property
    def adjacency_dict(self) -> AdjacencyDict:
        """A dictionary of clusters and their sets of edges."""
        if self.__adjacency_dict is constants.UNSET:
            msg = "Please call `build` before accessing this property."
            raise ValueError(msg)
        return self.__adjacency_dict  # type: ignore[return-value]

    @property
    def indices(self) -> list[int]:
        """Indices of instances in all clusters in the graph."""
        return [i for c in self.__clusters for i in c.indices]

    @property
    def diameter(self) -> int:
        """The maximum eccentricity of any cluster in the graph."""
        return max(self.eccentricity(c) for c in self.__clusters)

    @property
    def components(self) -> list["Graph"]:
        """List of connected components, as sub-`Graph`s of this graph.

        If there is more than one component, no component will uphold the graph
        invariant.
        """
        if self.__components is constants.UNSET:
            self.__components = []
            unvisited = self.__clusters.copy()

            while len(unvisited) > 0:
                start = unvisited.pop()
                visited, _ = self.__traverse(start)
                unvisited = unvisited - visited
                self.__components.append(self.subgraph_with(visited))

        return self.__components  # type: ignore[return-value]

    @property
    def distance_matrix(self) -> tuple[list[cluster.Cluster], numpy.ndarray]:
        """A list of clusters in the graph and a square matrix of edge lengths."""
        if self.__distance_matrix is constants.UNSET:
            clusters = list(self.__clusters)
            indices: dict[cluster.Cluster, int] = {c: i for i, c in enumerate(clusters)}

            matrix = numpy.zeros(
                shape=(self.vertex_cardinality, self.vertex_cardinality),
            )
            for e in self.__edges:  # type: ignore[union-attr]
                i, j = indices[e.left], indices[e.right]
                matrix[i, j] = e.distance
                matrix[j, i] = e.distance

            self.__distance_matrix = matrix.astype(numpy.float32)

        return list(self.__clusters), self.__distance_matrix

    @property
    def adjacency_matrix(self) -> tuple[list[cluster.Cluster], numpy.ndarray]:
        """A list of clusters in the graph and a binary square matrix of edges."""
        clusters, matrix = self.distance_matrix
        matrix = (matrix > 0).astype(numpy.uint8)
        return clusters, matrix

    def build(self) -> "Graph":
        """Builds the `edges` and `adjacency_dict` of the graph."""
        logger.info(
            f"Building graph with {self.vertex_cardinality} clusters in a depth "
            f"range of {self.depth_range} ...",
        )

        if self.__edges is constants.UNSET:
            self.__edges = {
                Edge(c, n, d)
                for c in self.__clusters
                for n, d in c.candidate_neighbors.items()
                if (n != c) and (n in self.__clusters) and (d <= (c.radius + n.radius))
            }

        self.__adjacency_dict = {c: set() for c in self.__clusters}
        for e in self.__edges:  # type: ignore[union-attr]
            self.__adjacency_dict[e.left].add(e)
            self.__adjacency_dict[e.right].add(e)

        return self

    def assert_contains(self, c: cluster.Cluster) -> None:
        """Raises a ValueError if `c` is not in the graph."""
        if c not in self.__clusters:
            msg = f"Cluster {c} is not in this Graph."
            raise AssertionError(msg)

    def jaccard(self, other: "Graph") -> float:
        """Compute the Jaccard similarity with another graph."""
        self_indices = set(self.indices)
        other_indices = set(other.indices)

        intersection = self_indices.intersection(other_indices)
        union = self_indices.union(other_indices)

        return len(intersection) / len(union)

    def vertex_degree(self, c: cluster.Cluster) -> int:
        """Returns the degree of the given cluster."""
        self.assert_contains(c)
        return len(self.adjacency_dict[c])

    def edges_of(self, c: cluster.Cluster) -> list[Edge]:
        """Returns the edges of the given cluster."""
        self.assert_contains(c)
        return list(self.adjacency_dict[c])

    def neighbors_of(self, c: cluster.Cluster) -> list[cluster.Cluster]:
        """Returns the neighbors of the given cluster."""
        self.assert_contains(c)
        return [e.neighbor(c) for e in self.adjacency_dict[c]]

    def edge_distances_of(self, c: cluster.Cluster) -> list[float]:
        """Returns the distances of the edges of the given cluster."""
        self.assert_contains(c)
        return [e.distance for e in self.adjacency_dict[c]]

    def __traverse(
        self,
        start: cluster.Cluster,
    ) -> tuple[set[cluster.Cluster], list[int]]:
        """Performs a traversal, in arbitrary order, starting at the given cluster.

        The traversal continues until no new clusters can be visited.

        Args:
            start: where the traversal will start.

        Returns:
            A 2-tuple of:
                - set of visited clusters.
                - list of number of clusters in the frontier at each iteration
                  of the traversal.
        """
        visited: set[cluster.Cluster] = set()
        frontier: set[cluster.Cluster] = {start}
        frontier_sizes = []

        while len(frontier) > 0:
            new_frontier = {
                n
                for c in frontier
                for n in self.neighbors_of(c)
                if not ((n in visited) or (n in frontier))
            }
            visited.update(frontier)
            frontier = new_frontier
            frontier_sizes.append(len(frontier))

        return visited, frontier_sizes

    def frontier_sizes(self, c: cluster.Cluster) -> list[int]:
        """Returns the number of clusters in each frontier during the traversal."""
        self.assert_contains(c)
        if c not in self.__neighborhood_sizes:
            _, n = self.__traverse(c)
            self.__neighborhood_sizes[c] = n
        return self.__neighborhood_sizes[c]

    def eccentricity(self, c: cluster.Cluster) -> int:
        """Returns the eccentricity of the given cluster."""
        return len(self.frontier_sizes(c))

    def subgraph_with(self, clusters: set[cluster.Cluster]) -> "Graph":
        """Returns a sub-graph with the given clusters."""
        for c in clusters:
            self.assert_contains(c)

        edges = {
            e
            for e in self.__edges  # type: ignore[union-attr]
            if (e.left in clusters) and (e.right in clusters)
        }
        return Graph(clusters, edges).build()

    def component_containing(self, c: cluster.Cluster) -> "Graph":
        """Returns the connected component containing the given cluster."""
        self.assert_contains(c)
        for component in self.components:
            if c in component.__clusters:
                return component

        msg = "Cluster is the graph but not in any component. This is a bug."
        raise ValueError(
            msg,
        )

    def as_dot_string(self) -> str:
        """Returns a dot string representation of the graph for graphviz."""
        raise NotImplementedError

    @staticmethod
    def from_dot_string(dot_string: str) -> "Graph":
        """Returns a graph from the given dot string."""
        raise NotImplementedError

    def __add(self, c: cluster.Cluster) -> None:
        """Adds the given cluster to the graph."""
        raise NotImplementedError

    def __remove(self, c: cluster.Cluster) -> None:
        """Removes the given cluster from the graph."""
        raise NotImplementedError

    def replace_clusters(
        self,
        removals: set[cluster.Cluster],
        additions: set[cluster.Cluster],
    ) -> "Graph":
        """Returns a new graph with the given clusters removed and added."""
        raise NotImplementedError


__all__ = [
    "AdjacencyDict",
    "Edge",
    "Graph",
]