def test_ufunc(nps_app_inst): import numpy as np from nums.numpy import BlockArray from nums.numpy import numpy_utils from nums import numpy as nps assert nps_app_inst is not None uops, bops = numpy_utils.ufunc_op_signatures() for name, _ in sorted(uops): if name in ("arccosh", "arcsinh"): np_val = np.array([np.e]) elif name == "invert" or name.startswith("bitwise") or name.startswith( "logical"): np_val = np.array([True, False], dtype=np.bool_) else: np_val = np.array([.1, .2, .3]) ns_val = nps.array(np_val) ns_ufunc = nps.__getattribute__(name) np_ufunc = np.__getattribute__(name) np_result = np_ufunc(np_val) ns_result: BlockArray = ns_ufunc(ns_val) assert np.allclose(np_result, ns_result.get()) def check_bop(_name, _np_a, _np_b): np_ufunc = np.__getattribute__(_name) ns_ufunc = nps.__getattribute__(_name) if _name in ("ldexp", ) and str( _np_b.dtype) not in ("int", "int32", "int64"): return _ns_a = nps.array(_np_a) _ns_b = nps.array(_np_b) _np_result = np_ufunc(_np_a, _np_b) _ns_result = ns_ufunc(_ns_a, _ns_b) assert np.allclose(_np_result, _ns_result.get()) for name, _ in bops: if name.startswith("bitwise") or name.startswith("logical"): np_a = np.array([True, False, True, False], dtype=np.bool_) np_b = np.array([True, True, False, False], dtype=np.bool_) check_bop(name, np_a, np_b) elif name in ("gcd", "lcm"): np_a = np.array([8, 3, 7], dtype=np.int) np_b = np.array([4, 12, 13], dtype=np.int) check_bop(name, np_a, np_b) elif name.endswith("shift"): np_a = np.array([7 * 10**3, 8 * 10**3, 9 * 10**3], dtype=np.int) np_b = np.array([1, 2, 3], dtype=np.int) check_bop(name, np_a, np_b) else: pairs = [ (np.array([.1, 5.0, .3]), np.array([.2, 6.0, .3])), (np.array([.1, 5.0, .3]), np.array([4, 2, 6], dtype=np.int)), (np.array([3, 7, 3], dtype=np.int), np.array([4, 2, 6], dtype=np.int)), ] for np_a, np_b in pairs: check_bop(name, np_a, np_b)
def api_stub(): import numpy as np def _uop_template(op_name): s = """ def {op_name}(x: BlockArray, out: BlockArray = None, where=True, **kwargs) -> BlockArray: return _instance().map_uop(op_name="{op_name}", arr=x, out=out, where=where, kwargs=numpy_utils.ufunc_kwargs(kwargs))""" return s.format(op_name=op_name) def _bop_template(op_name): s = """ def {op_name}(x1: BlockArray, x2: BlockArray, out: BlockArray = None, where=True, **kwargs) -> BlockArray: return _instance().map_bop(op_name="{op_name}", arr_1=x1, arr_2=x2, out=out, where=where, kwargs=numpy_utils.ufunc_kwargs(kwargs))""" return s.format(op_name=op_name) uops, bops = numpy_utils.ufunc_op_signatures() for name, func in sorted(systems_utils.get_module_functions(np).items()): if name in ("deprecate_with_doc", "loads"): continue try: sig: str = func.__doc__.split("\n")[0].strip() op_name, args = sig.split("(") args = args.split(")")[0] has_subok = "subok" in args if has_subok: # We don't support subok. args = args.split("subok")[0].strip()[:-1] args = list(map(lambda x: x.strip(), args.split(","))) if args == [ 'x', '/', 'out=None', '*', 'where=True', "casting='same_kind'", "order='K'", 'dtype=None' ]: # This is a uop. uops.append(_uop_template(name)) elif args == [ 'x1', "x2", '/', 'out=None', '*', 'where=True', "casting='same_kind'", "order='K'", 'dtype=None' ]: # This is a bop. bops.append(_bop_template(name)) else: print(name, op_name, args) except Exception as _: print("FAILED", name) for sig in uops + bops: print(sig) print("num ufuncs", len(uops + bops))