-
Notifications
You must be signed in to change notification settings - Fork 3
/
figure-4-n1.py
executable file
·141 lines (119 loc) · 4.78 KB
/
figure-4-n1.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
#!/usr/bin/env python3
import sys
sys.path.append('..')
import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
sns.set_context('paper')
sns.set(rc={'axes.facecolor':'none', 'grid.color':'none'})
#cmap = sns.color_palette('blend:#7AB,#EDA', as_cmap=True)
#cmap = sns.color_palette('dark:#5B9_r', as_cmap=True)
cmap = sns.color_palette('dark:#66c2a4_r', as_cmap=True)
import methods.parameters as parameters
import methods.heatmap as heatmap
results = 'figures'
prefix = 'figure-4-n1'
results = os.path.join(results, 'figure-4')
if not os.path.isdir(results):
os.makedirs(results)
compounds_1 = list(parameters._drug_training_list)
classes_1 = dict(parameters._drug_training_classes)
compounds_2 = list(parameters._drug_validation_list)
classes_2 = dict(parameters._drug_validation_classes)
shift = len(compounds_1) + 1
model_list = [f'{i}' for i in ['0a', '0b']]
model_list += [f'{i}' for i in range(1, 3)]
model_list += [f'{i}' for i in ['2i']]
model_list += [f'{i}' for i in range(3, 6)]
model_list += [f'{i}' for i in ['5i']]
model_list += [f'{i}' for i in range(6, 14)]
for base_model in ['li']: #['li', 'lei']: #TODO only because they're the same
if base_model == 'lei':
model_names = [f'm{m}' for m in model_list]
else:
model_names = [f'{base_model}-m{m}' for m in model_list]
exclude_model_list = parameters.exclude_model_list_n1[base_model]
fig, ax = plt.subplots(1, 1, figsize=(8.5, 9.5))
# >>> Get matrix
exclude = np.zeros((len(compounds_1) + len(compounds_2) + 1,
len(model_list))) # +1 for an empty row
for i, c in enumerate(compounds_1):
for j, m in enumerate(model_names):
if m in exclude_model_list[c]:
exclude[i, j] = 1
exclude[shift - 1, :] = np.NaN # empty row
for i, c in enumerate(compounds_2):
for j, m in enumerate(model_names):
if m in exclude_model_list[c]:
exclude[i + shift, j] = 1
# <<<
_model_list = model_list.copy()
_model_list[0] = r'0$\alpha$'
_model_list[1] = r'0$\beta$'
heatmap.heatmap(exclude, compounds_1 + [''] + compounds_2, model_list,
cmap=cmap, rotation=0, ha='center', cbarlabel=None,
alpha=0.95, ax=ax)
ax.axhline(shift - 1, c='#7f7f7f')
# >>> Classes
colors = ['C3', 'C0', 'C2']
cl_name = ['High', 'Intermediate', 'Low']
cl_v = []
i_i, cl_i = 0, classes_1[compounds_1[0]]
for i, c in enumerate(compounds_1):
if classes_1[c] != cl_i:
cl_v.append((i_i, i - 1))
i_i, cl_i = i, classes_1[c]
cl_v.append((i_i, i))
for i, (v, n) in enumerate(zip(cl_v, cl_name)):
ax.plot([len(model_list)]*2, v, c=colors[i], ls='-', marker='')
ax.text(len(model_list) + 0.5, np.mean(v), n, c=colors[i],
ha='center', va='center', rotation=90)
cl_v = []
i_i, cl_i = 0, classes_2[compounds_2[0]]
for i, c in enumerate(compounds_2):
if classes_2[c] != cl_i:
cl_v.append((i_i + shift, i + shift - 1))
i_i, cl_i = i, classes_2[c]
cl_v.append((i_i + shift, i + shift))
for i, (v, n) in enumerate(zip(cl_v, cl_name)):
ax.plot([len(model_list)]*2, v, c=colors[i], ls='-', marker='')
ax.text(len(model_list) + 0.5, np.mean(v), n, c=colors[i],
ha='center', va='center', rotation=90)
# <<<
# >>> Labels
ax.text(-0.285,
1 - 0.5 * len(compounds_1) / (len(compounds_1 + compounds_2) + 1.),
'Training compounds',
ha='center', va='center', rotation=90,
transform=ax.transAxes)
ax.text(-0.285,
0.5 * len(compounds_2) / (len(compounds_1 + compounds_2) + 1.),
'Validation compounds',
ha='center', va='center', rotation=90,
transform=ax.transAxes)
#if base_model == 'li':
# title = 'Binding model (physiological model A)'
#elif base_model == 'lei':
# title = 'Binding model (physiological model B)'
#else:
# raise ValueError(f'Unexpected base model {base_model}')
title = 'Binding model (physiological models A/B)'
ax.set_title(title + '\n')
# <<<
# >>> Legend
ax.scatter(np.NaN, np.NaN, marker='s', color=cmap(-np.inf), alpha=0.95,
label='Plausible')
ax.scatter(np.NaN, np.NaN, marker='s', color=cmap(np.inf), alpha=0.95,
label='Implausible')
ax.legend(loc='lower left', bbox_to_anchor=(-0.35, 1.045), ncol=2,
columnspacing=1.25)
# <<<
fig.tight_layout(rect=(0, 0, 1.1, 1))
#filename = f'{prefix}-{base_model}'
filename = f'{prefix}'
fig.savefig(os.path.join(results, filename), dpi=300)
fig.savefig(os.path.join(results, f'{filename}.pdf'), format='pdf')
plt.close(fig)