diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 080146149..001b8bed4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: | @@ -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: @@ -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 diff --git a/configs/models/dynedge_PID_classification_example.yml b/configs/models/dynedge_PID_classification_example.yml index 4b2fd0246..57fec3e88 100644 --- a/configs/models/dynedge_PID_classification_example.yml +++ b/configs/models/dynedge_PID_classification_example.yml @@ -38,6 +38,7 @@ arguments: - ModelConfig: arguments: nb_outputs: 3 # number of classes + prediction_labels: ['noise', 'muon', 'neutrino'] hidden_size: 128 loss_function: ModelConfig: diff --git a/examples/01_icetray/01_convert_i3_files.py b/examples/01_icetray/01_convert_i3_files.py index 495c9a13c..88dcf714a 100644 --- a/examples/01_icetray/01_convert_i3_files.py +++ b/examples/01_icetray/01_convert_i3_files.py @@ -8,7 +8,6 @@ I3FeatureExtractorIceCube86, I3RetroExtractor, I3TruthExtractor, - I3GenericExtractor, ) from graphnet.data.dataconverter import DataConverter from graphnet.data.parquet import ParquetDataConverter @@ -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, @@ -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": diff --git a/examples/01_icetray/02_compare_sqlite_and_parquet.py b/examples/01_icetray/02_compare_sqlite_and_parquet.py index 252f1a346..99250d4b0 100644 --- a/examples/01_icetray/02_compare_sqlite_and_parquet.py +++ b/examples/01_icetray/02_compare_sqlite_and_parquet.py @@ -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" @@ -34,7 +44,7 @@ def convert_data() -> None: I3FeatureExtractorIceCube86(PULSEMAP), ], outdir=OUTPUT_DIR, - workers=10, + workers=1, ) # Run data converters. @@ -85,7 +95,7 @@ def load_data() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() # Run example script(s) convert_data() diff --git a/examples/01_icetray/03_i3_deployer_example.py b/examples/01_icetray/03_i3_deployer_example.py index 5180fc7b7..f55aa769c 100644 --- a/examples/01_icetray/03_i3_deployer_example.py +++ b/examples/01_icetray/03_i3_deployer_example.py @@ -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 @@ -83,7 +93,7 @@ def main() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() # Run example script main() diff --git a/examples/01_icetray/04_i3_module_in_native_icetray_example.py b/examples/01_icetray/04_i3_module_in_native_icetray_example.py index 730eb5bc1..74da5e499 100644 --- a/examples/01_icetray/04_i3_module_in_native_icetray_example.py +++ b/examples/01_icetray/04_i3_module_in_native_icetray_example.py @@ -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( @@ -116,7 +126,7 @@ def main() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() # Run example script main() diff --git a/examples/01_icetray/_common_icetray.py b/examples/01_icetray/_common_icetray.py deleted file mode 100644 index c1d3b10a0..000000000 --- a/examples/01_icetray/_common_icetray.py +++ /dev/null @@ -1,11 +0,0 @@ -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." -) diff --git a/examples/02_data/01_read_dataset.py b/examples/02_data/01_read_dataset.py index be913e190..77d7c1438 100644 --- a/examples/02_data/01_read_dataset.py +++ b/examples/02_data/01_read_dataset.py @@ -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) diff --git a/examples/02_data/02_plot_feature_distributions.py b/examples/02_data/02_plot_feature_distributions.py index b46be0623..ac08ae9fb 100644 --- a/examples/02_data/02_plot_feature_distributions.py +++ b/examples/02_data/02_plot_feature_distributions.py @@ -66,6 +66,6 @@ def main() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() main() diff --git a/examples/02_data/03_convert_parquet_to_sqlite.py b/examples/02_data/03_convert_parquet_to_sqlite.py index 12a33d181..5757bb5c9 100644 --- a/examples/02_data/03_convert_parquet_to_sqlite.py +++ b/examples/02_data/03_convert_parquet_to_sqlite.py @@ -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) diff --git a/examples/02_data/04_ensemble_dataset.py b/examples/02_data/04_ensemble_dataset.py index 13998c3e7..f1cc9de68 100644 --- a/examples/02_data/04_ensemble_dataset.py +++ b/examples/02_data/04_ensemble_dataset.py @@ -88,4 +88,5 @@ def main() -> None: Combine multiple Datasets using EnsembleDataset. """ ) + args, unknown = parser.parse_known_args() main() diff --git a/examples/03_weights/01_fit_uniform_weights.py b/examples/03_weights/01_fit_uniform_weights.py index 629e8497b..e2f68487a 100644 --- a/examples/03_weights/01_fit_uniform_weights.py +++ b/examples/03_weights/01_fit_uniform_weights.py @@ -37,6 +37,6 @@ def main() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() main() diff --git a/examples/03_weights/02_fit_bjoern_low_weights.py b/examples/03_weights/02_fit_bjoern_low_weights.py index cde075812..4d54e8c7d 100644 --- a/examples/03_weights/02_fit_bjoern_low_weights.py +++ b/examples/03_weights/02_fit_bjoern_low_weights.py @@ -45,6 +45,6 @@ def main() -> None: """ ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() main() diff --git a/examples/04_training/01_train_dynedge.py b/examples/04_training/01_train_dynedge.py index 58e513ec2..7cde873b2 100644 --- a/examples/04_training/01_train_dynedge.py +++ b/examples/04_training/01_train_dynedge.py @@ -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", @@ -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, diff --git a/examples/04_training/02_train_tito_model.py b/examples/04_training/02_train_tito_model.py index ee3d89760..f586b0581 100644 --- a/examples/04_training/02_train_tito_model.py +++ b/examples/04_training/02_train_tito_model.py @@ -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__": @@ -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", @@ -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, diff --git a/examples/04_training/03_train_dynedge_from_config.py b/examples/04_training/03_train_dynedge_from_config.py index 9a6df73dd..4b4db17f8 100644 --- a/examples/04_training/03_train_dynedge_from_config.py +++ b/examples/04_training/03_train_dynedge_from_config.py @@ -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", @@ -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, diff --git a/examples/04_training/04_train_multiclassifier_from_configs.py b/examples/04_training/04_train_multiclassifier_from_configs.py index 6937b01b1..e71813b24 100644 --- a/examples/04_training/04_train_multiclassifier_from_configs.py +++ b/examples/04_training/04_train_multiclassifier_from_configs.py @@ -175,7 +175,7 @@ def main( ), ), "gpus", - ("max-epochs", 5), + ("max-epochs", 1), "early-stopping-patience", ("batch-size", 16), "num-workers", @@ -188,7 +188,7 @@ def main( default=None, ) - args = parser.parse_args() + args, unknown = parser.parse_known_args() main( args.dataset_config, diff --git a/src/graphnet/models/graphs/edges/edges.py b/src/graphnet/models/graphs/edges/edges.py index cb9bf9112..ad3cf3c46 100644 --- a/src/graphnet/models/graphs/edges/edges.py +++ b/src/graphnet/models/graphs/edges/edges.py @@ -26,7 +26,7 @@ def forward(self, graph: Data) -> Data: graph: a graph with edges """ if graph.edge_index is not None: - self.warnonce( + self.warning_once( "GraphBuilder received graph with pre-existing " "structure. Will overwrite." ) diff --git a/src/graphnet/models/graphs/graph_definition.py b/src/graphnet/models/graphs/graph_definition.py index 089eee008..9c4db4d47 100644 --- a/src/graphnet/models/graphs/graph_definition.py +++ b/src/graphnet/models/graphs/graph_definition.py @@ -148,7 +148,8 @@ def forward( # type: ignore if self._edge_definition is not None: graph = self._edge_definition(graph) else: - self.warnonce( + + self.warning_once( """No EdgeDefinition provided. Graphs will not have edges defined!""" # noqa ) @@ -305,10 +306,11 @@ def _add_features_individually( if feature not in ["x"]: # reserved for node features. graph[feature] = graph.x[:, index].detach() else: - self.warnonce( - """Cannot assign graph['x']. This field is reserved - for node features. Please rename your input feature.""" # noqa - ) + self.warning_once( + """Cannot assign graph['x']. This field is reserved for + node features. Please rename your input feature.""" + ) # noqa + return graph def _add_custom_labels( diff --git a/tests/examples/01_icetray/test_icetray_examples.py b/tests/examples/01_icetray/test_icetray_examples.py new file mode 100644 index 000000000..cbdf470fd --- /dev/null +++ b/tests/examples/01_icetray/test_icetray_examples.py @@ -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)) diff --git a/tests/examples/02_data/test_data_examples.py b/tests/examples/02_data/test_data_examples.py new file mode 100644 index 000000000..8faaca5c0 --- /dev/null +++ b/tests/examples/02_data/test_data_examples.py @@ -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)) diff --git a/tests/examples/03_weights/test_weights_examples.py b/tests/examples/03_weights/test_weights_examples.py new file mode 100644 index 000000000..5dddee264 --- /dev/null +++ b/tests/examples/03_weights/test_weights_examples.py @@ -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)) diff --git a/tests/examples/04_training/test_training_examples.py b/tests/examples/04_training/test_training_examples.py new file mode 100644 index 000000000..e97b4bb3c --- /dev/null +++ b/tests/examples/04_training/test_training_examples.py @@ -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__")