-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
185 lines (151 loc) · 6.53 KB
/
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
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
import os
import numpy as np
import matplotlib.pyplot as plt
from model import averages_file, ref_trace_file
plt.style.use('seaborn-v0_8-dark')
def save_figure(path, bbox_inches='tight', dpi=300):
"""
Custom function that lets you save a pyplot figure and creates the directory where necessary
"""
directory = os.path.split(path)[0]
filename = os.path.split(path)[1]
if directory == '':
directory = '.'
if not os.path.exists(directory):
os.makedirs(directory)
save_path = os.path.join(directory, filename)
# Actually save the figure
plt.savefig(save_path, bbox_inches=bbox_inches, dpi=dpi)
plt.close()
def plot_volumes(tspan, cyt_vols, nuc_vols, flag):
"""
Function that can be used to plot the volumes that are used in the model at any time. For example; the raw volumes
over the cell cycle, and the adjusted volumes (after manual tweaking) to fit the model better.
:param tspan:
:param cyt_vols:
:param nuc_vols:
:param flag:
:return:
"""
fig, ax1 = plt.subplots()
fig.suptitle(f"Cytosolic and nuclear volumes over the cell cycle ({flag} manual alterations)", y=0.95)
ax1.set_xlabel('Cell cycle progression')
ax1.grid(False)
ax1.set_ylabel("Cytosolic volume", color='darkcyan')
ax1.plot(tspan / 100, cyt_vols, c='darkcyan', lw=3, alpha=0.8)
ax1.tick_params(axis='y', labelcolor='darkcyan')
ax2 = ax1.twinx()
ax2.grid(False)
ax2.set_ylabel("Nuclear volume", color='darkred')
ax2.plot(tspan / 100, nuc_vols, c='darkred', lw=3, alpha=0.8)
ax2.tick_params(axis='y', labelcolor='darkred')
save_figure(f"./output/{averages_file}/combined_volumes_{flag}.png")
def plot_abundances(tspan, y1, y2):
fig, ax1 = plt.subplots()
fig.suptitle("Cytosolic and nuclear protein abundances over time")
ax1.set_xlabel('Cell cycle progression')
ax1.grid(False)
ax1.set_ylabel("Cytosolic protein abundance", color='orange')
ax1.plot(tspan / 100, y1, color='orange')
ax1.tick_params(axis='y', labelcolor='orange')
ax2 = ax1.twinx()
ax2.grid(False)
ax2.set_ylabel("Nuclear protein abundance", color='darkred')
ax2.plot(tspan / 100, y2, color='darkred')
ax2.tick_params(axis='y', labelcolor='darkred')
save_figure(f"./output/{averages_file}/abundances.png")
def plot_concentration_prediction(t_span, con_ratio, kIn_base, kOut_base):
cell_cycle_prog = t_span / 100 # convert x axis to cell cycle progression
con_ratio = con_ratio / np.average(con_ratio)
# fit polynomial through the data to make it look neater
polfit = np.polyfit(cell_cycle_prog, con_ratio, 10)
poly_y = np.polyval(polfit, cell_cycle_prog)
fig, ax = plt.subplots()
ax.plot(cell_cycle_prog, con_ratio, c='grey', lw=2, alpha=0.6, label="Raw prediction")
ax.plot(cell_cycle_prog, poly_y, c='darkred', lw=4, alpha=0.8, label="Polynomial prediction")
plt.title(
f"Sfp1 N/C concentration ratio"
f"\nParams: kIn base: {round(kIn_base, 6)}, kOut base: {round(kOut_base, 6)}"
)
plt.xlabel("Cell cycle progression")
plt.ylabel("N/C concentration ratio")
plt.legend()
save_figure(f"./output/{averages_file}/nc_concentration_ratio.png")
def plot_prediction_vs_reference(t_span, c_con, n_con, con_ratio, kIn_base, kOut_base, kin_mp, kout_mp, ref_trace, normalize):
cell_cycle_prog = t_span / 100 # convert x axis to cell cycle progression
if normalize:
con_ratio = con_ratio / np.average(con_ratio)
ref_trace = ref_trace / np.average(ref_trace)
mae, mse = get_similarity_measure(cell_cycle_prog, con_ratio, ref_trace)
# fit polynomial through the data to make it look neater
polfit = np.polyfit(cell_cycle_prog, con_ratio, 10)
poly_y = np.polyval(polfit, cell_cycle_prog)
plot_separate_concentrations(c_con, cell_cycle_prog, n_con)
fig, ax = plt.subplots()
ax.plot(cell_cycle_prog, poly_y, c='darkred', lw=4, alpha=0.8, label="Model prediction")
ax.plot(cell_cycle_prog, ref_trace, c='grey', lw=2, alpha=0.6, label=f"{ref_trace_file.split('.')[0]} reference")
plt.title(
f"Nuclear to cytosolic protein concentration ratio\nParams: kIn base"
f": {round(kIn_base, 6)}, kOut base: {round(kOut_base, 6)}"
)
plt.xlabel("Cell cycle progression")
plt.ylabel("Ratio")
plt.legend()
save_figure(f"./output/{averages_file}/nc_concentration_ratio.png")
def get_similarity_measure(cell_cycle_prog, con_ratio, ref_trace):
"""
Calculates a measure for the similarity of the predicted concentration ratio and the reference trace
:param cell_cycle_prog:
:param con_ratio:
:param ref_trace:
:return:
"""
exp_data = np.zeros((100, 2))
exp_data[:, 0] = cell_cycle_prog
exp_data[:, 1] = con_ratio
num_data = np.zeros((100, 2))
num_data[:, 0] = cell_cycle_prog
num_data[:, 1] = ref_trace
# mean absolute error
mae = np.mean(np.abs(exp_data - num_data))
mae = round(mae, 4)
# mean squared error
mse = np.mean(np.square(exp_data - num_data))
mse = round(mse, 4)
return mae, mse
def plot_separate_concentrations(c_con, cell_cycle_prog, n_con):
"""
Plots the nuclear and cytosolic protein concentrations in separate plots. Can be used in addition to plotting the
concentration ratio.
:param c_con:
:param cell_cycle_prog:
:param n_con:
:return:
"""
plt.plot(cell_cycle_prog, c_con, c='darkred', lw=2, alpha=0.8)
plt.title("Cytosolic protein concentration")
plt.xlabel("Cell cycle progression")
plt.ylabel("Concentration")
save_figure(f"./output/{averages_file}/cyt_concentration.png")
plt.plot(cell_cycle_prog, n_con, c='darkred', lw=2, alpha=0.8)
plt.title("Nuclear protein concentration")
plt.xlabel("Cell cycle progression")
plt.ylabel("Concentration")
save_figure(f"./output/{averages_file}/nuc_concentration.png")
def plot_rates(t_values, k_out_values, k_in_values):
"""
In order to intuitively assess what rate(s) (curves) underlay the observed concentration ratio, this function
plots the rates in one figure.
:param t_values:
:param k_out_values:
:param k_in_values:
:return:
"""
to_cc_progr = [x / 100 for x in t_values]
plt.scatter(to_cc_progr, k_out_values, s=2, label="nucl. export rate")
plt.scatter(to_cc_progr, k_in_values, s=2, label="nucl. import rate")
plt.title("Nuclear import and export rates over the cell cycle")
plt.xlabel("Cell cycle progression")
plt.ylabel("Rate")
plt.legend(loc='best')
save_figure(f"./output/{averages_file}/rates.png")