Skip to content

Commit 3a01346

Browse files
authored
Merge pull request #98 from ucl-cssb/examples-README
create a README for examples and update website
2 parents e08c0e6 + 7578213 commit 3a01346

9 files changed

Lines changed: 212 additions & 46 deletions

File tree

.github/workflows/python-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
continue-on-error: true # In case there are no changes
7676

7777
- name: Upload updated requirements.txt
78-
uses: actions/upload-artifact@v3
78+
uses: actions/upload-artifact@v4
7979
with:
8080
name: requirements
8181
path: requirements.txt
@@ -93,7 +93,7 @@ jobs:
9393
- uses: actions/checkout@v2
9494

9595
- name: Download updated requirements.txt
96-
uses: actions/download-artifact@v3
96+
uses: actions/download-artifact@v4
9797
with:
9898
name: requirements
9999

.github/workflows/test-and-coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
4949
# Upload the coverage report as an artifact
5050
- name: Upload coverage report
51-
uses: actions/upload-artifact@v3
51+
uses: actions/upload-artifact@v4
5252
with:
5353
name: coverage-report
5454
path: htmlcov

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Development Lead
77
* Chris Barnes <christopher.barnes@ucl.ac.uk>
88
* Alex Fedorec <a.fedorec@ucl.ac.uk>
99
* Pedro Fontanarrosa <pfontanarrosa@gmail.com>
10+
* Chania Clare <chania.clare.21@ucl.ac.uk>
1011

1112
Contributors
1213
------------

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ For macOS and Ubuntu
102102
103103
5. **Run the Code**
104104

105-
Refer to the `Usage`_ section below for instructions on how to run the code.
105+
Refer to the Usage section below for instructions on how to run the code.
106106

107107
For Windows
108108
"""""""""""
@@ -136,7 +136,7 @@ For Windows
136136
137137
5. **Run the Code**
138138

139-
Refer to the `Usage`_ section below for instructions on how to run the code.
139+
Refer to the Usage section below for instructions on how to run the code.
140140

141141
Alternative Installation Using Pip Only
142142
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

docs/source/examples.rst

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
Examples
22
========
33

4-
.. toctree::
5-
:maxdepth: 2
4+
Below is an overview of the examples, organized by model type or analysis type.
65

7-
notebooks/CRM/examples-sim-CRM
8-
notebooks/gLV/examples-bayes-gLV
9-
notebooks/gLV/examples-lasso-gLV
10-
notebooks/gLV/examples-ridge-gLV
11-
notebooks/gLV/examples-Rutter-Dekker
12-
notebooks/gLV/examples-sim-gLV
13-
notebooks/gLV/examples-Stein
14-
notebooks/gMLV/examples-ridge-lasso-gMLV
15-
notebooks/gMLV/examples-sim-gMLV
16-
notebooks/GP/examples-impute-GP
17-
notebooks/GP/examples-impute-GP_Stein
18-
notebooks/MultiModel/Herold/examples-Herold-sVAR
19-
notebooks/MultiModel/Herold/examples-Herold-VAR
20-
notebooks/MultiModel/Herold/examples_impute_data
21-
notebooks/MVAR/examples-infer-MVAR
22-
notebooks/MVAR/examples-sim-MVAR
23-
notebooks/VAR/examples-bayes-VAR
24-
notebooks/VAR/examples-sim-VAR
6+
.. include:: ../../examples/README.rst
7+
:start-after: readme-begin
8+
:end-before: readme-end
9+
10+
.. include:: examples_auto.rst

docs/source/examples_auto.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Jupyter Notebook Examples by Model
2+
----------------------------------
3+
4+
CRM
5+
---
6+
7+
.. toctree::
8+
:maxdepth: 2
9+
10+
notebooks/CRM/examples-sim-CRM
11+
12+
GP
13+
--
14+
15+
.. toctree::
16+
:maxdepth: 2
17+
18+
notebooks/GP/examples-impute-GP
19+
notebooks/GP/examples-impute-GP_Stein
20+
21+
MVAR
22+
----
23+
24+
.. toctree::
25+
:maxdepth: 2
26+
27+
notebooks/MVAR/examples-infer-MVAR
28+
notebooks/MVAR/examples-sim-MVAR
29+
30+
MultiModel
31+
----------
32+
33+
.. toctree::
34+
:maxdepth: 2
35+
36+
notebooks/MultiModel/Herold/examples-Herold-VAR
37+
notebooks/MultiModel/Herold/examples-Herold-sVAR
38+
notebooks/MultiModel/Herold/examples_impute_data
39+
40+
VAR
41+
---
42+
43+
.. toctree::
44+
:maxdepth: 2
45+
46+
notebooks/VAR/examples-bayes-VAR
47+
notebooks/VAR/examples-sim-VAR
48+
49+
gLV
50+
---
51+
52+
.. toctree::
53+
:maxdepth: 2
54+
55+
notebooks/gLV/examples-Rutter-Dekker
56+
notebooks/gLV/examples-Stein
57+
notebooks/gLV/examples-bayes-gLV
58+
notebooks/gLV/examples-lasso-gLV
59+
notebooks/gLV/examples-ridge-gLV
60+
notebooks/gLV/examples-sim-gLV
61+
62+
gMLV
63+
----
64+
65+
.. toctree::
66+
:maxdepth: 2
67+
68+
notebooks/gMLV/examples-ridge-lasso-gMLV
69+
notebooks/gMLV/examples-sim-gMLV
70+

docs/source/generate_examples_rst.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,88 @@ def copy_notebooks(notebooks, examples_dir, target_dir):
2323

2424

2525
def find_notebooks(directory):
26+
"""
27+
Returns a list of notebook paths (with no file extension),
28+
relative to 'directory'. For example, if a notebook is:
29+
examples/gLV/examples-bayes-gLV.ipynb
30+
we return 'gLV/examples-bayes-gLV' (forward slashes).
31+
"""
2632
notebooks = []
2733
for root, _, files in os.walk(directory):
2834
for file in files:
2935
if file.endswith(".ipynb"):
3036
path = os.path.join(root, file)
3137
relative_path = os.path.relpath(path, start=directory)
32-
notebooks.append(relative_path.replace(
33-
os.path.sep, '/').replace('.ipynb', ''))
38+
notebooks.append(
39+
relative_path.replace(
40+
os.path.sep, '/').replace('.ipynb', '')
41+
)
3442
return notebooks
3543

3644

45+
def group_notebooks_by_top_dir(notebooks):
46+
"""
47+
Takes a list of relative notebook paths like ['gLV/examples-bayes-gLV', 'CRM/examples-sim-CRM']
48+
and returns a dict grouping them by the top-level folder:
49+
{
50+
'CRM': ['CRM/examples-sim-CRM'],
51+
'gLV': ['gLV/examples-bayes-gLV']
52+
...
53+
}
54+
"""
55+
grouped: dict[str, list[str]] = {}
56+
for nb in notebooks:
57+
parts = nb.split('/')
58+
top_dir = parts[0] # The directory before the first slash
59+
grouped.setdefault(top_dir, []).append(nb)
60+
return grouped
61+
62+
3763
def generate_rst(notebooks, target_dir, output_file):
38-
with open(output_file, 'w') as f:
39-
f.write("Examples\n")
40-
f.write("========\n\n")
41-
f.write(".. toctree::\n")
42-
f.write(" :maxdepth: 2\n\n")
43-
for nb in notebooks:
44-
# Notebooks are now within `docs/source/notebooks`, adjust path
45-
# accordingly
46-
nb_path = os.path.join('notebooks', nb).replace(os.path.sep, '/')
47-
f.write(f" {nb_path}\n")
64+
"""
65+
Generates 'examples_auto.rst', grouping notebooks by their top-level folder.
66+
Each folder gets its own subheading and toctree.
67+
"""
68+
grouped = group_notebooks_by_top_dir(notebooks)
69+
70+
with open(output_file, 'w', encoding='utf-8') as f:
71+
f.write("Jupyter Notebook Examples by Model\n")
72+
f.write("----------------------------------\n\n")
73+
74+
# For each top-level folder, create a subheading and toctree
75+
# Sort the folder names so the output is consistent
76+
for folder in sorted(grouped.keys()):
77+
f.write(folder + "\n" + "-" * len(folder) + "\n\n")
78+
f.write(".. toctree::\n")
79+
f.write(" :maxdepth: 2\n\n")
80+
81+
# Write each notebook path
82+
for nb in sorted(grouped[folder]):
83+
# notebooks are now in docs/source/notebooks
84+
nb_path = os.path.join(
85+
'notebooks', nb).replace(os.path.sep, '/')
86+
f.write(f" {nb_path}\n")
87+
88+
f.write("\n") # blank line between sections
4889

4990

5091
def main():
92+
# Path to your top-level examples directory
5193
examples_dir = os.path.abspath('../../examples')
52-
# Target directory within docs/source
94+
95+
# The target dir in docs/source where notebooks will be copied
5396
target_dir = os.path.join(os.path.dirname(__file__), 'notebooks')
5497

55-
# Clear the target directory before copying notebooks
98+
# Clear the target directory before re-copying notebooks
5699
clear_directory(target_dir)
57100

58101
notebooks = find_notebooks(examples_dir)
59102

60-
# Copy notebooks to the Sphinx source directory
103+
# Copy all notebooks from examples/ to docs/source/notebooks/
61104
copy_notebooks(notebooks, examples_dir, target_dir)
62105

63-
# Now, generate examples.rst with paths relative to the new location
64-
output_rst = os.path.join(os.path.dirname(__file__), 'examples.rst')
65-
# Ensure paths are relative to `docs/source/notebooks`
106+
# Generate 'examples_auto.rst' (instead of 'examples.rst')
107+
output_rst = os.path.join(os.path.dirname(__file__), 'examples_auto.rst')
66108
generate_rst(notebooks, target_dir, output_rst)
67109
print(f"Generated {output_rst}")
68110

examples/README.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=========
2+
Examples
3+
=========
4+
5+
This directory contains subfolders with Jupyter notebooks (and related data files)
6+
demonstrating different models and methods in MIMIC. Some of these examples use real-life
7+
datasets published by Stein et al. and Herold et al.
8+
9+
.. readme-begin
10+
11+
- `CRM/ <CRM/>`_
12+
13+
- `examples-sim-CRM.ipynb <CRM/examples-sim-CRM.ipynb>`_: Simulate time-course data using the Cross-Feeding Resource Model.
14+
15+
- `gLV/ <gLV/>`_
16+
17+
- `examples-bayes-gLV.ipynb <gLV/examples-bayes-gLV.ipynb>`_: Bayesian inference for Generalized Lotka-Volterra.
18+
- `examples-lasso-gLV.ipynb <gLV/examples-lasso-gLV.ipynb>`_: Lasso-based inference for gLV.
19+
- `examples-ridge-gLV.ipynb <gLV/examples-ridge-gLV.ipynb>`_: Ridge-based inference for gLV.
20+
- `examples-Rutter-Dekker.ipynb <gLV/examples-Rutter-Dekker.ipynb>`_: Rutter-Dekker example.
21+
- `examples-sim-gLV.ipynb <gLV/examples-sim-gLV.ipynb>`_: Simulation of gLV dynamics.
22+
- `examples-Stein.ipynb <gLV/examples-Stein.ipynb>`_: Real-life dataset example (Stein et al.) applying gLV methods.
23+
*(Additional CSV files support these notebooks.)*
24+
25+
- `gMLV/ <gMLV/>`_
26+
27+
**Generalized Metabolic Lotka-Volterra (gMLV)** is a variation of gLV that includes
28+
metabolite interactions alongside microbial abundances.
29+
30+
- `examples-ridge-lasso-gMLV.ipynb <gMLV/examples-ridge-lasso-gMLV.ipynb>`_:
31+
Ridge/Lasso inference for gMLV.
32+
- `examples-sim-gMLV.ipynb <gMLV/examples-sim-gMLV.ipynb>`_: gMLV simulation examples.
33+
34+
- `GP/ <GP/>`_
35+
36+
- `examples-impute-GP.ipynb <GP/examples-impute-GP.ipynb>`_: Data imputation with Gaussian Processes.
37+
- `examples-impute-GP_Stein.ipynb <GP/examples-impute-GP_Stein.ipynb>`_: Extended GP-based imputation for Stein dataset.
38+
*(CSV files for input/output data are included here.)*
39+
40+
- `MultiModel/Herold/ <MultiModel/Herold/>`_
41+
42+
These notebooks use real-life data from Herold et al. to demonstrate multi-model workflows.
43+
44+
- `examples-Herold-sVAR.ipynb <MultiModel/Herold/examples-Herold-sVAR.ipynb>`_: sVAR approach on Herold dataset.
45+
- `examples-Herold-VAR.ipynb <MultiModel/Herold/examples-Herold-VAR.ipynb>`_: VAR approach on Herold dataset.
46+
- `examples_impute_data.ipynb <MultiModel/Herold/examples_impute_data.ipynb>`_: Data imputation for Herold multi-model workflows.
47+
*(`Source Data/` folder contains all raw files needed for these notebooks.)*
48+
49+
- `MVAR/ <MVAR/>`_
50+
51+
- `examples-infer-MVAR.ipynb <MVAR/examples-infer-MVAR.ipynb>`_: Inference with the Multivariate Autoregressive model.
52+
- `examples-sim-MVAR.ipynb <MVAR/examples-sim-MVAR.ipynb>`_: Simulation using MVAR.
53+
*(`parametersS.json` is included for these demos.)*
54+
55+
- `VAR/ <VAR/>`_
56+
57+
- `examples-bayes-VAR.ipynb <VAR/examples-bayes-VAR.ipynb>`_: Bayesian inference for Vector Autoregression.
58+
- `examples-sim-VAR.ipynb <VAR/examples-sim-VAR.ipynb>`_: Simulation examples for VAR.
59+
*(JSON files and CSV data support these notebooks.)*
60+
61+
- `run_gMLV_sims.py`: A script to run gMLV simulations from the command line.
62+
63+
Each subfolder includes one or more Jupyter notebooks that guide you step-by-step
64+
through setup, simulation, and analysis with the MIMIC package. Feel free to explore
65+
each subfolder for model-specific usage instructions, parameter files, and example data.
66+
67+
.. readme-end

mimic/model_infer/infer_VAR_bayes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def _run_inference_large(self, **kwargs) -> None:
279279
c2 = pm.InverseGamma("c2", 2, 8)
280280
tau = pm.HalfCauchy("tau", beta=tau0)
281281
lam = pm.HalfCauchy("lam", beta=1, shape=(ndim, ndim))
282-
A = pm.Normal('A', mu=A_prior_mu, sigma=tau * lam *
282+
A = pm.Normal('A', mu=A_prior_mu, sigma=tau * lam * \
283283
at.sqrt(c2 / (c2 + tau**2 * lam**2)), shape=(ndim, ndim))
284284

285285
# If noise covariance is provided, use it as a prior
@@ -438,14 +438,14 @@ def _run_inference_large_xs(self, **kwargs) -> None:
438438
c2_A = pm.InverseGamma("c2_A", 2, 1)
439439
tau_A = pm.HalfCauchy("tau_A", beta=tau0_A)
440440
lam_A = pm.HalfCauchy("lam_A", beta=1, shape=(nX, nX))
441-
Ah = pm.Normal('Ah', mu=A_prior_mu, sigma=tau_A * lam_A *
441+
Ah = pm.Normal('Ah', mu=A_prior_mu, sigma=tau_A * lam_A * \
442442
at.sqrt(c2_A / (c2_A + tau_A**2 * lam_A**2)), shape=(nX, nX))
443443

444444
tau0_B = (DB0 / (DB - DB0)) * 0.1 / np.sqrt(N)
445445
c2_B = pm.InverseGamma("c2_B", 2, 1)
446446
tau_B = pm.HalfCauchy("tau_B", beta=tau0_B)
447447
lam_B = pm.HalfCauchy("lam_B", beta=1, shape=(nS, nX))
448-
Bh = pm.Normal('Bh', mu=0, sigma=tau_B * lam_B *
448+
Bh = pm.Normal('Bh', mu=0, sigma=tau_B * lam_B * \
449449
at.sqrt(c2_B / (c2_B + tau_B**2 * lam_B**2)), shape=(nS, nX))
450450

451451
if noise_cov_prior is not None:

0 commit comments

Comments
 (0)