diff --git a/idi/util/__init__.py b/idi/util/__init__.py index e238afb..0787f5b 100644 --- a/idi/util/__init__.py +++ b/idi/util/__init__.py @@ -11,6 +11,7 @@ import itertools as _it from functools import lru_cache + def radial_profile(data, center=None, calcStd=False, os=1): """ calculates a ND radial profile of data around center. will ignore nans @@ -212,7 +213,9 @@ def atleastnd(array, n): array = _np.array(array) return array[tuple((n - array.ndim) * [None] + [...])] -@lru_cache + +@args2tuple +@lru_cache(maxsize=None) def fastlen(x, factors=(2, 3, 5, 7, 11)): """ return N>=x conisting only of the prime factors given as factors diff --git a/idi/util/funchelper.py b/idi/util/funchelper.py index 8e07613..5b4beb6 100644 --- a/idi/util/funchelper.py +++ b/idi/util/funchelper.py @@ -44,10 +44,12 @@ def asgen_helper(arg, *args, **kw): def parallel(fn=None): from collections import deque import time + try: from pathos.multiprocessing import Pool except ImportError: import warnings + warnings.warn('no pathos available, be careful with parallel decorator and pickling errors.') from multiprocessing import Pool @@ -78,3 +80,16 @@ def chain(*fns): def group(iterable, n): return zip(*([iter(iterable)] * n)) + + +def args2tuple(function): + import numpy + + def wrapper(*args, **kwargs): + args = [tuple(x) if type(x) == list else x for x in args] + kwargs = {k: tuple(x) if type(x) == list else x for k, x in kwargs.items()} + args = [tuple(x.ravel()) if type(x) == numpy.ndarray else x for x in args] + kwargs = {k: tuple(x.ravel()) if type(x) == numpy.ndarray else x for k, x in kwargs.items()} + return function(*args, **kwargs) + + return wrapper