defectpl.participation_ratio¶
High-level calculator¶
ParticipationRatioCalculator
¶
ParticipationRatioCalculator(
procar,
defect_entry,
defect_structure_info=None,
poscar=None,
cutoff_radius=3.5,
use_pymatgen=True,
)
High-level calculator for electronic-state P-ratio and IPR.
Orchestrates file reading, neighbour resolution, the core computation, and serialisation to JSON / CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procar
|
str or Path
|
Path to VASP PROCAR file. |
required |
defect_entry
|
str or Path
|
Path to |
required |
defect_structure_info
|
str or Path or None
|
Path to |
None
|
poscar
|
str or Path or None
|
Path to POSCAR/CONTCAR used as fallback for neighbour search. |
None
|
cutoff_radius
|
float
|
Cutoff radius (Å) for the distance-based fallback neighbour search. Default: 3.5 Å. |
3.5
|
use_pymatgen
|
bool
|
Use pymatgen's Procar parser when available. Default: |
True
|
Examples:
>>> calc = ParticipationRatioCalculator(
... procar="PROCAR",
... defect_entry="defect_entry.json",
... defect_structure_info="defect_structure_info.json",
... )
>>> result = calc.run()
>>> calc.to_json("participation_ratio.json")
>>> calc.to_csv("participation_ratio.csv")
Source code in defectpl\participation_ratio.py
run
¶
Execute the full P-ratio / IPR calculation pipeline.
Returns:
| Type | Description |
|---|---|
dict
|
Nested result dictionary (spin → k-point → band → metrics).
The same object is also stored as |
Source code in defectpl\participation_ratio.py
to_json
¶
Serialise the result to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
|
required |
Returns:
| Type | Description |
|---|---|
Path — the written file path.
|
|
Source code in defectpl\participation_ratio.py
to_csv
¶
Write a flat CSV summary of all (spin, k-point, band) metrics.
Columns: spin, kpt, band, energy, occ, p_ratio, ipr, p_neighbors, p_total
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
|
required |
Returns:
| Type | Description |
|---|---|
Path
|
|
Source code in defectpl\participation_ratio.py
top_localized
¶
Return the n most localised states sorted by metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int, optional (default 10)
|
|
10
|
metric
|
('p_ratio', 'ipr')
|
|
"p_ratio"
|
Returns:
| Type | Description |
|---|---|
list of dict, each with keys:
|
spin, kpt, band, energy, occ, p_ratio, ipr |
Source code in defectpl\participation_ratio.py
Plotting¶
plot_pr_vs_energy
¶
plot_pr_vs_energy(
result,
metric="p_ratio",
threshold=0.2,
vbm=None,
cbm=None,
emin=None,
emax=None,
kpt_idx=0,
title=None,
out=None,
ax=None,
figsize=(8, 5),
dpi=150,
)
Scatter plot of P-ratio or IPR versus Kohn-Sham energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
dict
|
Nested PR result dict (from :func: |
required |
metric
|
('p_ratio', 'ipr')
|
Quantity plotted on the Y-axis. |
"p_ratio"
|
threshold
|
float
|
Horizontal dashed threshold reference line. Default 0.2. |
0.2
|
vbm
|
float
|
Valence Band Maximum energy (eV). Shown as a vertical orange line. |
None
|
cbm
|
float
|
Conduction Band Minimum energy (eV). Shown as a vertical green line. |
None
|
emin
|
float
|
Lower energy boundary for filtering. |
None
|
emax
|
float
|
Upper energy boundary for filtering. |
None
|
kpt_idx
|
int
|
0-based k-point index to use. Default 0. |
0
|
title
|
str
|
Plot title. Defaults to the defect name in result. |
None
|
out
|
str or Path
|
If given, save the figure to this path. |
None
|
ax
|
Axes
|
Inject into an existing Axes. When None a new figure is created. |
None
|
figsize
|
tuple
|
Figure size (width, height) in inches. |
(8, 5)
|
dpi
|
int
|
Resolution when saving. |
150
|
Returns:
| Type | Description |
|---|---|
Axes
|
|
Source code in defectpl\participation_ratio.py
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | |
plot_pr_vs_band_index
¶
plot_pr_vs_band_index(
result,
metric="p_ratio",
threshold=0.2,
emin=None,
emax=None,
kpt_idx=0,
title=None,
out=None,
ax=None,
figsize=(10, 5),
dpi=150,
)
Scatter plot of P-ratio or IPR versus band index.
Band indices match the VASP band numbering (1-based). Separate spin channels are drawn in different colours; filled markers denote occupied states, open markers denote empty states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
dict
|
Nested PR result dict. |
required |
metric
|
('p_ratio', 'ipr')
|
Quantity plotted on the Y-axis. |
"p_ratio"
|
threshold
|
float
|
Horizontal dashed threshold reference line. Default 0.2. |
0.2
|
emin
|
float
|
Restrict displayed bands to those with energy ≥ emin (eV). |
None
|
emax
|
float
|
Restrict displayed bands to those with energy ≤ emax (eV). |
None
|
kpt_idx
|
int
|
0-based k-point index to use. Default 0. |
0
|
title
|
str
|
Plot title. Defaults to the defect name in result. |
None
|
out
|
str or Path
|
If given, save the figure to this path. |
None
|
ax
|
Axes
|
Inject into an existing Axes. |
None
|
figsize
|
tuple
|
Figure size in inches. |
(10, 5)
|
dpi
|
int
|
Resolution when saving. |
150
|
Returns:
| Type | Description |
|---|---|
Axes
|
|
Source code in defectpl\participation_ratio.py
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | |
flatten_pr_result
¶
Flatten the nested PR result dictionary into a list of per-band row dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
dict
|
As returned by :func: |
required |
kpt_idx
|
int
|
0-based k-point index to extract. Default 0 (Gamma point). |
0
|
Returns:
| Type | Description |
|---|---|
list of dict
|
Each dict has keys: |
Source code in defectpl\participation_ratio.py
Low-level functions¶
read_procar
¶
Parse a VASP PROCAR file and return projection arrays.
Tries pymatgen's Procar class first (if available and
use_pymatgen=True); falls back to the built-in native parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procar_path
|
str or Path
|
Path to the PROCAR file. |
required |
use_pymatgen
|
bool
|
Attempt to use pymatgen's parser first. Set to |
True
|
Returns:
| Type | Description |
|---|---|
dict with keys:
|
|
``n_kpoints`` : int
|
|
``n_bands`` : int
|
|
``n_ions`` : int
|
|
``n_spins`` : int
|
|
``spins`` : list of spin sentinels
|
|
``site_proj`` : dict[spin, np.ndarray shape (nk, nb, ni)]
|
Site-projected weight summed over all orbital channels. |
``eigenvalues`` : dict[spin, np.ndarray shape (nk, nb)]
|
|
``occupancies`` : dict[spin, np.ndarray shape (nk, nb)]
|
|
``kpoints`` : np.ndarray shape (nk, 3)
|
|
``weights`` : np.ndarray shape (nk,)
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the PROCAR file does not exist. |
Source code in defectpl\participation_ratio.py
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 | |
compute_participation_ratios
¶
compute_participation_ratios(
procar_data,
neighbor_indices,
defect_name="unknown",
defect_center=(0.5, 0.5, 0.5),
)
Compute P-ratio and IPR for every (spin, k-point, band) triplet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procar_data
|
dict
|
As returned by :func: |
required |
neighbor_indices
|
sequence of int
|
0-based atom indices that constitute the defect neighbourhood. Used in the P-ratio numerator. |
required |
defect_name
|
str
|
|
'unknown'
|
defect_center
|
sequence of 3 floats
|
Fractional coordinates of the defect centre (stored in output only). |
(0.5, 0.5, 0.5)
|
Returns:
| Type | Description |
|---|---|
dict
|
Serialisable result dictionary (spin → k-point → band → metrics). |
Source code in defectpl\participation_ratio.py
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 | |
resolve_neighbors
¶
Resolve neighbour atom indices using the best available source.
Priority:
1. defect_structure_info.json (authoritative source)
2. Distance-based search on POSCAR/CONTCAR
3. Empty list (no neighbour info; P-ratio will be 0 for all states)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dsi_path
|
path or None
|
|
required |
poscar_path
|
path or None
|
|
required |
defect_center_frac
|
sequence of 3 floats
|
|
required |
cutoff_radius
|
float(Å)
|
|
3.5
|
Returns:
| Type | Description |
|---|---|
(indices, source_description)
|
|
Source code in defectpl\participation_ratio.py
neighbors_from_defect_structure_info
¶
Extract neighbour atom indices from a defect_structure_info.json file.
The function tries all key names used across known pydefect versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dsi_path
|
str or Path
|
|
required |
Returns:
| Type | Description |
|---|---|
list of int (0-based POSCAR order) or None if not found / parse error.
|
|