custom_run.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ##################################################################
  4. #
  5. # Copyright (c) 2023 CICV, Inc. All Rights Reserved
  6. #
  7. ##################################################################
  8. """
  9. @Authors: yangzihao(yangzihao@china-icv.cn)
  10. @Data: 2024/01/30
  11. @Last Modified: 2024/01/30
  12. @Summary: Evaluateion functions
  13. """
  14. import os
  15. import importlib.util
  16. from collections import defaultdict
  17. import traceback
  18. # from process.data_process import DataProcess
  19. # from process.config_parser import ConfigParse
  20. def custom_run(folder_path, all_data, metric_list, case_name):
  21. # 初始化一个字典来存储结果
  22. results = defaultdict(dict)
  23. # metric_list = all_data.config.metric_list
  24. # metric_list = ['ldw_miss_warning_count', 'ldw_miss_warning_count2', 'ldw_miss_warning_count3']
  25. # 遍历文件夹中的所有文件
  26. for filename in os.listdir(folder_path):
  27. try:
  28. if filename.endswith('.py') and not filename.startswith('__') and filename[
  29. :-3] in metric_list: # 过滤掉非Python文件或特殊文件
  30. # 构造模块的完整路径
  31. module_path = os.path.join(folder_path, filename)
  32. # 使用importlib来动态导入模块
  33. spec = importlib.util.spec_from_file_location(filename[:-3], module_path)
  34. module = importlib.util.module_from_spec(spec)
  35. spec.loader.exec_module(module)
  36. # 假设每个模块都有一个CustomMetric类
  37. if hasattr(module, 'CustomMetric'):
  38. CustomMetric = getattr(module, 'CustomMetric')
  39. # 实例化CustomMetric类
  40. metric_instance = CustomMetric(all_data, case_name)
  41. metric_result = metric_instance.result
  42. # 将结果存储到字典中,key为文件名,value为各个方法的结果
  43. results[filename[:-3]] = metric_result
  44. except Exception as e:
  45. traceback.print_exc()
  46. continue
  47. return results
  48. # if __name__ == '__main__':
  49. # # 文件夹路径,自定义指标Python文件都在此文件夹下
  50. # folder_path = '../custom'
  51. # path = r"..\task_1228\case1228\data"
  52. # configPath = r"../configs/config.json"
  53. # config = ConfigParse(configPath)
  54. # all_data = DataProcess(path, config)
  55. #
  56. # custom_run(folder_path, all_data)