Skip to content

Commit b530081

Browse files
committed
completed restructure
1 parent 6441bed commit b530081

17 files changed

Lines changed: 1242 additions & 7795 deletions

examples/examples-mcmc-maria.ipynb

Lines changed: 0 additions & 920 deletions
This file was deleted.

examples/examples-mcmc-ode.ipynb

Lines changed: 0 additions & 554 deletions
This file was deleted.

examples/examples-mcmc.ipynb

Lines changed: 0 additions & 3324 deletions
This file was deleted.

examples/examples-sde-maria.ipynb

Lines changed: 0 additions & 787 deletions
This file was deleted.

examples/examples-sim-gLV/examples-ml-gLV.ipynb

Lines changed: 458 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "f07fa1f2-187e-4ce0-af95-31d6120977fe",
7+
"metadata": {
8+
"pycharm": {
9+
"name": "#%%\n"
10+
}
11+
},
12+
"outputs": [],
13+
"source": [
14+
"import pandas as pd\n",
15+
"import numpy as np\n",
16+
"import matplotlib.pyplot as plt\n",
17+
"from scipy.integrate import odeint\n",
18+
"from numpy import linalg as la\n",
19+
"\n",
20+
"import sklearn.linear_model\n",
21+
"from sklearn.linear_model import LinearRegression\n",
22+
"from sklearn.linear_model import Ridge\n",
23+
"from sklearn.linear_model import Lasso\n",
24+
"from sklearn.linear_model import ElasticNet, ElasticNetCV\n",
25+
"from sklearn.model_selection import RepeatedKFold\n",
26+
"from sklearn.model_selection import cross_val_score\n",
27+
"from sklearn.model_selection import KFold\n",
28+
"from sklearn.model_selection import GridSearchCV\n",
29+
"\n",
30+
"import sys\n",
31+
"sys.path.append(\"../../\")\n",
32+
"sys.path.append(\"../../gMLV\")\n",
33+
"\n",
34+
"from gMLV import *\n",
35+
"from utilities import plot_gLV\n",
36+
"from utilities import set_all_seeds\n"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "82eb9f01",
42+
"metadata": {
43+
"pycharm": {
44+
"name": "#%% md\n"
45+
}
46+
},
47+
"source": [
48+
"## Simulate some time course data from the gLV\n"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "fbc11bbc",
54+
"metadata": {
55+
"pycharm": {
56+
"name": "#%% md\n"
57+
}
58+
},
59+
"source": [
60+
"### Five species, single time course"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"id": "3e0845a5",
67+
"metadata": {
68+
"collapsed": false,
69+
"jupyter": {
70+
"outputs_hidden": false
71+
},
72+
"pycharm": {
73+
"name": "#%%\n"
74+
}
75+
},
76+
"outputs": [],
77+
"source": [
78+
"# In this example n >> p and it it is basically same as standard regression\n",
79+
"# We have to be careful as most of these gLV models are very weakly identifiable\n",
80+
"\n",
81+
"set_all_seeds(1234)\n",
82+
"\n",
83+
"## SETUP MODEL\n",
84+
"# establish size of model\n",
85+
"num_species = 5\n",
86+
"num_metabolites = 0\n",
87+
"\n",
88+
"# construct interaction matrix\n",
89+
"#TODO do this programmatically\n",
90+
"M = np.zeros((num_species, num_species))\n",
91+
"np.fill_diagonal(M, [-0.05, -0.1, -0.15, -0.01, -0.2])\n",
92+
"M[0, 2] = -0.025\n",
93+
"M[1, 3] = 0.05\n",
94+
"M[4, 0] = 0.02\n",
95+
"\n",
96+
"# construct growth rates matrix\n",
97+
"mu = np.random.lognormal(0.01, 0.5, num_species)\n",
98+
"\n",
99+
"# instantiate simulator\n",
100+
"simulator = gMLV_sim(num_species=num_species,\n",
101+
" num_metabolites=num_metabolites,\n",
102+
" M=M,\n",
103+
" mu=mu)\n",
104+
"simulator.print()\n",
105+
"\n",
106+
"## PRODUCE SIMULATED RESULTS\n",
107+
"# initial conditions\n",
108+
"init_species = 10 * np.ones(num_species)\n",
109+
"init_metabolites = 10 * np.ones(num_metabolites)\n",
110+
"\n",
111+
"times = np.arange(0, 5, 0.1)\n",
112+
"yobs, sobs, sy0, mu, M, _ = simulator.simulate(times=times, sy0=np.hstack((init_species, init_metabolites)))\n",
113+
"\n",
114+
"# add some gaussian noise\n",
115+
"yobs = yobs + np.random.normal(loc=0, scale=0.1, size=yobs.shape)\n",
116+
"sobs = sobs + np.random.normal(loc=0, scale=0.1, size=sobs.shape)\n",
117+
"\n",
118+
"# plot simulation\n",
119+
"plot_gLV(yobs, sobs, times)"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"id": "99ee1482",
125+
"metadata": {
126+
"pycharm": {
127+
"name": "#%% md\n"
128+
}
129+
},
130+
"source": [
131+
"### Five species, single time course, with a perturbation"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": null,
137+
"id": "cbab2390",
138+
"metadata": {
139+
"collapsed": false,
140+
"jupyter": {
141+
"outputs_hidden": false
142+
},
143+
"pycharm": {
144+
"name": "#%%\n"
145+
}
146+
},
147+
"outputs": [],
148+
"source": [
149+
"set_all_seeds(1234)\n",
150+
"\n",
151+
"## SETUP MODEL\n",
152+
"# establish size of model\n",
153+
"num_species = 5\n",
154+
"num_metabolites = 0\n",
155+
"\n",
156+
"# construct interaction matrix\n",
157+
"#TODO do this programmatically\n",
158+
"M = np.zeros((num_species, num_species))\n",
159+
"np.fill_diagonal(M, [-0.05, -0.1, -0.15, -0.01, -0.2])\n",
160+
"M[0, 2] = -0.025\n",
161+
"M[1, 3] = 0.05\n",
162+
"M[4, 0] = 0.02\n",
163+
"\n",
164+
"# construct growth rates matrix\n",
165+
"mu = np.random.lognormal(0.01, 0.5, num_species)\n",
166+
"\n",
167+
"# construct perturbation matrix\n",
168+
"epsilon = np.array([0, -1, 0, -1, 0])\n",
169+
"\n",
170+
"# instantiate simulator\n",
171+
"simulator = gMLV_sim(num_species=num_species,\n",
172+
" num_metabolites=num_metabolites,\n",
173+
" M=M,\n",
174+
" mu=mu,\n",
175+
" epsilon=epsilon)\n",
176+
"simulator.print()\n",
177+
"\n",
178+
"## PRODUCE SIMULATED RESULTS\n",
179+
"# initial conditions\n",
180+
"init_species = 10 * np.ones(num_species)\n",
181+
"init_metabolites = 10 * np.ones(num_metabolites)\n",
182+
"\n",
183+
"# perturbation\n",
184+
"tp = 2\n",
185+
"\n",
186+
"times = np.arange(0, 5, 0.1)\n",
187+
"yobs, sobs, sy0, mu, M, _ = simulator.simulate(times=times, \n",
188+
" sy0=np.hstack((init_species, init_metabolites)),\n",
189+
" tp=tp)\n",
190+
"\n",
191+
"\n",
192+
"# add some gaussian noise\n",
193+
"yobs = yobs + np.random.normal(loc=0, scale=0.1, size=yobs.shape)\n",
194+
"sobs = sobs + np.random.normal(loc=0, scale=0.1, size=sobs.shape)\n",
195+
"\n",
196+
"# plot simulation\n",
197+
"plot_gLV(yobs, sobs, times)"
198+
]
199+
}
200+
],
201+
"metadata": {
202+
"kernelspec": {
203+
"display_name": "Python 3",
204+
"language": "python",
205+
"name": "python3"
206+
},
207+
"language_info": {
208+
"codemirror_mode": {
209+
"name": "ipython",
210+
"version": 3
211+
},
212+
"file_extension": ".py",
213+
"mimetype": "text/x-python",
214+
"name": "python",
215+
"nbconvert_exporter": "python",
216+
"pygments_lexer": "ipython3",
217+
"version": "3.8.8"
218+
}
219+
},
220+
"nbformat": 4,
221+
"nbformat_minor": 5
222+
}

examples/examples-sim-gMLV/examples-ml-gMLV.ipynb

Lines changed: 306 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "f07fa1f2-187e-4ce0-af95-31d6120977fe",
7+
"metadata": {
8+
"pycharm": {
9+
"name": "#%%\n"
10+
}
11+
},
12+
"outputs": [],
13+
"source": [
14+
"import pandas as pd\n",
15+
"import numpy as np\n",
16+
"import matplotlib.pyplot as plt\n",
17+
"from scipy.integrate import odeint\n",
18+
"from numpy import linalg as la\n",
19+
"\n",
20+
"import sklearn.linear_model\n",
21+
"from sklearn.linear_model import LinearRegression\n",
22+
"from sklearn.linear_model import Ridge\n",
23+
"from sklearn.linear_model import Lasso\n",
24+
"from sklearn.linear_model import ElasticNet, ElasticNetCV\n",
25+
"from sklearn.model_selection import RepeatedKFold\n",
26+
"from sklearn.model_selection import cross_val_score\n",
27+
"from sklearn.model_selection import KFold\n",
28+
"from sklearn.model_selection import GridSearchCV\n",
29+
"\n",
30+
"import sys\n",
31+
"sys.path.append(\"../../\")\n",
32+
"sys.path.append(\"../../gMLV\")\n",
33+
"\n",
34+
"from gMLV import *\n",
35+
"from utilities import plot_gMLV\n",
36+
"from utilities import set_all_seeds\n"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "82eb9f01",
42+
"metadata": {
43+
"pycharm": {
44+
"name": "#%% md\n"
45+
}
46+
},
47+
"source": [
48+
"## Simulate some time course data from the gMLV\n",
49+
"\n",
50+
"### Five species, six metabolites, single time course"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"id": "4501a3a3",
56+
"metadata": {
57+
"pycharm": {
58+
"name": "#%% md\n"
59+
}
60+
},
61+
"source": [
62+
"This model assumes metabolite production is associated with abundance: dS/dt = alpha X <br>\n",
63+
"Note that this model needs rethinking as it cannot handle negative productivities <br>\n",
64+
"In this simple example we don't need to infer the time course. We just linearize and estimate the elements of alpha with Lasso<br>\n",
65+
"Number of metabolites is 6 here"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "9d348926",
72+
"metadata": {
73+
"collapsed": false,
74+
"jupyter": {
75+
"outputs_hidden": false
76+
},
77+
"pycharm": {
78+
"name": "#%%\n"
79+
}
80+
},
81+
"outputs": [],
82+
"source": [
83+
"# Simulate some microbiota and metabolites\n",
84+
"set_all_seeds(1234)\n",
85+
"\n",
86+
"# SETUP MODEL\n",
87+
"# establish size of model\n",
88+
"num_species = 5\n",
89+
"num_metabolites = 6\n",
90+
"\n",
91+
"# construct interaction matrix\n",
92+
"#TODO do this programmatically\n",
93+
"M = np.zeros((num_species, num_species))\n",
94+
"np.fill_diagonal(M, [-0.05, -0.1, -0.15, -0.01, -0.2])\n",
95+
"# M[0,2] = -0.025\n",
96+
"M[1, 3] = 0.05\n",
97+
"# M[4,0] = 0.02\n",
98+
"\n",
99+
"# construct growth rates matrix\n",
100+
"mu = np.random.lognormal(0.01, 0.5, num_species)\n",
101+
"\n",
102+
"# construct metabolite production matrix\n",
103+
"alpha = np.zeros((num_metabolites, num_species))\n",
104+
"alpha[1, 4] = 1\n",
105+
"alpha[4, 2] = -0.5\n",
106+
"\n",
107+
"# instantiate simulator\n",
108+
"simulator = gMLV_sim(num_species=num_species,\n",
109+
" num_metabolites=num_metabolites,\n",
110+
" M=M,\n",
111+
" mu=mu,\n",
112+
" beta=alpha)\n",
113+
"simulator.print()\n",
114+
"\n",
115+
"## PRODUCE SIMULATED RESULTS\n",
116+
"# initial conditions\n",
117+
"init_species = 10 * np.ones(num_species)\n",
118+
"init_metabolites = 10 * np.ones(num_metabolites)\n",
119+
"\n",
120+
"times = np.arange(0, 5, 0.1)\n",
121+
"yobs, sobs, sy0, _, _, _ = simulator.simulate(times=times, sy0=np.hstack((init_species, init_metabolites)))\n",
122+
"\n",
123+
"# add some gaussian noise\n",
124+
"yobs = yobs + np.random.normal(loc=0, scale=0.1, size=yobs.shape)\n",
125+
"sobs = sobs + np.random.normal(loc=0, scale=0.1, size=sobs.shape)\n",
126+
"\n",
127+
"# plot simulation\n",
128+
"plot_gMLV(yobs, sobs, times)\n"
129+
]
130+
}
131+
],
132+
"metadata": {
133+
"kernelspec": {
134+
"display_name": "Python 3",
135+
"language": "python",
136+
"name": "python3"
137+
},
138+
"language_info": {
139+
"codemirror_mode": {
140+
"name": "ipython",
141+
"version": 3
142+
},
143+
"file_extension": ".py",
144+
"mimetype": "text/x-python",
145+
"name": "python",
146+
"nbconvert_exporter": "python",
147+
"pygments_lexer": "ipython3",
148+
"version": "3.8.8"
149+
}
150+
},
151+
"nbformat": 4,
152+
"nbformat_minor": 5
153+
}

0 commit comments

Comments
 (0)