I am trying to create a python script with the ability to analyze individual NMR data and produce a set of analysis output back into each folder, but with just one python script. Here is the script I have for analyzing the individual NMR experiments:
import math
import numpy as np
from numpy import *
import scipy
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.cm as cm
import nmrglue as ng
import sys
from mpl_toolkits.mplot3d import Axes3D
import csv
def exp(x, a, b, c):
d=1/b
return a+c*np.exp(-b*x)
out=open(f'Okuno Lab Python Work/Okuno Test 1/okuno1output.txt', 'w')#ZY
out=open(f'Okuno Lab Python Work/Okuno Test 1/okuno1output.txt', 'a')#ZY
List_area=[]
List_noise=[]
List_inten=[]
with open(f'Okuno Lab Python Work/Okuno Test 1/okuno1tau.csv', 'r') as d:
reader = csv.reader(d)
pretauList = list(reader)
tauList = np.array(pretauList)
data=open(f'Okuno Lab Python Work/Okuno Test 1/okuno1noise.txt', 'r+')#ZY
lines=data.readlines()
for line in lines:
fields=line.split()
noise=float(fields[0])
for i in range(0, len(tauList), 1):
List_noise.append(noise)
dic,data = ng.pipe.read(str(f'Okuno Lab Python Work/Okuno Test 1/test036.ft1'))#ZY
udic = ng.pipe.guess_udic(dic,data)
uc = ng.fileiobase.uc_from_udic(udic)
ppm_1H = uc.ppm_scale()
uc0 = ng.pipe.make_uc(dic,data,dim=0)
ub=uc0("6.5ppm")
lb=uc0("8.2ppm")
ppm = ppm_1H[lb:ub]
inten = data[lb:ub]
maxvalue=max(inten.real)
pt=np.where(data.real==maxvalue)
intensity=data[pt]
maxpt=uc0.ppm(float(pt[0]))
display(uc0.ppm(float(pt[0])))
off=0
count=0
for i in range(1, len(tauList)+1, 1):
off=off+500
count=count+1
try:
dic,data = ng.pipe.read(str(f'Okuno Lab Python Work/Okuno Test 1/test00{str(i)}.ft1'))#ZY
except IOError:
dic,data = ng.pipe.read(str(f'Okuno Lab Python Work/Okuno Test 1/test0{str(i)}.ft1'))#ZY
ppm = ppm_1H[lb:ub]
inten = data[lb:ub]
intensity=data[pt]
area=inten.sum()
List_area.append(float(area.real))
List_inten.append(float(intensity.real))
colors = cm.gist_heat(np.linspace(0, 1, len(tauList)+1))
plt.xlim([6.5, 9.5])
plt.plot(ppm, inten.real, color=colors[count])
x=array(tauList)
y=array(List_inten)
plt.show()
try:
#initial guesses
guessB=2/max(x)
guessA=max(y)
guess=(guessA, guessB, -guessA)
popt, pcov = curve_fit(exp, x, y, guess)
#get the errors
errors=np.sqrt(pcov.diagonal())
print ('rate', popt[1], '+/-', errors[1] )
print(out,'rate', popt[1], '+/-', errors[1])
except RuntimeError:
print("@@@@@@@@@@@@@@@@@@@@@@@@")
print("Error - curve_fit failed")
print("@@@@@@@@@@@@@@@@@@@@@@@@")
xaxis=np.arange(0,tauList[-1], 0.0001)
rate_fig = plt.figure(1)#ZY
plt.plot(tauList, List_inten, marker='o', markersize=10, linestyle='None')
plt.plot(xaxis, exp(xaxis, *popt), marker='None', linestyle='-', linewidth=2, color='r')
plt.savefig(f'rates{ZY_i}.ps', transparent=True)
plt.show()
And here is the script for trying to analyze all NMR files instead of just one:
rawdata_folder = str(Path.cwd())
scripts_folder = rawdata_folder + '/analyze'
scripts_folder
col = ['ppm','rate','rate_error']
logs = pd.DataFrame()
all_rate_figs = []
for ZY_i in range(1,16):
currentdata_path = Path(f'{rawdata_folder}/{ZY_i}')
print('Current folder: ')
print(currentdata_path)
this_script = f'{scripts_folder}/Rcalc{ZY_i}.ipynb'
%run {this_script}
#extract 'folder' , 'ppm','rate' and 'rate_error'
this_data = {'ppm':uc0.ppm(float(pt[0])),'rate':popt[1],'rate_error':errors[1]}
logs = logs.append(this_data,ignore_index=True)
all_rate_figs.append(rate_fig)
for r in range(len(all_rate_figs)):
print('Rate Figure ' + str(r+1))
display(all_rate_figs[r])
Essentially, I have a large number of NMR data files and am hoping to be able to analyze all of them and create files with my desired output using just one python script. Does anyone have any ideas or tips? Thankyou!