cluster_analysis.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/5/11 14:43
  4. # file: cluster_power.py
  5. # author: David
  6. # company: shenyang JY
  7. import os
  8. import re
  9. import numpy as np
  10. import pandas as pd
  11. def read_cfs(cfs, input_path, output_path, is_folder=False):
  12. if not os.path.exists(output_path):
  13. os.makedirs(output_path)
  14. dfs = {}
  15. for j, ids in cfs.items():
  16. if is_folder:
  17. dirname = input_path.split('/')[-1]
  18. x = re.findall('(?<=Continuous_Turbine_Data_).*?(?=_)',dirname)[0]
  19. dfs_j = [pd.read_csv(os.path.join(input_path, f"turbine-{id}_{int(x)}.csv")) for id in ids]
  20. else:
  21. dfs_j = [pd.read_csv(os.path.join(input_path, f"turbine-{id}.csv")) for id in ids]
  22. dfj, time_series = dfs_j[0].loc[:, ['C_TIME', 'C_WS', 'C_ACTIVE_POWER']], dfs_j[0]['C_TIME']
  23. for df in dfs_j[1:]:
  24. if df['C_TIME'].equals(time_series) is False:
  25. print("风机之间的日期不一致!")
  26. raise ValueError
  27. dfj['C_ACTIVE_POWER'] += df['C_ACTIVE_POWER']
  28. dfj['C_WS'] += df['C_WS']
  29. dfj['C_WS'] /= len(dfs_j)
  30. dfj.rename(columns=({'C_ACTIVE_POWER':'C_ACTIVE_POWER'+str(j), 'C_WS': 'C_WS'+str(j)}), inplace=True)
  31. if is_folder:
  32. dfj[20:].to_csv(os.path.join(output_path, 'cluster_' + str(j) + '.csv'), index=False)
  33. else:
  34. dfj[20:].to_csv(os.path.join(output_path, 'cluster_' + str(j) + '.csv'), index=False)
  35. dfs[j] = dfj
  36. return dfs
  37. def get_cfs(cluster, turbine_id):
  38. cfs = {}
  39. for j in range(1, max(cluster) + 1):
  40. arr_j = np.where(cluster == j)[0] # cluster中聚类j的索引列表
  41. cfs.setdefault(j, [turbine_id[k] for k in arr_j])
  42. for key, value in cfs.items():
  43. print("第{}组:{}".format(key, cfs[key]))
  44. return cfs
  45. def cluster_data_indep(dfs_cluster, root_path):
  46. df_power = pd.read_csv(root_path + "power.csv")
  47. df_nwp = pd.read_csv(root_path + "NWP.csv",
  48. usecols=["C_TIME", "C_WS100", "C_WS170"])
  49. df_all = pd.concat([df_power.set_index("C_TIME"), df_nwp.set_index("C_TIME"),
  50. dfs_cluster], axis=1, join="inner")
  51. return df_all
  52. def cluster_power_list_file(cluster, turbine_id, input_path, output_path):
  53. """
  54. 从turbine-*.csv的文件列表中进行聚类功率相加
  55. cluster:聚类的结果
  56. turbine_id:风机ID
  57. input_path:输入路径 output_filtered_csv_files 所在路径
  58. output_path:输出每个聚类的功率,和所有聚类的功率cluster_data
  59. """
  60. if not os.path.exists(output_path):
  61. os.makedirs(output_path)
  62. cfs = get_cfs(cluster, turbine_id)
  63. dfs = read_cfs(cfs, input_path, output_path)
  64. dfs_cluster = pd.concat([df.set_index("C_TIME") for df in dfs.values()], join='inner', axis=1)
  65. dfs_cluster['SUM'] = dfs_cluster.filter(like='C_ACTIVE_POWER').sum(axis=1)
  66. dfs_cluster = cluster_data_indep(dfs_cluster, '../data-process/data/')
  67. dfs_cluster.reset_index().to_csv(os.path.join(output_path, 'cluster_data.csv'), index=False)
  68. def cluster_power_list_folder(cluster, turbine_id, input_path, output_path):
  69. """
  70. 从嵌套turbine-*.csv的多个文件夹列表中进行聚类功率相加
  71. cluster:聚类的结果
  72. turbine_id:风机ID
  73. input_path:输入路径 continuous_data 所在路径
  74. output_path:输出每个聚类的功率,和所有聚类的功率cluster_data
  75. """
  76. if not os.path.exists(output_path):
  77. os.makedirs(output_path)
  78. continuous_list = [os.path.join(input_path, path) for path in os.listdir(input_path)]
  79. cfs = get_cfs(cluster, turbine_id)
  80. for con in continuous_list:
  81. dirname = con.split('/')[-1]
  82. output = os.path.join(output_path, dirname)
  83. dfs = read_cfs(cfs, con, output, True)
  84. dfs_cluster = pd.concat([df.set_index("C_TIME") for df in dfs.values()], join='inner', axis=1)
  85. dfs_cluster.reset_index().to_csv(os.path.join(output, 'cluster_data.csv'), index=False)
  86. if __name__ == '__main__':
  87. turbine_id = list(range(102, 162))
  88. cluster = np.array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
  89. cluster[42] = 1
  90. output_path = '../data-process/data/cluster_power/'
  91. cluster_power_list_file(cluster, turbine_id,
  92. input_path='../data-process/data/output_filtered_csv_files/', output_path=output_path)
  93. cluster_power_list_folder(cluster, turbine_id, input_path='../data-process/data/continuous_data/',
  94. output_path=output_path)