-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from MachineLearningLifeScience/dev
Better testing, and more objective functions
- Loading branch information
Showing
130 changed files
with
91,788 additions
and
1,124 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
.github/workflows/python-tox-testing-including-conda-on-master.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Test (master, conda, python 3.9) | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build-linux: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
max-parallel: 5 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python 3.9 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: '3.9' | ||
- name: Add conda to system path | ||
run: | | ||
# $CONDA is an environment variable pointing to the root of the miniconda directory | ||
echo $CONDA/bin >> $GITHUB_PATH | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install tox | ||
- name: Test linting with tox | ||
run: | | ||
tox -c tox.master.ini -e lint | ||
- name: Test poli-base with tox | ||
run: | | ||
tox -c tox.master.ini -e poli-base-py39 | ||
- name: Test poli-chem with tox | ||
run: | | ||
tox -c tox.master.ini -e poli-chem-py39 | ||
- name: Test poli-protein with tox | ||
run: | | ||
tox -c tox.master.ini -e poli-protein-py39 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Contributing to `poli` | ||
|
||
![Linting: black](https://img.shields.io/badge/Linting-black-black) | ||
![Testing: pytest](https://img.shields.io/badge/Testing-pytest-blue) | ||
![Testing: tox](https://img.shields.io/badge/Testing-tox-blue) | ||
![Main branch: black](https://img.shields.io/badge/Pull_request_to-dev-blue) | ||
|
||
This note details how to contribute to `poli`. | ||
|
||
## Forking and making pull requests | ||
|
||
The main development branch is called `dev`. To contribute, we recommend creating a fork of this repository and making changes on your version. Once you are ready to contribute, we expect you to lint and test. | ||
|
||
## Linting your changes | ||
|
||
We expect you to lint the code you write or modify using `black`. | ||
|
||
```bash | ||
pip install black | ||
black ./path/to/files | ||
``` | ||
|
||
## Testing your changes for `dev`` | ||
|
||
Since we are testing multiple conda environments, we settled for using a combination of `tox` and `pytest`. | ||
|
||
```bash | ||
pip install tox | ||
|
||
# To test linting (from the root of the project) | ||
tox -c tox.dev.ini -e lint | ||
|
||
# To test in the base environment for poli | ||
tox -c tox.dev.ini -e poli-base-py39 | ||
``` | ||
|
||
If you want to run tests in all environments, remove `-e poli-base-py39` and just run `tox`. | ||
|
||
## More thorough testing | ||
|
||
In many cases, testing with the instructions above should be enough. However, since we are dealing with creating conda environments, the definite test comes by building the Docker image specified in `Dockerfile.test`, and running it. | ||
|
||
When contributing to the `@master` branch (i.e. to release), we will run these tests. | ||
|
||
## Create a pull request to dev | ||
|
||
Once all tests pass and you are ready to share your changes, create a pull request to the `dev` branch. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This dockerfile allows us to run the tests in a container | ||
FROM --platform=linux/amd64 continuumio/anaconda3:latest | ||
|
||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Copying the files from the host to the container | ||
COPY ./src /app/src | ||
COPY ./pyproject.toml /app/ | ||
COPY ./setup.cfg /app/ | ||
COPY ./requirements.txt /app/ | ||
COPY ./requirements-dev.txt /app/ | ||
COPY ./tox.ini /app/ | ||
|
||
# Installing distutils | ||
RUN apt-get update && \ | ||
apt-get install build-essential -y && \ | ||
apt-get install -y python3.9-distutils | ||
|
||
# Installing python dependencies | ||
RUN conda --version | ||
RUN pip install -r requirements.txt | ||
RUN pip install -r requirements-dev.txt | ||
|
||
# Creating the relevant conda environments | ||
# For chem | ||
RUN conda env create --file src/poli/objective_repository/rdkit_qed/environment.yml | ||
|
||
# For proteins | ||
RUN conda env create --file src/poli/objective_repository/foldx_stability/environment.yml | ||
RUN conda env create --file src/poli/objective_repository/rasp/environment.yml | ||
|
||
# Running the tests | ||
CMD ["tox"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
examples/adding_a_wandb_observer/example_logging_rdkit_qed_using_wandb.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
This script uses the wandb observer to log some examples of the rdkit_qed objective function. | ||
To run this example, you will need to install wandb: | ||
pip install wandb | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from poli import objective_factory | ||
|
||
from wandb_observer import WandbObserver | ||
|
||
THIS_DIR = Path(__file__).parent.resolve() | ||
|
||
if __name__ == "__main__": | ||
# Defining the observer | ||
observer = WandbObserver() | ||
|
||
# Initializing a QED objective function. | ||
alphabet = ["", "[C]", "..."] | ||
problem_info, f, x0, y0, run_info = objective_factory.create( | ||
name="rdkit_qed", | ||
observer=observer, | ||
alphabet=alphabet, | ||
string_representation="SELFIES", | ||
caller_info={"run_id": None, "experiment_id": None}, | ||
) | ||
|
||
# Logging some examples | ||
# The observer will register each call to f. | ||
f(np.array([["[C]", "[C]"]])) | ||
f(np.array([["[C]", "[C]", "[C]"]])) | ||
f(np.array([["[C]", "[C]", "[C]", "[C]"]])) | ||
|
||
# Finishing the observer, which will log a table that's | ||
# being maintained. | ||
observer.finish() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""A simple example of how to log objective function calls using wandb. | ||
To run this example, you will need to install wandb: | ||
pip install wandb | ||
""" | ||
|
||
import numpy as np | ||
from poli.core.problem_setup_information import ProblemSetupInformation | ||
import wandb | ||
|
||
from poli.core.util.abstract_observer import AbstractObserver | ||
|
||
|
||
class WandbObserver(AbstractObserver): | ||
def __init__(self) -> None: | ||
# Log into wandb | ||
wandb.login() | ||
|
||
# Some variables to keep track of the run | ||
self.step = 0 | ||
self.x_table = wandb.Table(columns=["step", "x", "y"]) | ||
super().__init__() | ||
|
||
def initialize_observer( | ||
self, | ||
problem_setup_info: ProblemSetupInformation, | ||
caller_info: object, | ||
x0: np.ndarray, | ||
y0: np.ndarray, | ||
seed: int, | ||
) -> object: | ||
wandb.init( | ||
config={ | ||
"name": problem_setup_info.name, | ||
"max_sequence_length": problem_setup_info.max_sequence_length, | ||
"alphabet": problem_setup_info.alphabet, | ||
"x0": x0, | ||
"y0": y0, | ||
"seed": seed, | ||
}, | ||
) | ||
|
||
def observe(self, x: np.ndarray, y: np.ndarray, context=None) -> None: | ||
for x_i, y_i in zip(x.tolist(), y.tolist()): | ||
self.x_table.add_data(self.step, "".join(x_i), y_i) | ||
|
||
wandb.log({"table of sequences": self.x_table}) | ||
wandb.log({"y": y}, step=self.step) | ||
|
||
self.step += 1 | ||
|
||
def finish(self) -> None: | ||
wandb.finish() |
47 changes: 47 additions & 0 deletions
47
examples/adding_an_mlflow_observer/example_logging_rdkit_logp_using_mlflow.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
This script implements an example of how to use a simple | ||
MLFlow observer (implemented in ./mlflow_observer.py). Running | ||
this script will create a new experiment in ./mlruns. | ||
To run this example, you will need to install mlflow: | ||
pip install mlflow | ||
To check its results, you will need to start a ui: | ||
mlflow ui --backend-store-uri ./mlruns | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from poli import objective_factory | ||
|
||
from mlflow_observer import MlFlowObserver | ||
|
||
THIS_DIR = Path(__file__).parent.resolve() | ||
|
||
if __name__ == "__main__": | ||
# Defining the observer | ||
TRACKING_URI = THIS_DIR / "mlruns" | ||
observer = MlFlowObserver(tracking_uri=TRACKING_URI) | ||
|
||
# Initializing a logP objective function. | ||
alphabet = ["", "[C]", "..."] | ||
problem_info, f, x0, y0, run_info = objective_factory.create( | ||
name="rdkit_logp", | ||
observer=observer, | ||
alphabet=alphabet, | ||
string_representation="SELFIES", | ||
caller_info={"run_id": None, "experiment_id": None}, | ||
) | ||
|
||
# Logging some examples | ||
# The observer will register each call to f. | ||
f(np.array([["[C]", "[C]"]])) | ||
f(np.array([["[C]", "[C]", "[C]"]])) | ||
f(np.array([["[C]", "[C]", "[C]", "[C]"]])) | ||
|
||
# Finishing the observer, which will close the MLFlow run. | ||
observer.finish() |
Oops, something went wrong.