-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
125 lines (104 loc) · 3.26 KB
/
utils.py
File metadata and controls
125 lines (104 loc) · 3.26 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
from ast import literal_eval
import pickle
import os
import numpy as np
import pandas as pd
import multiprocessing as mp
import sys
import random as rd
import torch as t
from pymatgen.core import lattice
from pymatgen.core.structure import Structure
#Parallel
def parallel_Featurize(featureFunc, s_list, n_jobs):
pool = mp.Pool(processes=n_jobs)
Feature_list = pool.starmap_async(func=featureFunc, iterable=s_list).get()
pool.close()
pool.join()
return Feature_list
def parallel_single(Func,s_list,n_jobs):
pool = mp.Pool(processes=n_jobs)
Feature_list = pool.map_async(func=Func, iterable=s_list).get()
pool.close()
pool.join()
return Feature_list
#x: AR, MB, Go, Mu, MV
def loadDictionary(df, str):
labels = df.columns
if str in labels and 'element' in labels:
elements = df['element'].values
x = df[str].values
else:
print('ERROR input csv')
exit(0)
x_dict = {}
nElements = len(elements)
for i in range(nElements):
x_dict[elements[i]] = x[i]
return x_dict
def loadmodel(name:str):
with open(os.path.join(os.getcwd(), 'formation_energy', 'methods', name + '.pickle'), 'rb') as f:
model = pickle.load(f)
return model
def get_value_electrons(full_e_structure):
outside=full_e_structure[-1]
inside=full_e_structure[-2]
total_number=outside[-1]+inside[-1]
return total_number
def dump_error_structure(s):
string=s.composition.reduced_formula
filename=string+'.cif'
s.to(filename=filename)
def site_property(s,p_key,condition):
'''
s: pymatgen structure
p_key: site_properties is a dictionary
'''
try:
L=s.site_properties[p_key]
except KeyError:
print('this structure has no key, use all sites')
return None
else:
indexs=[]
for i in range(len(L)):
if L[i]==condition:
indexs.append(i)
else:
continue
return indexs
def mae(prediction, target):
"""
Computes the mean absolute error between prediction and target
Parameters
----------
prediction: torch.Tensor (N, 1)
target: torch.Tensor (N, 1)
"""
return t.mean(t.abs(target - prediction))
class Normalizer(object):
"""Normalize a Tensor and restore it later. """
def __init__(self, tensor, dim=None):
"""tensor is taken as a sample to calculate the mean and std"""
if dim is None:
self.mean = t.mean(tensor)
self.std = t.std(tensor)
else:
self.mean = t.mean(tensor,dim)
self.std = t.std(tensor,dim)
def norm(self, tensor):
return (tensor - self.mean) / self.std
def denorm(self, normed_tensor):
return normed_tensor * self.std + self.mean
def state_dict(self):
return {'mean': self.mean,
'std': self.std}
def load_state_dict(self, state_dict):
self.mean = state_dict['mean']
self.std = state_dict['std']
if __name__=='__main__':
df=pd.read_csv('testdata\\test.csv')
S=df['structures'][1]
S=Structure.from_dict(literal_eval(S))
s3=S[3]
print(S.index(s3))