-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell_bio.py
More file actions
471 lines (365 loc) · 19.8 KB
/
cell_bio.py
File metadata and controls
471 lines (365 loc) · 19.8 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# General importations.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import string
from random import shuffle,seed,choices
from faker import Faker
from faker.providers.person.en import Provider
import networkx as nx
import itertools
# Custom scripts.
from utils import Utils
from task_generator import TaskGenerator
from dataset_generator import DataSetGenerator
class CellBio(TaskGenerator):
'''
Generates compositional causal reasoning tasks.
Estimands: ATE, path-specific effects, direct effect, indirect effect
SCM: linear causal functions, Gaussian noise
'''
def set_params(self):
self.params = np.random.randint(low = 1, high = 5, size = len(self.nodes))
def get_dag(self,
n_per_bcc: list = [[2,2,2],[2,2,2],[2,2,2]],
bcc_types: list = [["cycle"]*3,["cycle"]*3,["cycle"]*3],
label_seed: int = None,
plot: bool = True) -> nx.classes.graph.Graph:
'''
Construct a directed acyclic graph (DAG) with exactly one root, exaclty one leaf,
varying numbers of biconnected components (BCCs), and varying numbers of nodes in
each BCC.
Params:
- n_per_bcc: list of lists containing number of nodes per BCC. Length of n_per_bcc
corresponds to how many "arms" of indirect paths go from root to leaf, while the
length of each item in n_per_bcc corresponds to the number of BCCs per "arm". If
n_per_bcc[0][j] == 2 for all j, this means that arm 0 is just a simple directed
path from root to leaf.
- bcc_types: list of graph structure type for each BCC with options
"cycle" (nx.cycle_graph) and "wheel" (nx.wheel_graph).
- label_seed: random seed for name generator, if desired.
- plot: show plot of DAG.
Notes:
1. n_per_bcc[i] >= 2.
2. If n_per_bcc[i] == 2, bcc[i] will be a bridge.
3. len(n_per_bcc) must equal len(bcc_types).
Return: networkx digraph
'''
if len(n_per_bcc) != len(bcc_types):
raise Exception("len(n_per_bcc) must be equal to len(bcc_types).")
self.subgraph_dict = dict()
root_name = ''.join(choices(string.ascii_uppercase+string.digits, k=4))
leaf_name = ''.join(choices(string.ascii_uppercase+string.digits, k=4))
for j in range(len(n_per_bcc)):
# Construct first BCC.
if bcc_types[j][0] == "cycle":
dag = nx.cycle_graph(n = n_per_bcc[j][0])
elif bcc_types[j][0] == "wheel":
dag = nx.wheel_graph(n = n_per_bcc[j][0])
# Convert adjacency matrix to upper triangular to get DAG.
adj = nx.to_numpy_array(dag)
adj = np.triu(adj)
dag = nx.from_numpy_array(adj)
# Get leaf.
row_sums = adj.sum(axis = 1)
leaf_idx = np.where(row_sums == 0)[0]
# Add remaining BCCs.
bccs = []
for i in range(1,len(n_per_bcc[j])):
if bcc_types[j][i] == "cycle":
g = nx.cycle_graph(n = n_per_bcc[j][i])
elif bcc_types[j][i] == "wheel":
g = nx.wheel_graph(n = n_per_bcc[j][i])
adj = nx.to_numpy_array(g)
adj = np.triu(adj)
g = nx.from_numpy_array(adj)
g = nx.relabel_nodes(g, dict(zip(list(g.nodes), [x+(len(dag.nodes)-1) for x in g.nodes])))
dag = nx.relabel_nodes(dag, { n: str(n) if n==leaf_idx else 'a-'+str(n) for n in dag.nodes })
g = nx.relabel_nodes(g, { n: str(n) if n==leaf_idx else 'b-'+str(n) for n in g.nodes })
dag = nx.compose(dag,g)
adj = nx.to_numpy_array(dag)
adj = np.triu(adj)
dag = nx.relabel_nodes(dag, dict(zip(list(dag.nodes), range(len(dag.nodes)))))
row_sums = adj.sum(axis = 1)
leaf_idx = np.where(row_sums == 0)[0]
# Make acyclic, add edge weights, and add random node names.
cyclic_dict = nx.to_dict_of_dicts(dag)
acyclic_dict = dict()
coeff = lambda : round(np.random.uniform(low = 0.2, high = 4.5, size = 1).item(),1)
for parent,children in cyclic_dict.items():
child_dict = dict()
for child,weight in children.items():
if child > parent:
child_dict[child] = {"weight": coeff()}
acyclic_dict[parent] = child_dict
dag = nx.from_dict_of_dicts(acyclic_dict, create_using = nx.DiGraph)
if label_seed is not None:
seed(label_seed)
labels = [''.join(choices(string.ascii_uppercase+string.digits, k=4)) for _ in range(len(dag.nodes)-2)]
labels = [root_name]+labels+[leaf_name]
dag = nx.relabel_nodes(dag, dict(zip(dag.nodes,labels)))
self.subgraph_dict[j] = dag
#self.utils.plot_nx(nx.to_numpy_array(dag),
# labels = list(dag.nodes),
# figsize = (7,7),
# dpi = 50,
# node_size = 1500,
# arrow_size = 20)
#e_w = nx.get_edge_attributes(dag,"weight")
#nx.draw_networkx_edge_labels(dag,
# pos = nx.circular_layout(dag),
# edge_labels = e_w)
#plt.show()
#plt.close()
# Compose "arms" into full DAG.
dag = nx.compose(self.subgraph_dict.get(0),self.subgraph_dict.get(1))
if len(self.subgraph_dict.keys()) > 2:
for k in range(2,len(self.subgraph_dict.keys())):
dag = nx.compose(dag,self.subgraph_dict.get(k))
# Add direct edge from root to leaf.
dag.add_edge(root_name, leaf_name, weight = coeff())
self.direct_effects = nx.get_edge_attributes(dag,"weight")
if plot:
self.utils.plot_nx(nx.to_numpy_array(dag),
labels = list(dag.nodes),
figsize = (7,7),
dpi = 50,
node_size = 1500,
arrow_size = 20)
return dag
def get_cct(self,
plot: bool = True) -> nx.classes.graph.Graph:
self.cct = dict()
self.adj_cct = dict()
for key,cct_sort in self.cct_sort.items():
adj_cct = np.triu(np.ones((len(cct_sort),len(cct_sort))),k = 1)
adj_cct = adj_cct.astype(int)
cct = nx.from_numpy_array(adj_cct, create_using = nx.DiGraph)
cct = nx.relabel_nodes(cct, dict(zip(cct.nodes,cct_sort)))
self.adj_cct[key] = adj_cct
self.cct[key] = cct
if plot:
self.utils.plot_nx(nx.to_numpy_array(self.cct.get(key)),
labels = list(self.cct.get(key).nodes),
figsize = (7,7),
dpi = 50,
node_size = 1500,
arrow_size = 20)
return self.cct
def get_cct_all_paths(self) -> list:
'''
Getter for composition cause-effect pairs for inductive CCR evaluation
using Algorithm 1 / Theorem 1.
Input is commutative cut tree (CCT), not the original causal DAG.
'''
self.path_dict = dict()
for key,subgraph in self.cct.items():
self.path_dict[key] = list(nx.all_simple_paths(subgraph, self.root, self.leaf))
return self.path_dict
def get_cutpoints(self,
dag: nx.classes.graph.Graph,
topological_sort: bool = True) -> list:
'''
Getter for a topological sort of cutpoints.
'''
self.cutpoint_dict = dict()
for key,subgraph in self.subgraph_dict.items():
self.cutpoint_dict[key] = self._get_cutpoints(dag = subgraph,
topological_sort = topological_sort)
return self.cutpoint_dict
def get_cct_sort(self):
self.cct_sort = dict()
for key,nodes in self.cutpoint_dict.items():
self.cct_sort[key] = [self.root] + nodes + [self.leaf]
return self.cct_sort
def get_cause_effect_pairs(self,
dag: nx.classes.graph.Graph) -> list:
'''
Getter for all relevant cause-effect pairs for inductive CCR evaluation
using Algorithm 1 / Theorem 1.
'''
combos = list(itertools.combinations(nx.topological_sort(dag),2))
# This step appears redundant, but I will keep this just in case
# the apparent sorting by itertools is not consistent.
cause_effect_pairs = [x for x in combos if list(dag.nodes).index(x[0]) < list(dag.nodes).index(x[1])]
return cause_effect_pairs
def get_local(self) -> list:
'''
Getter for local quantity cause-effect pairs for inductive CCR evaluation
using Algorithm 1 / Theorem 1.
Returns a dictionary mapping subgraphs (each with their own CCT) to local quantities.
'''
self.local_dict = dict()
for key,cct in self.cct.items():
all_pairs = self.get_cause_effect_pairs(cct)
self.local_dict[key] = [x for x in all_pairs if x != (self.root, self.leaf)]
return self.local_dict
def get_compositions(self) -> list:
'''
Getter for composition cause-effect pairs for inductive CCR evaluation
using Algorithm 1 / Theorem 1.
'''
self.get_cct_all_paths()
self.comp_dict = dict()
for key,subgraph in self.cct.items():
compositions = []
paths = self.path_dict.get(key)
for path in paths:
comp = [(path[i],path[i+1]) for i in range(len(path)-1)]
if len(comp) >= 2:
compositions.append(comp)
self.comp_dict[key] = compositions
return self.comp_dict
def get_causal_context(self) -> str:
'''
Define causal model in text.
'''
# Set coefficients for structural equations.
self.set_params()
# Get variable metadata for context prompt.
self.var_dict = dict()
self.cell_type = ''.join(choices(string.ascii_uppercase+string.digits, k=6))
self.organism = ''.join(choices(string.ascii_uppercase+string.digits, k=6))
self.compound = ''.join(choices(string.ascii_uppercase+string.digits, k=6))
#exog = ["enzyme"]*len(self.nodes)
#endog = ["mRNA"]*len(self.nodes)
#units = "pg" #(picograms)
#units = "g/mL"
for var,u in zip(self.nodes,self.exog_names):
parents = self.get_parents(var, return_idx = False)
self.var_dict[var] = {"parents": parents,
"endog type": "mRNA",
"exog var name": u,
"exog type": "enzyme"}
'''
A cellular biologist is studying the impacts of exposure to compound {} on
transcription and translation in cell type {} of organism {}. When stilumated with compound {},
cell type {} will produce mRNA transcripts for gene {} at {} times
the current volume of enzyme {}. The cell will produce mRNA transcripts for gene {} at {} times the
current volume of enzyme {} plus {} times the current volume of {} transcripts. The cell will produce
mRNA transcripts for gene {} at {} times the current volume of enzyme {} plus {} times the current
volume of {} transcripts plus {} times the current volume of {} transcripts. The total volume of protein
{} will be three times the current volume of {} transcripts plus the current volume of enzyme {}.
Do this for all paths from root to leaf.
Assume that all factorus influencing the transcription and translation of these
macromolecules are described here.
At the time of the experiment, the biologist measures {} pg of enzyme {}, {} pg enzyme {}, etc... How
much protein {} will be present in the cell?
Now suppose that the biologist can artificially induce the cell to produce more/less of a
specific transcript, and now X pg of transcript J are present in the cell regardless of all
other circumstances. With this new assumption, how much protein will be present in the cell?
'''
# Construct prompt.
intro = "A cellular biologist is studying the impacts of exposure to compound {}".format(self.compound)
intro += " on transcription and translation in cell type {} of organism {}.".format(self.cell_type,self.organism)
intro += " When stilumated with compound {}, cell type {}".format(self.compound,self.cell_type)
outro = "Assume that all factors influencing the transcription and translation of"
outro += " these molecules are fully described here."
strngs = [intro]
for var,terms in self.var_dict.items():
parents = terms.get("parents")
exog = "the patient "+terms.get("exog type")+" "+terms.get("exog var name")
if len(parents) > 0:
strng = " When stimulated with compound {}, cell type {}".format(self.compound,self.cell_type)
strng += " will produce mRNA transcripts for gene {}".format(var)
strng += " equal in volume to the current volume of enzyme {}.".format(terms.get("exog var name"))
else:
parent_strngs = []
strng = "The cell will produce mRNA transcripts for gene {}".format(var)
for parent in parents:
parent_strngs.append(" at {} times the current volume of {} transcripts".format(self.direct_effects.get((parent,var)),
parent))
parent_strngs.append("the current volume of enzyme {}".format(terms.get("exog var name")))
parent_strngs = " plus ".join(parent_strngs)+"."
strng += parent_strngs
strngs.append(strng)
strngs.append(outro)
self.causal_context = " ".join(strngs)
return self.causal_context
def get_sample_context(self,
n_extra_vars: int = 5) -> str:
'''
Sample exogenous variables and construct text prompt.
'''
# Get extraneous details.
self.extra_names = [''.join(choices(string.ascii_uppercase+string.digits, k=4)) for _ in range(n_extra_vars)]
# Get observed quantities.
self.exog_obs = [np.random.uniform(low = 0.1, high = 3.0, size = 1).item() for _ in range(len(self.nodes))]
self.extra_obs = [np.random.uniform(low = 0.1, high = 3.0, size = 1).item() for _ in range(len(self.nodes))]
# Construct context.
self.sample_context = "At the time of the experiment, the biologist measures"
volumes = [str(pg)+" pg of enzyme "+u for pg,u in zip(self.exog_obs,self.exog_names)]
volumes += [str(pg)+" pg of enzyme "+u for pg,u in zip(self.extra_obs,self.extra_names)]
shuffle(volumes)
self.sample_context += ", ".join(volumes[:-1])
self.sample_context += " and "+volumes[-1]+"."
return self.sample_context
def get_factual_queries(self) -> dict:
'''
Returns a dictionary of all causal queries of interest mapped to their
corresponding factual text prompts.
'''
self.f_query_dict = dict()
outro = " Begin your response with Yes or No and be as concise as possible."
for pair in [self.global_quantity]+self.local:
effect = pair[1]
if effect != "surgery":
q = "Given these history and physical notes, will {} {} be {}?".format(self.var_dict.get(effect).get("endog type"),
effect,
self.var_dict.get(effect).get("endog magnitude"))
else:
q = "Given these history and physical notes, will the surgeon recommend surgery?"
true_all = dict(zip(self.nodes,self.get_truth(intervene_node = None)))
true_exog = dict(zip(self.exog_names,self.exog_true_binary))
true_response = true_all.get(effect)
self.f_query_dict[effect] = {"Prompt": q+outro,
"True endogenous": true_all,
"True exogenous": true_exog,
"True response": true_response}
return self.f_query_dict
def get_counterfactual_queries(self) -> dict:
'''
Returns a dictionary of all causal queries of interest mapped to their
corresponding counterfactual text prompts (for intervention = 0 and = 1).
'''
if self.f_query_dict is None:
_ = self.get_factual_queries()
self.cf_0_query_dict = dict()
self.cf_1_query_dict = dict()
for pair in [self.global_quantity]+self.local:
cause, effect = pair[0], pair[1]
cause_type = self.var_dict.get(cause).get("endog type")
effect_type = self.var_dict.get(effect).get("endog type")
effect_mag = self.var_dict.get(effect).get("endog magnitude")
cf_1 = "be " + self.var_dict.get(cause).get("endog magnitude")
cf_0 = "not " + cf_1
if effect == "surgery":
outro_a = " With this new assumption, will the surgeon recommend surgery?"
else:
outro_a = " With this new assumption, will {} {} be {}?".format(effect_type, effect, effect_mag)
outro_b = " Begin your response with Yes or No and be as concise as possible."
# Query under counterfactual cause = True.
if cause == "pain":
q_1 = "Now suppose that the patient will be in significant pain regardless of all other circumstances."
else:
q_1 = "Now suppose that {} {} will {} regardless of all other circumstances.".format(cause_type,cause,cf_1)
true_all = dict(zip(self.nodes,self.get_truth(intervene_node = cause, intervene_value = 1)))
true_exog = dict(zip(self.exog_names,self.exog_true_binary))
true_response = true_all.get(effect)
self.cf_1_query_dict[pair] = {"Prompt": q_1 + outro_a + outro_b,
"True endogenous": true_all,
"True exogenous": true_exog,
"True response": true_response}
# Query under counterfactual cause = False.
if cause == "pain":
q_0 = "Now suppose that the patient will not be in pain regardless of all other circumstances."
else:
q_0 = "Now suppose that {} {} will {} regardless of all other circumstances.".format(cause_type,cause,cf_0)
true_all = dict(zip(self.nodes,self.get_truth(intervene_node = cause, intervene_value = 0)))
true_response = true_all.get(effect)
self.cf_0_query_dict[pair] = {"Prompt": q_0 + outro_a + outro_b,
"True endogenous": true_all,
"True exogenous": true_exog,
"True response": true_response}
return self.cf_1_query_dict, self.cf_0_query_dict