Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/wmayner/pyphi into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wmayner committed May 30, 2018
2 parents 470a2b5 + 8e8f8e6 commit 6756a36
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.PHONY: test docs dist

src = pyphi
Expand All @@ -17,6 +16,9 @@ coverage:
coverage html
open htmlcov/index.html

lint:
pylint $(src)

watch-tests:
watchmedo shell-command \
--command='make coverage' \
Expand Down
7 changes: 3 additions & 4 deletions pyphi/compute/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ def get_num_processes():
'Invalid NUMBER_OF_CORES; value may not be 0.')

if config.NUMBER_OF_CORES > cpu_count:
raise ValueError(
'Invalid NUMBER_OF_CORES; value must be less than or '
'equal to the available number of cores ({} for this '
'system).'.format(cpu_count))
log.info('Requesting {} cores; only {} available'.format(
config.NUMBER_OF_CORES, cpu_count))
return cpu_count

if config.NUMBER_OF_CORES < 0:
num = cpu_count + config.NUMBER_OF_CORES + 1
Expand Down
33 changes: 17 additions & 16 deletions pyphi/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ def cache(ignore=None):
"""Decorator for memoizing a function using either the filesystem or a
database.
"""
def decorator(func):
# Initialize both cached versions
joblib_cached = constants.joblib_memory.cache(func, ignore=ignore)
db_cached = DbMemoizedFunc(func, ignore)

def joblib_decorator(func):
if func.__name__ == '_sia' and not config.CACHE_SIAS:
return func
return constants.joblib_memory.cache(func, ignore=ignore)

def db_decorator(func):
if func.__name__ == '_sia' and not config.CACHE_SIAS:
return func
return DbMemoizedFunc(func, ignore)

if config.CACHING_BACKEND == 'fs':
# Decorate the function with the filesystem memoizer.
return joblib_decorator
if config.CACHING_BACKEND == 'db':
# Decorate the function with the database memoizer.
return db_decorator
@functools.wraps(func)
def wrapper(*args, **kwargs):
"""Dynamically choose the cache at call-time, not at import."""
if func.__name__ == '_sia' and not config.CACHE_SIAS:
f = func
elif config.CACHING_BACKEND == 'fs':
f = joblib_cached
elif config.CACHING_BACKEND == 'db':
f = db_cached
return f(*args, **kwargs)

return wrapper
return decorator


class DbMemoizedFunc:
Expand Down
20 changes: 10 additions & 10 deletions pyphi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
external use.
"""

import functools
import hashlib
import os
from itertools import chain, combinations, product
from time import time

import decorator
import numpy as np
from scipy.misc import comb

Expand Down Expand Up @@ -201,17 +201,17 @@ def get_path(i): # pylint: disable=missing-docstring
return [np.load(get_path(i)) for i in range(num)]


def time_annotated(func):
# Using ``decorator`` preserves the function signature of the wrapped function,
# allowing joblib to properly introspect the function arguments.
@decorator.decorator
def time_annotated(func, *args, **kwargs):
"""Annotate the decorated function or method with the total execution
time.
The result is annotated with a `time` attribute.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time()
result = func(*args, **kwargs)
end = time()
result.time = round(end - start, config.PRECISION)
return result
return wrapper
start = time()
result = func(*args, **kwargs)
end = time()
result.time = round(end - start, config.PRECISION)
return result
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
exec(f.read(), about)

install_requires = [
'decorator >=4.0.0',
'joblib >=0.8.0',
'numpy >=1.11.0',
'psutil >=2.1.1',
Expand Down
5 changes: 2 additions & 3 deletions test/test_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def test_num_processes():
with pytest.raises(ValueError):
parallel.get_num_processes()

# Requesting too many cores
# Requesting more cores than available
with config.override(NUMBER_OF_CORES=3):
with pytest.raises(ValueError):
parallel.get_num_processes()
assert parallel.get_num_processes() == 2

# Ok
with config.override(NUMBER_OF_CORES=1):
Expand Down

0 comments on commit 6756a36

Please sign in to comment.