efficient.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ##################################################################
  4. #
  5. # Copyright (c) 2024 CICV, Inc. All Rights Reserved
  6. #
  7. ##################################################################
  8. """
  9. @Authors: xieguijin(xieguijin@china-icv.cn)
  10. @Data: 2024/12/23
  11. @Last Modified: 2024/12/23
  12. @Summary: Efficient metrics calculation
  13. """
  14. from modules.lib.score import Score
  15. from modules.lib import log_manager
  16. class Efficient:
  17. STOP_SPEED_THRESHOLD = 0.05 # Speed threshold to consider as stop
  18. STOP_TIME_THRESHOLD = 0.5 # Time threshold to consider as stop (in seconds)
  19. FRAME_RANGE = 13 # Frame range to classify stop duration
  20. def __init__(self, data_processed):
  21. # self.logger = log.get_logger() # 使用时再初始化
  22. self.data_processed = data_processed
  23. self.df = data_processed.object_df.copy()
  24. self.ego_df = data_processed.ego_data
  25. self.stop_count = 0 # Initialize stop count
  26. self.stop_duration = 0 # Initialize stop duration
  27. def _max_speed(self):
  28. """Return the maximum speed in the ego data."""
  29. return self.ego_df['v'].max()
  30. def _deviation_speed(self):
  31. """Return the variance of the speed in the ego data."""
  32. return self.ego_df['v'].var()
  33. def average_velocity(self):
  34. """
  35. Calculate the average velocity of the ego data.
  36. Average velocity = total distance / total time
  37. """
  38. self.average_v = self.ego_df['v'].mean()
  39. return self.average_v
  40. def stop_duration_and_count(self):
  41. """
  42. Calculate stop duration and stop count based on the following:
  43. - Stops are detected when speed is <= STOP_SPEED_THRESHOLD
  44. - Stops should last more than STOP_TIME_THRESHOLD (in seconds)
  45. """
  46. stop_time_list = self.ego_df[self.ego_df['v'] <= self.STOP_SPEED_THRESHOLD]['simTime'].values.tolist()
  47. stop_frame_list = self.ego_df[self.ego_df['v'] <= self.STOP_SPEED_THRESHOLD]['simFrame'].values.tolist()
  48. stop_frame_group = []
  49. stop_time_group = []
  50. sum_stop_time = 0
  51. f1, t1 = stop_frame_list[0] if stop_frame_list else 0, stop_time_list[0] if stop_time_list else 0
  52. for i in range(1, len(stop_frame_list)):
  53. if stop_frame_list[i] - stop_frame_list[i - 1] != 1: # Frame discontinuity
  54. f2, t2 = stop_frame_list[i - 1], stop_time_list[i - 1]
  55. # If stop is valid (frame gap >= FRAME_RANGE and duration > STOP_TIME_THRESHOLD)
  56. if f2 - f1 >= self.FRAME_RANGE:
  57. stop_frame_group.append((f1, f2))
  58. stop_time_group.append((t1, t2))
  59. sum_stop_time += (t2 - t1)
  60. self.stop_count += 1
  61. # Update f1, t1
  62. f1, t1 = stop_frame_list[i], stop_time_list[i]
  63. # Check last stop segment
  64. if len(stop_frame_list) > 0:
  65. f2, t2 = stop_frame_list[-1], stop_time_list[-1]
  66. if f2 - f1 >= self.FRAME_RANGE and f2 != self.ego_df['simFrame'].values[-1]:
  67. stop_frame_group.append((f1, f2))
  68. stop_time_group.append((t1, t2))
  69. sum_stop_time += (t2 - t1)
  70. self.stop_count += 1
  71. # Calculate stop duration if stop count is not zero
  72. self.stop_duration = sum_stop_time / self.stop_count if self.stop_count != 0 else 0
  73. return self.stop_duration
  74. def report_statistic(self):
  75. """Generate the statistics and report the results."""
  76. efficient_result = {
  77. 'maxSpeed': self._max_speed(),
  78. 'deviationSpeed': self._deviation_speed(),
  79. 'averagedSpeed': self.average_velocity(),
  80. 'stopDuration': self.stop_duration_and_count()
  81. }
  82. # self.logger.info(f"Efficient metrics calculation completed. Results: {efficient_result}")
  83. evaluator = Score(self.data_processed.efficient_config)
  84. result = evaluator.evaluate(efficient_result)
  85. print("\n[高效性表现及评价结果]")
  86. return result