diff --git a/.gitignore b/.gitignore index afd089b49..e0035356c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ junit-*.xml __pycache__ brainiak/fcma/cython_blas.c +brainiak/eventseg/_utils.c diff --git a/brainiak/eventseg/_utils.pyx b/brainiak/eventseg/_utils.pyx new file mode 100644 index 000000000..ebcc94ac3 --- /dev/null +++ b/brainiak/eventseg/_utils.pyx @@ -0,0 +1,31 @@ +# Copyright 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from libc.math cimport log +import numpy as np +cimport numpy as np + +def masked_log(x): + """x is a 1D numpy array""" + """returns -Inf for x <=0 and log(x) otherwise""" + y = np.empty(x.shape, dtype=x.dtype) + lim = x.shape[0] + for i in range(lim): + if x[i] <= 0: + y[i] = float('-inf') + else: + y[i] = log(x[i]) + return y + diff --git a/brainiak/eventseg/event.py b/brainiak/eventseg/event.py index 7a46f909b..d864f5490 100644 --- a/brainiak/eventseg/event.py +++ b/brainiak/eventseg/event.py @@ -27,6 +27,7 @@ import copy from sklearn.base import BaseEstimator from sklearn.utils.validation import check_is_fitted, check_array +import brainiak.eventseg.utils as utils logger = logging.getLogger(__name__) @@ -299,8 +300,11 @@ def _log(self, x): log_ma: ndarray of floats log of x, with x<=0 values replaced with -inf """ - log_ma = np.ma.log(x).filled(float("-inf")) - return log_ma + + xshape = x.shape + _x = x.flatten() + y = utils.masked_log(_x) + return y.reshape(xshape) def find_events(self, testing_data, var=None, scramble=False): """Applies learned event segmentation to new testing dataset diff --git a/brainiak/hyperparamopt/__init__.py b/brainiak/hyperparamopt/__init__.py index 7867be74a..4bdf3b1af 100644 --- a/brainiak/hyperparamopt/__init__.py +++ b/brainiak/hyperparamopt/__init__.py @@ -1,4 +1 @@ """ Hyper parameter optimization package """ - -import pyximport -pyximport.install() diff --git a/setup.py b/setup.py index 704090f98..fdb5b76e4 100644 --- a/setup.py +++ b/setup.py @@ -19,56 +19,23 @@ long_description = f.read() -class get_pybind_include(object): - """Helper class to determine the pybind11 include path - - The purpose of this class is to postpone importing pybind11 - until it is actually installed, so that the ``get_include()`` - method can be invoked. """ - - def __init__(self, user=False): - self.user = user - - # Required by Cython - def __add__(self, x): - import pybind11 - return pybind11.get_include(self.user) + x - - def __str__(self): - import pybind11 - return pybind11.get_include(self.user) - - def endswith(self, x): - import pybind11 - return pybind11.get_include(self.user).endswith(x) - - def startswith(self, x): - import pybind11 - return pybind11.get_include(self.user).startswith(x) - ext_modules = [ Extension( 'brainiak.factoranalysis.tfa_extension', ['brainiak/factoranalysis/tfa_extension.cpp'], - include_dirs=[ - # Path to pybind11 headers - get_pybind_include(), - get_pybind_include(user=True) - ], ), Extension( 'brainiak.fcma.fcma_extension', ['brainiak/fcma/src/fcma_extension.cc'], - include_dirs=[ - # Path to pybind11 headers - get_pybind_include(), - get_pybind_include(user=True) - ], ), Extension( 'brainiak.fcma.cython_blas', ['brainiak/fcma/cython_blas.pyx'], ), + Extension( + 'brainiak.eventseg.utils', + ['brainiak/eventseg/_utils.pyx'], + ), ] @@ -133,10 +100,24 @@ def build_extensions(self): ext.extra_link_args.append(cpp_flag(self.compiler)) build_ext.build_extensions(self) + def finalize_options(self): + super().finalize_options() + import numpy + import pybind11 + self.include_dirs.extend([ + numpy.get_include(), + pybind11.get_include(user=True), + pybind11.get_include(), + ]) + + setup( name='brainiak', use_scm_version=True, setup_requires=[ + 'cython', + 'numpy', + 'pybind11>=1.7', 'setuptools_scm', ], install_requires=[