forked from umarov90/DeepCellState
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_models.py
executable file
·180 lines (158 loc) · 9.29 KB
/
train_models.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
import os
from scipy import stats
from shutil import copyfile
import deepfake
from figures import profiles_viz
from CellData import CellData
import numpy as np
import pandas as pd
import random
random.seed(0)
np.random.seed(0)
# parameters
wdir = "results_2cells"
test_folds = ["1"]
# test_folds = range(1, 11)
# test_folds = ["antibiotics_ids", "adrenergic_ids", "cholinergic_ids",
# "5-HT modulator_ids", "TKI_ids", "COX inh._ids",
# "histaminergic_ids", "antipsychotic_ids", "GABAergic_ids", "dopaminergic_ids"]
input_size = 978
latent_dim = 128
data_folder = "/home/user/data/DeepFake/" + wdir
def test_loss(prediction, ground_truth):
return np.sqrt(np.mean((prediction - ground_truth) ** 2))
os.chdir(data_folder)
# copyfile("/home/user/PycharmProjects/DeepFake/deepfake.py", "temp")
print(data_folder)
df = pd.read_csv("../LINCS/GSE70138_Broad_LINCS_pert_info.txt", sep="\t")
good = []
tsne_perts = []
tsne_input = []
tsne_latent = []
for r, test_fold in enumerate(test_folds):
test_fold = str(test_fold)
tr_size = 1280
cell_data = CellData("../LINCS/lincs_phase_1_2.tsv", "../LINCS/folds/" + test_fold)
autoencoder, cell_decoders = deepfake.get_best_autoencoder(input_size, latent_dim,
cell_data, test_fold, 2)
encoder = autoencoder.get_layer("encoder")
results = {}
img_count = 0
seen_perts = []
print("Total test objects: " + str(len(cell_data.test_data)))
all_results = []
good_perts = []
test_trt = "trt_cp"
vectors = []
input_profiles = []
perts_order = []
for i in range(len(cell_data.test_data)):
if i % 100 == 0:
print(str(i) + " - ", end="", flush=True)
test_meta_object = cell_data.test_meta[i]
if test_meta_object[2] != test_trt:
continue
if test_meta_object[0] not in ["MCF7", "PC3"]:
continue
closest, closest_profile, mean_profile, all_profiles = cell_data.get_profile(cell_data.test_data,
cell_data.meta_dictionary_pert_test[
test_meta_object[1]],
test_meta_object)
if closest_profile is None:
continue
# if test_meta_object[1] in seen_perts:
# continue
seen_perts.append(test_meta_object[1])
test_profile = np.asarray([cell_data.test_data[i]])
weights = cell_decoders[cell_data.test_meta[i][0]]
autoencoder.get_layer("decoder").set_weights(weights)
decoded1 = autoencoder.predict(closest_profile)
results["count"] = results.get("count", 0) + 1
results["Our performance is: "] = results.get("Our performance is: ", 0) + test_loss(decoded1, test_profile)
results["Our correlation is: "] = results.get("Our correlation is: ", 0) + \
stats.pearsonr(decoded1.flatten(), test_profile.flatten())[0]
predictions = []
for p in all_profiles:
predictions.append(autoencoder.predict(np.asarray([p])))
special_decoded = np.mean(np.asarray(predictions), axis=0, keepdims=True)
results["Our multi-correlation is: "] = results.get("Our multi-correlation is: ", 0) + \
stats.pearsonr(special_decoded.flatten(), test_profile.flatten())[0]
results["Our multi-performance is: "] = results.get("Our multi-performance is: ", 0) + \
test_loss(special_decoded, test_profile)
decoded1 = autoencoder.predict(mean_profile)
results["Our performance is (mean profile): "] = results.get("Our performance is (mean profile): ",
0) + test_loss(decoded1, test_profile)
results["Our correlation (mean profile): "] = results.get("Our correlation (mean profile): ", 0) + \
stats.pearsonr(decoded1.flatten(), test_profile.flatten())[0]
results["Baseline correlation (mean profile): "] = results.get("Baseline correlation (mean profile): ", 0) + \
stats.pearsonr(mean_profile.flatten(),
test_profile.flatten())[0]
results["Baseline performance (mean profile): "] = results.get("Baseline performance (mean profile): ", 0) + \
test_loss(mean_profile, test_profile)
all_results.append(str(stats.pearsonr(special_decoded.flatten(), test_profile.flatten())[0]) + ", " +
str(stats.pearsonr(mean_profile.flatten(), test_profile.flatten())[0]) + ", "
+ test_meta_object[0] + ", " + test_meta_object[1] + ", " + str(len(all_profiles)))
results["closest profile: "] = results.get("closest profile: ", 0) + test_loss(closest_profile, test_profile)
results["closest profile correlation is: "] = results.get("closest profile correlation is: ", 0) + \
stats.pearsonr(closest_profile.flatten(), test_profile.flatten())[
0]
bp = stats.pearsonr(mean_profile.flatten(), test_profile.flatten())[0]
dp = stats.pearsonr(special_decoded.flatten(), test_profile.flatten())[0]
if dp > 0.4: # and bp < 0.5
os.makedirs("profiles", exist_ok=True)
pname = profiles_viz.fix(df.query('pert_id=="' + str(test_meta_object[1]) + '"')["pert_iname"].tolist()[0])
profiles_viz.draw_profiles(test_profile, special_decoded, closest_profile, pname,
input_size, "profiles/" + cell_data.test_meta[i][0] + "_" + str(i)
+ "_" + str(dp) + "_" + str(bp) + "_" + pname + ".svg")
profiles_viz.draw_scatter_profiles(test_profile, special_decoded, closest_profile, pname,
"profiles/" + cell_data.test_meta[i][0] + "_" + str(i)
+ "_" + str(dp) + "_" + str(bp) + "_" +
pname + "_scatter.svg")
tsne_perts.append(["PC3" if test_meta_object[0] == "MCF7" else "MCF7",
df.query('pert_id=="' + str(test_meta_object[1]) + '"')["pert_iname"].tolist()[0]])
tsne_input.append(closest_profile.flatten())
tsne_latent.append(encoder.predict(closest_profile).flatten())
if test_meta_object[0] == "MCF7":
good_perts.append([test_meta_object[1], bp])
np.savetxt("../figures_data/tsne_perts.csv", np.array(tsne_perts), delimiter=',', fmt="%s")
np.savetxt("../figures_data/tsne_input.csv", np.array(tsne_input), delimiter=',')
np.savetxt("../figures_data/tsne_latent.csv", np.array(tsne_latent), delimiter=',')
good_perts.sort(key=lambda x: x[1], reverse=True)
# matrix = np.zeros((len(good_perts), len(good_perts)))
# for i in range(len(good_perts)):
# for j in range(len(good_perts)):
# a = cell_data.get_profile_cell_pert(cell_data.test_data, cell_data.test_meta, "MCF7",
# good_perts[i][0])
# b = cell_data.get_profile_cell_pert(cell_data.test_data, cell_data.test_meta, "PC3",
# good_perts[j][0])
# if a is None or b is None:
# continue
# vector1 = encoder.predict(np.asarray(a))
# vector2 = encoder.predict(np.asarray(b))
# vpcc = stats.pearsonr(vector1.flatten(), vector2.flatten())[0]
# matrix[i][j] = vpcc
# for i in range(len(good_perts)):
# good_perts[i] = df.query('pert_id=="'+str(good_perts[i][0]) + '"')["pert_iname"].tolist()[0]
# df1 = pd.DataFrame(data=matrix, index=good_perts, columns=good_perts)
# df1.to_pickle("../figures_data/latent.p")
print(" Done")
with open("log.txt", 'a+') as f:
for key, value in results.items():
if key == "count":
continue
f.write(key + str(value / results["count"]))
f.write("\n")
performance = str(results["Our performance is: "] / results["count"]) + "\t" + \
str(results["Our correlation is: "] / results["count"]) + "\t" + \
str(results["Our multi-performance is: "] / results["count"]) + "\t" + \
str(results["Our multi-correlation is: "] / results["count"]) + "\t" + \
str(results["closest profile: "] / results["count"]) + "\t" + \
str(results["closest profile correlation is: "] / results["count"]) + "\t" + \
str(results["Baseline correlation (mean profile): "] / results["count"]) + "\t" + \
str(results["Baseline performance (mean profile): "] / results["count"])
with open("final_result.tsv", 'a+') as f:
f.write(str(latent_dim) + "\t" + performance) # str(tr_size) + "\t" +
f.write("\n")
with open("all_results", 'a+') as f:
f.write("\n".join(all_results))
f.write("\n")