-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_reaction_system.py
90 lines (69 loc) · 2.61 KB
/
create_reaction_system.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
import numpy as np
import matplotlib
matplotlib.use('Agg')
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
import os
plt.ion()
plt.clf()
def create_change_vector(species, rule, p):
change_vector = [0]*len(species)
for i, (s, p_i) in enumerate(species):
if p_i == p and s == rule[0]:
change_vector[i] = -1
elif p_i == p and s == rule[1]:
change_vector[i] = 1
return change_vector
def compute_mean_rate(graph, node, rule, partitioning, species):
p = partitioning[node]
current_species = (rule[0], p)
current_species_i = species.index(current_species)
nodes_in_p = partitioning.count(p)
func = '(species[{current_species_i}]/{nodes_in_p})'
func = func.format(current_species_i=current_species_i, nodes_in_p=nodes_in_p)
return func
def create_propensity(species, rule, graph, partitioning, p):
current_species = (rule[0], p)
current_species_i = species.index(current_species)
func = '0'
for node in graph:
if partitioning[node] != p:
continue
mean_rate_current_node = compute_mean_rate(graph, node, rule, partitioning, species)
func += '+'
func += mean_rate_current_node
return func
# TODO prob that node itself is in state
def create_reaction(species, rule, graph, partitioning, p):
change_vector = create_change_vector(species, rule, p)
propensity_function = create_propensity(species, rule, graph, partitioning, p)
return (change_vector, propensity_function)
def write_output(species, reactions, outpath):
df = {'change_vector': [x[0] for x in reactions], 'propensity': [x[1] for x in reactions]}
df = pd.DataFrame(df)
df.to_csv(outpath, sep='\t', index=False)
def main_node_exact(graph, partitioning, states, rules, outpath):
graph = nx.convert_node_labels_to_integers(graph)
species = list()
reactions = list()
partition_list = sorted(list(set(partitioning)))
for s in states:
for p in partition_list:
species.append((s,p))
for rule in rules:
for p in partition_list:
r = create_reaction(species, rule, graph, partitioning, p)
reactions.append(r)
print(species)
print(reactions)
write_output(species, reactions, outpath)
pass
if __name__ == "__main__":
graph = nx.Graph([(0,1),(1,2),(2,3), (3,0)])
partitioning = [0,1,2,2]
states = ['S', 'I']
rules = [('S', 'I', lambda x: x['I'] * 1.0), ('I','S',lambda x : .5)]
outpath = 'reaction_system.csv'
main_node_exact(graph, partitioning, states, rules, outpath)