-
Notifications
You must be signed in to change notification settings - Fork 25
/
periodic_trends.py
275 lines (249 loc) · 8.13 KB
/
periodic_trends.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
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
from bokeh.models import (
ColumnDataSource,
LinearColorMapper,
LogColorMapper,
ColorBar,
BasicTicker,
)
from bokeh.plotting import figure, output_file
from bokeh.io import show as show_
from bokeh.sampledata.periodic_table import elements
from bokeh.transform import dodge
from csv import reader
from matplotlib.colors import Normalize, LogNorm, to_hex
from matplotlib.cm import (
plasma,
inferno,
magma,
viridis,
cividis,
turbo,
ScalarMappable,
)
from pandas import options
from typing import List
import warnings
def plotter(
filename: str,
show: bool = True,
output_filename: str = None,
width: int = 1050,
cmap: str = "plasma",
alpha: float = 0.65,
extended: bool = True,
periods_remove: List[int] = None,
groups_remove: List[int] = None,
log_scale: bool = False,
cbar_height: float = None,
cbar_standoff: int = 12,
cbar_fontsize: int = 14,
blank_color: str = "#c4c4c4",
under_value: float = None,
under_color: str = "#140F0E",
over_value: float = None,
over_color: str = "#140F0E",
special_elements: List[str] = None,
special_color: str = "#6F3023",
) -> figure:
"""
Plot a heatmap over the periodic table of elements.
Parameters
----------
filename : str
Path to the .csv file containing the data to be plotted.
show : str
If True, the plot will be shown.
output_filename : str
If not None, the plot will be saved to the specified (.html) file.
width : float
Width of the plot.
cmap : str
plasma, inferno, viridis, magma, cividis, turbo
alpha : float
Alpha value (transparency).
extended : bool
If True, the lanthanoids and actinoids will be shown.
periods_remove : List[int]
Period numbers to be removed from the plot.
groups_remove : List[int]
Group numbers to be removed from the plot.
log_scale : bool
If True, the colorbar will be logarithmic.
cbar_height : int
Height of the colorbar.
cbar_standoff : int
Distance between the colorbar and the plot.
cbar_fontsize : int
Fontsize of the colorbar label.
blank_color : str
Hexadecimal color of the elements without data.
under_value : float
Values <= under_value will be colored with under_color.
under_color : str
Hexadecimal color to be used for the lower bound color.
over_value : float
Values >= over_value will be colored with over_color.
under_color : str
Hexadecial color to be used for the upper bound color.
special_elements: List[str]
List of elements to be colored with special_color.
special_color: str
Hexadecimal color to be used for the special elements.
Returns
-------
figure
Bokeh figure object.
"""
options.mode.chained_assignment = None
# Assign color palette based on input argument
if cmap == "plasma":
cmap = plasma
bokeh_palette = "Plasma256"
elif cmap == "inferno":
cmap = inferno
bokeh_palette = "Inferno256"
elif cmap == "magma":
cmap = magma
bokeh_palette = "Magma256"
elif cmap == "viridis":
cmap = viridis
bokeh_palette = "Viridis256"
elif cmap == "cividis":
cmap = cividis
bokeh_palette = "Cividis256"
elif cmap == "turbo":
cmap = turbo
bokeh_palette = "Turbo256"
else:
ValueError("Invalid color map.")
# Define number of and groups
period_label = ["1", "2", "3", "4", "5", "6", "7"]
group_range = [str(x) for x in range(1, 19)]
# Remove any groups or periods
if groups_remove:
for gr in groups_remove:
gr = gr.strip()
group_range.remove(str(gr))
if periods_remove:
for pr in periods_remove:
pr = pr.strip()
period_label.remove(str(pr))
# Read in data from CSV file
data_elements = []
data_list = []
for row in reader(open(filename)):
data_elements.append(row[0])
data_list.append(row[1])
data = [float(i) for i in data_list]
if len(data) != len(data_elements):
raise ValueError("Unequal number of atomic elements and data points")
period_label.append("blank")
period_label.append("La")
period_label.append("Ac")
if extended:
count = 0
for i in range(56, 70):
elements.period[i] = "La"
elements.group[i] = str(count + 4)
count += 1
count = 0
for i in range(88, 102):
elements.period[i] = "Ac"
elements.group[i] = str(count + 4)
count += 1
# Define matplotlib and bokeh color map
if log_scale:
for datum in data:
if datum < 0:
raise ValueError(
f"Entry for element {datum} is negative but log-scale is selected"
)
color_mapper = LogColorMapper(
palette=bokeh_palette, low=min(data), high=max(data)
)
norm = LogNorm(vmin=min(data), vmax=max(data))
else:
color_mapper = LinearColorMapper(
palette=bokeh_palette, low=min(data), high=max(data)
)
norm = Normalize(vmin=min(data), vmax=max(data))
color_scale = ScalarMappable(norm=norm, cmap=cmap).to_rgba(data, alpha=None)
# Set blank color
color_list = [blank_color] * len(elements)
# Compare elements in dataset with elements in periodic table
for i, data_element in enumerate(data_elements):
element_entry = elements.symbol[
elements.symbol.str.lower() == data_element.lower()
]
if element_entry.empty == False:
element_index = element_entry.index[0]
else:
warnings.warn("Invalid chemical symbol: " + data_element)
if color_list[element_index] != blank_color:
warnings.warn("Multiple entries for element " + data_element)
elif under_value is not None and data[i] <= under_value:
color_list[element_index] = under_color
elif over_value is not None and data[i] >= over_value:
color_list[element_index] = over_color
else:
color_list[element_index] = to_hex(color_scale[i])
if special_elements:
for k, v in elements["symbol"].iteritems():
if v in special_elements:
color_list[k] = special_color
# Define figure properties for visualizing data
source = ColumnDataSource(
data=dict(
group=[str(x) for x in elements["group"]],
period=[str(y) for y in elements["period"]],
sym=elements["symbol"],
atomic_number=elements["atomic number"],
type_color=color_list,
)
)
# Plot the periodic table
p = figure(x_range=group_range, y_range=list(reversed(period_label)), tools="save")
p.width = width
p.outline_line_color = None
p.background_fill_color = None
p.border_fill_color = None
p.toolbar_location = "above"
p.rect("group", "period", 0.9, 0.9, source=source, alpha=alpha, color="type_color")
p.axis.visible = False
text_props = {
"source": source,
"angle": 0,
"color": "black",
"text_align": "left",
"text_baseline": "middle",
}
x = dodge("group", -0.4, range=p.x_range)
y = dodge("period", 0.3, range=p.y_range)
p.text(
x=x,
y="period",
text="sym",
text_font_style="bold",
text_font_size="16pt",
**text_props,
)
p.text(x=x, y=y, text="atomic_number", text_font_size="11pt", **text_props)
color_bar = ColorBar(
color_mapper=color_mapper,
ticker=BasicTicker(desired_num_ticks=10),
border_line_color=None,
label_standoff=cbar_standoff,
location=(0, 0),
orientation="vertical",
scale_alpha=alpha,
major_label_text_font_size=f"{cbar_fontsize}pt",
)
if cbar_height is not None:
color_bar.height = cbar_height
p.add_layout(color_bar, "right")
p.grid.grid_line_color = None
if output_filename:
output_file(output_filename)
if show:
show_(p)
return p