Skip to content

Commit

Permalink
Merge pull request #57 from jajcayn/feature/python3.9
Browse files Browse the repository at this point in the history
Support python 3.9
  • Loading branch information
jajcayn authored May 18, 2021
2 parents da682aa + 725aace commit 62a395b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/pythontesting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ name: pytest

on:
push:
branches:
- "*"
pull_request:
branches:
- "*"

jobs:
build:
Expand All @@ -18,14 +14,20 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: [3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install MacOS dependencies
if: startsWith(matrix.os, 'macos')
run: |
set -e
brew update
brew install hdf5 c-blosc
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://github.com/jajcayn/pygpso/workflows/pytest/badge.svg)](https://github.com/jajcayn/pygpso/actions) ![](https://img.shields.io/github/v/release/jajcayn/pygpso) [![codecov](https://codecov.io/gh/jajcayn/pygpso/branch/master/graph/badge.svg)](https://codecov.io/gh/jajcayn/pygpso) [![PyPI license](https://img.shields.io/pypi/l/pygpso.svg)](https://pypi.python.org/pypi/pygpso/) [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jajcayn/pygpso.git/master?filepath=examples) [![DOI](https://zenodo.org/badge/236983676.svg)](https://zenodo.org/badge/latestdoi/236983676) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

# pyGPSO

*Optimise anything (but mainly large-scale biophysical models) using Gaussian Processes surrogate*

`pyGPSO` is a python package for Gaussian-Processes Surrogate Optimisation. GPSO is a Bayesian optimisation method designed to cope with costly, high-dimensional, non-convex problems by switching between exploration of the parameter space (using partition tree) and exploitation of the gathered knowledge (by training the surrogate function using Gaussian Processes regression). The motivation for this method stems from the optimisation of large-scale biophysical models in neuroscience when the modelled data should match the experimental one. This package leverages [`GPFlow`](https://github.com/GPflow/GPflow) for training and predicting the Gaussian Processes surrogate.
Expand All @@ -21,17 +22,32 @@ Example of ternary partition tree after optimisation.

## Installation

`GPSO` package is tested and should run without any problems on python versions 3.6 -- 3.8.
`GPSO` package is tested and should run without any problems on python versions 3.6 -- 3.9.

### Note on python3.9 with macOS

Installing `pytables` might give you hdf5 errors. If this is the case, please do

```bash
brew install hdf5 c-blosc
```

and all should work like a charm afterwards.

### One-liner

For those who want to optimise right away just

```bash
pip install pygpso
```

and go ahead! Make sure to check example notebooks in [the **examples** directory](examples/) to see how it works and what it can do. Or, alternatively, you can run interactive notebooks in binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jajcayn/pygpso.git/master?filepath=examples)

### Go proper

When you are the type of girl or guy who likes to install packages properly, start by cloning (or forking) this repository, then installing all the dependencies and finally install the package itself

```bash
git clone https://github.com/jajcayn/pygpso
cd pygpso/
Expand All @@ -40,15 +56,19 @@ pip install -r requirements.txt
pip install -r requirements_optional.txt
pip install .
```

Don't forget to test!

```bash
pytest
```

## Usage

A guide on how to optimise and what can be done using this package is given as jupyter notebooks in [the **examples** directory](examples/). You can also try them out live thanks to binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jajcayn/pygpso.git/master?filepath=examples).

The basic idea is to initialise the parameter space in which the optimisation is to be run and then iteratively dig deeper and evaluate the objective function when necessary

```python
from gpso import ParameterSpace, GPSOptimiser

Expand All @@ -72,19 +92,23 @@ best_point = opt.run(objective_function)
The package also offers plotting functions for visualising the results. Again, those are documented and showcased in [the **examples** directory](examples/).

### Notes

Gaussian Processes regression uses normalised coordinates within the bounds [0, 1]. All normalisation and de-normalisation is done automatically, however when you want to call `predict_y` on GPR model, do not forget to pass normalised coordinates. The normalisation is handled by `sklearn.MinMaxScaler` and `ParameterSpace` instance offers a convenience functions for this: `ParameterSpace.normalise_coords(orig_coords)` and `ParameterSpace.denormalise_coords(normed_coords)`.

Plotting of the ternary tree (`gpso.plotting.plot_ternary_tree()`) requires `igraph` package, whose layout function is exploited. If you want to see the resulting beautiful tree, please install `python-igraph`.

Support of saver (for saving models run, e.g. timeseries along with the optimisation) is provided by `PyTables` (and `pandas` if you're saving results to `DataFrame`s).

## Known bugs and future improvements

* saving of GP surrogate is now hacky, as `GPFlow` supports only saving model for future prediction but AFAIK they cannot be trained anymore, since the information on kernels and mean-functions are not saved (only the trained weights in the computational graph). Thus, `pyGPSO` still relies on hacky saving to `pkl` files and recreating kernels and mean-function on-the-go when loading from saved.

## Final notes

When you encounter a bug or have any idea for an improvement, please open an issue and/or contact me.

When using this package in publications, please cite the original Jonathan's paper for the methodology as

```bibtex
@article{hadida2018bayesian,
title={Bayesian Optimisation of Large-Scale Biophysical Networks},
Expand All @@ -96,4 +120,5 @@ When using this package in publications, please cite the original Jonathan's pap
publisher={Elsevier}
}
```

and acknowledge the usage of this software via its DOI: [![DOI](https://zenodo.org/badge/236983676.svg)](https://zenodo.org/badge/latestdoi/236983676). After clicking, you will see citation data.
4 changes: 2 additions & 2 deletions gpso/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def run(self, optimiser):
if self.first_update:
ckpt = tf.train.Checkpoint(
model=optimiser.gp_surr.gpflow_model,
evluations=self.n_evals,
evaluations=self.n_evals,
)
self.manager = tf.train.CheckpointManager(
ckpt, self.path, max_to_keep=self.max_to_keep
Expand All @@ -150,4 +150,4 @@ def run(self, optimiser):
self.first_update = False
else:
saved_to = self.manager.save()
logging.debug(f"Checkppoint saved to {saved_to}")
logging.debug(f"Checkpoint saved to {saved_to}")
5 changes: 3 additions & 2 deletions gpso/param_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def _replace_normed_coord(self, index, new_coord):
def ternary_split(self):
"""
Parcellate this leaf using the ternary split function along the largest
dimension. For discussion why ternary is the best choise see the paper.
dimension. For the discussion on why ternary is the best choice, see the
paper.
Hadida, J., Sotiropoulos, S. N., Abeysuriya, R. G., Woolrich, M. W., &
Jbabdi, S. (2018). Bayesian Optimisation of Large-Scale Biophysical
Expand Down Expand Up @@ -436,7 +437,7 @@ def normalise_coords(self, orig_coords):

def denormalise_coords(self, normed_coords):
"""
Deormalise normed coordinates.
Denormalise normed coordinates.
:param normed_coords: normed coordinates in the parameter space as
[n points x ndim]
Expand Down
2 changes: 1 addition & 1 deletion gpso/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def plot_ternary_tree(
:type param_space: `gpso.param_space.ParameterSpace`
:param cmap: colormap for scores
:type cmap: str
:param cmap_limits: limits for the colormapping of the scores, if None will
:param cmap_limits: limits for the color mapping of the scores, if None will
be inferred from the data
:param center_root_node: whether to center root node within the figure -
graph might become less readable
Expand Down
8 changes: 4 additions & 4 deletions gpso/saving_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, filename, extras=None):
:param filename: filename for the HDF file
:type filename: str
:param extras: extras to write as a group into HDF file, e.g. default
parameters of the model, which are not subject to optimisaion
parameters of the model, which are not subject to optimisation
:type extras: dict|None
"""
if not filename.endswith(H5_EXT):
Expand Down Expand Up @@ -54,7 +54,7 @@ def save_runs(self, result, score, parameters):
:param result: result(s) of the run, can be single result or multiple
results (same parameters, different results, typically valid for
stochastic systems)
:type result: any pytables supported + pd.DataFrame|list of thereof
:type result: any `pytables` supported + pd.DataFrame|list of thereof
:param score: score(s) of the run
:type score: float|list[float]
:param parameters: parameters for this particular run(s)
Expand Down Expand Up @@ -100,7 +100,7 @@ def _write_result(self, group, result, score):
:param group: group to write to
:type group: `tables.group.Group`
:param result: result to write
:type result: any pytables supported + pd.DataFrame
:type result: any `pytables` supported + pd.DataFrame
:param score: score to write
:type score: float
"""
Expand Down Expand Up @@ -138,7 +138,7 @@ def table_reader(filename):
:param filename: filename of the HDF file
:type filename: str
:return: results, scores, parameters and extras if present; if mutliple
:return: results, scores, parameters and extras if present; if multiple
runs per parameter set, that item is a list itself
:rtype: (list,list,list,dict|None)
"""
Expand Down

0 comments on commit 62a395b

Please sign in to comment.