point_cloud_graph
PointCloudGraph Link
PointCloudGraph(G=None)
Bases: Graph
A graph class representing point cloud data and providing various functionalities for graph processing.
This class is designed to represent and manipulate a graph derived from point cloud data. It supports storing and processing graph-related attributes such as adjacency matrix, eigenvectors, and gradient information. The class enables operations including spectral graph analysis and gradient computation on specific signal vectors.
Attributes:
-
nodes_coords
(ndarray or None
) –Coordinates of the nodes in the graph, derived from point cloud data. None if not initialized.
-
laplacian
(ndarray or None
) –The Laplacian matrix of the graph. None if not computed.
-
keigenvec
(ndarray or None
) –Array of eigenvectors of the graph Laplacian. None if not computed.
-
keigenval
(ndarray or None
) –Array of eigenvalues of the graph Laplacian. None if not computed.
-
gradient_on_fiedler
(ndarray or None
) –Gradient computed on the Fiedler vector of the graph. None if not computed.
-
direction_gradient_on_fiedler_scaled
(ndarray or None
) –Scaled direction gradient computed on the Fiedler vector of the graph. None if not computed.
-
clustering_labels
(ndarray or None
) –Labels for node clustering based on graph properties. None if not computed.
-
clusters_leaves
(list or None
) –Leaf nodes in each cluster. None if not computed.
-
kmeans_labels_gradient
(ndarray or None
) –Labels for k-means clustering applied to gradient data. None if not computed.
-
minimum_local
(list or None
) –Local minima within a processed graph signal. None if not initialized.
-
pcd
(PointCloud or None
) –Point cloud data associated with the graph. None if not provided or initialized.
-
extrem_local_fiedler
(list or None
) –Local extrema computed on the Fiedler vector. None if not initialized.
Source code in spectral_clustering/point_cloud_graph.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
add_anything_as_attribute Link
add_anything_as_attribute(anything, name_of_the_new_attribute)
Assigns specified values as attributes to the nodes of a graph.
This function adds attributes to the nodes of a graph by mapping the given values to the nodes based on their order, and then assigning these mapped values to a new attribute name provided by the user.
Parameters:
-
anything
(list
) –A list of values corresponding to nodes of the graph. The values should be in the same order as the nodes for proper mapping.
-
name_of_the_new_attribute
(str
) –The name of the new attribute that will be added to each node, with corresponding values from the input list.
Source code in spectral_clustering/point_cloud_graph.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
add_coordinates_as_attribute_for_each_node Link
add_coordinates_as_attribute_for_each_node()
Adds coordinates as attributes to each node in the graph.
This method assigns the X, Y, and Z coordinates of each node in the graph as attributes named 'X_coordinate', 'Y_coordinate', and 'Z_coordinate', respectively. The node attributes are created by mapping the node IDs to their respective coordinate values in the provided coordinate data.
Each node's X, Y, and Z coordinate values are extracted from nodes_coords
and then applied as attributes to the nodes using NetworkX's
set_node_attributes
method.
Notes
This method assumes that the nodes_coords
attribute is a NumPy array and that
it contains node coordinate data in the format where:
- The first column represents the X coordinates.
- The second column represents the Y coordinates.
- The third column represents the Z coordinates.
The number of nodes in the graph must match the number of rows in the
nodes_coords
array.
Source code in spectral_clustering/point_cloud_graph.py
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 |
|
add_eigenvector_value_as_attribute Link
add_eigenvector_value_as_attribute(k=2)
Adds the k-th eigenvector's values as a node attribute to the graph.
This method assigns the values of the specified eigenvector from the
precomputed eigenvector matrix keigenvec
to the nodes of the graph. The
attribute is stored under the name 'eigenvector_k'
, where k
corresponds
to the specified eigenvector index. It is required that the graph spectrum
has been computed prior to using this method; otherwise, it will prompt
the user to compute the spectrum.
Parameters:
-
k
(int
, default:2
) –Index of the eigenvector to be added as a node attribute, starting from 1. Defaults to 2. Must be in the range of available eigenvectors in the precomputed eigenvector matrix
keigenvec
.
Notes
This method modifies the graph in place by assigning the eigenvector values
as node attributes. The spectrum of the graph must be computed before using
this method, and the computed eigenvector matrix should be stored in the
keigenvec
attribute of the instance.
Source code in spectral_clustering/point_cloud_graph.py
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 |
|
add_gradient_of_fiedler_vector_as_attribute Link
add_gradient_of_fiedler_vector_as_attribute()
Adds the gradient of the Fiedler vector for each node as a node attribute.
This method computes the gradient of the Fiedler vector and assigns it to a new node attribute called 'gradient_of_fiedler_vector' for all nodes in the graph. The gradient is given as a dictionary where the keys are node identifiers and the values are the gradient values corresponding to those nodes. This information can be useful for analyzing the structural properties of the graph.
Returns:
-
None
–This method does not return any value. It modifies the graph in place by adding a new node attribute.
Source code in spectral_clustering/point_cloud_graph.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
|
clustering_by_fiedler Link
clustering_by_fiedler(method='agglomerative', number_of_clusters=2, criteria=[], name_attribute='clustering_label')
Performs clustering on a graph using the specified method and criteria.
This method applies clustering techniques to a graph using a given method (either 'agglomerative' or 'optics') based on the provided criteria. For the 'agglomerative' method, the graph's adjacency matrix is used as the connectivity constraint. The clustering labels generated are stored as node attributes within the graph.
Parameters:
-
method
(str
, default:'agglomerative'
) –The clustering method to be used. Accepted values are 'agglomerative' and 'optics'. Default is 'agglomerative'.
-
number_of_clusters
(int
, default:2
) –The number of clusters to be formed. Only applicable when the method is 'agglomerative'. Default is 2.
-
criteria
(list
, default:[]
) –The criteria or feature matrix used as input for the clustering algorithm.
-
name_attribute
(str
, default:'clustering_label'
) –The attribute name to store the clustering labels in the graph nodes. Default is 'clustering_label'.
Source code in spectral_clustering/point_cloud_graph.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
clustering_by_fiedler_and_agglomerative Link
clustering_by_fiedler_and_agglomerative(number_of_clusters=2, criteria=[])
Clusters the nodes of the graph using Fiedler embeddings and an agglomerative clustering algorithm.
This method applies the adjacency matrix of the graph to create clusters of
nodes based on their similarity or proximity as determined by the provided criteria
.
Parameters:
-
number_of_clusters
(int
, default:2
) –The number of clusters to form. Defaults to 2.
-
criteria
(array - like
, default:[]
) –The attributes or features of the nodes, which are used to compute the clustering.
Source code in spectral_clustering/point_cloud_graph.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
clustering_by_fiedler_and_optics Link
clustering_by_fiedler_and_optics(criteria=[])
Clusters data points using Fiedler vector and OPTICS algorithm.
This method applies the OPTICS clustering algorithm with a specified minimum number of samples (min_samples=100) on the given input criteria. The computed clustering labels are then used to assign an 'optics_label' to each node in the object's nodes list.
Parameters:
-
criteria
(list
, default:[]
) –A list of criteria representing data points, typically required by the OPTICS clustering algorithm for analysis and computation. Each element in the list corresponds to a feature vector representing a node.
Source code in spectral_clustering/point_cloud_graph.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
clustering_by_kmeans_using_gradient_norm Link
clustering_by_kmeans_using_gradient_norm(export_in_labeled_point_cloud=False, number_of_clusters=4)
Performs K-means clustering on the gradient norm of the graph's Fiedler vector.
This method applies the K-means clustering algorithm to nodes of a graph, based on their gradient norm values calculated on the graph's Fiedler vector. The number of clusters and the option to export the clustering results in a labeled point cloud file are configurable. The method also stores the clustering labels as node attributes in the graph.
Parameters:
-
export_in_labeled_point_cloud
(bool
, default:False
) –If True, exports the clustering labels into a labeled point cloud file named 'kmeans_clusters.txt'. Defaults to False.
-
number_of_clusters
(int
, default:4
) –The number of clusters to create using K-means. Defaults to 4.
Returns:
-
ndarray
–The coordinates of the cluster centers as determined by K-means.
Source code in spectral_clustering/point_cloud_graph.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
compute_angles_from_gradient_directions Link
compute_angles_from_gradient_directions(angle_computed='angle_max')
Computes and assigns a specified statistical value derived from gradient direction angles between a node and its neighbors to each node in the graph.
The computation is performed based on the gradient direction of the current node and its neighbors. The user can specify the statistical value to be computed from the angles, such as maximum, mean, variance, standard deviation, or median.
Parameters:
-
angle_computed
(str
, default:'angle_max'
) –The type of statistical value to compute from the gradient direction angles. Acceptable values are: - 'angle_max': Maximum angle among all neighbors. - 'angle_mean': Mean of the angles. - 'angle_variance': Variance of the angles. - 'angle_standard_deviation': Standard deviation of the angles. - 'angle_median': Median of the angles. Default is 'angle_max'.
Source code in spectral_clustering/point_cloud_graph.py
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 455 456 457 458 459 460 |
|
compute_gradient_of_fiedler_vector Link
compute_gradient_of_fiedler_vector(method='simple')
Computes the gradient of the Fiedler vector for a graph using various methods.
The Fiedler vector is used for a variety of graph analysis tasks, including spectral clustering and structure discovery. The gradient is computed based on adjacency relationships and associated weights depending on the chosen method.
Parameters:
-
method
(str
, default:'simple'
) –The method to compute the gradient of the Fiedler vector. It supports the following options: - 'simple': Computes the gradient as the difference between the maximum and minimum values of the Fiedler vector in the neighborhood of each node. - 'simple_divided_by_distance': Computes the same as 'simple', but normalizes the gradient values by the Euclidean distance between nodes corresponding to the maximum and minimum values in the neighborhood. - 'simple_divided_by_distance_along_edges': Normalizes the gradient by summing Euclidean distances along edges linking the nodes with the maximum, minimum, and central (current node) Fiedler values. - 'by_laplacian_matrix_on_fiedler_signal': Uses the graph Laplacian matrix to compute the gradient directly on the Fiedler vector. - 'by_weight': Computes the gradient using edge weights and Fiedler values. - 'by_fiedler_weight': Computes the gradient as the weighted difference of positional displacements and Fiedler values between neighboring nodes.
Raises:
-
ValueError
–If an unsupported method is provided.
-
AttributeError
–If any required attributes such as
keigenvec
,nodes_coords
, orlaplacian
is missing on the object or improperly initialized.
Notes
This function modifies several instance attributes, including:
- gradient_on_fiedler
: Stores the computed gradient tensor for the Fiedler vector.
- direction_gradient_on_fiedler_scaled
: Stores the normalized direction vectors of the computed
gradient for each node.
- Direction gradient as an added attribute on the object.
This function requires the graph to be represented in a NetworkX-compatible adjacency matrix or with node and edge attributes such as positional coordinates or weight information. Different methods may involve additional computational costs; for instance, distance-based normalizations loop over nodes and their neighborhoods, which could be computationally expensive for dense graphs.
Source code in spectral_clustering/point_cloud_graph.py
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 |
|
compute_graph_eigenvectors Link
compute_graph_eigenvectors(is_sparse=True, k=50, smallest_first=True, laplacian_type='classical')
Computes the eigenvectors of the graph's Laplacian matrix.
This method calculates the eigenvalues and eigenvectors of the graph's Laplacian matrix. It uses either a sparse or dense matrix representation based on the input parameters and retains the desired number of smallest or largest eigenvectors as specified. The computed eigenvectors and eigenvalues are stored as attributes of the graph, and the eigenvector values are added as attributes to graph nodes.
Before computation, the graph Laplacian is generated if it does not already
exist. The graph_spectrum
function is used to perform the spectral
decomposition. Finally, node attributes are updated using the computed
eigenvector values.
Parameters:
-
is_sparse
(bool
, default:True
) –Determines if the sparse representation of the graph's Laplacian is used during the computation. If set to True, a sparse implementation is applied. Default is True.
-
k
(int
, default:50
) –The number of eigenvalues and eigenvectors to compute. For example, if set to 50, the 50 smallest (or largest) eigenvalues are selected based on the
smallest_first
parameter. Default is 50. -
smallest_first
(bool
, default:True
) –Indicates whether the k smallest or largest eigenvalues are computed. If True, the k smallest eigenvalues (and corresponding eigenvectors) are computed. Default is True.
-
laplacian_type
(str
, default:'classical'
) –The type of graph Laplacian to compute. Options could include "classical" or other variations depending on the implementation of
compute_graph_laplacian
andgraph_spectrum
. Default is "classical".
Source code in spectral_clustering/point_cloud_graph.py
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 |
|
compute_graph_laplacian Link
compute_graph_laplacian(laplacian_type='classical')
Compute the graph Laplacian for the graph object.
This method computes the Laplacian matrix of a graph represented by the
current object using the specified type of Laplacian. The computed Laplacian
differentiate various aspects of graph structure depending upon the laplacian_type
parameter value provided.
The resulting Laplacian is stored in the laplacian
attribute of the object
for subsequent graph-based analysis.
Parameters:
-
laplacian_type
–The type of Laplacian matrix to compute. Default is 'classical'. Other options include normalized or non-standard Laplacians depending on the library capabilities or analysis requirements.
Source code in spectral_clustering/point_cloud_graph.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
find_local_extremum_of_Fiedler Link
find_local_extremum_of_Fiedler()
Identifies the nodes in the graph that are local extrema based on the second-smallest eigenvector (Fiedler vector) of the graph's Laplacian matrix.
Nodes are compared to their immediate neighbors to determine whether they are local maxima or minima.
Attributes:
-
extrem_local_fiedler
(list
) –Contains the IDs of nodes identified as local extrema based on the Fiedler vector values. Updated after the function is executed.
Notes
Each node in the graph is evaluated by comparing its eigenvector_2 value
to those of its neighbors. Nodes are marked as local extrema if their
eigenvector_2 value is higher or lower than all of their neighbors. The
property extrem_local_Fiedler
is updated for each node to indicate its
status (1 if it is a local extremum, 0 otherwise).
Source code in spectral_clustering/point_cloud_graph.py
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
|
find_local_minimum_of_gradient_norm Link
find_local_minimum_of_gradient_norm()
Find and store local minima of the gradient norm in the provided field.
This method identifies the elements where the calculated gradient norm is smaller than the minimum gradient norm of their direct neighbors in the field. It then stores these local minima for further processing or usage within the object.
Notes
The method assumes the object contains a gradient_on_fiedler
attribute
associated with the relevant gradient values for analysis and an iterable
structure to define neighborhood relationships. It updates the min_local
attribute upon execution, which stores the identified local minima indices.
Source code in spectral_clustering/point_cloud_graph.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
|
create_normalized_vector_field Link
create_normalized_vector_field(list_of_max_nodes, list_of_min_nodes, pcd_coordinates)
Creates a normalized vector field based on input node indices and point cloud coordinates.
This function computes vectors between corresponding nodes specified in the list of maximum and minimum nodes using their respective coordinates in the point cloud. The computed vectors are then normalized to unit length using L2 norm.
Parameters:
-
list_of_max_nodes
(list of int
) –A list containing the indices of the maximum nodes in the point cloud.
-
list_of_min_nodes
(list of int
) –A list containing the indices of the minimum nodes in the point cloud.
-
pcd_coordinates
(ndarray
) –A two-dimensional array representing the coordinates of the point cloud. Each row corresponds to a point in the point cloud, and its dimensions are determined by the spatial representation of the data (e.g., 3 for 3D Cartesian coordinates).
Returns:
-
ndarray
–A two-dimensional array containing the normalized vectors. Each row corresponds to the normalized vector computed for the respective node pairs from the input lists and the point cloud coordinates. The shape is the same as the number of node pairs, with the dimensionality matching the input point cloud coordinates.
Source code in spectral_clustering/point_cloud_graph.py
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
|
graph_laplacian Link
graph_laplacian(G, laplacian_type='classical')
Compute the graph Laplacian matrix of a given graph.
This function computes the graph Laplacian matrix of the input graph
G
based on the specified type. The graph Laplacian is widely used in
various graph-based algorithms, such as spectral clustering and network
analysis. Depending on the value of laplacian_type
, the function
returns either the classical graph Laplacian matrix or the simple
normalized Laplacian matrix. The computed Laplacian is returned as a
sparse matrix.
Parameters:
-
G
(Graph
) –The input graph for which the Laplacian matrix is computed. It can be any NetworkX-compatible graph object.
-
laplacian_type
(str
, default:'classical'
) –A string specifying the type of graph Laplacian to compute. Accepts either 'classical' or 'simple_normalized'. If 'classical' is passed, the classical unnormalized Laplacian matrix is computed. If 'simple_normalized' is passed, the simple normalized Laplacian matrix is returned.
Returns:
-
csr_matrix
–The computed Laplacian matrix in Compressed Sparse Row (CSR) format.
Source code in spectral_clustering/point_cloud_graph.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|
graph_spectrum Link
graph_spectrum(G, sparse=True, k=50, smallest_first=True, laplacian_type='classical', laplacian=None)
Compute the spectrum of a graph using its Laplacian matrix.
The function computes eigenvalues and eigenvectors of the graph's Laplacian matrix. It supports both dense and sparse computation methods, and allows control over whether the smallest or largest eigenvalues are computed. The type of Laplacian used can also be specified.
Parameters:
-
G
(Graph
) –The input graph for which the spectrum is computed.
-
sparse
(bool
, default:True
) –If True, uses sparse matrix computation for efficiency. Defaults to True.
-
k
(int
, default:50
) –The number of eigenvalues and eigenvectors to compute. Defaults to 50.
-
smallest_first
(bool
, default:True
) –If True, computes the smallest eigenvalues first. Defaults to True.
-
laplacian_type
(str
, default:'classical'
) –The type of Laplacian matrix to use. Possible values include 'classical'. Defaults to 'classical'.
-
laplacian
(ndarray
, default:None
) –Precomputed Laplacian matrix to use instead of computing it internally from the graph
G
. If None, the Laplacian will be computed based onG
.
Returns:
-
ndarray
–Eigenvalues of the Laplacian matrix, as a 1D array.
-
ndarray
–Eigenvectors of the Laplacian matrix, as a 2D array where each column is an eigenvector.
Source code in spectral_clustering/point_cloud_graph.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
|
simple_graph_to_test_methods Link
simple_graph_to_test_methods()
Generate a test graph and perform spectral graph analysis
This function creates a simple undirected weighted graph, computes its Laplacian matrix, eigenvalues, and eigenvectors. It also analyzes node-level relationships based on eigenvector components, identifying nodes associated with the maximum and minimum values of specific eigenvector components among their neighbors. Additionally, it retrieves the corresponding weights of these connections.
Source code in spectral_clustering/point_cloud_graph.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
|