Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF/API: numba decorators only turn on for numba 0.11.* #83

Merged
merged 2 commits into from
Feb 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions trackpy/tests/test_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")


Expand Down
47 changes: 0 additions & 47 deletions trackpy/tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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'],
Expand Down
37 changes: 26 additions & 11 deletions trackpy/try_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import inspect
from functools import wraps, partial
from warnings import warn
import logging


Expand All @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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)