Skip to content

Commit

Permalink
Merge pull request graphnet-team#605 from RasmusOrsoe/example_unit_tests
Browse files Browse the repository at this point in the history
Unit tests for Examples
  • Loading branch information
RasmusOrsoe authored Sep 29, 2023
2 parents fab9e4f + 5a5317b commit cd3311c
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 39 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ jobs:
editable: true
- name: Run unit tests and generate coverage report
run: |
coverage run --source=graphnet -m pytest tests/
coverage run --source=graphnet -m pytest tests/ --ignore=tests/examples
coverage run --source=graphnet -m pytest tests/examples/01_icetray
coverage xml -o coverage.xml
- name: Work around permission issue
run: |
Expand Down Expand Up @@ -88,7 +89,8 @@ jobs:
- name: Run unit tests and generate coverage report
run: |
set -o pipefail # To propagate exit code from pytest
coverage run --source=graphnet -m pytest tests/ --ignore=tests/data/ --ignore=tests/deployment/
coverage run --source=graphnet -m pytest tests/ --ignore=tests/utilities --ignore=tests/data/ --ignore=tests/deployment/ --ignore=tests/examples/01_icetray/
coverage run --source=graphnet -m pytest tests/utilities
coverage report -m
build-macos:
Expand All @@ -108,5 +110,5 @@ jobs:
- name: Run unit tests and generate coverage report
run: |
set -o pipefail # To propagate exit code from pytest
coverage run --source=graphnet -m pytest tests/ --ignore=tests/data/ --ignore=tests/deployment/
coverage run --source=graphnet -m pytest tests/ --ignore=tests/data/ --ignore=tests/deployment/ --ignore=tests/examples/
coverage report -m
1 change: 1 addition & 0 deletions configs/models/dynedge_PID_classification_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ arguments:
- ModelConfig:
arguments:
nb_outputs: 3 # number of classes
prediction_labels: ['noise', 'muon', 'neutrino']
hidden_size: 128
loss_function:
ModelConfig:
Expand Down
15 changes: 12 additions & 3 deletions examples/01_icetray/01_convert_i3_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
I3FeatureExtractorIceCube86,
I3RetroExtractor,
I3TruthExtractor,
I3GenericExtractor,
)
from graphnet.data.dataconverter import DataConverter
from graphnet.data.parquet import ParquetDataConverter
Expand All @@ -17,7 +16,17 @@
from graphnet.utilities.imports import has_icecube_package
from graphnet.utilities.logging import Logger

from _common_icetray import ERROR_MESSAGE_MISSING_ICETRAY
ERROR_MESSAGE_MISSING_ICETRAY = (
"This example requires IceTray to be installed, which doesn't seem to be "
"the case. Please install IceTray; run this example in the GraphNeT "
"Docker container which comes with IceTray installed; or run an example "
"script in one of the other folders:"
"\n * examples/02_data/"
"\n * examples/03_weights/"
"\n * examples/04_training/"
"\n * examples/05_pisa/"
"\nExiting."
)

CONVERTER_CLASS = {
"sqlite": SQLiteDataConverter,
Expand Down Expand Up @@ -90,7 +99,7 @@ def main_icecube_upgrade(backend: str) -> None:
"detector", choices=["icecube-86", "icecube-upgrade"]
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

# Run example script
if args.detector == "icecube-86":
Expand Down
16 changes: 13 additions & 3 deletions examples/01_icetray/02_compare_sqlite_and_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
from graphnet.utilities.imports import has_icecube_package
from graphnet.utilities.logging import Logger

from _common_icetray import ERROR_MESSAGE_MISSING_ICETRAY
ERROR_MESSAGE_MISSING_ICETRAY = (
"This example requires IceTray to be installed, which doesn't seem to be "
"the case. Please install IceTray; run this example in the GraphNeT "
"Docker container which comes with IceTray installed; or run an example "
"script in one of the other folders:"
"\n * examples/02_data/"
"\n * examples/03_weights/"
"\n * examples/04_training/"
"\n * examples/05_pisa/"
"\nExiting."
)

OUTPUT_DIR = f"{EXAMPLE_OUTPUT_DIR}/compare_sqlite_and_parquet"
PULSEMAP = "SRTInIcePulses"
Expand All @@ -34,7 +44,7 @@ def convert_data() -> None:
I3FeatureExtractorIceCube86(PULSEMAP),
],
outdir=OUTPUT_DIR,
workers=10,
workers=1,
)

# Run data converters.
Expand Down Expand Up @@ -85,7 +95,7 @@ def load_data() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

# Run example script(s)
convert_data()
Expand Down
14 changes: 12 additions & 2 deletions examples/01_icetray/03_i3_deployer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@
I3InferenceModule,
)

from _common_icetray import ERROR_MESSAGE_MISSING_ICETRAY
ERROR_MESSAGE_MISSING_ICETRAY = (
"This example requires IceTray to be installed, which doesn't seem to be "
"the case. Please install IceTray; run this example in the GraphNeT "
"Docker container which comes with IceTray installed; or run an example "
"script in one of the other folders:"
"\n * examples/02_data/"
"\n * examples/03_weights/"
"\n * examples/04_training/"
"\n * examples/05_pisa/"
"\nExiting."
)

# Constants
features = FEATURES.UPGRADE
Expand Down Expand Up @@ -83,7 +93,7 @@ def main() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

# Run example script
main()
14 changes: 12 additions & 2 deletions examples/01_icetray/04_i3_module_in_native_icetray_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@
GraphNeTI3Module,
)

from _common_icetray import ERROR_MESSAGE_MISSING_ICETRAY
ERROR_MESSAGE_MISSING_ICETRAY = (
"This example requires IceTray to be installed, which doesn't seem to be "
"the case. Please install IceTray; run this example in the GraphNeT "
"Docker container which comes with IceTray installed; or run an example "
"script in one of the other folders:"
"\n * examples/02_data/"
"\n * examples/03_weights/"
"\n * examples/04_training/"
"\n * examples/05_pisa/"
"\nExiting."
)


def apply_to_files(
Expand Down Expand Up @@ -116,7 +126,7 @@ def main() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

# Run example script
main()
11 changes: 0 additions & 11 deletions examples/01_icetray/_common_icetray.py

This file was deleted.

10 changes: 8 additions & 2 deletions examples/02_data/01_read_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ def main(backend: str) -> None:
"""
)

parser.add_argument("backend", choices=["sqlite", "parquet"])
parser.add_argument(
"backend",
choices=["sqlite", "parquet"],
default="sqlite",
const="sqlite",
nargs="?",
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(args.backend)
2 changes: 1 addition & 1 deletion examples/02_data/02_plot_feature_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def main() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main()
2 changes: 1 addition & 1 deletion examples/02_data/03_convert_parquet_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ def main(parquet_path: str, mc_truth_table: str) -> None:
default="truth",
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(args.parquet_path, args.mc_truth_table)
1 change: 1 addition & 0 deletions examples/02_data/04_ensemble_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ def main() -> None:
Combine multiple Datasets using EnsembleDataset.
"""
)
args, unknown = parser.parse_known_args()
main()
2 changes: 1 addition & 1 deletion examples/03_weights/01_fit_uniform_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def main() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main()
2 changes: 1 addition & 1 deletion examples/03_weights/02_fit_bjoern_low_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def main() -> None:
"""
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main()
4 changes: 2 additions & 2 deletions examples/04_training/01_train_dynedge.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def main(

parser.with_standard_arguments(
"gpus",
("max-epochs", 5),
("max-epochs", 1),
"early-stopping-patience",
("batch-size", 16),
"num-workers",
Expand All @@ -225,7 +225,7 @@ def main(
help="If True, Weights & Biases are used to track the experiment.",
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(
args.path,
Expand Down
7 changes: 4 additions & 3 deletions examples/04_training/02_train_tito_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def main(

# Save model config and state dict - Version safe save method.
model.save_state_dict(f"{path}/state_dict.pth")
model.save_config(f"{path}/model_config.yml")
# model.save_config(f"{path}/model_config.yml")
# Pending https://github.com/graphnet-team/graphnet/issues/606


if __name__ == "__main__":
Expand Down Expand Up @@ -223,7 +224,7 @@ def main(

parser.with_standard_arguments(
"gpus",
("max-epochs", 5),
("max-epochs", 1),
("early-stopping-patience", 2),
("batch-size", 16),
"num-workers",
Expand All @@ -235,7 +236,7 @@ def main(
help="If True, Weights & Biases are used to track the experiment.",
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(
args.path,
Expand Down
4 changes: 2 additions & 2 deletions examples/04_training/03_train_dynedge_from_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def main(
"dataset-config",
"model-config",
"gpus",
("max-epochs", 5),
("max-epochs", 1),
"early-stopping-patience",
("batch-size", 16),
"num-workers",
Expand All @@ -157,7 +157,7 @@ def main(
help="If True, Weights & Biases are used to track the experiment.",
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(
args.dataset_config,
Expand Down
4 changes: 2 additions & 2 deletions examples/04_training/04_train_multiclassifier_from_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def main(
),
),
"gpus",
("max-epochs", 5),
("max-epochs", 1),
"early-stopping-patience",
("batch-size", 16),
"num-workers",
Expand All @@ -188,7 +188,7 @@ def main(
default=None,
)

args = parser.parse_args()
args, unknown = parser.parse_known_args()

main(
args.dataset_config,
Expand Down
17 changes: 17 additions & 0 deletions tests/examples/01_icetray/test_icetray_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Test for examples in 01_icetray."""
import runpy
import os
import pytest
from glob import glob

from graphnet.constants import GRAPHNET_ROOT_DIR

EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/01_icetray")

examples = glob(EXAMPLE_PATH + "/*.py")


@pytest.mark.parametrize("example", examples)
def test_script_execution(example: str) -> None:
"""Test function that executes example."""
runpy.run_path(os.path.join(EXAMPLE_PATH, example))
16 changes: 16 additions & 0 deletions tests/examples/02_data/test_data_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Tests for examples in 02_data."""
import runpy
import os
import pytest
from glob import glob

from graphnet.constants import GRAPHNET_ROOT_DIR

EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/02_data")
examples = glob(EXAMPLE_PATH + "/*.py")


@pytest.mark.parametrize("example", examples)
def test_script_execution(example: str) -> None:
"""Test function that executes example."""
runpy.run_path(os.path.join(EXAMPLE_PATH, example))
16 changes: 16 additions & 0 deletions tests/examples/03_weights/test_weights_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Test for examples in 03_weights."""
import runpy
import os
from graphnet.constants import GRAPHNET_ROOT_DIR
from glob import glob
import pytest

EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/03_weights")

examples = glob(EXAMPLE_PATH + "/*.py")


@pytest.mark.parametrize("example", examples)
def test_script_execution(example: str) -> None:
"""Test function that executes example."""
runpy.run_path(os.path.join(EXAMPLE_PATH, example))
18 changes: 18 additions & 0 deletions tests/examples/04_training/test_training_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Test for examples in 04_training."""
import runpy
import os
from glob import glob
import pytest

from graphnet.constants import GRAPHNET_ROOT_DIR

EXAMPLE_PATH = os.path.join(GRAPHNET_ROOT_DIR, "examples/04_training")


examples = glob(EXAMPLE_PATH + "/*.py")


@pytest.mark.parametrize("example", examples)
def test_script_execution(example: str) -> None:
"""Test function that executes example."""
runpy.run_path(os.path.join(EXAMPLE_PATH, example), run_name="__main__")

0 comments on commit cd3311c

Please sign in to comment.