forked from uiocompcat/AABBA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
112 lines (86 loc) · 4.02 KB
/
utilities.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
import numpy as np
import csv
import pandas as pd
def round_numbers(value, feature):
"""Round the values according to their feature
Args:
feature (str): Attribute name
value (float): Value of the attribute to be rounded
Returns:
value_out: Rounded attribute value
"""
if feature == 'feature_atomic_number' or 'feature_identity' or \
'feature_node_degree' or 'feature_atomic_number1' or \
'feature_atomic_number2' or 'feature_node_degree1' or \
'feature_node_degree2':
value_out = np.round(value, decimals= 1)
if feature == 'feature_covalent_radius' or 'feature_electronegativity' or \
'feature_covalent_radius1' or 'feature_covalent_radius2' or \
'feature_electronegativity1' or 'feature_electronegativity2':
value_out = np.round(value, decimals = 3)
return value_out
def join_vectors(vectors, feature):
"""Join all the autocorrelation vector of each graph in a list of dict
Args:
vectors (list): AC vector of each graph
feature (list[str]): Labels to define the elements of the AC vector
Returns:
list: List of dict with the AC vector of each graph
"""
print('Pack the vectors')
out_dict = []
for values in vectors:
keys = feature
outfile = dict((k, v) for (k, v) in zip(keys, values))
out_dict.append(outfile)
return out_dict
def round_csv(path, depth_max, ac_operator, walk, model_number):
"""Round the values of the csv file according to their feature
Args:
path (path): Path to .csv file folder
depth_max (int): Maximum distance to read the graph
ac_operator (str): Arithmetic operator applied to the properties
walk (str): Type of autocorrelation to be performed
"""
df = pd.read_csv(path/f'{ac_operator}_{walk}_d{depth_max}_{model_number}.csv', decimal = '.')
# feature heading for node and edges PT and NBO properties
one_decimal = ('Z-', 'I-', 'T-', 'Zi-', 'Zj-', 'Ti-', 'Tj-', 'Nlp-', \
'Nlv-', 'Nlp_i-', 'Nlp_j-', 'Nlv_i-', 'Nlv_j-','Nbn-', 'Nbn_-')
three_decimal = ('S', 'chi', 'Si', 'Sj', 'chi_i', 'chi_j', 'Ns', 'Np', \
'Nd', 'Ns_i', 'Np_i', 'Nd_i' ,'Ns_j', 'Np_j', 'Nd_j')
five_decimal = ('BO-')
six_decimal = ('qnat-', 'qnat_i-', 'qnat_j-','Vnat-','Vnat_i-', 'Vnat_j-', \
'LPde-', 'LPde_i-', 'LPde_j-', 'LPe_i-', 'LPe_j-', 'LPe-', 'LPocc-', \
'LPs-', 'LPs-', 'LPp-', 'LPd-', 'LVde-', 'LVde_i-', 'LVde_j-', \
'LVe-', 'LVe_i-', 'LVe_j-', 'LVocc-', 'LVs-', 'LVp-', 'LVd-', \
'BNde-','BNe-', 'BNocc-', 'BNs-', 'BNp-', 'BNd-', \
'BNde_-', 'BNe_-', 'BNocc_-', 'BNs_-', 'BNp_-', 'BNd_-')
seven_decimal = ('d-' , 'Davg-', 'Aavg-')
for col in df.columns:
if col.startswith(one_decimal):
df[col] = df[col].round(1)
if col.startswith(three_decimal):
df[col] = df[col].round(3)
if col.startswith(five_decimal):
df[col] = df[col].round(5)
if col.startswith(six_decimal):
df[col] = df[col].round(6)
if col.startswith(seven_decimal):
df[col] = df[col].round(7)
# save the rounded csv file
df.to_csv(path/f'{ac_operator}_{walk}_d{depth_max}_{model_number}.csv', index=False)
def save_vectors(path, vectors, depth_max, ac_operator, walk, model_number):
"""Save the list of vectors in a .csv file
Args:
path (path): Path to .csv file folder
depth_max (int): Maximum distance to read the graph
ac_operator (str): Arithmetic operator applied to the properties
walk (str): Type of autocorrelation to be performed
model_number (int): Number of the model to be saved (0, 1, 2, 3, 4, 5)
"""
keys = vectors[0].keys()
with open(path/f'{ac_operator}_{walk}_d{depth_max}_{model_number}.csv', 'w', newline='') as f:
dict_writer = csv.DictWriter(f, fieldnames = keys)
dict_writer.writeheader()
dict_writer.writerows(vectors)
f.close()