import numpy as np
import argparse
import matplotlib
import matplotlib.pyplot as plt
import os
import sys
import subprocess
import shutil

def save_error_plot(float_data, fixed_data, axes):
  mx = np.max(float_data)
  mn = np.min(float_data)
  org_diff = (fixed_data - float_data)
  combined = np.vstack((float_data, fixed_data, org_diff)).T
  # #np.savetxt("figs\"+str(i).zfill(4)+"_float.txt", combined, fmt='%10.6f, %10.6f, %10.6f')
  abs_diff = abs(fixed_data - float_data)
  maxIndex = np.argmax(abs_diff)
  max_abs_diff = np.max(abs_diff)
  mean_abs_diff = np.mean(abs_diff)
  var_abs_diff = np.var(abs_diff)
  abs_diff = np.append(abs_diff, abs(mx))

  axes.hist(abs_diff, color='blue', edgecolor='black', bins=60)
  # image_txt = "mean = " + str(mean) +", Var = "+ str(var) +", MAx = "+ str(mx)
  image_txt = "MeanAbsDiff=%7.4f, MaxAbsDiff=%7.4f, MaxVal=%7.3f" % (mean_abs_diff, max_abs_diff, mx)
  #plt.title(image_txt)
  axes.set_title(image_txt, fontdict = {'fontsize' : 8})
  
def save_pc_ref_plot(float_output, fixed_output, axes):
  axes.set_title("Float output Vs Fixed Output : Plot 1")
  axes.set_xlabel('Float Output')
  axes.set_ylabel('Fixed Output')
  axes.plot(float_output, fixed_output, '.')

def save_pc_ref_plot2(float_output, fixed_output, axes):
  axes.set_title("Float output Vs Fixed Output : Plot 2")
  axes.plot(float_output, "bs", label = "Float")
  axes.plot(fixed_output, "c.", label = "Fixed")
  axes.legend(loc='upper right', frameon=True)

def compare_tensor_files(file_path_float, file_path_fixed, plot_name):
  fileHandle = open(file_path_float, 'rb')
  float_data = np.fromfile(fileHandle, dtype=np.float32)
  fileHandle.close()
  fileHandle = open(file_path_fixed, 'rb')
  fixed_data = np.fromfile(fileHandle, dtype=np.float32)
  fileHandle.close()
  fig = plt.figure(figsize=(10, 10))
  axes1 = plt.subplot2grid((16, 16), (0, 0), rowspan=6, colspan=7)
  axes2 = plt.subplot2grid((16, 16), (0, 8), rowspan=6, colspan=8)
  axes3 = plt.subplot2grid((16, 16), (8, 0), rowspan=8, colspan=16)
  save_error_plot(float_data, fixed_data, axes1)
  save_pc_ref_plot(float_data, fixed_data, axes2)
  save_pc_ref_plot2(float_data, fixed_data, axes3)
  plt.savefig(plot_name)
  plt.clf()
  plt.close(fig)


import glob

# 使用通配符指定文件模式
pattern = r'../tidl_trace_int8/*_float.bin'

# 获取匹配的文件列表
files = glob.glob(pattern)

# 打印列表
for file in files:
  filename = os.path.basename(file)  # 获取文件名
  parts = filename.split('_')
  print(parts)
  # if (int(parts[3]) < 135):
  imageName="dataid_" + parts[4] + ".png"
  file_path_float = "../tidl_trace_int16/" + filename
  file_path_fixed = "../tidl_trace_int8/" + filename
  # print(imageName)
  compare_tensor_files(file_path_float,file_path_fixed, imageName)



# imageName="dataid_" + str(158) + ".png"

# filename = 'tidl_trace_facelandmarksgaze3dmm_0158_0001_0001_00001_00001_00002x00068_float.bin'
# file_path_fixed = "Y:/task2024/edgeai-tidl/edgeai-tidl-tools-09_02_06_00/trace_int8/" + filename
# file_path_float = "Y:/task2024/edgeai-tidl/edgeai-tidl-tools-09_02_06_00/trace/" + filename.replace('158', '162')

# print(file_path_float)
# compare_tensor_files(file_path_float, file_path_fixed,  imageName)
# display(Image(filename=imageName))