Edpzou
10/25/2018 - 2:29 AM

比较两个浮点或者定点数据的二进制文件是否一致

Python读取二进制文件进行差异性比较,文件必须是浮点或者定点数据的二进制, 里面有个zbin第三方库,可从这里下载安装,体积非常小,https://github.com/aksenventwo/Zbin/releases/tag/0.1,下载tar.gz然后使用pip install xx.tar.gz安装即可

import os
import zbin
import numpy as np


def main(dir1, dir2):
	total_err = 0
	idx  = 0
	files = [f for f in os.listdir(dir1) if os.path.splitext(f)[-1] == '.bin']
	total = len(files)
	err_f = []
	for f in files:
		f_1 = os.path.join(dir1, f)
		f_2 = os.path.join(dir2, f)
		
		f_1_bin = zbin.read(f_1, 32, num_type='float')
		f_1_bin = np.array(f_1_bin)

		f_2_bin = zbin.read(f_2, 32, num_type='float')
		f_2_bin = np.array(f_2_bin)

		err = np.max(np.abs(f_1_bin - f_2_bin))
		if err != 0:
			err_f.append(f)
		total_err += err

		pos = int((idx / total) * 100) + 1
		print("\rSuccessfully cmp {}{}{}/{}".format(pos*'>', (100-pos)*'=', pos, 100), end='', flush=True)
		idx += 1

	print('\n')
	print("Result: %d" % total_err)
	for f in err_f:
		print(f)


if __name__ == '__main__':
	main('float_little_old', 'float_little')