-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacegroups.py
187 lines (157 loc) · 5.02 KB
/
spacegroups.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 31 15:09:08 2018
@author: RSH
"""
import warnings
import os
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
warnings.filterwarnings("ignore")
ga_cols = []
al_cols = []
o_cols = []
in_cols = []
for i in range(6):
ga_cols.append("Ga_" + str(i))
for i in range(6):
al_cols.append("Al_" + str(i))
for i in range(6):
o_cols.append("O_" + str(i))
for i in range(6):
in_cols.append("In_" + str(i))
ga_df = pd.DataFrame(columns=ga_cols)
def get_xyz_data(filename):
pos_data = []
lat_data = []
with open(filename) as f:
for line in f.readlines():
x = line.split()
if x[0] == 'atom':
pos_data.append([np.array(x[1:4], dtype=np.float), x[4]])
elif x[0] == 'lattice_vector':
lat_data.append(np.array(x[1:4], dtype=np.float))
return pos_data, np.array(lat_data)
def load_train():
train_name = 'train_with_coords.csv'
if os.path.exists(train_name):
return pd.read_csv(train_name, index_col=0)
train = pd.read_csv("../df_train.csv")
ga_df = pd.DataFrame(columns=ga_cols)
al_df = pd.DataFrame(columns=al_cols)
o_df = pd.DataFrame(columns=o_cols)
in_df = pd.DataFrame(columns=in_cols)
for i in train.id.values:
fn = "../Data/train/{}/geometry.xyz".format(i)
train_xyz, train_lat = get_xyz_data(fn)
ga_list = []
al_list = []
o_list = []
in_list = []
for li in train_xyz:
try:
if li[1] == "Ga":
ga_list.append(li[0])
except:
pass
try:
if li[1] == "Al":
al_list.append(li[0])
except:
pass
try:
if li[1] == "In":
in_list.append(li[0])
except:
pass
try:
if li[1] == "O":
o_list.append(li[0])
except:
pass
temp_ga = transform(ga_list, ga_cols, i)
temp_al = transform(al_list, al_cols, i)
temp_o = transform(o_list, o_cols, i)
temp_in = transform(in_list, in_cols, i)
ga_df = pd.concat([ga_df, temp_ga])
al_df = pd.concat([al_df, temp_al])
o_df = pd.concat([o_df, temp_o])
in_df = pd.concat([in_df, temp_in])
ga_df["id"] = ga_df.index
al_df["id"] = al_df.index
o_df["id"] = o_df.index
in_df["id"] = in_df.index
train = pd.merge(train, ga_df, on=["id"], how="left")
train = pd.merge(train, al_df, on=["id"], how="left")
train = pd.merge(train, o_df, on=["id"], how="left")
train = pd.merge(train, in_df, on=["id"], how="left")
train.to_csv(train_name)
return train
def load_test():
test_name = 'test_with_coords.csv'
if os.path.exists(test_name):
return pd.read_csv(test_name, index_col=0)
test = pd.read_csv("../df_test.csv")
ga_df = pd.DataFrame(columns=ga_cols)
al_df = pd.DataFrame(columns=al_cols)
o_df = pd.DataFrame(columns=o_cols)
in_df = pd.DataFrame(columns=in_cols)
for i in test.id.values:
fn = "../Data/test/{}/geometry.xyz".format(i)
train_xyz, train_lat = get_xyz_data(fn)
ga_list = []
al_list = []
o_list = []
in_list = []
for li in train_xyz:
try:
if li[1] == "Ga":
ga_list.append(li[0])
except:
pass
try:
if li[1] == "Al":
al_list.append(li[0])
except:
pass
try:
if li[1] == "In":
in_list.append(li[0])
except:
pass
try:
if li[1] == "O":
o_list.append(li[0])
except:
pass
temp_ga = transform(ga_list, ga_cols, i)
temp_al = transform(al_list, al_cols, i)
temp_o = transform(o_list, o_cols, i)
temp_in = transform(in_list, in_cols, i)
ga_df = pd.concat([ga_df, temp_ga])
al_df = pd.concat([al_df, temp_al])
o_df = pd.concat([o_df, temp_o])
in_df = pd.concat([in_df, temp_in])
ga_df["id"] = ga_df.index
al_df["id"] = al_df.index
o_df["id"] = o_df.index
in_df["id"] = in_df.index
test = pd.merge(test, ga_df, on=["id"], how="left")
test = pd.merge(test, al_df, on=["id"], how="left")
test = pd.merge(test, o_df, on=["id"], how="left")
test = pd.merge(test, in_df, on=["id"], how="left")
test.to_csv(test_name)
return test
def transform(input_list, cols, index_n):
try:
model = PCA(n_components=2)
temp_ga = model.fit_transform(np.array(input_list).transpose())
tmp = [item for sublist in temp_ga for item in sublist]
except:
tmp = [0, 0, 0, 0, 0, 0]
tmp = pd.DataFrame(tmp).transpose()
tmp.columns = cols
tmp.index = np.array([index_n])
return tmp