-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteep.py
173 lines (155 loc) · 6.67 KB
/
steep.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import print_function
from __future__ import division
import os
import numpy as np
from matplotlib import pyplot
import sys
import glob
from NeuroRDanal import h5utils
from NeuroRDanal import plot_h5 as pu5
import h5py as h5
from scipy import optimize
Avogadro=6.023e14 #to convert to nanoMoles
mol_per_nM_u3=Avogadro*1e-15
msec_per_sec=1000
window_size=1 #number of seconds on either side of peak value to average for maximum
showplot=1 #2 indicates plot the head conc, 0 means no plots
textsize=10
try:
args = ARGS.split(",")
print("ARGS =", ARGS, "commandline=", args)
do_exit = False
except NameError: #NameError refers to an undefined variable (in this case ARGS)
args = sys.argv[1:]
print("commandline =", args)
do_exit = True
ftuples,parlist,params=h5utils.argparse(args)
figtitle=args[0].split('/')[-1]+args[1]
input_molecules=args[2].split()
output_molecules=args[3].split()
plot_molecules=input_molecules+output_molecules
try:
data.close()
except Exception:
pass
parval=[]
numfiles=len(ftuples)
whole_plot_array=[]
whole_time_array=[]
for fnum,ftuple in enumerate(ftuples):
data,maxvols,TotVol,trials,seeds,arraysize,p=h5utils.initialize(ftuple,numfiles,parval)
plot_array=[]
time_array=[]
if fnum==0:
molecules=h5utils.decode(data['model']['species'][:])
#initialize plot stuff, arrays for static measurements, and find molecules among the output sets
if numfiles>1:
whole_plot_array=[[] for mol in plot_molecules]
whole_time_array=[[] for mol in plot_molecules]
num_mols=len(plot_molecules)
baseline=np.zeros((arraysize,num_mols))
auc=np.zeros((arraysize,num_mols))
peakval=np.zeros((arraysize,num_mols))
#which columns are needed for each molecule
out_location,dt,rows=h5utils.get_mol_info(data,plot_molecules,maxvols)
#
#Which "rows" should be used for baseline value, specifed in args[3]. If different for each file then will have problems later
sstart,ssend=h5utils.sstart_end(plot_molecules,args,4,out_location,dt,rows)
voxel=0
for mol in plot_molecules:
if out_location[mol]!=-1:
outset = list(out_location[mol]['location'].keys())[0]
imol=out_location[mol]['location'][outset]['mol_index']
tempConc=np.zeros((len(trials),out_location[mol]['samples']))
time_array.append(data[trials[0]]['output'][outset]['times'][:]/msec_per_sec)
#generate output files for these cases
for trialnum,trial in enumerate(trials):
tempConc[trialnum]=data[trial]['output'][outset]['population'][:,voxel,imol]/TotVol/mol_per_nM_u3
plot_array.append(np.mean(tempConc,axis=0))
#plot_array dimensions=numfiles x number of molecules x sample times
else:
if fnum==0 and molecule_name_issue==0:
print("Choose molecules from:", molecules)
molecule_name_issue=1
time_array.append([])
plot_array.append([])
if numfiles>1:
#plot_array dimensions=num molecules x sample times
#whole_plot_array dimension = num molecules*num files*sample time
for mol in range(num_mols):
whole_plot_array[mol].append(plot_array[mol])
whole_time_array[mol].append(time_array[mol])
def peakval(pnum,plot_molecules,mol,sstart,ssend):
imol=plot_molecules.index(mol)
endpt=len(whole_plot_array[imol][pnum])
if out_location[mol]!=-1:
dt=out_location[mol]['dt']
window=int(window_size/dt)
baseline=whole_plot_array[imol][pnum][sstart[imol]:ssend[imol]].mean()
peakpt=whole_plot_array[imol][pnum][ssend[imol]:].argmax()+ssend[imol]
auc=np.sum(whole_plot_array[imol][pnum][ssend[imol]:endpt]-baseline)*dt
peakval=whole_plot_array[imol][pnum][max(peakpt-window,0):min(peakpt+window,endpt)].mean()
return peakval,auc
def f1(conc,Kd,N,maxval):
return maxval*conc**N/(conc**N+Kd**N)
def f2(conc,Kd,N,maxval):
return maxval*(conc/(conc+Kd))**N
################# This part is new compared to sig.py and nrdh5_anal.py
#reshape and print
inputvals=np.zeros((len(parlist[0]),len(parlist[1])))
outputvals=np.zeros((len(output_molecules),len(parlist[0]),len(parlist[1])))
input_auc=np.zeros((len(parlist[0]),len(parlist[1])))
output_auc=np.zeros((len(output_molecules),len(parlist[0]),len(parlist[1])))
print(params[0],' ',params[1]," peakvals ", output_molecules)
in_mol=input_molecules[0]
for p1,par1 in enumerate(parlist[0]):
for p2,par2 in enumerate(parlist[1]):
par_index=parval.index((par1,par2))
print (' ',par1,' ',par2, ' ', in_mol,end=': ')
inputvals[p1,p2],input_auc[p1,p2]=peakval(par_index,plot_molecules,in_mol,sstart,ssend)
print("%8.2f"%(inputvals[p1,p2]),end=', ')
for imol,mol in enumerate(output_molecules):
outputvals[imol,p1,p2],output_auc[imol,p1,p2]=peakval(par_index,plot_molecules,mol,sstart,ssend)
print(outputvals[:,p1,p2])
# fit outputvalues vs inputvalues to sigmoid
pyplot.ion()
pyplot.figure()
halfmax=500
N_guess=2
min_fit=np.array([0,0,0])
max_fit=np.array([10000,20,5000])
fit1={}
fit_auc={}
colors=['r','b','k','c','m','g']
shapes=['*','o','d','s','D','+','x']
lines=['-', '--', '-.','-', '--', '-.']
for imol,mol in enumerate(output_molecules):
max_guess=np.max(outputvals[imol])
fit1[mol]={}; fit_auc[mol]={}
for p1,par1 in enumerate(parlist[0]):
marker=colors[imol%len(colors)]+shapes[p1%len(shapes)]
line=colors[imol%len(colors)]+lines[p1%len(lines)]
guess=np.array([halfmax,N_guess,max_guess])
popt,pcov=optimize.curve_fit(f1,inputvals[p1,:], outputvals[imol,p1,:],p0=guess,bounds=[min_fit,max_fit])
fit1[mol][par1]={'Kd':popt[0],'N':popt[1], 'max': popt[2]}
#popt,pcov=optimize.curve_fit(f1,input_auc[p1,:], output_auc[imol,p1,:],p0=guess)
#fit_auc[mol][par1]={'Kd':popt[0],'N':popt[1], 'max': popt[2]}
pyplot.plot(inputvals[p1,:],outputvals[imol,p1,:], marker,label=mol+'-'+par1)
pars=fit1[mol][par1]
pyplot.plot(inputvals[p1,:],f1(inputvals[p1,:],pars['Kd'],pars['N'],pars['max']),line)
pyplot.xlabel(in_mol+' (nM)')
pyplot.ylabel('molecule conc (nM)')
pyplot.legend()
#pyplot.xscale('log')
for key in fit1.keys():
print(key,fit1[key])#,'auc:',fit_auc[key])
for k in fit1.keys():
print (k,np.round(fit1[k]['2']['N'],2),np.round(fit1[k]['2p4']['N'],2))
'''
if showplot:
fig,col_inc,scale=pu5.plot_setup(plot_molecules,parlist,params,0,showplot)
#need fnames
fig.canvas.set_window_title(figtitle)
pu5.plottrace(plot_molecules,whole_time_array,whole_plot_array,parval,fig,col_inc,scale,parlist,textsize,[],showplot)
#
'''