Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebook showing how to use LinearSimulator to generate particle images from a given volume #119

Merged
merged 12 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
!simSPI/**
!notebooks/**
!tests/**
!docker/**
!docker/**
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*__pycache__*
temp_workspace/*
data/pdb_files/*
notebooks/.ipynb_checkpoints
notebooks/.ipynb_checkpoints
.ipynb_checkpoints/
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh &

# Copy the code and generate the environment
COPY . /work/

RUN . /opt/anaconda/etc/profile.d/conda.sh && conda update -c conda-forge conda
RUN . /opt/anaconda/etc/profile.d/conda.sh && \
conda env update --name base --file environment.yml && \
Expand All @@ -41,4 +42,4 @@ RUN . /opt/anaconda/etc/profile.d/conda.sh && \
# Copy and add the entrypoint script
ADD docker/entrypoint.sh /opt/entrypoint.sh
RUN chmod +x /opt/entrypoint.sh
ENTRYPOINT ["/opt/entrypoint.sh"]
ENTRYPOINT ["/opt/entrypoint.sh"]
437 changes: 437 additions & 0 deletions notebooks/linearsimulator_tutorial.ipynb

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions notebooks/path_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
metadata_params_file: './notebooks/metadata_fields.yaml'
mrc_keyword: '_randomrot'
output_dir: './notebooks/'
local_sim_dir: '/work/TEM-simulator_1.3/src/TEM-simulator'

local_sim_dir: './TEM-simulator_1.3/src/TEM-simulator'
19 changes: 15 additions & 4 deletions notebooks/sim_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,25 @@
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"execution_count": null,
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"root_dir = \"/work/\" # change this to your desired path.\n",
"sim_config = f\"{root_dir}notebooks/sim_config.yaml\"\n",
"path_config = f\"{root_dir}notebooks/path_config.yaml\"\n",
"\n",
"path_config = f\"{root_dir}notebooks/path_config.yaml\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.chdir(root_dir)"
]
},
Expand Down
6 changes: 3 additions & 3 deletions notebooks/tem_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"This is the root: /Users/callumhepworth/Developer/ENPH-459/compSPI\n"
"This is the root: /Users/bogdan/Work/cryoem/compspi\n"
]
}
],
Expand Down Expand Up @@ -97,7 +97,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/callumhepworth/anaconda3/envs/simSPI/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
"/opt/anaconda3/envs/simspi/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
Expand Down Expand Up @@ -502,7 +502,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
Binary file added notebooks/vol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/linear_simulator_vol.mrc
Binary file not shown.
73 changes: 0 additions & 73 deletions tests/test_notebook.py

This file was deleted.

108 changes: 108 additions & 0 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""Unit tests for demo notebooks."""
import os
import subprocess
import tempfile
from pathlib import Path

import pytest


@pytest.fixture
def test_resources_tem_sim():
"""Return resources for testing the sim_tutorial and the tem_tutorial notebooks."""
test_files_path = "/work/tests/test_files/tem"
notebooks_path = "/work/notebooks/"
cwd = os.getcwd()
resources = {
"notebook_path": str(Path(cwd, notebooks_path)),
"arg_paths": {
"path_config": str(Path(cwd, test_files_path, "path_config.yaml")),
"sim_config": str(Path(cwd, test_files_path, "sim_config.yaml")),
"sim_yaml_clean": str(Path(cwd, test_files_path, "sim_config_clean.yaml")),
},
}

return resources


@pytest.fixture
def test_resources_linsim():
"""Return resources for testing the linearsimulator_tutorial notebook."""
test_data_path = "/work/tests/data/"
notebooks_path = "/work/notebooks/"
cwd = os.getcwd()
resources = {
"notebook_path": str(Path(cwd, notebooks_path)),
"arg_paths": {
"vol_path": str(Path(cwd, test_data_path, "linear_simulator_vol.mrc")),
"data_path": str(Path(cwd, test_data_path, "linear_simulator_data.npy")),
},
}

return resources


def _exec_notebook(path, arg_paths):
"""Execute notebook on path using papermill to fill config paths.

Parameters
----------
path : String
The full path of the notebook to test.

arg_paths: dict[String, String]
Dictionary containing the path to the config files passed to papermill
for running the current notebook, e.g. arg_paths["path_config"] = /work/...
"""
file_name = tempfile.NamedTemporaryFile(suffix=".ipynb").name
args = [
"papermill",
path,
file_name,
"--execution-timeout",
"360",
]

for arg_name, arg_val in arg_paths.items():
args.extend(["-p", arg_name, arg_val])

subprocess.check_call(args)


def test_tem_tutorial(test_resources_tem_sim):
"""Test execution of tem_tutorial.ipynb notebook."""
notebook_name = "/tem_tutorial.ipynb"
notebook_path = test_resources_tem_sim["notebook_path"]

try:
_exec_notebook(
notebook_path + notebook_name, test_resources_tem_sim["arg_paths"]
)
except subprocess.CalledProcessError as exc:
assert False, f"{notebook_name} raised exception: {exc}"


def test_sim_tutorial(test_resources_tem_sim):
"""Test execution of sim_tutorial.ipynb notebook."""
notebook_name = "/sim_tutorial.ipynb"
notebook_path = test_resources_tem_sim["notebook_path"]

try:
_exec_notebook(
notebook_path + notebook_name, test_resources_tem_sim["arg_paths"]
)
except subprocess.CalledProcessError as exc:
assert False, f"{notebook_name} raised exception: {exc}"


def test_linearsimulator_tutorial(test_resources_linsim):
"""Test execution of the linearsimulator_tutorial.ipynb notebook."""
notebook_name = "/linearsimulator_tutorial.ipynb"
notebook_path = test_resources_linsim["notebook_path"]

try:
_exec_notebook(
notebook_path + notebook_name, test_resources_linsim["arg_paths"]
)
except subprocess.CalledProcessError as exc:
assert False, f"{notebook_name} raised exception: {exc}"