Skip to content

Commit

Permalink
Merge pull request #511 from mala-project/code_formatting_standard
Browse files Browse the repository at this point in the history
Adopting a code formatting standard
  • Loading branch information
RandomDefaultUser authored Apr 18, 2024
2 parents 62bbaeb + 2572538 commit 8af3efe
Show file tree
Hide file tree
Showing 92 changed files with 8,601 additions and 5,090 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cpu-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:
conda env export -n mala-cpu > env_1.yml
# install mala package
pip --no-cache-dir install -e .[opt,test]
pip --no-cache-dir install -e .[opt,test] --no-build-isolation
- name: Check if Conda environment meets the specified requirements
shell: 'bash -c "docker exec -i mala-cpu bash < {0}"'
Expand Down
9 changes: 8 additions & 1 deletion docs/source/CONTRIBUTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ the core development team.
* If you're adding code that should be tested, add tests
* If you're adding or modifying examples, make sure to add them to `test_examples.py`

### Formatting code

* MALA uses [`black`](https://github.com/psf/black) for code formatting
* The `black` configuration is located in `pyproject.toml`
* Currently, no automatic code reformatting will be done in the CI, thus
please ensure that your code is properly formatted before creating a pull
request

### Adding dependencies

If you add additional dependencies, make sure to add them to `requirements.txt`
Expand All @@ -98,7 +106,6 @@ they are not.
Further, in order for them to be available during the CI tests, make sure to
add _required_ dependencies to the appropriate environment files in folder `install/` and _extra_ requirements directly in the `Dockerfile` for the `conda` environment build.


## Pull Requests
We actively welcome pull requests.
1. Fork the repo and create your branch from `develop`
Expand Down
33 changes: 23 additions & 10 deletions examples/advanced/ex01_checkpoint_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

"""
Expand Down Expand Up @@ -35,15 +36,27 @@ def initial_setup():
parameters.running.checkpoint_name = "ex01_checkpoint"

data_handler = mala.DataHandler(parameters)
data_handler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path, "tr")
data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "va")
data_handler.add_snapshot(
"Be_snapshot0.in.npy",
data_path,
"Be_snapshot0.out.npy",
data_path,
"tr",
)
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"va",
)
data_handler.prepare_data()

parameters.network.layer_sizes = [data_handler.input_dimension,
100,
data_handler.output_dimension]
parameters.network.layer_sizes = [
data_handler.input_dimension,
100,
data_handler.output_dimension,
]

test_network = mala.Network(parameters)
test_trainer = mala.Trainer(parameters, test_network, data_handler)
Expand All @@ -52,12 +65,12 @@ def initial_setup():


if mala.Trainer.run_exists("ex01_checkpoint"):
parameters, network, datahandler, trainer = \
mala.Trainer.load_run("ex01_checkpoint")
parameters, network, datahandler, trainer = mala.Trainer.load_run(
"ex01_checkpoint"
)
printout("Starting resumed training.")
else:
parameters, network, datahandler, trainer = initial_setup()
printout("Starting original training.")

trainer.train_network()

15 changes: 9 additions & 6 deletions examples/advanced/ex02_shuffle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
parameters.data.shuffling_seed = 1234

data_shuffler = mala.DataShuffler(parameters)
data_shuffler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path)
data_shuffler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path)
data_shuffler.shuffle_snapshots(complete_save_path=".",
save_name="Be_shuffled*")
data_shuffler.add_snapshot(
"Be_snapshot0.in.npy", data_path, "Be_snapshot0.out.npy", data_path
)
data_shuffler.add_snapshot(
"Be_snapshot1.in.npy", data_path, "Be_snapshot1.out.npy", data_path
)
data_shuffler.shuffle_snapshots(
complete_save_path=".", save_name="Be_shuffled*"
)
26 changes: 17 additions & 9 deletions examples/advanced/ex03_tensor_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")


Expand All @@ -29,17 +30,24 @@


data_handler = mala.DataHandler(parameters)
data_handler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path, "tr")
data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "va")
data_handler.add_snapshot(
"Be_snapshot0.in.npy", data_path, "Be_snapshot0.out.npy", data_path, "tr"
)
data_handler.add_snapshot(
"Be_snapshot1.in.npy", data_path, "Be_snapshot1.out.npy", data_path, "va"
)
data_handler.prepare_data()
parameters.network.layer_sizes = [data_handler.input_dimension,
100,
data_handler.output_dimension]
parameters.network.layer_sizes = [
data_handler.input_dimension,
100,
data_handler.output_dimension,
]

network = mala.Network(parameters)
trainer = mala.Trainer(parameters, network, data_handler)
trainer.train_network()
printout("Run finished, launch tensorboard with \"tensorboard --logdir " +
trainer.full_visualization_path + "\"")
printout(
'Run finished, launch tensorboard with "tensorboard --logdir '
+ trainer.full_visualization_path
+ '"'
)
22 changes: 15 additions & 7 deletions examples/advanced/ex04_acsd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os

import mala
import numpy as np
from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

"""
Expand All @@ -29,12 +29,20 @@
# When adding data for the ACSD analysis, add preprocessed LDOS data for
# and a calculation output for the descriptor calculation.
####################
hyperoptimizer.add_snapshot("espresso-out", os.path.join(data_path, "Be_snapshot1.out"),
"numpy", os.path.join(data_path, "Be_snapshot1.out.npy"),
target_units="1/(Ry*Bohr^3)")
hyperoptimizer.add_snapshot("espresso-out", os.path.join(data_path, "Be_snapshot2.out"),
"numpy", os.path.join(data_path, "Be_snapshot2.out.npy"),
target_units="1/(Ry*Bohr^3)")
hyperoptimizer.add_snapshot(
"espresso-out",
os.path.join(data_path, "Be_snapshot1.out"),
"numpy",
os.path.join(data_path, "Be_snapshot1.out.npy"),
target_units="1/(Ry*Bohr^3)",
)
hyperoptimizer.add_snapshot(
"espresso-out",
os.path.join(data_path, "Be_snapshot2.out"),
"numpy",
os.path.join(data_path, "Be_snapshot2.out.npy"),
target_units="1/(Ry*Bohr^3)",
)

# If you plan to plot the results (recommended for exploratory searches),
# the optimizer can return the necessary quantities to plot.
Expand Down
47 changes: 30 additions & 17 deletions examples/advanced/ex05_checkpoint_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import mala
from mala import printout

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

"""
Expand All @@ -29,34 +29,47 @@ def initial_setup():
parameters.hyperparameters.checkpoint_name = "ex05_checkpoint"

data_handler = mala.DataHandler(parameters)
data_handler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path, "tr")
data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "va")
data_handler.add_snapshot(
"Be_snapshot0.in.npy",
data_path,
"Be_snapshot0.out.npy",
data_path,
"tr",
)
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"va",
)
data_handler.prepare_data()

hyperoptimizer = mala.HyperOpt(parameters, data_handler)
hyperoptimizer.add_hyperparameter("float", "learning_rate",
0.0000001, 0.01)
hyperoptimizer.add_hyperparameter(
"float", "learning_rate", 0.0000001, 0.01
)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_00", 10, 100)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_01", 10, 100)
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_00",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_01",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_02",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)

return parameters, data_handler, hyperoptimizer


if mala.HyperOptOptuna.checkpoint_exists("ex05_checkpoint"):
parameters, datahandler, hyperoptimizer = \
mala.HyperOptOptuna.resume_checkpoint(
"ex05_checkpoint")
parameters, datahandler, hyperoptimizer = (
mala.HyperOptOptuna.resume_checkpoint("ex05_checkpoint")
)
else:
parameters, datahandler, hyperoptimizer = initial_setup()

# Perform hyperparameter optimization.
hyperoptimizer.perform_study()

46 changes: 28 additions & 18 deletions examples/advanced/ex06_distributed_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import mala
from mala import printout

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

"""
Expand Down Expand Up @@ -36,7 +36,7 @@
parameters.hyperparameters.checkpoint_name = "ex06"
parameters.hyperparameters.hyper_opt_method = "optuna"
parameters.hyperparameters.study_name = "ex06"
parameters.hyperparameters.rdb_storage = 'sqlite:///ex06.db'
parameters.hyperparameters.rdb_storage = "sqlite:///ex06.db"

# Hyperparameter optimization can be further refined by using ensemble training
# at each step and by using a different metric then the validation loss
Expand All @@ -50,27 +50,37 @@

data_handler = mala.DataHandler(parameters)

data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "tr",
calculation_output_file=
os.path.join(data_path, "Be_snapshot1.out"))
data_handler.add_snapshot("Be_snapshot2.in.npy", data_path,
"Be_snapshot2.out.npy", data_path, "va",
calculation_output_file=
os.path.join(data_path, "Be_snapshot2.out"))
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"tr",
calculation_output_file=os.path.join(data_path, "Be_snapshot1.out"),
)
data_handler.add_snapshot(
"Be_snapshot2.in.npy",
data_path,
"Be_snapshot2.out.npy",
data_path,
"va",
calculation_output_file=os.path.join(data_path, "Be_snapshot2.out"),
)
data_handler.prepare_data()


hyperoptimizer = mala.HyperOpt(parameters, data_handler)
hyperoptimizer.add_hyperparameter("float", "learning_rate",
0.0000001, 0.01)
hyperoptimizer.add_hyperparameter("float", "learning_rate", 0.0000001, 0.01)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_00", 10, 100)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_01", 10, 100)
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_00",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_01",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_02",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.perform_study()
hyperoptimizer.set_optimal_parameters()
Loading

0 comments on commit 8af3efe

Please sign in to comment.