import numpy as np import bottleneck as bn INT_DTYPES = [np.int64, np.int32] FLOAT_DTYPES = [np.float64, np.float32] DTYPES = tuple(FLOAT_DTYPES + INT_DTYPES) def get_functions(module_name, as_string=False): """Returns a list of functions, optionally as string function names""" if module_name == "all": funcs = [] funcs_in_dict = func_dict() for key in funcs_in_dict: for func in funcs_in_dict[key]: funcs.append(func) else: funcs = func_dict()[module_name] if as_string: funcs = [f.__name__ for f in funcs] return funcs def func_dict(): d = {} d["reduce"] = [ bn.nansum, bn.nanmean, bn.nanstd, bn.nanvar, bn.nanmin, bn.nanmax, bn.median, bn.nanmedian, bn.ss, bn.nanargmin, bn.nanargmax, bn.anynan, bn.allnan, ] d["move"] = [ bn.move_sum, bn.move_mean, bn.move_std, bn.move_var, bn.move_min, bn.move_max, bn.move_argmin, bn.move_argmax, bn.move_median, bn.move_rank, ] d["nonreduce"] = [bn.replace] d["nonreduce_axis"] = [ bn.partition, bn.argpartition, bn.rankdata, bn.nanrankdata, bn.push, ] return d # --------------------------------------------------------------------------- def arrays(func_name, dtypes=DTYPES): return array_iter(array_generator, func_name, dtypes) def array_iter(arrays_func, *args): for a in arrays_func(*args): if a.ndim < 2: yield a # this is good for an extra check but in everyday development it # is a pain because it doubles the unit test run time # elif a.ndim == 3: # for axes in permutations(range(a.ndim)): # yield np.transpose(a, axes) else: yield a yield a.T def array_generator(func_name, dtypes): """Iterator that yields arrays to use for unit testing.""" f_dtypes = list(set(dtypes) & set(FLOAT_DTYPES)) # define nan and inf if func_name in ("partition", "argpartition"): nan = 0 else: nan = np.nan if func_name in ("move_sum", "move_mean", "move_std", "move_var"): # these functions can't handle inf inf = 8 else: inf = np.inf # nan and inf for dtype in f_dtypes: yield np.array([inf, nan], dtype=dtype) yield np.array([inf, -inf], dtype=dtype) yield np.array([nan, 2, 3], dtype=dtype) yield np.array([-inf, 2, 3], dtype=dtype) if func_name != "nanargmin": yield np.array([nan, inf], dtype=dtype) # byte swapped yield np.array([1, 2, 3], dtype=">f4") yield np.array([1, 2, 3], dtype="