From 6981c54de1c8d48be4038bac9f5f7103f7d2df25 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Thu, 22 Sep 2016 18:25:58 -0700 Subject: [PATCH 1/9] cython masked log --- brainiak/eventseg/_utils.pyx | 15 +++++++++++++++ brainiak/eventseg/event.py | 22 ++++++++++++++++++++++ setup.py | 4 ++++ 3 files changed, 41 insertions(+) create mode 100644 brainiak/eventseg/_utils.pyx diff --git a/brainiak/eventseg/_utils.pyx b/brainiak/eventseg/_utils.pyx new file mode 100644 index 000000000..0f16a4998 --- /dev/null +++ b/brainiak/eventseg/_utils.pyx @@ -0,0 +1,15 @@ +from libc.math cimport log +import numpy as np +cimport numpy as np + +def masked_log(x): + """log(x) for all x <= 0 and -Inf otherwise""" + y = np.empty(x.shape, dtype=x.dtype) + lim = x.shape[0] + for i in range(lim): + if x[i] >= 0: + y[i] = log(x[i]) + else: + y[i] = float('-inf') + return y + diff --git a/brainiak/eventseg/event.py b/brainiak/eventseg/event.py index 7a46f909b..da3ceb3e8 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,6 +300,27 @@ def _log(self, x): log_ma: ndarray of floats log of x, with x<=0 values replaced with -inf """ + + xshape = x.shape + _x = x.flatten() + y = utils.masked_log(_x) + return y.reshape(xshape) + + + def _log_old(self, x): + """Modified version of np.log that manually sets values <=0 to -inf + + Parameters + ---------- + x: ndarray of floats + Input to the log function + + Returns + ------- + 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 diff --git a/setup.py b/setup.py index a0eb1b366..6f8a95824 100644 --- a/setup.py +++ b/setup.py @@ -71,6 +71,10 @@ def startswith(self, x): 'brainiak.fcma.cython_blas', ['brainiak/fcma/cython_blas.pyx'], ), + Extension( + 'brainiak.eventseg.utils', + ['brainiak/eventseg/_utils.pyx'], + ), ] From 8e9a3b9c24875959ff38d8367b8e41b5eeb57224 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Thu, 22 Sep 2016 18:36:29 -0700 Subject: [PATCH 2/9] Removed old log --- brainiak/eventseg/event.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/brainiak/eventseg/event.py b/brainiak/eventseg/event.py index da3ceb3e8..7efd74eca 100644 --- a/brainiak/eventseg/event.py +++ b/brainiak/eventseg/event.py @@ -307,23 +307,6 @@ def _log(self, x): return y.reshape(xshape) - def _log_old(self, x): - """Modified version of np.log that manually sets values <=0 to -inf - - Parameters - ---------- - x: ndarray of floats - Input to the log function - - Returns - ------- - 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 - def find_events(self, testing_data, var=None, scramble=False): """Applies learned event segmentation to new testing dataset From d19fd8ef3d764a84a686f66b797201f8f3381e36 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Thu, 22 Sep 2016 18:54:32 -0700 Subject: [PATCH 3/9] changed if stmt --- brainiak/eventseg/_utils.pyx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/brainiak/eventseg/_utils.pyx b/brainiak/eventseg/_utils.pyx index 0f16a4998..ebcc94ac3 100644 --- a/brainiak/eventseg/_utils.pyx +++ b/brainiak/eventseg/_utils.pyx @@ -1,15 +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): - """log(x) for all x <= 0 and -Inf otherwise""" + """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] = log(x[i]) - else: + if x[i] <= 0: y[i] = float('-inf') + else: + y[i] = log(x[i]) return y From cb714c6fc0833e5c05ee4c6d0918b234ecad3f35 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Thu, 22 Sep 2016 19:16:12 -0700 Subject: [PATCH 4/9] removed extra line --- brainiak/eventseg/event.py | 1 - 1 file changed, 1 deletion(-) diff --git a/brainiak/eventseg/event.py b/brainiak/eventseg/event.py index 7efd74eca..d864f5490 100644 --- a/brainiak/eventseg/event.py +++ b/brainiak/eventseg/event.py @@ -306,7 +306,6 @@ def _log(self, x): 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 From 944d30e9ed367a7f0abdbda88597d7a8ac0c3ff5 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Fri, 23 Sep 2016 10:23:46 -0700 Subject: [PATCH 5/9] _utils.c to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index afd089b49..e5201232f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ junit-*.xml __pycache__ brainiak/fcma/cython_blas.c +brainiak/fcma/_utils.c From 31c221d9432caab67e6e6c1b78be7de679f52ac9 Mon Sep 17 00:00:00 2001 From: Michael Anderson Date: Fri, 23 Sep 2016 10:39:29 -0700 Subject: [PATCH 6/9] _utils.c to gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e5201232f..e0035356c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,4 @@ junit-*.xml __pycache__ brainiak/fcma/cython_blas.c -brainiak/fcma/_utils.c +brainiak/eventseg/_utils.c From 7571f1f2c5ff62c6e2e86cd53d8dcb69a77559df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mihai=20Capot=C4=83?= Date: Wed, 28 Sep 2016 17:40:23 -0700 Subject: [PATCH 7/9] Add NumPy include dirs to setup Also, simplify pybind11 include dirs usage and remove cython and pybind11 from the install dependencies. --- brainiak/hyperparamopt/__init__.py | 3 -- setup.py | 55 +++++++++--------------------- 2 files changed, 16 insertions(+), 42 deletions(-) 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 6f8a95824..b654c92a4 100644 --- a/setup.py +++ b/setup.py @@ -21,51 +21,14 @@ 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', @@ -139,16 +102,30 @@ 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', version=__version__, - install_requires=[ + setup_requires=[ 'cython', + 'numpy', + 'pybind11>=1.7', + ], + install_requires=[ 'mpi4py', 'numpy', 'scikit-learn', 'scipy', - 'pybind11>=1.7', ], author='Princeton Neuroscience Institute and Intel Corporation', author_email='bryn.keller@intel.com', From f0fa8b3a221a78f7a22baec96920a6d24d35001e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mihai=20Capot=C4=83?= Date: Wed, 28 Sep 2016 19:14:40 -0700 Subject: [PATCH 8/9] Add pybind11 to install dependencies Travis tests fail otherwise. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index b654c92a4..04bc27f7f 100644 --- a/setup.py +++ b/setup.py @@ -126,6 +126,7 @@ def finalize_options(self): 'numpy', 'scikit-learn', 'scipy', + 'pybind11>=1.7', ], author='Princeton Neuroscience Institute and Intel Corporation', author_email='bryn.keller@intel.com', From e35f26ec215a846fad713cd728ba09eb984b8d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mihai=20Capot=C4=83?= Date: Wed, 28 Sep 2016 19:25:39 -0700 Subject: [PATCH 9/9] Add cython to install dependencies Travis tests fail otherwise. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 04bc27f7f..4ec32bfe3 100644 --- a/setup.py +++ b/setup.py @@ -122,6 +122,7 @@ def finalize_options(self): 'pybind11>=1.7', ], install_requires=[ + 'cython', 'mpi4py', 'numpy', 'scikit-learn',