custom_run0926.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. # from process.data_process import DataProcess
  18. # from process.config_parser import ConfigParse
  19. def custom_run(folder_path, all_data, metric_list, case_name):
  20. # 初始化一个字典来存储结果
  21. results = defaultdict(dict)
  22. # metric_list = all_data.config.metric_list
  23. # metric_list = ['ldw_miss_warning_count', 'ldw_miss_warning_count2', 'ldw_miss_warning_count3']
  24. # 遍历文件夹中的所有文件
  25. for filename in os.listdir(folder_path):
  26. if filename.endswith('.py') and not filename.startswith('__') and filename[
  27. :-3] in metric_list: # 过滤掉非Python文件或特殊文件
  28. # 构造模块的完整路径
  29. module_path = os.path.join(folder_path, filename)
  30. # 使用importlib来动态导入模块
  31. spec = importlib.util.spec_from_file_location(filename[:-3], module_path)
  32. module = importlib.util.module_from_spec(spec)
  33. spec.loader.exec_module(module)
  34. # 假设每个模块都有一个CustomMetric类
  35. if hasattr(module, 'CustomMetric'):
  36. CustomMetric = getattr(module, 'CustomMetric')
  37. # 实例化CustomMetric类
  38. metric_instance = CustomMetric(all_data, case_name)
  39. metric_result = metric_instance.result
  40. # 将结果存储到字典中,key为文件名,value为各个方法的结果
  41. results[filename[:-3]] = metric_result
  42. return results
  43. # if __name__ == '__main__':
  44. # # 文件夹路径,自定义指标Python文件都在此文件夹下
  45. # folder_path = '../custom'
  46. # path = r"..\task_1228\case1228\data"
  47. # configPath = r"../configs/config.json"
  48. # config = ConfigParse(configPath)
  49. # all_data = DataProcess(path, config)
  50. #
  51. # custom_run(folder_path, all_data)