Refactor checking for cython availability

This commit is contained in:
Jayanth Koushik 2017-11-16 20:29:49 -05:00
parent 6605b736d8
commit 928559216c
3 changed files with 11 additions and 11 deletions

View file

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

View file

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

View file

@ -7,10 +7,10 @@ from .config import config
try: try:
from . import choleskies_cython from . import choleskies_cython
cython_choleskies_working = True use_choleskies_cython = config.getboolean('cython', 'working')
except ImportError: except ImportError:
print('warning in choleskies: failed to import cython module: falling back to numpy') print('warning in choleskies: failed to import cython module: falling back to numpy')
cython_choleskies_working = False use_choleskies_cython = False
def safe_root(N): def safe_root(N):
@ -103,7 +103,7 @@ def indexes_to_fix_for_low_rank(rank, size):
return np.setdiff1d(np.arange((size**2+size)/2), keep) return np.setdiff1d(np.arange((size**2+size)/2), keep)
if cython_choleskies_working and config.getboolean('cython', 'working'): if use_choleskies_cython:
triang_to_flat = _triang_to_flat_cython triang_to_flat = _triang_to_flat_cython
flat_to_triang = _flat_to_triang_cython flat_to_triang = _flat_to_triang_cython
backprop_gradient = choleskies_cython.backprop_gradient_par_c backprop_gradient = choleskies_cython.backprop_gradient_par_c