forked from ray38/ElectroLens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectroLens.py
194 lines (166 loc) · 6.08 KB
/
ElectroLens.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
"""
Communicate between Python and Javascript asynchronously using
inter-process messaging with the use of Javascript Bindings.
"""
from cefpython3 import cefpython as cef
import os
import json
import csv
from ase import Atoms
from ase.io.trajectory import TrajectoryReader
def view(data):
#print type(data)
#config = trajToConfig2(data)
if isinstance(data, Atoms):
config = atomsToConfig(data)
elif isinstance(data, TrajectoryReader):
config = trajToConfig(data)
else:
config = data
with open('temp_data.json', 'w') as fp:
json.dump(config , fp)
settings = {
"debug": True,
"log_severity": cef.LOGSEVERITY_INFO,
"log_file": "debug.log",
}
cef.Initialize(settings=settings)
cwd = os.getcwd()
try:
os.chdir("ElectroLens-python")
except:
pass
browser_setting = { "file_access_from_file_urls_allowed":True,\
"universal_access_from_file_urls_allowed": True,\
"web_security_disabled":True}
browser = cef.CreateBrowserSync(url='file://' + os.path.realpath("index_cefpython.html"),
window_title="Javascript Bindings", settings = browser_setting)
os.chdir(cwd)
browser.SetClientHandler(LoadHandler(config))
bindings = cef.JavascriptBindings()
browser.SetJavascriptBindings(bindings)
cef.MessageLoop()
del browser
cef.Shutdown()
return
def atomsToConfig(a):
#print "converting atoms to config"
systemDimension = {}
systemDimension["x"] = [0,a.cell[0][0]]
systemDimension["y"] = [0,a.cell[1][1]]
systemDimension["z"] = [0,a.cell[2][2]]
config = {}
config["views"] = []
temp = {}
temp["viewType"] = "3DView"
temp["moleculeName"] = "test"
temp["moleculeData"] = {}
temp["moleculeData"]["data"] = []
for atom in a:
temp_atom = {}
temp_atom["x"] = atom.position[0]
temp_atom["y"] = atom.position[1]
temp_atom["z"] = atom.position[2]
temp_atom["atom"] = atom.symbol
temp["moleculeData"]["data"].append(temp_atom)
temp["moleculeData"]["gridSpacing"] = {"x":0.1,"y":0.1,"z":0.1}
temp["systemDimension"] = systemDimension
config["views"].append(temp)
config["plotSetup"] = {}
config["plotSetup"]["moleculePropertyList"] = ["atom"]
return config
def trajToConfig(a):
#print "converting traj to config"
systemDimension = {}
systemDimension["x"] = [0,a[0].cell[0][0]]
systemDimension["y"] = [0,a[0].cell[1][1]]
systemDimension["z"] = [0,a[0].cell[2][2]]
length = len(a) * len(a[0])
if length < 10000000:
config = {}
config["views"] = []
temp = {}
temp["viewType"] = "3DView"
temp["moleculeName"] = "test"
temp["moleculeData"] = {}
temp["moleculeData"]["data"] = []
for i in range(len(a)):
atoms = a[i]
for atom in atoms:
temp_atom = {}
temp_atom["x"] = atom.position[0]
temp_atom["y"] = atom.position[1]
temp_atom["z"] = atom.position[2]
temp_atom["atom"] = atom.symbol
temp_atom["frame"] = i
temp["moleculeData"]["data"].append(temp_atom)
temp["moleculeData"]["gridSpacing"] = {"x":0.1,"y":0.1,"z":0.1}
temp["systemDimension"] = systemDimension
config["views"].append(temp)
config["plotSetup"] = {}
config["plotSetup"]["frameProperty"] = "frame"
config["plotSetup"]["moleculePropertyList"] = ["atom","frame"]
else:
config = {}
config["views"] = []
temp = {}
temp["viewType"] = "3DView"
temp["moleculeName"] = "test"
temp["moleculeData"] = {}
temp["moleculeData"]["dataFilename"] = os.getcwd() + "/__ElectroLens_View_Intermediate__.csv"
with open('__ElectroLens_View_Intermediate__.csv', mode='w') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(['x', 'y','z','atom','frame'])
for i in range(len(a)):
atoms = a[i]
for atom in atoms:
temp_row = []
temp_row.append(atom.position[0])
temp_row.append(atom.position[1])
temp_row.append(atom.position[2])
temp_row.append(atom.symbol)
temp_row.append(i)
writer.writerow(temp_row)
temp["moleculeData"]["gridSpacing"] = {"x":0.1,"y":0.1,"z":0.1}
temp["systemDimension"] = systemDimension
config["views"].append(temp)
config["plotSetup"] = {}
config["plotSetup"]["frameProperty"] = "frame"
config["plotSetup"]["moleculePropertyList"] = ["atom","frame"]
return config
def trajToConfig2(data):
a, fingerprint = data
#print "converting traj to config"
systemDimension = {}
systemDimension["x"] = [0,a.cell[0][0]]
systemDimension["y"] = [0,a.cell[1][1]]
systemDimension["z"] = [0,a.cell[2][2]]
config = {}
config["views"] = []
temp = {}
temp["viewType"] = "3DView"
temp["moleculeName"] = "test"
temp["moleculeData"] = {}
temp["moleculeData"]["data"] = []
atoms = a
for i, atom in enumerate(atoms):
temp_atom = {}
temp_atom["x"] = atom.position[0]
temp_atom["y"] = atom.position[1]
temp_atom["z"] = atom.position[2]
temp_atom["atom"] = atom.symbol
temp_atom["p1"] = fingerprint[i][1][0]
temp_atom["p2"] = fingerprint[i][1][1]
temp_atom["p3"] = fingerprint[i][1][2]
temp["moleculeData"]["data"].append(temp_atom)
temp["moleculeData"]["gridSpacing"] = {"x":0.1,"y":0.1,"z":0.1}
temp["systemDimension"] = systemDimension
config["views"].append(temp)
config["plotSetup"] = {}
config["plotSetup"]["moleculePropertyList"] = ["atom","p1","p2","p3"]
return config
class LoadHandler(object):
def __init__(self, config):
self.config = config
def OnLoadEnd(self, browser, **_):
browser.ExecuteFunction("defineData", self.config)