-
Notifications
You must be signed in to change notification settings - Fork 16
/
coordination_hist.py
178 lines (145 loc) · 6.16 KB
/
coordination_hist.py
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
# %%
from glob import glob
from matminer.datasets import load_dataset
from pymatgen.analysis.local_env import CrystalNN, VoronoiNN
from pymatgen.core import Structure
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
import pymatviz as pmv
from pymatviz.coordination import CnSplitMode
from pymatviz.utils.testing import TEST_FILES
pmv.set_plotly_template("pymatviz_white")
df_phonon = load_dataset(data_name := "matbench_phonons")
# %%
formula_spg_str = (
lambda struct: f"{struct.formula} ({struct.get_space_group_info()[1]})"
)
structures = {
formula_spg_str(struct := Structure.from_file(file)): struct
for file in (
glob(f"{TEST_FILES}/structures/*.json.gz") + glob(f"{TEST_FILES}/xrd/*.cif")
)
}
key1, key2, key3, *_ = structures
for struct in structures.values():
spga = SpacegroupAnalyzer(struct)
sym_struct = spga.get_symmetrized_structure()
# add wyckoff symbols to each site
struct.add_oxidation_state_by_guess()
wyckoff_symbols = ["n/a"] * len(struct)
for indices, symbol in zip(
sym_struct.equivalent_indices, sym_struct.wyckoff_symbols, strict=True
):
for index in indices:
wyckoff_symbols[index] = symbol
if any(sym == "n/a" for sym in wyckoff_symbols):
raise ValueError(f"{struct.formula} has n/a {wyckoff_symbols=}")
struct.add_site_property("wyckoff", wyckoff_symbols)
# %% Single structure example
fig = pmv.coordination_hist(structures[key1], strategy=4.0)
fig.layout.title = dict(text=f"Coordination Histogram: {key1}", x=0.5, y=0.98)
fig.layout.margin.t = 50
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-single")
# %% Example with CrystalNN
for strategy in (CrystalNN(), VoronoiNN()):
cls_name = type(strategy).__name__
fig = pmv.coordination_hist(structures[key1], strategy=strategy)
title = f"Coordination Histogram ({cls_name}): {key1}"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.layout.margin.t = 50
fig.show()
pmv.io.save_and_compress_svg(fig, f"coordination-hist-{cls_name.lower()}")
# %% Custom analyzer example
fig = pmv.coordination_hist(structures[key1], strategy=VoronoiNN())
fig.layout.margin.t = 50
title = f"Coordination Histogram (VoronoiNN): {key1}"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-voronoi")
# %% Multiple structures example
fig = pmv.coordination_hist({key1: structures[key1], key2: structures[key2]})
fig.layout.margin.t = 50
title = "Coordination Histogram: Multiple Structures"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-multiple")
# %% By element example (now default, but explicitly specified for clarity)
fig = pmv.coordination_hist(structures[key1], split_mode=CnSplitMode.by_element)
fig.layout.margin.t = 50
title = f"Coordination Histogram by Element: {key1}"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-by-element")
# %% Multiple structures with by_element split
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
)
fig.layout.margin.t = 50
title = "Coordination Histogram by Element: Multiple Structures"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-multiple-by-element")
# %% Multiple structures with by_structure split
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
split_mode=CnSplitMode.by_structure,
)
fig.layout.margin.t = 50
title = "Coordination Histogram by Structure"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-by-structure")
# %% Multiple structures with by_structure_and_element split
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
split_mode=CnSplitMode.by_structure_and_element,
)
fig.layout.margin.t = 60
title = "Coordination Histogram by Structure and Element"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-by-structure-and-element")
# %% Multiple structures with by_element split and custom hover data
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
hover_data=("oxidation_state", "wyckoff"),
)
fig.layout.margin.t = 50
title = "Coordination Histogram by Element: Multiple Structures (with custom hover)"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-multiple-by-element-custom-hover")
# %% Multiple structures with by_element split and custom element color scheme
custom_colors = {"Fe": "red", "O": "blue", "H": "green"}
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
hover_data=("oxidation_state", "wyckoff"),
element_color_scheme=custom_colors,
)
fig.layout.margin.t = 60
title = "Coordination Histogram by Element: Multiple Structures (with custom colors)"
fig.layout.title = dict(text=title, x=0.5, y=0.99)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-multiple-by-element-custom-colors")
# %% Example with split_mode=CnSplitMode.none
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
split_mode=CnSplitMode.none,
)
fig.layout.margin.t = 50
title = "Coordination Histogram: All Structures Combined"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-all-combined")
# %% Example with split_mode=CnSplitMode.none and side-by-side bars
fig = pmv.coordination_hist(
{key1: structures[key1], key2: structures[key2], key3: structures[key3]},
split_mode=CnSplitMode.none,
bar_mode="group",
bar_kwargs=dict(width=0.2),
)
fig.layout.margin.t = 50
title = "Coordination Histogram: All Structures Combined (Side-by-side)"
fig.layout.title = dict(text=title, x=0.5, y=0.98)
fig.show()
pmv.io.save_and_compress_svg(fig, "coordination-hist-all-combined-side-by-side")