Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/windows mogp #107

Merged
merged 3 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +74 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did these change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed these to make the test run on a single process to test that the single thread version gives the same results as the old version (and to ensure general consistency across systems). The lack of multiple threads changed the sequence of random seeds, meaning that the second emulator started from a different point, so this corrects that. This is really a regression test, so the numbers don't really mean anything, just that we get consistent results when starting from the same point.

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