-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsynthetic.py
executable file
·197 lines (162 loc) · 5.92 KB
/
synthetic.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
#!/usr/bin/env python3
"""Creates datasets for simulations using purified data."""
import argparse
import os
import pathlib
import random
import numpy as np
import scipy.io
import scipy.sparse
POPULATION = [
"b_cells",
"cd4_t_helper",
"naive_t",
"cd14_monocytes",
"cd56_nk",
"memory_t",
"regulatory_t",
"cd34",
"cytotoxic_t",
"naive_cytotoxic"
]
def main():
"""Creates datasets for simulations using purified data."""
parser = argparse.ArgumentParser(description="Generate simulations")
parser.add_argument("-s", "--seed", type=int, default=0,
help="seed for RNG")
parser.add_argument("-v", "--variation", type=str, default="0.0",
help="variation parameter (float)")
parser.add_argument("--split", type=str, default="0.0",
help="split parameter (float)")
parser.add_argument("-n", "--examples", type=int, default=200,
help="number of examples")
parser.add_argument("--interaction", action="store_true", default=False,
help="interaction between two types")
args = parser.parse_args()
# Load purified data
counts = []
for p in POPULATION:
c = scipy.sparse.load_npz(os.path.join("data", "purified", "{}.npz".format(p))).transpose()
mean = np.mean(c, axis=1)
# Faster version than running
# >> c -= mean
# >> c = scipy.sparse.csr_matrix(np.maximum(c, 0))
# https://stackoverflow.com/questions/39685168/scipy-sparse-matrix-subtract-row-mean-to-nonzero-elements
d = scipy.sparse.diags(np.array(mean).squeeze(1), 0)
ones = c.copy()
ones.data = np.ones_like(ones.data)
x = c - (d * ones)
x.data[x.data < 0] = 0
counts.append(c)
# Generate splits of cells
count_train = []
count_test = []
random.seed(args.seed)
np.random.seed(args.seed)
for c in counts:
index = np.arange(np.shape(c)[0])
np.random.shuffle(index)
c = c[index, :]
count_train.append(c[:np.shape(c)[0] // 2, :])
count_test.append(c[np.shape(c)[0] // 2:, :])
# Create example patients, and save each patient to a file
if args.interaction:
path = os.path.join("data", "interaction",
"{}_{}_{}".format(args.variation, args.examples, args.seed))
create = lambda disease, c: create_interaction(disease, c, float(args.variation))
generate(path,
create,
count_train,
count_test,
args.examples)
else:
path = os.path.join("data", "signature",
"{}_{}_{}_{}".format(args.variation, args.split,
args.examples, args.seed))
create = lambda disease, c: create_signature(disease, c,
float(args.variation), float(args.split))
generate(path,
create,
count_train,
count_test,
args.examples)
def generate(root, create_example, count_train, count_test, n_examples=100):
"""Creates simulated patients."""
pathlib.Path(root).mkdir(parents=True, exist_ok=True)
for d in ["Disease", "Healthy", "Test"]:
pathlib.Path(os.path.join(root, d)).mkdir(parents=True, exist_ok=True)
for i in range(n_examples):
if d == "Disease":
disease = True
elif d == "Healthy":
disease = False
elif d == "Test":
disease = random.random() < 0.5
else:
raise NotImplementedError()
if d == "Test":
c = count_test
else:
c = count_train
example = create_example(disease, c)
if isinstance(example, np.ndarray):
example = scipy.sparse.csr_matrix(example)
counts = scipy.sparse.save_npz(
os.path.join(root, d, "{}_{}".format(d.lower(), i)), example)
def create_signature(disease, c, variation, split):
"""Creates patient with a mixed signature."""
cells = 1000
def r(low, high):
return variation * random.uniform(low, high) + (1 - variation) * (high + low) / 2.
p = np.zeros(len(POPULATION))
p[0] = r(4, 69) # bcell
p[1] = r(0, 49) # mono
p[2] = r(0, 50)
p[3] = r(22, 93) # cd4
p[4] = r(0, 59) # nk cells
p[5] = r(0, 50)
p[6] = r(0, 50)
p[7] = r(6, 65) # CD8 cytotoxic
p[8] = r(0, 50)
p[9] = r(0, 50)
if disease:
if random.random() < split:
p[3] = random.uniform(0, 22)
else:
p[3] = random.uniform(93, 115)
p /= sum(p)
pop = np.random.choice(len(POPULATION), size=cells, p=p)
return scipy.sparse.vstack(
[c[p][random.randint(0, c[p].shape[0] - 1), :] for p in pop]
).transpose()
def create_interaction(disease, c, variation):
"""Creates patient with interactions between cell types."""
cells = 1000
def r(low, high):
return variation * random.uniform(low, high) + (1 - variation) * (high + low) / 2.
p = np.zeros(len(POPULATION))
p[0] = r(4, 69) # bcell
p[1] = r(0, 49) # mono
p[2] = r(0, 50)
p[3] = r(22, 93) # cd4
p[4] = r(0, 59) # nk cells
p[5] = r(0, 50)
p[6] = r(0, 50)
p[7] = r(6, 65) # CD8 cytotoxic
p[8] = r(0, 50)
p[9] = r(0, 50)
if disease:
p[1] = random.uniform(49, 69)
p[3] = random.uniform(93, 115)
else:
r = random.random()
if r < 1. / 3:
p[1] = random.uniform(49, 69)
elif r < 2. / 3:
p[3] = random.uniform(93, 115)
p /= sum(p)
pop = np.random.choice(len(POPULATION), size=cells, p=p)
return scipy.sparse.vstack(
[c[p][random.randint(0, c[p].shape[0] - 1), :] for p in pop]).transpose()
if __name__ == "__main__":
main()