fix: dev: cython import errors

This commit fixes issues observed in Windows where some
cython modules are successfully imported, and some are not.
This causes the global config cython.working to be inconsistent,
which causes import errors when unavailable cython modules
are tried to be imported (example
https://github.com/SheffieldML/GPy/issues/266). This commit uses
a separate flag for each module to fix the issue.
This commit is contained in:
Jayanth Koushik 2017-10-23 15:58:17 -04:00
parent 754c67f71d
commit a7af12e6ea
5 changed files with 32 additions and 21 deletions

View file

@ -6,11 +6,14 @@ import numpy as np
from ...core.parameterization import Param
from paramz.transformations import Logexp
from ...util.config import config # for assesing whether to use cython
try:
from . import coregionalize_cython
config.set('cython', 'working', 'True')
cython_coregionalize_working = True
except ImportError:
config.set('cython', 'working', 'False')
print('warning in coregionalize: failed to import cython module: falling back to numpy')
cython_coregionalize_working = False
class Coregionalize(Kern):
"""
@ -61,7 +64,7 @@ class Coregionalize(Kern):
self.B = np.dot(self.W, self.W.T) + np.diag(self.kappa)
def K(self, X, X2=None):
if config.getboolean('cython', 'working'):
if cython_coregionalize_working and config.getboolean('cython', 'working'):
return self._K_cython(X, X2)
else:
return self._K_numpy(X, X2)
@ -92,7 +95,7 @@ class Coregionalize(Kern):
index2 = np.asarray(X2, dtype=np.int)
#attempt to use cython for a nasty double indexing loop: fall back to numpy
if config.getboolean('cython', 'working'):
if cython_coregionalize_working and config.getboolean('cython', 'working'):
dL_dK_small = self._gradient_reduce_cython(dL_dK, index, index2)
else:
dL_dK_small = self._gradient_reduce_numpy(dL_dK, index, index2)

View file

@ -14,9 +14,10 @@ from paramz.transformations import Logexp
try:
from . import stationary_cython
cython_stationary_working = True
except ImportError:
print('warning in stationary: failed to import cython module: falling back to numpy')
config.set('cython', 'working', 'false')
cython_stationary_working = False
class Stationary(Kern):
@ -196,7 +197,7 @@ class Stationary(Kern):
tmp = dL_dr*self._inv_dist(X, X2)
if X2 is None: X2 = X
if config.getboolean('cython', 'working'):
if cython_stationary_working and config.getboolean('cython', 'working'):
self.lengthscale.gradient = self._lengthscale_grads_cython(tmp, X, X2)
else:
self.lengthscale.gradient = self._lengthscale_grads_pure(tmp, X, X2)
@ -239,7 +240,7 @@ class Stationary(Kern):
"""
Given the derivative of the objective wrt K (dL_dK), compute the derivative wrt X
"""
if config.getboolean('cython', 'working'):
if cython_stationary_working and config.getboolean('cython', 'working'):
return self._gradients_X_cython(dL_dK, X, X2)
else:
return self._gradients_X_pure(dL_dK, X, X2)