def test_diagonal_axis(self,a): if bh.check(a[0]): cmd = "res = bh.diagonal(a[0], axis1=%d, axis2=%d)" % (self.axis1, self.axis2) else: cmd = "res = np.diagonal(a[0], axis1=%d, axis2=%d)" % (self.axis1, self.axis2) exec(cmd) return (res,cmd)
def test_add(self,a): if bh.check(a[0]): cmd = "bh.add(a[0][:-1], 42, a[0][1:])" else: cmd = "t = np.add(a[0][:-1], 42); a[0][1:] = t" exec(cmd) return (a[0],cmd)
def test_trace(self,a): if bh.check(a[0]): cmd = "res = bh.trace(a[0])" else: cmd = "res = np.trace(a[0])" exec(cmd) return (res,cmd)
def test_diagonal_offset(self,a): if bh.check(a[0]): cmd = "res = bh.diagonal(a[0], offset=%d)" % self.offset else: cmd = "res = np.diagonal(a[0], offset=%d)" % self.offset exec(cmd) return (res,cmd)
def test_cumprod(self,a): if bh.check(a[0]): cmd = "res = bh.multiply.accumulate(a[0],axis=%d)"%self.axis else: cmd = "res = np.multiply.accumulate(a[0],axis=%d)"%self.axis exec(cmd) return (res,cmd)
def test_cumsum(self,a): if bh.check(a[0]): cmd = "res = bh.add.accumulate(a[0],axis=%d)"%self.axis else: cmd = "res = np.add.accumulate(a[0],axis=%d)"%self.axis exec(cmd) return (res,cmd)
def test_trace_axis_and_offset(self,a): if bh.check(a[0]): cmd = "res = bh.trace(a[0], offset=%d, axis1=%d, axis2=%d)" % (self.offset, self.axis1, self.axis2) else: cmd = "res = np.trace(a[0], offset=%d, axis1=%d, axis2=%d)" % (self.offset, self.axis1, self.axis2) exec(cmd) return (res,cmd)
def test_trace_offset(self,a): if bh.check(a[0]): cmd = "res = bh.trace(a[0], offset=%d)" % self.offset else: cmd = "res = np.trace(a[0], offset=%d)" % self.offset exec(cmd) return (res,cmd)
def test_diagonal(self,a): if bh.check(a[0]): cmd = "res = bh.diagonal(a[0])" else: cmd = "res = np.diagonal(a[0])" exec(cmd) return (res,cmd)
def test_diagonal_axis(self, a): if bh.check(a[0]): cmd = "res = bh.diagonal(a[0], axis1=%d, axis2=%d)" % (self.axis1, self.axis2) else: cmd = "res = np.diagonal(a[0], axis1=%d, axis2=%d)" % (self.axis1, self.axis2) exec(cmd) return (res, cmd)
def test_ufunc(self,a): if bh.check(a[0]): cmd = "bh.%s("%self.name else: cmd = "np.%s("%self.name if self.name in ["real","imag"]: cmd = "a[0] = %sa[1])"%cmd else: for i in xrange(1,self.nops): cmd += "a[%d],"%(i) cmd += "a[0])" exec(cmd) return (a[0],cmd)
if ary.dtype not in complex_nptypes: non_complex[index] = ary index += 1 np_arays = non_complex bh_arys = [] # Get Bohrium arrays for a in np_arys.values(): bh_arys.append(bh.array(a)) # Execute using NumPy (res1,cmd1) = getattr(cls_inst,mth)(np_arys) res1 = res1.copy() # Execute using Bohrium (res2,cmd2) = getattr(cls_inst,mth)(bh_arys) cmd += cmd1 try: # Compare if not np.isscalar(res2) and bh.check(res2): res2 = res2.copy2numpy() except RuntimeError as error_msg: test_okay = False print() print(" " + _C.OKBLUE + "[CMD] %s"%cmd + _C.ENDC) print(" " + _C.FAIL + str(error_msg) + _C.ENDC) else: rtol = cls_inst.config['maxerror'] atol = rtol * 0.1 if not np.allclose(res1, res2, rtol=rtol, atol=atol): test_okay = False if 'warn_on_err' in cls_inst.config: print() print(_C.WARNING + " [Warning] %s"%(name) + _C.ENDC)
def test_tally(self, a): cmd = "res = a[0] + a[0]" exec(cmd) if bh.check(a[0]): bh.target.tally() return (res,cmd)
def test_tally(self, a): cmd = "res = a[0] + a[0]" exec(cmd) if bh.check(a[0]): bh.target.tally() return (res, cmd)
def run(args): for filename in args.files: if not filename.endswith("py"): # Ignore non-python files continue # Remove ".py" module_name = os.path.basename(filename)[:-3] m = imp.load_source(module_name, filename) if len(args.class_list) > 0: cls_name_list = args.class_list else: cls_name_list = get_test_object_names(m) for cls_name in cls_name_list: if cls_name in args.exclude_class: continue cls_obj = getattr(m, cls_name) cls_inst = cls_obj() for mth_name in get_test_object_names(cls_obj): mth_obj = getattr(cls_inst, mth_name) name = "%s/%s/%s" % (filename, cls_name[5:], mth_name[5:]) print("Testing %s%s%s" % (OKGREEN, name, ENDC), end="") sys.stdout.flush() start_time = time.time() for ret in getattr(cls_inst, "init")(): # Let's retrieve the NumPy and Bohrium commands cmd = mth_obj(ret) if len(cmd) == 2: (cmd_np, cmd_bh) = cmd else: # If not returning a pair, the NumPy and Bohrium command are identical cmd_np = cmd cmd_bh = cmd # For convenient, we replace "M" and "BH" in the command to represent NumPy or Bohrium cmd_np = cmd_np.replace("M", "np").replace("BH", "False") cmd_bh = cmd_bh.replace("M", "bh").replace("BH", "True") if args.verbose: print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) # Let's execute the two commands env = {"np": numpy, "bh": bohrium} exec(cmd_np, env) assert (not bohrium.check(env['res'])) res_np = env['res'] env = {"np": numpy, "bh": bohrium} exec(cmd_bh, env) if bohrium.check(env['res']): res_bh = env['res'].copy2numpy() else: res_bh = env['res'] try: similar = numpy.allclose(res_np, res_bh, equal_nan=True) except TypeError as err: try: # Old versions of NumPy do not have the 'equal_nan' option similar = numpy.allclose(res_np, res_bh) except: similar = res_np == res_bh or res_np is res_bh if not similar: print("\n") print("%s [Error] %s%s" % (FAIL, name, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) print("%s [BH RES]\n%s%s" % (FAIL, res_bh, ENDC)) if not args.cont_on_error: sys.exit(1) print("%s (%.2fs) ✓%s" % (OKBLUE, time.time() - start_time, ENDC))
def run(args): print("*** Testing the equivalency of Bohrium-NumPy and NumPy ***") test_suite_start_time = time.time() for f in args.file: if f.startswith("test_") and f.endswith( "py") and f not in args.exclude: # Remove ".py" m = __import__(f[:-3]) # All test classes starts with "test_" for cls in [o for o in dir(m) if o.startswith("test_") and \ (True if args.test and o in args.test or not args.test else False)]: # Exclude specific test if cls in args.exclude_test: continue cls_obj = getattr(m, cls) cls_inst = cls_obj() cls_inst.args = args # Exclude benchmarks import inspect is_benchmark = BenchHelper.__name__ in [ c.__name__ for c in inspect.getmro(cls_obj) ] if args.exclude_benchmarks and is_benchmark: continue test_okay = True test_start_time = time.time() # All test methods starts with "test_" for mth in [o for o in dir(cls_obj) if o.startswith("test_")]: name = "%s/%s/%s" % (f, cls[5:], mth[5:]) print("Testing " + _C.OKGREEN + str(name) + _C.ENDC, end=" ") sys.stdout.flush() for (np_arys, cmd) in getattr(cls_inst, "init")(): # Exclude complex if args.exclude_complex_dtype: complex_nptypes = [ eval(dtype) for dtype in TYPES.COMPLEX ] index = 0 non_complex = {} for ary in np_arys.values(): if ary.dtype not in complex_nptypes: non_complex[index] = ary index += 1 np_arays = non_complex # Get Bohrium arrays bh_arys = [] for a in np_arys.values(): bh_arys.append(bh.array(a)) # Execute using NumPy (res1, cmd1) = getattr(cls_inst, mth)(np_arys) res1 = res1.copy() # Execute using Bohrium (res2, cmd2) = getattr(cls_inst, mth)(bh_arys) cmd += cmd1 # Compare try: if not np.isscalar(res2) and bh.check(res2): res2 = res2.copy2numpy() except RuntimeError as error_msg: test_okay = False print() print(" " + _C.OKBLUE + "[CMD] %s" % cmd + _C.ENDC) print(" " + _C.FAIL + str(error_msg) + _C.ENDC) else: rtol = cls_inst.config['maxerror'] atol = rtol * 0.1 if not np.allclose( res1, res2, rtol=rtol, atol=atol): test_okay = False if 'warn_on_err' in cls_inst.config: print() print(_C.WARNING + " [Warning] %s" % (name) + _C.ENDC) print(_C.OKBLUE + " [CMD] %s" % cmd + _C.ENDC) print(_C.OKGREEN + " NumPy result: %s" % (res1) + _C.ENDC) print(_C.FAIL + " Bohrium result: %s" % (res2) + _C.ENDC) print(_C.WARNING + " " + str(cls_inst.config['warn_on_err']) + _C.ENDC) print(_C.OKBLUE + " Manual verification is needed." + _C.ENDC) else: print() print(_C.FAIL + " [Error] %s" % (name) + _C.ENDC) print(_C.OKBLUE + " [CMD] %s" % cmd + _C.ENDC) print(_C.OKGREEN + " NumPy result: %s" % (res1) + _C.ENDC) print(_C.FAIL + " Bohrium result: %s" % (res2) + _C.ENDC) sys.exit(1) if test_okay: print( _C.OKBLUE + "({:.2f}s)".format(time.time() - test_start_time) + _C.ENDC, "✓") print("*** Finished in: " + _C.OKBLUE + "{:.2f}s".format(time.time() - test_suite_start_time) + _C.ENDC + " ***")
def run(args): for filename in args.files: if not filename.endswith("py"): # Ignore non-python files continue # Remove ".py" module_name = os.path.basename(filename)[:-3] m = imp.load_source(module_name, filename) if len(args.class_list) > 0: cls_name_list = args.class_list else: cls_name_list = get_test_object_names(m) for cls_name in cls_name_list: if cls_name in args.exclude_class: continue cls_obj = getattr(m, cls_name) cls_inst = cls_obj() for mth_name in get_test_object_names(cls_obj): mth_obj = getattr(cls_inst, mth_name) name = "%s/%s/%s" % (filename, cls_name[5:], mth_name[5:]) print("Testing %s%s%s " % (OKGREEN, name, ENDC), end="") sys.stdout.flush() start_time = time.time() for ret in getattr(cls_inst, "init")(): # Let's retrieve the NumPy and Bohrium commands cmd = mth_obj(ret) cmd_bh107 = None if len(cmd) == 3: (cmd_np, cmd_bh, cmd_bh107) = cmd elif len(cmd) == 2: (cmd_np, cmd_bh) = cmd else: # If not returning a tuple, the NumPy and Bohrium command are identical and bh107n is ignored cmd_np = cmd cmd_bh = cmd # For convenient, we replace "M" and "BH" in the command to represent NumPy or Bohrium cmd_np = cmd_np.replace("M", "np").replace("BH", "False") cmd_bh = cmd_bh.replace("M", "bh").replace("BH", "True") if cmd_bh107 is not None: cmd_bh107 = cmd_bh107.replace("M", "bh107") if args.verbose: print("\n%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) if cmd_bh107 is not None: print("%s [BH107 CMD] %s%s" % (OKBLUE, cmd_bh107, ENDC)) # Let's execute the NumPy commands env = {"np": numpy, "bh": bohrium} exec(cmd_np, env) res_np = env['res'] if bohrium.check(res_np): print("\n") print( "%s [Error] The NumPy command returns a Bohrium array!%s" % (FAIL, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) if not args.cont_on_error: sys.exit(1) # Let's execute the Bohrium commands env = {"np": numpy, "bh": bohrium} exec(cmd_bh, env) if bohrium.check(env['res']): res_bh = env['res'].copy2numpy() else: res_bh = env['res'] if not check_result(res_np, res_bh): print("\n") print("%s [Error] %s%s" % (FAIL, name, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) print("%s [BH RES]\n%s%s" % (FAIL, res_bh, ENDC)) if not args.cont_on_error: sys.exit(1) # Let's execute the bh107 commands if cmd_bh107 is not None: env = {"np": numpy, "bh": bohrium, "bh107": bh107} exec(cmd_bh107, env) res_bh107 = env['res'] if bohrium.check(res_bh107): print("\n") print( "%s [Error] The bh107 command returns a Bohrium array!%s" % (FAIL, ENDC)) print("%s [bh107 CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [bh107 RES]\n%s%s" % (OKGREEN, res_bh107, ENDC)) if not args.cont_on_error: sys.exit(1) if isinstance(res_bh107, bh107.BhArray): res_bh107 = res_bh107.copy2numpy() if not check_result(res_np, res_bh107): print("\n") print("%s [Error] %s%s" % (FAIL, name, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) print("%s [BH107 CMD] %s%s" % (OKBLUE, cmd_bh107, ENDC)) print("%s [BH107 RES]\n%s%s" % (FAIL, res_bh107, ENDC)) if not args.cont_on_error: sys.exit(1) print("%s(%.2fs) %s✓%s" % (OKBLUE, time.time() - start_time, OKGREEN, ENDC))
def run(args): for filename in args.files: if not filename.endswith("py"): # Ignore non-python files continue # Remove ".py" module_name = os.path.basename(filename)[:-3] m = imp.load_source(module_name, filename) if len(args.class_list) > 0: cls_name_list = args.class_list else: cls_name_list = get_test_object_names(m) for cls_name in cls_name_list: if cls_name in args.exclude_class: continue cls_obj = getattr(m, cls_name) cls_inst = cls_obj() for mth_name in get_test_object_names(cls_obj): mth_obj = getattr(cls_inst, mth_name) name = "%s/%s/%s" % (filename, cls_name[5:], mth_name[5:]) print("Testing %s%s%s " % (OKGREEN, name, ENDC), end="") sys.stdout.flush() start_time = time.time() for ret in getattr(cls_inst, "init")(): # Let's retrieve the NumPy and Bohrium commands cmd = mth_obj(ret) if len(cmd) == 2: (cmd_np, cmd_bh) = cmd else: # If not returning a pair, the NumPy and Bohrium command are identical cmd_np = cmd cmd_bh = cmd # For convenient, we replace "M" and "BH" in the command to represent NumPy or Bohrium cmd_np = cmd_np.replace("M", "np").replace("BH", "False") cmd_bh = cmd_bh.replace("M", "bh").replace("BH", "True") if args.verbose: print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) # Let's execute the two commands env = {"np": numpy, "bh": bohrium} exec (cmd_np, env) res_np = env['res'] if bohrium.check(res_np): print("\n") print("%s [Error] The NumPy command returns a Bohrium array!%s" % (FAIL, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) if not args.cont_on_error: sys.exit(1) env = {"np": numpy, "bh": bohrium} exec (cmd_bh, env) if bohrium.check(env['res']): res_bh = env['res'].copy2numpy() else: res_bh = env['res'] if not check_result(res_np, res_bh): print("\n") print("%s [Error] %s%s" % (FAIL, name, ENDC)) print("%s [NP CMD] %s%s" % (OKBLUE, cmd_np, ENDC)) print("%s [NP RES]\n%s%s" % (OKGREEN, res_np, ENDC)) print("%s [BH CMD] %s%s" % (OKBLUE, cmd_bh, ENDC)) print("%s [BH RES]\n%s%s" % (FAIL, res_bh, ENDC)) if not args.cont_on_error: sys.exit(1) print("%s(%.2fs) %s✓%s" % (OKBLUE, time.time() - start_time, OKGREEN, ENDC))