-
Notifications
You must be signed in to change notification settings - Fork 104
/
Carbon_plotting.py
executable file
·153 lines (90 loc) · 4.02 KB
/
Carbon_plotting.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
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('Agg')
import numpy as np
import os
from pathlib import Path
def PlotCarbon(NMRData,Isomers,settings):
xdata = NMRData.carbondata["xdata"]
ydata = NMRData.carbondata["ydata"]
exppeaks = NMRData.carbondata["exppeaks"]
simulated_ydata = NMRData.carbondata["simulated_ydata"]
removed = NMRData.carbondata["removed"]
if settings.OutputFolder == '':
gdir = Path.cwd() / "Graphs" / settings.InputFiles[0]
else:
gdir = settings.OutputFolder / "Graphs" / settings.InputFiles[0]
for isomerindex,isomer in enumerate(Isomers):
assigned_peaks = []
assigned_shifts = []
assigned_labels = []
for i,peak in enumerate(isomer.Cexp):
if peak != '':
assigned_peaks.append(peak)
assigned_shifts.append(isomer.Cshifts[i])
assigned_labels.append(isomer.Clabels[i])
plt.close()
fig1 = plt.figure(1)
fig1.set_size_inches(30, 17)
ax1 = fig1.add_subplot(111)
exppeaks_ppm = xdata[exppeaks].tolist()
shiftlist = assigned_shifts
totallist = exppeaks_ppm + shiftlist
plt.xlim([max(totallist) + 10, min(totallist) - 10])
plt.plot(xdata, ydata, color='grey', linewidth=0.75, label='experimental spectrum')
plt.plot(xdata, simulated_ydata, label='simulated spectrum')
plt.xlabel('PPM') # axis labels
# plt.yticks([], [])
plt.title(str(settings.InputFiles[0]) +
"\nCarbon NMR of Isomer " + str(isomerindex + 1) + "\n Number of Peaks Found = " + str(len(exppeaks)))
# plot assignments
for ind1, peak in enumerate(assigned_peaks):
wh = np.argmin(abs(xdata - peak))
plt.plot([peak, assigned_shifts[ind1]],
[ydata[wh], 1.1], linewidth=0.5, color='cyan')
prev = round(exppeaks_ppm[0], 2)
count = 0
# annotate peak locations
for x, txt in enumerate([round(i, 2) for i in exppeaks_ppm]):
if abs(prev - txt) < 5:
count += 1
else:
count = 0
prev = txt
if exppeaks_ppm[x] in assigned_peaks:
color = 'C1'
else:
color = 'grey'
ax1.annotate(txt, (exppeaks_ppm[x], -0.06 - 0.025 * count), color=color, size=10)
plt.plot(exppeaks_ppm[x], ydata[exppeaks[x]], 'o', color=color)
if len(removed) > 0:
plt.plot(xdata[removed],
simulated_ydata[removed], "ro")
# annotate shift positions
count = 0
####some quick sorting
sortshifts = isomer.Cshifts[::-1]
slabels = isomer.Clabels[::-1]
prev = sortshifts[0]
for x, txt in enumerate(slabels):
if abs(prev - sortshifts[x]) < 4:
count += 1
else:
count = 0
prev = sortshifts[x]
ax1.annotate(txt, (sortshifts[x], + 2.05 + 0.05 * count), size=18)
simulated_calc_ydata = simulate_calc_data(xdata, isomer.Cshifts, simulated_ydata)
plt.plot(xdata, simulated_calc_ydata + 1.1, label='calculated spectrum')
plt.legend()
f_name = "Carbon_" + str(isomerindex + 1 ) + ".svg"
plt.savefig(gdir / f_name,format = "svg", bbox_inches='tight')
plt.close()
def simulate_calc_data(spectral_xdata_ppm, calculated_locations, simulated_ydata):
###simulate calcutated data
simulated_calc_ydata = np.zeros(len(spectral_xdata_ppm))
for peak in calculated_locations:
y = np.exp(-0.5 * ((spectral_xdata_ppm - peak) / 0.002) ** 2)
simulated_calc_ydata += y
scaling_factor = np.amax(simulated_ydata) / np.amax(simulated_calc_ydata)
simulated_calc_ydata = simulated_calc_ydata * scaling_factor
return simulated_calc_ydata