-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_script_for_app.py
More file actions
179 lines (164 loc) · 7.2 KB
/
make_script_for_app.py
File metadata and controls
179 lines (164 loc) · 7.2 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
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
import streamlit as st
import pickle
import io
import zipfile
from make_script.make_pyscript import (make_py_script,
BallAndStick,
Stick,SpaceFilling,
Animation,TEMP)
from default import sizes
from function import hex2rgba
ELEMENTS = list(sizes.keys())
def _get_cartoon_param():
if st.session_state["cartoon"]:
IOR = st.session_state["IOR"]
color = hex2rgba(st.session_state["cartoon_color"])
return {"apply":True,"IOR":IOR,"color":color}
else:
return {"apply":False,"IOR":0.8,"color":(0,0,0,1)}
def _get_color_param():
kwargs = {}
for symb in ELEMENTS:
if f"color_{symb}" in st.session_state.keys():
kwargs[symb] = hex2rgba(st.session_state[f"color_{symb}"])
return kwargs
def _get_sizes_param():
kwargs = {}
for symb in ELEMENTS:
if f"size_{symb}" in st.session_state.keys():
kwargs[symb] = st.session_state[f"size_{symb}"]
return kwargs
def _get_subdivision_surface_param():
if st.session_state["subdivision_surface"]:
level = st.session_state["level"]
render_levels = st.session_state["render_levels"]
return {"apply":True,"level":level,"render_levels":render_levels}
else:
return {"apply":False,"level":2,"render_levels":2}
def make_script(atoms):
style = st.session_state["style_radio"]
kwargs = {}
if style == "Ball and Stick":
style_obj = BallAndStick
kwargs["bicolor"] = False
kwargs["colors"] = _get_color_param()
kwargs["radius"] = st.session_state["radius"]
kwargs["scale"] = st.session_state["scale"]
kwargs["sizes"] = _get_sizes_param()
kwargs["bond_color"] = hex2rgba(st.session_state["bond_color"])
elif style == "Ball and Stick (bicolor)":
style_obj = BallAndStick
kwargs["bicolor"] = True
kwargs["colors"] = _get_color_param()
kwargs["radius"] = st.session_state["radius"]
kwargs["scale"] = st.session_state["scale"]
kwargs["sizes"] = _get_sizes_param()
elif style == "Space Filling":
style_obj = SpaceFilling
kwargs["bicolor"] = False
kwargs["colors"] = _get_color_param()
kwargs["scale"] = st.session_state["scale"]
kwargs["sizes"] = _get_sizes_param()
elif style == "Stick":
style_obj = Stick
kwargs["bicolor"] = False
kwargs["radius"] = st.session_state["radius"]
kwargs["bond_color"] = hex2rgba(st.session_state["bond_color"])
elif style == "Stick (bicolor)":
style_obj = Stick
kwargs["bicolor"] = True
kwargs["colors"] = _get_color_param()
kwargs["radius"] = st.session_state["radius"]
elif style == "Animation":
style_obj = Animation
kwargs["bicolor"] = False
kwargs["colors"] = _get_color_param()
kwargs["scale"] = st.session_state["scale"]
kwargs["sizes"] = _get_sizes_param()
kwargs["start"] = st.session_state["start"]
kwargs["step"] = st.session_state["step"]
kwargs["cartoon"] = _get_cartoon_param()
kwargs["subdivision_surface"] = _get_subdivision_surface_param()
pyscript = make_py_script("-",style_obj(atoms,**kwargs))
st.session_state["script"] = pyscript
def make_combine_script(partial_atoms_list,property_list):
style_list = []
for atoms,kwargs in zip(partial_atoms_list,property_list):
style = kwargs.pop("style")
if style == "Ball and Stick": # ,"Animation"]
style_cls = BallAndStick
kwargs["bicolor"] = False
kwargs["bond_color"] = hex2rgba(kwargs["bond_color"])
kwargs["colors"] = {symb:hex2rgba(color) for symb,color in kwargs["colors"].items()}
elif style == "Ball and Stick (bicolor)":
style_cls = BallAndStick
kwargs["bicolor"] = True
kwargs["colors"] = {symb:hex2rgba(color) for symb,color in kwargs["colors"].items()}
elif style == "Space Filling":
style_cls = SpaceFilling
kwargs["colors"] = {symb:hex2rgba(color) for symb,color in kwargs["colors"].items()}
elif style == "Stick":
style_cls = Stick
kwargs["bicolor"] = False
kwargs["bond_color"] = hex2rgba(kwargs["bond_color"])
elif style == "Stick (bicolor)":
style_cls = Stick
kwargs["bicolor"] = True
kwargs["colors"] = {symb:hex2rgba(color) for symb,color in kwargs["colors"].items()}
elif style == "Animation":
style_cls = Animation
traj = atoms
kwargs["colors"] = {symb:hex2rgba(color) for symb,color in kwargs["colors"].items()}
kwargs["cartoon"] = _get_cartoon_param()
kwargs["subdivision_surface"] = _get_subdivision_surface_param()
style_obj = style_cls(atoms,**kwargs)
style_list.append(style_obj)
if not style == "Animation":
pyscript = make_py_script("-",style_list)
st.session_state["script"] = pyscript
else:
st.session_state["script"] = None
data_list = []
for i,style in enumerate(style_list):
d_dict = style.todict()
if style.style == "animation":
filename = "positions.pkl"
d_dict["file"] = filename
data_list.append(d_dict)
data = {
"data_list":data_list,
}
pyscript = TEMP.render(data)
zip_stream = io.BytesIO()
with zipfile.ZipFile(zip_stream, 'w', compression=zipfile.ZIP_DEFLATED) as zf:
with zf.open("animation.py","w") as f:
f.write(pyscript.encode())
with zf.open("positions.pkl","w") as f:
for atoms in traj:
positions = atoms.get_positions()
f.write(pickle.dumps(positions))
return zip_stream.getvalue()
def make_animation_script(structure):
property_dict = {}
unique_symbols = structure.get_unique_symbols()
property_dict["scale"] = st.session_state["scale"]
property_dict["sizes"] = {symb:st.session_state[f"size_{symb}"] for symb in unique_symbols}
property_dict["colors"] = {symb:hex2rgba(st.session_state[f"color_{symb}"]) for symb in unique_symbols}
property_dict["cartoon"] = _get_cartoon_param()
property_dict["subdivision_surface"] = _get_subdivision_surface_param()
property_dict["start"] = st.session_state["start"]
property_dict["step"] = st.session_state["step"]
property_dict["style"] = "animation"
property_dict["file"] = "positions.pkl"
property_dict["chemical_symbols"] = structure.atoms[0].get_chemical_symbols()
property_dict["unique_symbols"] = unique_symbols
pyscript = TEMP.render({"data_list":[property_dict]})
zip_stream = io.BytesIO()
with zipfile.ZipFile(zip_stream, 'w', compression=zipfile.ZIP_DEFLATED) as zf:
with zf.open("animation.py","w") as f:
f.write(pyscript.encode())
with zf.open("positions.pkl","w") as f:
for atoms in structure.atoms:
positions = atoms.get_positions()
f.write(pickle.dumps(positions))
return zip_stream.getvalue()