From 38d476d6f1dcfd2dd7219db5f2883b622e22572d Mon Sep 17 00:00:00 2001 From: Felix Zimmermann Date: Thu, 6 May 2021 09:24:19 -0700 Subject: [PATCH] fix fastlen? --- idi/util/__init__.py | 5 ++++- idi/util/funchelper.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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