-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathchem_sys_sunburst.py
More file actions
129 lines (110 loc) · 3.98 KB
/
chem_sys_sunburst.py
File metadata and controls
129 lines (110 loc) · 3.98 KB
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
"""Plot sunburst distribution of chemical systems."""
# %%
from __future__ import annotations
import pandas as pd
import plotly.express as px
import pymatviz as pmv
from pymatviz.enums import Key
from pymatviz.utils import ROOT
# %% Basic examples: Different grouping modes for the same formulas
formulas = [
"Fe2O3", # binary
"Fe4O6", # same as Fe2O3 when group_by="reduced_formula"
"FeO", # different formula but same system when group_by="chem_sys"
"Li2O", # binary
"LiFeO2", # ternary
]
# Count each formula separately
fig = pmv.chem_sys_sunburst(
formulas,
group_by="formula",
title="Group by formula (count each formula separately)",
)
fig.show()
# Group by reduced formulas (Fe2O3 and Fe4O6 count as same)
fig = pmv.chem_sys_sunburst(
formulas,
group_by="reduced_formula",
title="Group by reduced formula (same stoichiometry)",
)
fig.show()
# Group by chemical systems (default, all Fe-O formulas count as same)
fig = pmv.chem_sys_sunburst(
formulas,
group_by="chem_sys",
title="Group by chemical system (same elements)",
)
fig.show()
# %% Example 2: Complex formulas with fractional occupancies
complex_formulas = [
"Pb(Zr0.52Ti0.48)O3", # PZT with fractional occupancy
"La0.7Sr0.3MnO3", # LSMO with fractional composition
"Li0.5Na0.5O", # mixed alkali
"LiNaO2", # same system, different notation
]
fig = pmv.chem_sys_sunburst(
complex_formulas,
show_counts="value+percent",
title="Complex formulas with fractional occupancies",
)
fig.show()
# %% Load the Ward metallic glasses https://pubs.acs.org/doi/10.1021/acs.chemmater.6b04153
data_path = "ward_metallic_glasses/ward-metallic-glasses.csv.xz"
df_mg = pd.read_csv(f"{ROOT}/examples/{data_path}", na_values=()).query(
"comment.isna()"
)
fig = pmv.chem_sys_sunburst(
df_mg[Key.composition],
group_by="chem_sys",
show_counts="value+percent",
title="Ward Metallic Glasses - Grouped by chemical system",
color_discrete_sequence=px.colors.qualitative.Set2,
)
title = "Ward Metallic Glasses - Grouped by chemical system"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.5, y=0.96, font_size=18)
fig.layout.update(height=500)
fig.show()
# %% Create a plot focusing on glass-forming ability (GFA)
for key, df_group in df_mg.groupby("gfa_type"):
fig = pmv.chem_sys_sunburst(
df_group[Key.composition],
show_counts="value+percent",
title=f"Ward Metallic Glasses - {key} Compositions",
color_discrete_sequence=px.colors.qualitative.Set2,
)
title = f"Ward Metallic Glasses - {key} Compositions"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.5, y=0.96, font_size=18)
fig.layout.update(height=500)
fig.show()
# pmv.io.save_and_compress_svg(fig, "chem-sys-sunburst-ward-bmg")
break
# %% Show only top 3 systems per arity with "Other" slices
max_slices = 15
fig = pmv.chem_sys_sunburst(
df_mg[Key.composition],
group_by="chem_sys",
show_counts="value+percent",
max_slices=max_slices,
max_slices_mode="other", # combine remaining systems into "Other" slices
color_discrete_sequence=px.colors.qualitative.Set2,
)
title = f"Ward Metallic Glasses - Top {max_slices} Systems per Arity (with Other)"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.5, y=0.96, font_size=18)
fig.layout.update(height=500)
fig.show()
# pmv.io.save_and_compress_svg(fig, "chem-sys-sunburst-ward-bmg-top3-other")
# %% Show only top 3 systems per arity, dropping the rest
max_slices = 12
fig = pmv.chem_sys_sunburst(
df_mg[Key.composition],
group_by="chem_sys",
show_counts="value+percent",
max_slices=max_slices,
max_slices_mode="drop", # discard remaining systems
color_discrete_sequence=px.colors.qualitative.Set2,
)
title = f"Ward Metallic Glasses<br>Top {max_slices} Systems per Arity (dropped)"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.5, y=0.9, font_size=18)
fig.layout.update(height=500)
fig.show()
# pmv.io.save_and_compress_svg(fig, "chem-sys-sunburst-ward-bmg-top3-drop")