Analysis Module¶
The analysis module provides low-level utilities for structure graph construction and compositional sequence computation.
StructureGraph¶
Bases: StructureGraph
Extended StructureGraph with methods for Graph ID computation.
This class extends pymatgen's StructureGraph with additional functionality for computing compositional sequences and handling loops/rings in the structure graph.
Attributes:
| Name | Type | Description |
|---|---|---|
starting_labels |
list of str
|
Labels for each site used as starting points for compositional sequences. |
cc_cs |
list of dict
|
Compositional sequences for each connected component.
Each dict contains |
See Also
pymatgen.analysis.graphs.StructureGraph : Base class
Source code in graph_id/analysis/graphs.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 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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 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 560 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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
Methods:¶
from_local_env_strategy(structure, strategy, weights=False)
staticmethod
¶
Create a StructureGraph using a neighbor-finding strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
A pymatgen Structure object. |
required |
strategy
|
NearNeighbors
|
A neighbor-finding strategy from pymatgen.analysis.local_env, such as MinimumDistanceNN, CrystalNN, etc. |
required |
weights
|
bool
|
If True, include bond weights from the strategy. |
False
|
Returns:
| Type | Description |
|---|---|
StructureGraph
|
A new StructureGraph with edges representing bonds. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the strategy does not support structures. |
Examples:
>>> from pymatgen.analysis.local_env import MinimumDistanceNN
>>> sg = StructureGraph.from_local_env_strategy(
... structure, MinimumDistanceNN()
... )
Source code in graph_id/analysis/graphs.py
with_indivisual_state_comp_strategy(structure, strategy, _sg, n, weights=False, rank_k=1, cutoff=6.0)
staticmethod
¶
Add edges for a specific site using a distance clustering strategy.
This method is used by DistanceClusteringGraphID to add bonds for a specific site and distance cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
A pymatgen Structure object. |
required |
strategy
|
DistanceClusteringNN
|
A distance clustering neighbor-finding strategy. |
required |
_sg
|
StructureGraph
|
An existing StructureGraph to modify. |
required |
n
|
int
|
The site index to add edges for. |
required |
weights
|
bool
|
If True, include bond weights from the strategy. |
False
|
rank_k
|
int
|
The distance cluster index (0-based). |
1
|
cutoff
|
float
|
Maximum distance cutoff in Angstroms. |
6.0
|
Returns:
| Type | Description |
|---|---|
StructureGraph
|
The modified StructureGraph with new edges. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the strategy does not support structures. |
Source code in graph_id/analysis/graphs.py
set_elemental_labels()
¶
Set element symbols as starting labels for compositional sequences.
This is the default labeling scheme where each site is labeled by its element symbol (e.g., "Na", "Cl").
Source code in graph_id/analysis/graphs.py
set_wyckoffs(symmetry_tol: float = 0.01) -> None
¶
Set Wyckoff position labels for each site.
Labels each site with its element, Wyckoff letter, and space group
number in the format "{element}_{wyckoff}_{spacegroup}".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symmetry_tol
|
float
|
Tolerance for symmetry detection in Angstroms. |
0.01
|
Notes
If symmetry detection fails, falls back to elemental labels.
Source code in graph_id/analysis/graphs.py
set_compositional_sequence_node_attr(hash_cs: bool = False, wyckoff: bool = False, additional_depth: int = 0, diameter_factor: int = 2, use_previous_cs: bool = False) -> None
¶
Compute and set compositional sequences as node attributes.
This is the core method that computes the local environment fingerprint for each site by traversing the graph and counting neighbors at each depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_cs
|
bool
|
If True, hash the compositional sequence incrementally during computation for memory efficiency. |
False
|
wyckoff
|
bool
|
If True, use Wyckoff labels in the computation. |
False
|
additional_depth
|
int
|
Extra traversal depth to add. |
0
|
diameter_factor
|
int
|
Multiplier for graph diameter to determine traversal depth. |
2
|
use_previous_cs
|
bool
|
If True, use previous compositional sequence as starting labels. |
False
|
Notes
After calling this method:
- Node attributes are set with key
"compositional_sequence" self.cc_cscontains compositional sequences per component
Source code in graph_id/analysis/graphs.py
get_loops(depth: int, index: int, shortest: bool = True)
¶
Find all loops/rings starting from a given atom.
Traverses the graph to find closed loops that start and end at the specified atom index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
int
|
Maximum loop size to search for. |
required |
index
|
int
|
The starting atom index. |
required |
shortest
|
bool
|
If True, stop searching when all theoretically possible shortest loops are found. |
True
|
Returns:
| Type | Description |
|---|---|
list of list of tuple
|
A list of loops, where each loop is a list of |
Notes
Loops are found by breadth-first traversal and tracking when paths return to their starting point.
Source code in graph_id/analysis/graphs.py
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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
set_loops(diameter_factor: int, additional_depth: int) -> None
¶
Set loop-based labels for each site.
Computes all loops for each site and creates a hashed label representing the ring topology around that site.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diameter_factor
|
int
|
Multiplier for graph diameter to determine search depth. |
required |
additional_depth
|
int
|
Extra depth to add to the search. |
required |
Notes
Sets self.starting_labels to hashed loop representations.
Used when loop=True in GraphIDGenerator.
Source code in graph_id/analysis/graphs.py
Extended version of pymatgen's StructureGraph with additional methods for Graph ID generation.
Import¶
Class Methods¶
from_local_env_strategy¶
Constructor for StructureGraph using a neighbor detection strategy.
Parameters:
structure(Structure): pymatgen Structure objectstrategy(NearNeighbors): A neighbor detection strategyweights(bool): If True, use weights from the strategy
Returns:
StructureGraph: Constructed structure graph
Example:
from graph_id.analysis.graphs import StructureGraph
from pymatgen.analysis.local_env import MinimumDistanceNN
sg = StructureGraph.from_local_env_strategy(structure, MinimumDistanceNN())
Instance Methods¶
set_elemental_labels¶
Set elemental species strings as starting labels for compositional sequence computation.
set_wyckoffs¶
Set Wyckoff position labels for each site.
Parameters:
symmetry_tol(float): Tolerance for symmetry detection
set_compositional_sequence_node_attr¶
def set_compositional_sequence_node_attr(
self,
hash_cs: bool = False,
wyckoff: bool = False,
additional_depth: int = 0,
diameter_factor: int = 2,
use_previous_cs: bool = False
)
Compute and set compositional sequences as node attributes.
Parameters:
hash_cs(bool): Hash the compositional sequence during computationwyckoff(bool): Use Wyckoff-labeled sequencesadditional_depth(int): Extra traversal depthdiameter_factor(int): Multiplier for graph diameteruse_previous_cs(bool): Use previous CS as starting point
get_loops¶
Compute loops/rings starting from a given atom.
Parameters:
depth(int): Maximum loop size to searchindex(int): Starting atom indexshortest(bool): Stop when all theoretical shortest loops are found
Returns:
list: List of loops, each as a list of (index, image) tuples
CompositionalSequence¶
Compute the compositional sequence for a site in a structure graph.
A compositional sequence is a fingerprint of the local chemical environment around an atom, computed by traversing the graph in shells and counting the elements encountered at each depth.
For example, for Na in NaCl rock salt structure:
- Depth 0: Na (the central atom)
- Depth 1: Cl6 (6 nearest Cl neighbors)
- Depth 2: Na12 (12 next-nearest Na neighbors)
The sequence "Na-Cl6-Na12-..." uniquely identifies the local environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
focused_site_i
|
int
|
The index of the central atom. |
required |
starting_labels
|
list of str
|
Labels for each site in the structure. |
required |
hash_cs
|
bool
|
If True, hash the sequence incrementally to save memory. |
False
|
use_previous_cs
|
bool
|
If True, use previous compositional sequences as labels (for iterative refinement). |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
focused_site_i |
int
|
The central site index. |
first_element |
str
|
The label of the central site. |
compositional_seq |
list of str
|
The composition at each depth (if hash_cs=False). |
cs_for_hashing |
str
|
The incrementally hashed sequence (if hash_cs=True). |
Examples:
>>> cs = CompositionalSequence(0, ["Na", "Cl", "Na", "Cl"])
>>> # ... add neighbors at each depth ...
>>> print(str(cs))
'Na-Cl6-Na12...'
Source code in graph_id/analysis/compositional_sequence.py
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 | |
Methods:¶
__init__(focused_site_i, starting_labels, hash_cs=False, use_previous_cs=False)
¶
Initialize the compositional sequence computation.
Source code in graph_id/analysis/compositional_sequence.py
count_composition_for_neighbors(nsites: list[Neighbor]) -> None
¶
Count the composition of neighboring sites.
Adds new neighbors to the frontier and counts their labels for the current depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nsites
|
list of Neighbor
|
The neighboring sites to count. |
required |
Source code in graph_id/analysis/compositional_sequence.py
finalize_this_depth()
¶
Finalize counting for the current depth.
Converts the composition counter to a formula string and either appends it to the sequence or hashes it incrementally. Resets the counter for the next depth.
Source code in graph_id/analysis/compositional_sequence.py
get_current_starting_sites()
¶
Get the sites to expand from for the next depth.
Returns:
| Type | Description |
|---|---|
list of tuple
|
List of |
Source code in graph_id/analysis/compositional_sequence.py
Class for computing compositional sequences around an atom.
Import¶
Constructor¶
Parameters:
focused_site_i(int): Index of the central atomstarting_labels(list[str]): Labels for each sitehash_cs(bool): Hash sequences incrementallyuse_previous_cs(bool): Use previous sequence as labels
Methods¶
count_composition_for_neighbors¶
Count the composition of neighboring sites.
finalize_this_depth¶
Finalize counting for the current depth level.
String Representation¶
The string representation gives the full compositional sequence:
cs = CompositionalSequence(0, labels)
# ... compute neighbors ...
print(str(cs)) # "Na-Cl6-Na12-..."
DistanceClusteringNN¶
Bases: NearNeighbors
Neighbor detection using DBSCAN clustering on interatomic distances.
This class identifies neighbors by clustering the distribution of interatomic distances using the DBSCAN algorithm. This allows for automatic detection of distinct bond length populations, which is useful for structures with multiple bond types or unusual bonding.
The algorithm:
- Computes all pairwise distances within a cutoff
- Applies DBSCAN clustering (eps=0.5, min_samples=2)
- Each cluster represents a distinct bond length population
- Neighbors are assigned to clusters by their distance
Examples:
>>> from graph_id.analysis.local_env import DistanceClusteringNN
>>> nn = DistanceClusteringNN()
>>> neighbors = nn.get_nn_info(structure, site_index=0, rank_k=0)
See Also
DistanceClusteringGraphID : Graph ID generator using this class
Source code in graph_id/analysis/local_env.py
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 | |
Attributes¶
structures_allowed: bool
property
¶
Check if this neighbor finder can be used with Structure objects.
Returns:
| Type | Description |
|---|---|
bool
|
Always True for this class. |
Methods:¶
__init__() -> None
¶
get_nn_info(structure: Structure, n: int, rank_k: int, cutoff: float = 6.0) -> list[dict[str, Any]]
¶
Get neighbor information for a specific site and distance cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
The input pymatgen Structure. |
required |
n
|
int
|
Index of the site to find neighbors for. |
required |
rank_k
|
int
|
The distance cluster index (0-based). Cluster 0 contains the shortest bonds, cluster 1 the next shortest, etc. |
required |
cutoff
|
float
|
Maximum distance cutoff in Angstroms. |
6.0
|
Returns:
| Type | Description |
|---|---|
list of dict
|
List of neighbor information dictionaries, each containing:
|
Source code in graph_id/analysis/local_env.py
get_cutoff_cluster(structure: Structure, n: int, cutoff: float = 6.0) -> list
¶
Get distance cluster cutoffs using DBSCAN clustering.
Computes all interatomic distances from site n within the cutoff, then clusters them using DBSCAN. Returns the maximum distance in each cluster as cutoff thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
The input pymatgen Structure. |
required |
n
|
int
|
Index of the central site. |
required |
cutoff
|
float
|
Maximum distance to consider in Angstroms. |
6.0
|
Returns:
| Type | Description |
|---|---|
list of float
|
Sorted list of maximum distances for each cluster. The i-th element is the cutoff for cluster i. |
Notes
Uses DBSCAN with eps=0.5 and min_samples=2 to cluster distances. Distances that don't fit into any cluster are ignored.
Source code in graph_id/analysis/local_env.py
Neighbor detection based on DBSCAN clustering of interatomic distances.
Import¶
Constructor¶
Methods¶
get_nn_info¶
def get_nn_info(
self,
structure: Structure,
n: int,
rank_k: int,
cutoff: float = 6.0
) -> list[dict]
Get neighbor information for a specific site and distance cluster.
Parameters:
structure(Structure): Input structuren(int): Site indexrank_k(int): Cluster index (0-based)cutoff(float): Maximum distance cutoff
Returns:
list[dict]: List of neighbor information dictionaries
get_cutoff_cluster¶
Get distance cutoffs for each cluster using DBSCAN.
Parameters:
structure(Structure): Input structuren(int): Site indexcutoff(float): Maximum distance to consider
Returns:
list: Sorted list of maximum distances for each cluster
How DBSCAN Clustering Works¶
The algorithm:
- Computes all pairwise distances within the cutoff
- Runs DBSCAN with
eps=0.5andmin_samples=2 - Groups distances into clusters
- Returns cutoffs as the maximum distance in each cluster
This is useful for structures with distinct bond length populations.