-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_manuscript.py
360 lines (296 loc) · 10.9 KB
/
plot_manuscript.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Import libraries
import os
import h5py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from src.dict2hdf5 import h5_to_dict
from src.load_data import load_data
# Load configuration
from config import PATH_DATA
### Define load functions ###
# Load results
def load_results(datapath, statespath, ratespath, merge=None, root=None):
# Check if root is specified
if root is None:
root = ''
# Load data, states, and rates
data, metadata = load_data(root+datapath, merge=merge)
with h5py.File(root+statespath, "r") as f:
statevars = h5_to_dict(f)
states = statevars["states"]
mu_flor = statevars["mu_flor"]
mu_back = statevars["mu_back"]
with h5py.File(root+ratespath, "r") as f:
ratesvars = h5_to_dict(f)
# Filter data
dataraw = data.copy()
statemask = statevars["mask"]
ratemask = ratesvars["statesmask"]
data = data[statemask, :]
data = data[ratemask, :]
states = states[ratemask, :]
mu_flor = mu_flor[ratemask]
mu_back = mu_back[ratemask]
# Calculate trace
mu_back = mu_back + np.min(states, axis=1)*mu_flor # Shift baseline
states = states - np.min(states, axis=1)[:, None] # Shift states
trace = mu_flor[:, None]*states
trace = (
trace
- np.mean(trace, axis=1)[:, None]
+ np.mean(data, axis=1)[:, None]
)
# Return
return data, trace, ratesvars
### Define plot functions ###
# Lifetimes
def plot_lifetimes(ax, data, groundtruth=None, bins=10):
# Take out burn-in
last = np.where(data != 0)[0][-1]
data = data[last//2:last]
print(data.mean() * 1e6, data.std() * 1e6)
# Rescale data to mHz
data = data*1e6
if groundtruth is not None:
groundtruth = groundtruth*1e6
# Histogram data
ax.hist(data, bins=bins, density=True, color='b', alpha=0.5, label='Samples')
if groundtruth is not None:
ax.axvline(groundtruth, color='r', label='Ground truth')
# Return
return ax
# Data
def plot_data(ax, data, states=None):
# Plot data
times = .124*np.arange(data.shape[0])
ax.plot(times, data, linewidth=2, color='g', alpha=0.5, label='Data')
if states is not None:
ax.plot(times, states, ':', linewidth=1, color='b', label='States')
# Return
return ax
### Set up figure ###
# Results
def figure_results(filelist):
# Set up files and paths
root = os.join(PATH_DATA, "/Binding/")
# Create a figure
fig = plt.figure(figsize=(10, 6))
plt.ion()
plt.show()
# Add axes
gs = gridspec.GridSpec(6, 4)
ax = np.empty((3, 4), dtype=object)
for i in range(3):
if i == 0:
sharex = [None, None, None, None]
else:
sharex = [ax[0, 0], ax[0, 1], ax[0, 2], ax[0, 3]]
ax[i, 0] = fig.add_subplot(gs[2*i, 0:2], sharex=sharex[0])
ax[i, 1] = fig.add_subplot(gs[2*i+1, 0:2], sharex=sharex[1])
ax[i, 2] = fig.add_subplot(gs[2*i:2*(i+1), 2], sharex=sharex[2])
ax[i, 3] = fig.add_subplot(gs[2*i:2*(i+1), 3], sharex=sharex[3])
# Loop over files
for i, (datafile, statesfile, ratesfile, merge) in enumerate(filelist):
# Load results
data, trace, rates = load_results(
datapath=datafile,
statespath=statesfile,
ratespath=ratesfile,
merge=merge,
root=root,
)
# Plot data
rois = np.random.permutation(np.arange(data.shape[0]))[:2]
plot_data(ax[i, 0], data[rois[0], :], trace[rois[0], :])
plot_data(ax[i, 1], data[rois[1], :], trace[rois[1], :])
# Plot lifetimes
plot_lifetimes(ax[i, 2], rates['samples']["k_on"][:, 0])
plot_lifetimes(ax[i, 3], rates['samples']["k_off"][:, 0])
# Remove axes from traces
for i in range(ax.shape[0]):
for j in range(2):
ax[i, j].set_yticks([])
if not (i == 2 and j == 1):
plt.setp(ax[i, j].get_xticklabels(), visible=False)
# Remove axes from lifetimes
for i in range(ax.shape[0]):
for j in range(2, 4):
ax[i, j].set_yticks([])
if i == ax.shape[0]-1:
ax[i, j].tick_params(axis='x', rotation=45)
else:
plt.setp(ax[i, j].get_xticklabels(), visible=False)
# Set labels
ax[0, 0].set_title('Sample Data')
ax[0, 2].set_title('K on')
ax[0, 3].set_title('K off')
ax[-1, 1].set_xlabel("Time (s)")
ax[-1, 2].set_xlabel("K on (mHz/nM)")
ax[-1, 3].set_xlabel("K off (mHz)")
# Rotate binding site labels
ax[0, 0].set_ylabel("One\nBinding\nSite", rotation=0, labelpad=20)
ax[1, 0].set_ylabel("Two\nBinding\nSites", rotation=0, labelpad=20)
ax[2, 0].set_ylabel("Five\nBinding\nSites", rotation=0, labelpad=20)
# Set legend
ax[0, 0].legend(loc='upper right')
ax[0, -1].legend(loc='upper right')
# Return
return fig, ax
# Simulation
def figure_simulation(test='kon'):
# Set up files
if test == 'kon':
sim_files = [
"simulated_kon=1e-6_koff=2e-5_kphoto=1e-7_Nstates=2",
"simulated_kon=2e-6_koff=2e-5_kphoto=1e-7_Nstates=2",
"simulated_kon=5e-6_koff=2e-5_kphoto=1e-7_Nstates=2",
]
sim_files = [(f+".h5", f+"_STATES.h5", f+"_minstep=5_nummicro=2_RATES.h5", None) for f in sim_files]
elif test == 'koff':
sim_files = [
"simulated_kon=1e-6_koff=1e-5_kphoto=1e-7_Nstates=2",
"simulated_kon=1e-6_koff=2e-5_kphoto=1e-7_Nstates=2",
"simulated_kon=1e-6_koff=5e-5_kphoto=1e-7_Nstates=2",
]
sim_files = [(f+".h5", f+"_STATES.h5", f+"_minstep=5_nummicro=2_RATES.h5", None) for f in sim_files]
elif test == 'kphoto':
sim_files = [
"simulated_kon=1e-6_koff=2e-5_kphoto=1e-7_Nstates=2",
"simulated_kon=1e-6_koff=2e-5_kphoto=2e-7_Nstates=2",
"simulated_kon=1e-6_koff=2e-5_kphoto=5e-7_Nstates=2",
]
sim_files = [(f+".h5", f+"_STATES.h5", f+"_minstep=5_nummicro=2_RATES.h5", None) for f in sim_files]
elif test == 'ndata':
f = "simulated_kon=1e-6_koff=2e-5_kphoto=1e-7_Nstates=2"
sim_files = [
(f+".h5", f+"_STATES.h5", f+"_minstep=5_nummicro=2_RATES.h5", None),
(f+".h5", f+"_STATES.h5", f+"_downsample=2_minstep=5_nummicro=2_RATES.h5", None),
(f+".h5", f+"_STATES.h5", f+"_downsample=5_minstep=5_nummicro=2_RATES.h5", None),
]
# Initialize figure
fig, ax = figure_results(sim_files)
# Add ground truth
for i in range(ax.shape[0]):
if test == 'kon':
k_on = 0.000001 * [1, 2, 5][i] * 1e6
k_off = 0.000025 * 1e6
elif test == 'koff':
k_on = 0.000001 * 1e6
k_off = 0.00001 * [1, 2, 5][i] * 1e6
elif test == 'kphoto':
k_on = 0.000001 * 1e6
k_off = 0.000025 * 1e6
elif test == 'ndata':
k_on = 0.000001 * 1e6
k_off = 0.000025 * 1e6
ax[i, 2].axvline(k_on, color='r', label='Ground truth')
ax[i, 3].axvline(k_off, color='r', label='Ground truth')
# Set xlims of ax[:, [2, 3]] to start at 0
for i in range(ax.shape[0]):
for j in [2, 3]:
ax[i, j].set_xlim([0, ax[i, j].get_xlim()[1]])
# Set labels
ax[0, 0].set_title('Sample Data')
ax[0, 2].set_title('K bind')
ax[0, 3].set_title('K un')
ax[-1, 1].set_xlabel("Time (s)")
ax[-1, 2].set_xlabel("K bind (mHz/nM)")
ax[-1, 3].set_xlabel("K un (mHz)")
# Rotate binding site labels
if test == 'kon':
ax[0, 0].set_ylabel(r"$K_{bind} = 1$ mHz/nM", rotation=0, labelpad=30)
ax[1, 0].set_ylabel(r"$K_{bind} = 2$ mHz/nM", rotation=0, labelpad=30)
ax[2, 0].set_ylabel(r"$K_{bind} = 5$ mHz/nM", rotation=0, labelpad=30)
elif test == 'koff':
ax[0, 0].set_ylabel(r"$K_{un} = 10$ mHz", rotation=0, labelpad=30)
ax[1, 0].set_ylabel(r"$K_{un} = 20$ mHz", rotation=0, labelpad=30)
ax[2, 0].set_ylabel(r"$K_{un} = 50$ mHz", rotation=0, labelpad=30)
elif test == 'kphoto':
ax[0, 0].set_ylabel(r"$K_{photo} = .1$ mHz", rotation=0, labelpad=30)
ax[1, 0].set_ylabel(r"$K_{photo} = .2$ mHz", rotation=0, labelpad=30)
ax[2, 0].set_ylabel(r"$K_{photo} = .5$ mHz", rotation=0, labelpad=30)
elif test == 'ndata':
ax[0, 0].set_ylabel(r"$N = 2000$", rotation=0, labelpad=30)
ax[1, 0].set_ylabel(r"$N = 1000$", rotation=0, labelpad=30)
ax[2, 0].set_ylabel(r"$N = 400$", rotation=0, labelpad=30)
# Finalize
plt.tight_layout()
plt.pause(.1)
# Return
return fig, ax
# Experment
def figure_experiment():
# Set up files and paths
exp_files = [
# One Binding Site
(
"ST114_filtered.h5",
"ST114_filtered_STATES.h5",
"ST114_filtered_downsample=10_minstep=5_nummicro=2_RATES.h5",
None,
),
# Two Binding Sites
(
"ST128_filtered.h5",
"ST128_filtered_STATES.h5",
"ST128_filtered_downsample=10_minstep=5_nummicro=2_RATES.h5",
None,
),
# Four Binding Sites
(
"ST129_filtered.h5",
"ST129_filtered_STATES.h5",
"ST129_filtered_downsample=10_minstep=5_nummicro=2_RATES.h5",
None,
),
]
# Initialize figure
fig, ax = figure_results(exp_files)
# Set axis
ax[0, 2].set_xlim([0, 10])
ax[0, 3].set_xlim([0, 20])
# Set labels
ax[0, 0].set_title('Sample Data')
ax[0, 2].set_title('K bind')
ax[0, 3].set_title('K un')
ax[-1, 1].set_xlabel("Time (s)")
ax[-1, 2].set_xlabel("K bind (mHz/nM)")
ax[-1, 3].set_xlabel("K un (mHz)")
# Rotate binding site labels
ax[0, 0].set_ylabel("One\nBinding\nSite", rotation=0, labelpad=20)
ax[1, 0].set_ylabel("Two\nBinding\nSites", rotation=0, labelpad=20)
ax[2, 0].set_ylabel("Five\nBinding\nSites", rotation=0, labelpad=20)
# Set legend
ax[0, 0].legend(loc='upper right')
ax[0, -1].legend(loc='upper right')
# Finalize
plt.tight_layout()
plt.pause(.1)
# Return
return fig, ax
# Main function
if __name__ == '__main__':
## Results
# Simulation kon
print("Simulation kon")
figure_simulation(test='kon')
# plt.savefig("outfiles/results_simulation_kon.png", dpi=300)
# Simulation koff
print("Simulation koff")
figure_simulation(test='koff')
# plt.savefig("outfiles/results_simulation_koff.png", dpi=300)
# Simulation kphoto
print("Simulation kphoto")
figure_simulation(test='kphoto')
# plt.savefig("outfiles/results_simulation_kphoto.png", dpi=300)
# Simulation ndata
print("Simulation ndata")
figure_simulation(test='ndata')
# plt.savefig("outfiles/results_simulation_ndata.png", dpi=300)
# Experiment
print("Experiment")
figure_experiment()
# plt.savefig("outfiles/results_experiment.png", dpi=300)
# Done
print("Done")