diff --git a/.github/workflows/cleanup-caches.yml b/.github/workflows/cleanup-caches.yml index 2b65aa371..86dd7569d 100644 --- a/.github/workflows/cleanup-caches.yml +++ b/.github/workflows/cleanup-caches.yml @@ -3,12 +3,19 @@ on: pull_request_target: types: - closed + push: + # Trigger on pushes to master or develop and for git tag pushes + branches: + - master + - develop + tags: + - v* jobs: cleanup: runs-on: ubuntu-latest steps: - - name: Cleanup + - name: Cleanup caches run: | gh extension install actions/gh-actions-cache diff --git a/.github/workflows/cpu-tests.yml b/.github/workflows/cpu-tests.yml index 347163ab7..48f0a456c 100644 --- a/.github/workflows/cpu-tests.yml +++ b/.github/workflows/cpu-tests.yml @@ -1,6 +1,7 @@ name: CPU tests on: + workflow_dispatch: pull_request: # Trigger on pull requests to master or develop that are # marked as "ready for review" (non-draft PRs) @@ -33,7 +34,7 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Check out repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set environment variables run: | @@ -45,7 +46,7 @@ jobs: echo "IMAGE_REPO=$IMAGE_REPO" - name: Restore cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-docker with: path: ${{ env.DOCKER_CACHE_PATH }} @@ -73,7 +74,7 @@ jobs: CACHE=$IMAGE_REPO/$IMAGE_NAME:latest fi - docker build . --file Dockerfile --tag $IMAGE_NAME:local --cache-from=$CACHE --build-arg DEVICE=cpu + DOCKER_BUILDKIT=0 docker build . --file Dockerfile --tag $IMAGE_NAME:local --cache-from=$CACHE --build-arg DEVICE=cpu # Show images docker images --filter=reference=$IMAGE_NAME --filter=reference=$IMAGE_REPO/$IMAGE_NAME @@ -122,7 +123,7 @@ jobs: steps: - name: "Prepare environment: Restore cache" if: env.DOCKER_TAG != 'latest' - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-docker with: path: ${{ env.DOCKER_CACHE_PATH }} @@ -153,14 +154,14 @@ jobs: [[ $(docker inspect --format '{{json .State.Running}}' mala-cpu) == 'true' ]] - name: Check out repository (mala) - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install mala package # Exec all commands inside the mala-cpu container shell: 'bash -c "docker exec -i mala-cpu bash < {0}"' run: | - # epxort Docker image Conda environment for a later comparison - conda env export -n mala-cpu > env_1.yml + # export Docker image Conda environment for a later comparison + conda env export -n mala-cpu > env_before.yml # install mala package pip --no-cache-dir install -e .[opt,test] --no-build-isolation @@ -169,26 +170,46 @@ jobs: shell: 'bash -c "docker exec -i mala-cpu bash < {0}"' run: | # export Conda environment _with_ mala package installed in it (and extra dependencies) - conda env export -n mala-cpu > env_2.yml + conda env export -n mala-cpu > env_after.yml # if comparison fails, `install/mala_cpu_[base]_environment.yml` needs to be aligned with # `requirements.txt` and/or extra dependencies are missing in the Docker Conda environment - diff env_1.yml env_2.yml + diff --side-by-side --color=always env_before.yml env_after.yml - - name: Download test data repository - shell: 'bash -c "docker exec -i mala-cpu bash < {0}"' + - name: Download test data repository from RODARE + shell: 'bash -c "docker exec -i mala-cpu python < {0}"' run: | - # Download test data repository from RODARE. If the version changes - # this URL has to be adapted (the number after /record/ and the - # version have to be incremented) - wget "https://rodare.hzdr.de/record/2999/files/mala-project/test-data-1.8.0.zip" - - # Once downloaded, we have to unzip the file. The name of the root - # folder in the zip file has to be updated for data repository - # updates as well - the string at the end is the hash of the data - # repository commit. - unzip -q test-data-1.8.0.zip - mv mala-project-test-data-d5694c7 mala_data + import requests, shutil, zipfile + + # This DOI represents all versions, and will always resolve to the latest one + DOI = "https://doi.org/10.14278/rodare.2900" + + # Resolve DOI and get record ID and the associated API URL + response = requests.get(DOI) + *_, record_id = response.url.split("/") + api_url = f"https://rodare.hzdr.de/api/records/{record_id}" + + # Download record from API and get the first file + response = requests.get(api_url) + record = response.json() + size = record["files"][0]["size"] + download_link = record["files"][0]["links"]["self"] + + print(size, "bytes", "--", download_link) + + # TODO: implement some sort of auto retry for failed HTTP requests + response = requests.get(download_link) + + # Saving downloaded content to a file + with open("test-data.zip", mode="wb") as file: + file.write(response.content) + + # Get top level directory name + dir_name = zipfile.ZipFile("test-data.zip").namelist()[0] + shutil.unpack_archive("test-data.zip", ".") + + print(f"Rename {dir_name} to mala_data") + shutil.move(dir_name, "mala_data") - name: Test mala shell: 'bash -c "docker exec -i mala-cpu bash < {0}"' @@ -209,11 +230,11 @@ jobs: || startsWith(github.ref, 'refs/tags/') steps: - name: Check out repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: "Prepare environment: Restore cache" if: env.DOCKER_TAG != 'latest' - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-docker with: path: ${{ env.DOCKER_CACHE_PATH }} diff --git a/.github/workflows/mirror-to-casus.yml b/.github/workflows/mirror-to-casus.yml index a231093bc..a862d6cac 100644 --- a/.github/workflows/mirror-to-casus.yml +++ b/.github/workflows/mirror-to-casus.yml @@ -1,4 +1,4 @@ -name: mirror +name: Mirror to CASUS on: [push, delete] @@ -6,13 +6,14 @@ jobs: mirror-to-CASUS: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: mirror-repository - uses: spyoungtech/mirror-action@v0.6.0 - with: - REMOTE: 'ssh://git@github.com/casus/mala.git' - GIT_SSH_PRIVATE_KEY: ${{ secrets.GIT_SSH_KEY }} - GIT_SSH_NO_VERIFY_HOST: "true" - DEBUG: "true" + - name: Check out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: mirror-repository + uses: spyoungtech/mirror-action@v0.6.0 + with: + REMOTE: 'ssh://git@github.com/casus/mala.git' + GIT_SSH_PRIVATE_KEY: ${{ secrets.GIT_SSH_KEY }} + GIT_SSH_NO_VERIFY_HOST: "true" + DEBUG: "true" diff --git a/Dockerfile b/Dockerfile index 724ed44e5..3167d4ed7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,6 @@ RUN conda env create -f mala_${DEVICE}_environment.yml && rm -rf /opt/conda/pkgs RUN /opt/conda/envs/mala-${DEVICE}/bin/pip install --no-input --no-cache-dir \ pytest \ oapackage==2.6.8 \ - openpmd-api==0.15.1 \ pqkmeans RUN echo "source activate mala-${DEVICE}" > ~/.bashrc diff --git a/examples/advanced/ex01_checkpoint_training.py b/examples/advanced/ex01_checkpoint_training.py index eb8dda0a5..ca1c1bc21 100644 --- a/examples/advanced/ex01_checkpoint_training.py +++ b/examples/advanced/ex01_checkpoint_training.py @@ -3,9 +3,7 @@ import mala from mala import printout -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ Shows how a training run can be paused and diff --git a/examples/advanced/ex02_shuffle_data.py b/examples/advanced/ex02_shuffle_data.py index 467da7922..db75d5154 100644 --- a/examples/advanced/ex02_shuffle_data.py +++ b/examples/advanced/ex02_shuffle_data.py @@ -2,14 +2,12 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ Shows how data can be shuffled amongst multiple -snapshots, which is very useful in the lazy loading case, where this cannot be -easily done in memory. +snapshots, which is very useful in the lazy loading case, where this cannot be +easily done in memory. """ diff --git a/examples/advanced/ex03_tensor_board.py b/examples/advanced/ex03_tensor_board.py index 00728a560..b15239495 100644 --- a/examples/advanced/ex03_tensor_board.py +++ b/examples/advanced/ex03_tensor_board.py @@ -3,13 +3,10 @@ import mala from mala import printout -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") - +from mala.datahandling.data_repo import data_path """ -Shows how a NN training by MALA can be visualized using +Shows how a NN training by MALA can be visualized using tensorboard. The training is a basic MALA network training. """ diff --git a/examples/advanced/ex04_acsd.py b/examples/advanced/ex04_acsd.py index 5390ae210..53b4b82bd 100644 --- a/examples/advanced/ex04_acsd.py +++ b/examples/advanced/ex04_acsd.py @@ -1,13 +1,11 @@ import os import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ Shows how MALA can be used to optimize descriptor -parameters based on the ACSD analysis (see hyperparameter paper in the +parameters based on the ACSD analysis (see hyperparameter paper in the documentation for mathematical details). """ diff --git a/examples/advanced/ex05_checkpoint_hyperparameter_optimization.py b/examples/advanced/ex05_checkpoint_hyperparameter_optimization.py index c7f741d70..cef7c8f4f 100644 --- a/examples/advanced/ex05_checkpoint_hyperparameter_optimization.py +++ b/examples/advanced/ex05_checkpoint_hyperparameter_optimization.py @@ -2,16 +2,14 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ -Shows how a hyperparameter optimization run can +Shows how a hyperparameter optimization run can be paused and resumed. Delete all ex04_*.pkl and ex04_*.pth prior to execution. -Afterwards, execute this script twice to see how MALA progresses from a +Afterwards, execute this script twice to see how MALA progresses from a checkpoint. As the number of trials cannot be divided by the number -of epochs after which a checkpoint is created without residual, this will +of epochs after which a checkpoint is created without residual, this will lead to MALA performing the missing trials again. """ diff --git a/examples/advanced/ex06_distributed_hyperparameter_optimization.py b/examples/advanced/ex06_distributed_hyperparameter_optimization.py index 2a67acb3c..b34f9bb8b 100644 --- a/examples/advanced/ex06_distributed_hyperparameter_optimization.py +++ b/examples/advanced/ex06_distributed_hyperparameter_optimization.py @@ -2,14 +2,12 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ -ex09_distributed_hyperopt.py: Shows how a hyperparameter +ex09_distributed_hyperopt.py: Shows how a hyperparameter optimization can be sped up using a RDB storage. Ideally this should be done -using a database server system, such as PostgreSQL or MySQL. +using a database server system, such as PostgreSQL or MySQL. For this easy example, sqlite will be used. It is highly advisory not to to use this for actual, at-scale calculations! diff --git a/examples/advanced/ex07_advanced_hyperparameter_optimization.py b/examples/advanced/ex07_advanced_hyperparameter_optimization.py index 629d47962..8165ef01e 100644 --- a/examples/advanced/ex07_advanced_hyperparameter_optimization.py +++ b/examples/advanced/ex07_advanced_hyperparameter_optimization.py @@ -3,12 +3,10 @@ import mala from mala import printout -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ -Shows how recent developments in hyperparameter optimization techniques can be +Shows how recent developments in hyperparameter optimization techniques can be used (OAT / training-free NAS). REQUIRES OAPACKAGE. diff --git a/examples/advanced/ex08_visualize_observables.py b/examples/advanced/ex08_visualize_observables.py index 3b8bbed3d..be344b878 100644 --- a/examples/advanced/ex08_visualize_observables.py +++ b/examples/advanced/ex08_visualize_observables.py @@ -2,18 +2,15 @@ import mala -from mala.datahandling.data_repo import data_repo_path +from mala.datahandling.data_repo import data_path -atoms_path = os.path.join( - os.path.join(data_repo_path, "Be2"), "Be_snapshot1.out" -) -ldos_path = os.path.join( - os.path.join(data_repo_path, "Be2"), "Be_snapshot1.out.npy" -) """ -Shows how MALA can be used to visualize observables of interest. +Shows how MALA can be used to visualize observables of interest. """ +atoms_path = os.path.join(data_path, "Be_snapshot1.out") +ldos_path = os.path.join(data_path, "Be_snapshot1.out.npy") + #################### # 1. READ ELECTRONIC STRUCTURE DATA # This data may be read as part of an ML-DFT model inference. diff --git a/examples/basic/ex01_train_network.py b/examples/basic/ex01_train_network.py index a5d14d890..95eb2d51b 100644 --- a/examples/basic/ex01_train_network.py +++ b/examples/basic/ex01_train_network.py @@ -2,9 +2,7 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ This example shows how a neural network can be trained on material @@ -12,7 +10,6 @@ from *.npy files. """ - #################### # 1. PARAMETERS # The first step of each MALA workflow is to define a parameters object and @@ -93,5 +90,5 @@ test_trainer.train_network() additional_calculation_data = os.path.join(data_path, "Be_snapshot0.out") test_trainer.save_run( - "be_model", additional_calculation_data=additional_calculation_data + "Be_model", additional_calculation_data=additional_calculation_data ) diff --git a/examples/basic/ex02_test_network.py b/examples/basic/ex02_test_network.py index 6ef81f880..2e4b8953c 100644 --- a/examples/basic/ex02_test_network.py +++ b/examples/basic/ex02_test_network.py @@ -3,17 +3,16 @@ import mala from mala import printout -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ This example shows how a trained network can be tested with additional test snapshots. Either execute ex01 before executing this one or download the appropriate model from the provided test data repo. """ -assert os.path.exists("be_model.zip"), "Be model missing, run ex01 first." +model_name = "Be_model" +model_path = "./" if os.path.exists("Be_model.zip") else data_path #################### # 1. LOADING A NETWORK @@ -27,7 +26,9 @@ # (output_format="list") or as an averaged value (output_format="mae") #################### -parameters, network, data_handler, tester = mala.Tester.load_run("be_model") +parameters, network, data_handler, tester = mala.Tester.load_run( + run_name=model_name, path=model_path +) tester.observables_to_test = ["band_energy", "number_of_electrons"] tester.output_format = "list" parameters.data.use_lazy_loading = True diff --git a/examples/basic/ex03_preprocess_data.py b/examples/basic/ex03_preprocess_data.py index 72ec9490a..b0a104885 100644 --- a/examples/basic/ex03_preprocess_data.py +++ b/examples/basic/ex03_preprocess_data.py @@ -2,13 +2,11 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ Shows how this framework can be used to preprocess -data. Preprocessing here means converting raw DFT calculation output into +data. Preprocessing here means converting raw DFT calculation output into numpy arrays of the correct size. For the input data, this means descriptor calculation. diff --git a/examples/basic/ex04_hyperparameter_optimization.py b/examples/basic/ex04_hyperparameter_optimization.py index 77985f033..4c68179c2 100644 --- a/examples/basic/ex04_hyperparameter_optimization.py +++ b/examples/basic/ex04_hyperparameter_optimization.py @@ -2,14 +2,12 @@ import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path """ Shows how a hyperparameter optimization can be done using this framework. There are multiple hyperparameter optimizers available in this framework. This example -focusses on the most universal one - optuna. +focusses on the most universal one - optuna. """ diff --git a/examples/basic/ex05_run_predictions.py b/examples/basic/ex05_run_predictions.py index 40e4303ee..84676b21d 100644 --- a/examples/basic/ex05_run_predictions.py +++ b/examples/basic/ex05_run_predictions.py @@ -5,19 +5,19 @@ import mala from mala import printout -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") - -assert os.path.exists("be_model.zip"), "Be model missing, run ex01 first." +from mala.datahandling.data_repo import data_path """ -Show how a prediction can be made using MALA, based on only a -trained network and atomic configurations. +Show how a prediction can be made using MALA, based on only a trained network and atomic +configurations. Either execute ex01 before executing this one or download the +appropriate model from the provided test data repo. REQUIRES LAMMPS (and potentially the total energy module). """ +model_name = "Be_model" +model_path = "./" if os.path.exists("Be_model.zip") else data_path + #################### # 1. LOADING A NETWORK @@ -25,7 +25,7 @@ # Tester class interface. Afterwards, set the necessary parameters. #################### parameters, network, data_handler, predictor = mala.Predictor.load_run( - "be_model" + run_name=model_name, path=model_path ) diff --git a/examples/basic/ex06_ase_calculator.py b/examples/basic/ex06_ase_calculator.py index f4ab2d337..7ba0eee1b 100644 --- a/examples/basic/ex06_ase_calculator.py +++ b/examples/basic/ex06_ase_calculator.py @@ -1,21 +1,21 @@ import os -import mala from ase.io import read +import mala -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") - -assert os.path.exists("be_model.zip"), "Be model missing, run ex01 first." +from mala.datahandling.data_repo import data_path """ -Shows how MALA can be used as an ASE calculator. -Currently, calculation of forces is not supported. +Shows how MALA can be used as an ASE calculator. +Currently, calculation of forces is not supported. Either execute ex01 before executing +this one or download the appropriate model from the provided test data repo. REQUIRES LAMMPS AND QUANTUM ESPRESSO (TOTAL ENERGY MODULE). """ +model_name = "Be_model" +model_path = "./" if os.path.exists("Be_model.zip") else data_path + #################### # 1. LOADING A NETWORK @@ -23,7 +23,7 @@ # Further make sure to set the path to the pseudopotential used during # data generation- #################### -calculator = mala.MALA.load_model("be_model") +calculator = mala.MALA.load_model(run_name=model_name, path=model_path) calculator.mala_parameters.targets.pseudopotential_path = data_path #################### diff --git a/install/mala_cpu_base_environment.yml b/install/mala_cpu_base_environment.yml index 626008b16..f8309f5b9 100644 --- a/install/mala_cpu_base_environment.yml +++ b/install/mala_cpu_base_environment.yml @@ -14,3 +14,5 @@ dependencies: - mpmath - tensorboard - scikit-spatial + - pip: + - openpmd-api diff --git a/install/mala_cpu_environment.yml b/install/mala_cpu_environment.yml index 97fb82bd8..eaf4b88bc 100644 --- a/install/mala_cpu_environment.yml +++ b/install/mala_cpu_environment.yml @@ -63,8 +63,8 @@ dependencies: - libdeflate=1.14 - libffi=3.4.2 - libgcc-ng=12.2.0 - - libgfortran-ng=12.2.0 - - libgfortran5=12.2.0 + - libgfortran-ng=12.3.0 + - libgfortran5=12.3.0 - libgrpc=1.51.1 - libhwloc=2.8.0 - libiconv=1.17 @@ -95,7 +95,7 @@ dependencies: - numpy=1.24.0 - oauthlib=3.2.2 - openjpeg=2.5.0 - - openssl=3.0.7 + - openssl=3.3.1 - optuna=3.0.5 - packaging=22.0 - pandas=1.5.2 @@ -113,7 +113,7 @@ dependencies: - pyparsing=3.0.9 - pyperclip=1.8.2 - pysocks=1.7.1 - - python=3.8.15 + - python=3.8.16 - python-dateutil=2.8.2 - python_abi=3.8 - pytorch=1.13.0 @@ -153,4 +153,6 @@ dependencies: - yarl=1.8.1 - zipp=3.11.0 - zlib=1.2.13 - - zstd=1.5.2 + - zstd=1.5.5 + - pip: + - openpmd-api==0.15.2 diff --git a/mala/common/physical_data.py b/mala/common/physical_data.py index e756e96d1..7ec85623d 100644 --- a/mala/common/physical_data.py +++ b/mala/common/physical_data.py @@ -642,6 +642,11 @@ def write_to_openpmd_iteration( # Third loop: Extra flushes to harmonize ranks for _ in range(extra_flushes): + # This following line is a workaround for issue + # https://github.com/openPMD/openPMD-api/issues/1616 + # Fixed in openPMD-api 0.16 by + # https://github.com/openPMD/openPMD-api/pull/1619 + iteration.dt = iteration.dt iteration.series_flush() iteration.close(flush=True) diff --git a/mala/datahandling/data_repo.py b/mala/datahandling/data_repo.py index 178872b60..203885c12 100644 --- a/mala/datahandling/data_repo.py +++ b/mala/datahandling/data_repo.py @@ -14,9 +14,11 @@ name = "MALA_DATA_REPO" if name in os.environ: data_repo_path = os.environ[name] + data_path = os.path.join(data_repo_path, "Be2") else: parallel_warn( f"Environment variable {name} not set. You won't be able " "to run all examples and tests." ) data_repo_path = None + data_path = None diff --git a/requirements.txt b/requirements.txt index b8c1d7b64..b784a6c69 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,5 @@ optuna scipy pandas tensorboard -openpmd-api>=0.15 +openpmd-api scikit-spatial diff --git a/test/all_lazy_loading_test.py b/test/all_lazy_loading_test.py index f5cc74006..065cbb86e 100644 --- a/test/all_lazy_loading_test.py +++ b/test/all_lazy_loading_test.py @@ -7,9 +7,7 @@ import torch import pytest -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # This test compares the data scaling using the regular scaling procedure and # the lazy-loading one (incremental fitting). diff --git a/test/basic_gpu_test.py b/test/basic_gpu_test.py index 943862b3d..dcd588ad1 100644 --- a/test/basic_gpu_test.py +++ b/test/basic_gpu_test.py @@ -6,9 +6,9 @@ which MALA relies on). Two things are tested: 1. Whether or not your system has GPU support. -2. Whether or not the GPU does what it is supposed to. For this, +2. Whether or not the GPU does what it is supposed to. For this, a training is performed. It is measured whether or not the utilization -of the GPU results in a speed up. +of the GPU results in a speed up. """ import os import time @@ -19,9 +19,7 @@ import pytest import torch -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path test_checkpoint_name = "test" diff --git a/test/checkpoint_hyperopt_test.py b/test/checkpoint_hyperopt_test.py index f3435e7ab..28889c2df 100644 --- a/test/checkpoint_hyperopt_test.py +++ b/test/checkpoint_hyperopt_test.py @@ -4,9 +4,7 @@ from mala import printout import numpy as np -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path checkpoint_name = "test_ho" diff --git a/test/checkpoint_training_test.py b/test/checkpoint_training_test.py index bf7f62090..4c56ed8eb 100644 --- a/test/checkpoint_training_test.py +++ b/test/checkpoint_training_test.py @@ -4,9 +4,8 @@ from mala import printout import numpy as np -from mala.datahandling.data_repo import data_repo_path +from mala.datahandling.data_repo import data_path -data_path = os.path.join(data_repo_path, "Be2") test_checkpoint_name = "test" # Define the accuracy used in the tests. diff --git a/test/complete_interfaces_test.py b/test/complete_interfaces_test.py index 127ba8f82..d793da77f 100644 --- a/test/complete_interfaces_test.py +++ b/test/complete_interfaces_test.py @@ -8,9 +8,7 @@ import pytest -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # This test checks whether MALA interfaces to other codes, mainly the ASE diff --git a/test/descriptor_test.py b/test/descriptor_test.py index 4a208f832..74cae40f5 100644 --- a/test/descriptor_test.py +++ b/test/descriptor_test.py @@ -6,9 +6,7 @@ import numpy as np import pytest -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # Accuracy of test. accuracy_descriptors = 5e-8 diff --git a/test/examples_test.py b/test/examples_test.py index 5d74ec164..b5aa9143a 100644 --- a/test/examples_test.py +++ b/test/examples_test.py @@ -1,61 +1,126 @@ """Test whether the examples are still working.""" import importlib +import os import runpy import pytest - @pytest.mark.examples class TestExamples: - def test_basic_ex01(self): - runpy.run_path("../examples/basic/ex01_train_network.py") + dir_path = os.path.dirname(__file__) + + def test_basic_ex01(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex01_train_network.py" + ) - def test_basic_ex02(self): - runpy.run_path("../examples/basic/ex02_test_network.py") + @pytest.mark.order(after="test_basic_ex01") + def test_basic_ex02(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex02_test_network.py" + ) - def test_basic_ex03(self): - runpy.run_path("../examples/basic/ex03_preprocess_data.py") + @pytest.mark.order(after="test_basic_ex01") + def test_basic_ex03(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex03_preprocess_data.py" + ) - def test_basic_ex04(self): - runpy.run_path("../examples/basic/ex04_hyperparameter_optimization.py") + @pytest.mark.order(after="test_basic_ex01") + def test_basic_ex04(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex04_hyperparameter_optimization.py" + ) - def test_basic_ex05(self): - runpy.run_path("../examples/basic/ex05_run_predictions.py") + @pytest.mark.order(after="test_basic_ex01") + def test_basic_ex05(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex05_run_predictions.py" + ) - def test_basic_ex06(self): - runpy.run_path("../examples/basic/ex06_ase_calculator.py") + @pytest.mark.order(after="test_basic_ex01") + def test_basic_ex06(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/basic/ex06_ase_calculator.py" + ) - def test_advanced_ex01(self): - runpy.run_path("../examples/advanced/ex01_checkpoint_training.py") + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex01(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/advanced/ex01_checkpoint_training.py" + ) - def test_advanced_ex02(self): - runpy.run_path("../examples/advanced/ex02_shuffle_data.py") + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex02(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/advanced/ex02_shuffle_data.py" + ) - def test_advanced_ex03(self): - runpy.run_path("../examples/advanced/ex03_tensor_board.py") + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex03(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/advanced/ex03_tensor_board.py" + ) - def test_advanced_ex04(self): - runpy.run_path("../examples/advanced/ex04_acsd.py") + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex04(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/advanced/ex04_acsd.py" + ) - def test_advanced_ex05(self): + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex05(self, tmp_path): + os.chdir(tmp_path) runpy.run_path( - "../examples/advanced/ex05_checkpoint_hyperparameter_optimization.py" + self.dir_path + + "/../examples/advanced/ex05_checkpoint_hyperparameter_optimization.py" ) - def test_advanced_ex06(self): + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex06(self, tmp_path): + os.chdir(tmp_path) runpy.run_path( - "../examples/advanced/ex06_distributed_hyperparameter_optimization.py" + self.dir_path + + "/../examples/advanced/ex06_distributed_hyperparameter_optimization.py" ) @pytest.mark.skipif( importlib.util.find_spec("oapackage") is None, reason="No OAT found on this machine, skipping this " "test.", ) - def test_advanced_ex07(self): + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex07(self, tmp_path): + os.chdir(tmp_path) runpy.run_path( - "../examples/advanced/ex07_advanced_hyperparameter_optimization.py" + self.dir_path + + "/../examples/advanced/ex07_advanced_hyperparameter_optimization.py" ) - def test_advanced_ex08(self): - runpy.run_path("../examples/advanced/ex08_visualize_observables.py") + @pytest.mark.order(after="test_basic_ex01") + def test_advanced_ex08(self, tmp_path): + os.chdir(tmp_path) + runpy.run_path( + self.dir_path + + "/../examples/advanced/ex08_visualize_observables.py" + ) diff --git a/test/hyperopt_test.py b/test/hyperopt_test.py index b2d93f872..bb003082a 100644 --- a/test/hyperopt_test.py +++ b/test/hyperopt_test.py @@ -7,9 +7,7 @@ import mala import numpy as np -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # Control how much the loss should be better after hyperopt compared to # before. This value is fairly high, but we're training on absolutely diff --git a/test/inference_test.py b/test/inference_test.py index 4e874570b..84e0e9cca 100644 --- a/test/inference_test.py +++ b/test/inference_test.py @@ -3,10 +3,8 @@ import numpy as np from mala import Tester, Runner -from mala.datahandling.data_repo import data_repo_path +from mala.datahandling.data_repo import data_path -data_path = os.path.join(data_repo_path, "Be2") -param_path = os.path.join(data_repo_path, "workflow_test/") accuracy_strict = 1e-16 accuracy_coarse = 5e-7 accuracy_very_coarse = 3 @@ -18,7 +16,7 @@ class TestInference: def test_unit_conversion(self): """Test that RAM inexpensive unit conversion works.""" parameters, network, data_handler = Runner.load_run( - "workflow_test", load_runner=False, path=param_path + "Be_model", load_runner=False, path=data_path ) parameters.data.use_lazy_loading = False parameters.running.mini_batch_size = 50 @@ -99,7 +97,7 @@ def test_inference_lazy_loading(self): def __run(use_lazy_loading=False, batchsize=46): # First we load Parameters and network. parameters, network, data_handler, tester = Tester.load_run( - "workflow_test", path=param_path + "Be_model", path=data_path ) parameters.data.use_lazy_loading = use_lazy_loading parameters.running.mini_batch_size = batchsize diff --git a/test/integration_test.py b/test/integration_test.py index b27abb872..e4e22ea95 100644 --- a/test/integration_test.py +++ b/test/integration_test.py @@ -6,7 +6,7 @@ import scipy as sp import pytest -from mala.datahandling.data_repo import data_repo_path +from mala.datahandling.data_repo import data_path # In order to test the integration capabilities of MALA we need a # QuantumEspresso @@ -18,7 +18,6 @@ # Scripts to reproduce the data files used in this test script can be found # in the data repo. -data_path = os.path.join(data_repo_path, "Be2") path_to_out = os.path.join(data_path, "Be_snapshot0.out") path_to_ldos_npy = os.path.join(data_path, "Be_snapshot0.out.npy") path_to_dos_npy = os.path.join(data_path, "Be_snapshot0.dos.npy") diff --git a/test/parallel_run_test.py b/test/parallel_run_test.py index 89b0cbad8..6ca5c8c8d 100644 --- a/test/parallel_run_test.py +++ b/test/parallel_run_test.py @@ -6,9 +6,7 @@ from ase.io import read import pytest -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # Control the various accuracies.. accuracy_snaps = 1e-4 diff --git a/test/scaling_test.py b/test/scaling_test.py index d43648430..b7925cd9f 100644 --- a/test/scaling_test.py +++ b/test/scaling_test.py @@ -4,9 +4,7 @@ import numpy as np import torch -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # This test checks that all scaling options are working and are not messing # up the data. diff --git a/test/shuffling_test.py b/test/shuffling_test.py index 202e40c9d..e637c7d2b 100644 --- a/test/shuffling_test.py +++ b/test/shuffling_test.py @@ -3,9 +3,7 @@ import mala import numpy as np -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # Accuracy for the shuffling test. accuracy = np.finfo(float).eps diff --git a/test/tensor_memory_test.py b/test/tensor_memory_test.py index 4a70d9719..b3cb25672 100644 --- a/test/tensor_memory_test.py +++ b/test/tensor_memory_test.py @@ -5,9 +5,7 @@ from torch.utils.data import TensorDataset from torch.utils.data import DataLoader -from mala.datahandling.data_repo import data_repo_path - -data_path = os.path.join(data_repo_path, "Be2") +from mala.datahandling.data_repo import data_path # Define the accuracy used in the tests. accuracy = 1e-5 diff --git a/test/workflow_test.py b/test/workflow_test.py index a652546fd..fa7dee018 100644 --- a/test/workflow_test.py +++ b/test/workflow_test.py @@ -5,9 +5,8 @@ import numpy as np import pytest -from mala.datahandling.data_repo import data_repo_path +from mala.datahandling.data_repo import data_path -data_path = os.path.join(data_repo_path, "Be2") # Control how much the loss should be better after training compared to # before. This value is fairly high, but we're training on absolutely # minimal amounts of data. @@ -382,7 +381,7 @@ def test_training_with_postprocessing_data_repo(self): """ # Load parameters, network and data scalers. parameters, network, data_handler, tester = mala.Tester.load_run( - "workflow_test", path=os.path.join(data_repo_path, "workflow_test") + "Be_model", path=data_path ) parameters.targets.target_type = "LDOS" @@ -431,7 +430,7 @@ def test_predictions(self): #################### parameters, network, data_handler, tester = mala.Tester.load_run( - "workflow_test", path=os.path.join(data_repo_path, "workflow_test") + "Be_model", path=data_path ) parameters.targets.target_type = "LDOS" parameters.targets.ldos_gridsize = 11 @@ -518,7 +517,7 @@ def test_total_energy_predictions(self): #################### parameters, network, data_handler, predictor = mala.Predictor.load_run( - "workflow_test", path=os.path.join(data_repo_path, "workflow_test") + "Be_model", path=data_path ) parameters.targets.target_type = "LDOS" parameters.targets.ldos_gridsize = 11