-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.py
147 lines (116 loc) · 3.41 KB
/
example.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
from cplvm import CPLVM, CPLVMLogNormalApprox
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib
font = {"size": 30}
matplotlib.rc("font", **font)
matplotlib.rcParams["text.usetex"] = True
############ Load data ############
X = pd.read_csv("./data/toy/toy_background.csv", header=None).values
Y = pd.read_csv("./data/toy/toy_foreground.csv", header=None).values
n, m = X.shape[0], Y.shape[0]
assert X.shape[1] == Y.shape[1]
p = X.shape[1]
# Plot the data
plt.figure(figsize=(12, 7))
plt.scatter(X[:, 0], X[:, 1], label="Background", color="gray", alpha=0.4)
plt.scatter(
Y[: m // 2, 0], Y[: m // 2, 1], label="Foreground group 1", color="green", alpha=0.4
)
plt.scatter(
Y[m // 2 :, 0],
Y[m // 2 :, 1],
label="Foreground group 2",
color="orange",
alpha=0.4,
)
plt.legend(bbox_to_anchor=(1.2, 1.05), fontsize=20)
plt.xlabel("Gene 1")
plt.ylabel("Gene 2")
plt.title("Toy data")
plt.tight_layout()
plt.savefig("./experiments/simulation_experiments/toy_example/out/toy_data.png")
# plt.show()
plt.close()
############ Fit CPLVM ############
# Set up CPLVM
cplvm = CPLVM(
k_shared=1,
k_foreground=2,
compute_size_factors=True,
offset_term=False)
# Set up approximate model
approx_model = CPLVMLogNormalApprox(
X.T,
Y.T,
k_shared=1,
k_foreground=2,
compute_size_factors=True,
offset_term=False
)
# Fit model
model_output = cplvm.fit_model_vi(
X.T,
Y.T,
approximate_model=approx_model,
)
## Extract parameters
# Foreground-specific loadings
W_mean = model_output["approximate_model"].qw_mean.numpy()
W_stddev = model_output["approximate_model"].qw_stddv.numpy()
W = np.exp(W_mean + W_stddev ** 2)
# Shared loadings
S_mean = model_output["approximate_model"].qs_mean.numpy()
S_stddev = model_output["approximate_model"].qs_stddv.numpy()
S = np.exp(S_mean + S_stddev ** 2)
############ Visualize result ############
## Plot results
plt.figure(figsize=(12, 7))
# Plot data
plt.scatter(X[:, 0], X[:, 1], label="Background", color="gray", alpha=0.4)
plt.scatter(
Y[: m // 2, 0], Y[: m // 2, 1], label="Foreground group 1", color="green", alpha=0.4
)
plt.scatter(
Y[m // 2 :, 0],
Y[m // 2 :, 1],
label="Foreground group 2",
color="orange",
alpha=0.4,
)
axes = plt.gca()
xlims = np.array(axes.get_xlim())
ylims = np.array(axes.get_ylim())
# Plot S
X_mean = np.mean(X, axis=0)
S_slope = S[1, 0] / S[0, 0]
S_intercept = X_mean[1] - X_mean[0] * S_slope
x_vals = np.linspace(xlims[0], xlims[1], 100)
y_vals = S_slope * x_vals + S_intercept
plt.plot(x_vals, y_vals, "--", label="S", color="black", linewidth=3)
# Plot W1
Y_mean = np.mean(Y, axis=0)
W_slope = W[1, 0] / W[0, 0]
W_intercept = Y_mean[1] - Y_mean[0] * W_slope
y_vals = np.linspace(ylims[0], ylims[1], 100)
y_vals = W_slope * x_vals + W_intercept
plt.plot(x_vals, y_vals, "--", label="W1", color="red", linewidth=3)
# Plot W2
W_slope = W[1, 1] / W[0, 1]
W_intercept = Y_mean[1] - Y_mean[0] * W_slope
ylims = np.array(axes.get_ylim())
y_vals = np.linspace(ylims[0], ylims[1], 100)
y_vals = W_slope * x_vals + W_intercept
plt.plot(x_vals, y_vals, "--", label="W2", color="blue", linewidth=3)
plt.xlim(xlims)
plt.ylim(ylims)
plt.legend(bbox_to_anchor=(1.2, 1.05), fontsize=20)
plt.xlabel("Gene 1")
plt.ylabel("Gene 2")
plt.title("CPLVM")
plt.tight_layout()
plt.savefig("./experiments/simulation_experiments/toy_example/out/cplvm_toy.png")
plt.show()
import ipdb
ipdb.set_trace()