From cad88f1e7a678028f48a9d9d69e39eb38e7dbff2 Mon Sep 17 00:00:00 2001 From: danielballan Date: Thu, 13 Feb 2014 13:09:42 -0500 Subject: [PATCH 1/2] REF/API: numba is enabled only for v0.11 and waits to compile --- trackpy/tests/test_feature.py | 12 ++------- trackpy/tests/test_link.py | 47 ----------------------------------- trackpy/try_numba.py | 37 +++++++++++++++++++-------- 3 files changed, 28 insertions(+), 68 deletions(-) diff --git a/trackpy/tests/test_feature.py b/trackpy/tests/test_feature.py index b91202f4..e84a4e33 100644 --- a/trackpy/tests/test_feature.py +++ b/trackpy/tests/test_feature.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import DataFrame, Series import trackpy as tp +from trackpy.try_numba import NUMBA_AVAILABLE import unittest import nose @@ -379,18 +380,9 @@ class TestFeatureIdentificationWithNumba( def setUp(self): self.engine = 'numba' - try: - import numba - except ImportError: - pass # Test will be skipped -- see below. - else: - import trackpy as tp - tp.enable_numba() def check_skip(self): - try: - import numba - except ImportError: + if not NUMBA_AVAILABLE: raise nose.SkipTest("Numba not installed. Skipping.") diff --git a/trackpy/tests/test_link.py b/trackpy/tests/test_link.py index 4f117f67..3ecd04ea 100644 --- a/trackpy/tests/test_link.py +++ b/trackpy/tests/test_link.py @@ -34,13 +34,6 @@ def hash_generator(dims, box_size): return lambda: Hash_table(dims, box_size) -def _skip_if_no_pytables(): - try: - import tables - except ImportError: - raise nose.SkipTest('PyTables not installed. Skipping.') - - def _skip_if_no_numba(): if not NUMBA_AVAILABLE: raise nose.SkipTest('numba not installed. Skipping.') @@ -508,46 +501,6 @@ def setUp(self): self.linker_opts = dict(link_strategy='numba', neighbor_strategy='BTree') -# Removed after trackpy refactor -- restore with new API. -# class TestLinkOnDisk(unittest.TestCase): -# -# def setUp(self): -# _skip_if_no_pytables() -# filename = os.path.join(path, 'features_size9_masscut2000.df') -# f = pd.read_pickle(filename) -# self.key = 'features' -# with pd.get_store('temp1.h5') as store: -# store.put(self.key, f) -# with pd.get_store('temp2.h5') as store: -# store.append(self.key, f, data_columns=['frame']) -# -# def test_nontabular_raises(self): -# # Attempting to Link a non-tabular node should raise. -# _skip_if_no_pytables() -# f = lambda: tp.LinkOnDisk('temp1.h5', self.key) -# self.assertRaises(ValueError, f) -# -# def test_nontabular_with_use_tabular_copy(self): -# # simple smoke test -# _skip_if_no_pytables() -# linker = tp.LinkOnDisk('temp1.h5', self.key, use_tabular_copy=True) -# linker.link(8, 2) -# linker.save('temp3.h5', 'traj') -# -# def test_tabular(self): -# # simple smoke test -# _skip_if_no_pytables() -# linker = tp.LinkOnDisk('temp2.h5', self.key) -# linker.link(8, 2) -# linker.save('temp4.h5', 'traj') -# -# def tearDown(self): -# temp_files = ['temp1.h5', 'temp2.h5', 'temp3.h5', 'temp4.h5'] -# for filename in temp_files: -# try: -# os.remove(filename) -# except OSError: -# pass if __name__ == '__main__': import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], diff --git a/trackpy/try_numba.py b/trackpy/try_numba.py index 5f906bcc..5e9f4569 100644 --- a/trackpy/try_numba.py +++ b/trackpy/try_numba.py @@ -2,6 +2,7 @@ import sys import inspect from functools import wraps, partial +from warnings import warn import logging @@ -17,16 +18,25 @@ def _hush_llvm(): pass -ENABLE_NUMBA_ON_IMPORT = True +ENABLE_NUMBA_ON_IMPORT = False _registered_functions = list() # functions that can be numba-compiled +NUMBA_AVAILABLE = False try: import numba except ImportError: - NUMBA_AVAILABLE = False + message = ("To use numba-accelerated variants of core " + "functions, you must install numba.") else: - NUMBA_AVAILABLE = True - _hush_llvm() + v = numba.__version__ + major, minor, micro = v.split('.') + if major == '0' and minor == '11': + NUMBA_AVAILABLE = True + _hush_llvm() + else: + message = ("Trackpy currently supports numba 0.11* only. " + "Version {0} is currently installed. Trackpy will run " + "with numba disabled.".format(v)) class RegisteredFunction(object): @@ -38,16 +48,22 @@ def __init__(self, func, fallback=None, autojit_kw=None): module_name = inspect.getmoduleinfo(func.func_globals['__file__']).name module_name = '.'.join(['trackpy', module_name]) self.module_name = module_name - if NUMBA_AVAILABLE: - if autojit_kw is not None: - self.compiled = numba.autojit(**autojit_kw)(func) - else: - self.compiled = numba.autojit(func) + self.autojit_kw = autojit_kw if fallback is not None: self.ordinary = fallback else: self.ordinary = func + @property + def compiled(self): + # Compile it if this is the first time. + if (not hasattr(self, '_compiled')) and NUMBA_AVAILABLE: + if self.autojit_kw is not None: + self._compiled = numba.autojit(**self.autojit_kw)(self.func) + else: + self._compiled = numba.autojit(self.func) + return self._compiled + def point_to_compiled_func(self): setattr(sys.modules[self.module_name], self.func_name, self.compiled) @@ -91,5 +107,4 @@ def enable_numba(): for f in _registered_functions: f.point_to_compiled_func() else: - raise ImportError("To use numba-accelerated variants of core " - "functions, you must install numba.") + raise ImportError(message) From f5c9e8b03305967d2199c6823e7d2d69182a0956 Mon Sep 17 00:00:00 2001 From: danielballan Date: Thu, 13 Feb 2014 15:28:31 -0500 Subject: [PATCH 2/2] BLD: Add a numba 0.12 (numpy 1.8) build. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 9eae833d..afecf3e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ python: env: - DEPS="numpy=1.7.1 scipy=0.13.0 nose matplotlib=1.3 pandas=0.13.0 PIL pyyaml" - DEPS="numpy=1.7.1 scipy=0.13.0 nose matplotlib=1.3 pandas=0.13.0 PIL pyyaml numba=0.11" + - DEPS="numpy=1.8 scipy nose matplotlib=1.3 pandas=0.13.0 PIL pyyaml numba=0.12" install: - conda create -n testenv --yes pip python=$TRAVIS_PYTHON_VERSION