Skip to content

Commit

Permalink
Fix/windows mogp (#107)
Browse files Browse the repository at this point in the history
* default to single process when running on windows

* made multi output tests only use a single processor for consistency

* incremented version number
  • Loading branch information
edaub authored Jun 17, 2020
1 parent d424bf6 commit 83a2d20
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
8 changes: 6 additions & 2 deletions mogp_emulator/MultiOutputGP.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from multiprocessing import Pool
import platform
import numpy as np
from mogp_emulator.GaussianProcess import GaussianProcess, PredictResult
from mogp_emulator.MeanFunction import MeanBase
Expand Down Expand Up @@ -137,8 +138,11 @@ def predict(self, testing, unc=True, deriv=True, processes=None):
processes = int(processes)
assert processes > 0, "number of processes must be a positive integer"

with Pool(processes) as p:
predict_vals = p.starmap(GaussianProcess.predict, [(gp, testing, unc, deriv) for gp in self.emulators])
if platform.system() == "Windows":
predict_vals = [GaussianProcess.predict(gp, testing, unc, deriv) for gp in self.emulators]
else:
with Pool(processes) as p:
predict_vals = p.starmap(GaussianProcess.predict, [(gp, testing, unc, deriv) for gp in self.emulators])

# repackage predictions into numpy arrays

Expand Down
11 changes: 8 additions & 3 deletions mogp_emulator/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mogp_emulator.MultiOutputGP import MultiOutputGP
from multiprocessing import Pool
from functools import partial
import platform

def fit_GP_MAP(*args, n_tries=15, theta0=None, method="L-BFGS-B", **kwargs):
"""
Expand Down Expand Up @@ -165,9 +166,13 @@ def _fit_MOGP_MAP(gp, n_tries=15, theta0=None, method='L-BFGS-B', **kwargs):

n_tries = int(n_tries)

with Pool(processes) as p:
fit_MOGP = p.starmap(partial(fit_GP_MAP, n_tries=n_tries, theta0=theta0, method=method, **kwargs),
[(emulator,) for emulator in gp.emulators])
if platform.system() == "Windows":
fit_MOGP = [fit_GP_MAP(emulator, n_tries=n_tries, theta0=theta0, method=method, **kwargs)
for emulator in gp.emulators]
else:
with Pool(processes) as p:
fit_MOGP = p.starmap(partial(fit_GP_MAP, n_tries=n_tries, theta0=theta0, method=method, **kwargs),
[(emulator,) for emulator in gp.emulators])

gp.emulators = fit_MOGP

Expand Down
14 changes: 7 additions & 7 deletions mogp_emulator/tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def test_fit_GP_MAP_MOGP():

theta_exp = np.zeros((2, 3))
theta_exp[0] = np.array([ 1.6030532031342832, -2.090511425471982 , -0.7803960307137198])
theta_exp[1] = np.array([ 1.2807839212942222, -0.5095550757153631, -2.009987161484929 ])
theta_exp[1] = np.array([ 1.414112951818647 , -0.5974688573393576, 0.6857536842773265])
logpost_exp = np.zeros(2)
logpost_exp[0] = -296.0297245831661
logpost_exp[1] = -249.82550942156362
logpost_exp[1] = -250.06025683867367
np.random.seed(4335)

gp = fit_GP_MAP(gp)
gp = fit_GP_MAP(gp, processes=1)

assert isinstance(gp, MultiOutputGP)
for i in range(2):
Expand All @@ -88,7 +88,7 @@ def test_fit_GP_MAP_MOGP():

np.random.seed(4335)

gp = fit_GP_MAP(x, y, mean="0.", use_patsy=False, method="L-BFGS-B")
gp = fit_GP_MAP(x, y, mean="0.", use_patsy=False, method="L-BFGS-B", processes=1)
assert isinstance(gp, MultiOutputGP)
for i in range(2):
assert_allclose(gp.emulators[i].theta, theta_exp[i])
Expand Down Expand Up @@ -176,13 +176,13 @@ def test_fit_MOGP_MAP_MOGP():

theta_exp = np.zeros((2, 3))
theta_exp[0] = np.array([ 1.6030532031342832, -2.090511425471982 , -0.7803960307137198])
theta_exp[1] = np.array([ 1.2807839212942222, -0.5095550757153631, -2.009987161484929 ])
theta_exp[1] = np.array([ 1.414112951818647 , -0.5974688573393576, 0.6857536842773265])
logpost_exp = np.zeros(2)
logpost_exp[0] = -296.0297245831661
logpost_exp[1] = -249.82550942156362
logpost_exp[1] = -250.06025683867367
np.random.seed(4335)

gp = _fit_MOGP_MAP(gp)
gp = _fit_MOGP_MAP(gp, processes=1)

assert isinstance(gp, MultiOutputGP)
for i in range(2):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MAJOR = 0
MINOR = 3
MICRO = 0
PRERELEASE = 9
PRERELEASE = 10
ISRELEASED = False
version = "{}.{}.{}".format(MAJOR, MINOR, MICRO)

Expand Down

0 comments on commit 83a2d20

Please sign in to comment.