forked from Matt-HJ-Bailey/TMCS-Dimensionality-Reduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malonaldehyde.py
83 lines (55 loc) · 2.09 KB
/
malonaldehyde.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
malonaldehyde = ["./Resources/malonaldehyde_IRC.xyz"]
import matplotlib
matplotlib.use("TkAgg")
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
from preprocessing import *
from processing import *
from sklearn.decomposition import PCA
from main import *
from matplotlib.pyplot import cm
def cumulative_variance():
# empty lists for importing data and plotting
inputxyzs = []
PCA_test = []
all_cumulative_variances = []
component = list(range(1,26))
# get data from .xyz files
for file in malonaldehyde:
xyzresult = XYZFile(file)
inputxyzs.append(xyzresult)
PCA_test.append(PCAResults(xyzresult,25))
# loop over all input files
for i in range(len(inputxyzs)):
# calculate variance
input_values = PCA_test[i].get_comp_variance()
# calculate total variance across all principle components
total_variance = 0.
for i in range(len(input_values)):
total_variance += input_values[i]
# Set up lists/values for plotting
cumulative_variances = [] # y-axis values
cum_variance = 0.
# get data to plot from input values
for j in range(len(input_values)):
variance = input_values[j]/ total_variance
cumulative_variances.append(variance)
# nested lists for plotting multiple trajectories on one graph
all_cumulative_variances.append(cumulative_variances)
# ---------------------------- plotting ------------------------------- #
fig = plt.figure(figsize = (5,5))
ax = fig.add_subplot(111)
xmin, xmax = -1,25
ymin, ymax = -0.1,1.1
for k in range(len(all_cumulative_variances)):
ax.plot(component,all_cumulative_variances[k], marker = 'o', c ='black')
ax.set_xlabel('Number of components')
ax.set_ylabel('Cumulative variance')
ax.set_title("Cumulative variance for malonaldehyde")
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
fig.show()
fig.savefig('variances_malonaldehyde.png')
if __name__ == "__main__":
cumulative_variance()