Skip to content

Commit

Permalink
Merge pull request #517 from pcagas/test-improvement
Browse files Browse the repository at this point in the history
Resolving pytest issues
  • Loading branch information
RandomDefaultUser authored Jun 7, 2024
2 parents a1faeba + 032b21b commit e967711
Show file tree
Hide file tree
Showing 31 changed files with 176 additions and 157 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/cpu-tests.yml
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -181,14 +182,14 @@ jobs:
# 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"
wget "https://rodare.hzdr.de/record/3004/files/mala-project/test-data-1.8.1.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
unzip -q test-data-1.8.1.zip
mv mala-project-test-data-741eda6 mala_data
- name: Test mala
shell: 'bash -c "docker exec -i mala-cpu bash < {0}"'
Expand Down
12 changes: 5 additions & 7 deletions examples/advanced/ex01_checkpoint_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +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

"""
Shows how a training run can be paused and
Shows how a training run can be paused and
resumed. Delete the ex07.zip file prior to execution to see the effect
of checkpointing.
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 total epochs cannot be divided by the number
of epochs after which a checkpoint is created without residual, this will
lead to MALA performing the missing epochs again.
of epochs after which a checkpoint is created without residual, this will
lead to MALA performing the missing epochs again.
"""


Expand Down
8 changes: 3 additions & 5 deletions examples/advanced/ex02_shuffle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""


Expand Down
7 changes: 2 additions & 5 deletions examples/advanced/ex03_tensor_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand Down
6 changes: 2 additions & 4 deletions examples/advanced/ex04_acsd.py
Original file line number Diff line number Diff line change
@@ -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).
"""

Expand Down
10 changes: 4 additions & 6 deletions examples/advanced/ex05_checkpoint_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions examples/advanced/ex08_visualize_observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions examples/basic/ex01_train_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +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

"""
This example shows how a neural network can be trained on material
data using this framework. It uses preprocessed data, that is read in
from *.npy files.
"""


####################
# 1. PARAMETERS
# The first step of each MALA workflow is to define a parameters object and
Expand Down Expand Up @@ -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
)
11 changes: 6 additions & 5 deletions examples/basic/ex02_test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions examples/basic/ex03_preprocess_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions examples/basic/ex04_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""


Expand Down
16 changes: 8 additions & 8 deletions examples/basic/ex05_run_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
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 potentiall the total energy module).
"""

model_name = "Be_model"
model_path = "./" if os.path.exists("Be_model.zip") else data_path


####################
# 1. LOADING A NETWORK
# To use the predictor class to test an ML-DFT model, simply load it via the
# 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
)


Expand Down
18 changes: 9 additions & 9 deletions examples/basic/ex06_ase_calculator.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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
# To use the ASE calculator, just load a MALA ML-DFT model.
# 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

####################
Expand Down
2 changes: 2 additions & 0 deletions mala/datahandling/data_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions test/all_lazy_loading_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 3 additions & 5 deletions test/basic_gpu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down
Loading

0 comments on commit e967711

Please sign in to comment.