common.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import sys
  3. import json
  4. from pathlib import Path
  5. def dict2json(data_dict, file_path):
  6. """
  7. 将字典转换为JSON格式并保存到文件中。
  8. 参数:
  9. data_dict (dict): 要转换的字典。
  10. file_path (str): 保存JSON文件的路径。
  11. """
  12. try:
  13. with open(file_path, 'w', encoding='utf-8') as json_file:
  14. json.dump(data_dict, json_file, ensure_ascii=False, indent=4)
  15. print(f"JSON文件已保存到 {file_path}")
  16. except Exception as e:
  17. print(f"保存JSON文件时出错: {e}")
  18. def get_interpolation(x, point1, point2):
  19. """
  20. According to the two extreme value points, the equation of one variable is determined,
  21. and the solution of the equation is obtained in the domain of definition.
  22. Arguments:
  23. x: A float number of the independent variable.
  24. point1: A set of the coordinate extreme point.
  25. point2: A set of the other coordinate extreme point.
  26. Returns:
  27. y: A float number of the dependent variable.
  28. """
  29. try:
  30. k = (point1[1] - point2[1]) / (point1[0] - point2[0])
  31. b = (point1[0] * point2[1] - point1[1] * point2[0]) / (point1[0] - point2[0])
  32. y = x * k + b
  33. return y
  34. except Exception as e:
  35. return f"Error: {str(e)}"
  36. def get_frame_with_time(df1, df2):
  37. # 将dataframe1按照start_time与simTime进行合并
  38. df1_start = df1.merge(df2[['simTime', 'simFrame']], left_on='start_time', right_on='simTime')
  39. df1_start = df1_start[['start_time', 'simFrame']].copy()
  40. df1_start.rename(columns={'simFrame': 'start_frame'}, inplace=True)
  41. # 将dataframe1按照end_time与simTime进行合并
  42. df1_end = df1.merge(df2[['simTime', 'simFrame']], left_on='end_time', right_on='simTime')
  43. df1_end = df1_end[['end_time', 'simFrame']].copy()
  44. df1_end.rename(columns={'simFrame': 'end_frame'}, inplace=True)
  45. # 将两个合并后的数据框按照行索引进行拼接
  46. result = pd.concat([df1_start, df1_end], axis=1)
  47. return result
  48. if __name__ == "__main__":
  49. pass