taipingli_biaozhunhua.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import datetime
  2. import multiprocessing
  3. import os
  4. import sys
  5. sys.path.insert(0, os.path.abspath(__file__).split("tmp_file")[0])
  6. import pandas as pd
  7. from utils.file.trans_methods import read_file_to_df
  8. def get_time_space_count(start_time: datetime.datetime, end_time: datetime.datetime, time_space=1):
  9. """
  10. 获取俩个时间之间的个数
  11. :return: 查询时间间隔
  12. """
  13. delta = end_time - start_time
  14. total_seconds = delta.days * 24 * 60 * 60 + delta.seconds
  15. return abs(int(total_seconds / time_space)) + 1
  16. def save_percent(value, save_decimal=7):
  17. return round(value, save_decimal) * 100
  18. def read_and_select(df, wind_name, read_cols):
  19. result_df = pd.DataFrame()
  20. # wind_name = os.path.basename(file_path).split('.')[0]
  21. result_df['风机号'] = [wind_name]
  22. # df = df.query("(Time>='2024-06-01 00:00:00') & (Time<'2024-12-01 00:00:00')")
  23. count = get_time_space_count(df['Time'].min(), df['Time'].max(), 1)
  24. print(df['Time'].min(), df['Time'].max(), count)
  25. repeat_time_count = df.shape[0] - len(df['Time'].unique())
  26. print(wind_name, count, repeat_time_count)
  27. result_df['重复率'] = [save_percent(repeat_time_count / count)]
  28. result_df['重复次数'] = [repeat_time_count]
  29. result_df['总记录数'] = [count]
  30. for read_col in read_cols:
  31. if read_col != 'Time':
  32. df[read_col] = pd.to_numeric(df[read_col], errors='coerce')
  33. group_df = df.groupby(by=['风机号']).count()
  34. group_df.reset_index(inplace=True)
  35. count_df = pd.DataFrame(group_df)
  36. total_count = count_df[read_cols].values[0].sum()
  37. print(wind_name, total_count, count * len(read_cols))
  38. result_df['平均缺失率,单位%'] = [save_percent(1 - total_count / (count * len(read_cols)))]
  39. result_df['缺失数值'] = [
  40. '-'.join(
  41. [str(read_cols[index]) + ':' + str(count - i) for index, i in enumerate(count_df[read_cols].values[0])])]
  42. del group_df
  43. fengsu_count = 0
  44. fengsu_cols = [i for i in read_cols if i.find('风速') > -1]
  45. fengsu_str = ''
  46. for col in fengsu_cols:
  47. now_count = df[(df[col] < 0) | (df[col] > 80)].shape[0]
  48. fengsu_count = fengsu_count + now_count
  49. fengsu_str = fengsu_str + ',' + col + ':' + str(fengsu_count)
  50. result_df['风速异常'] = [fengsu_str]
  51. gonglv_cols = ['有功功率(kW)', '风机出口有功功率(kW)']
  52. gonglv_count = 0
  53. gonglv_str = ''
  54. for col in gonglv_cols:
  55. now_count = df[(df[col] < -200) | (df[col] > 3000)].shape[0]
  56. gonglv_count = gonglv_count + now_count
  57. gonglv_str = gonglv_str + ',' + col + ':' + str(gonglv_count)
  58. result_df['功率异常'] = [gonglv_str]
  59. result_df['平均异常率'] = [
  60. save_percent((fengsu_count + fengsu_count) / ((len(fengsu_cols) + len(gonglv_cols)) * count))]
  61. return result_df
  62. if __name__ == '__main__':
  63. # read_cols = ['Time', '设备主要状态', '功率曲线风速', '湍流强度', '实际风速', '有功功率', '桨叶角度A', '桨叶角度B',
  64. # '桨叶角度C', '机舱内温度', '机舱外温度', '绝对风向', '机舱绝对位置', '叶轮转速', '发电机转速',
  65. # '瞬时风速',
  66. # '有功设定反馈', '当前理论可发最大功率', '空气密度', '偏航误差', '发电机扭矩', '瞬时功率', '风向1s',
  67. # '偏航压力', '桨叶1速度', '桨叶2速度', '桨叶3速度', '桨叶1角度给定', '桨叶2角度给定', '桨叶3角度给定',
  68. # '轴1电机电流', '轴2电机电流', '轴3电机电流', '轴1电机温度', '轴2电机温度', '轴3电机温度', '待机',
  69. # '启动',
  70. # '偏航', '并网', '限功率', '正常发电', '故障', '计入功率曲线', '运行发电机冷却风扇1',
  71. # '运行发电机冷却风扇2',
  72. # '激活偏航解缆阀', '激活偏航刹车阀', '激活风轮刹车阀', '激活顺时针偏航', '激活逆时针偏航', '电缆扭角']
  73. read_dir = r'D:\data\tmp\sec.csv'
  74. df = read_file_to_df(read_dir)
  75. print(df.columns)
  76. del df['Unnamed: 79']
  77. df.rename(columns={'Unnamed: 0': 'Time'}, inplace=True)
  78. print(df.columns)
  79. df['Time'] = pd.to_datetime(df['Time'], errors='coerce')
  80. df['风机号'] = pd.to_numeric(df['风机号'], errors='coerce')
  81. df = df[df['风机号'].isin([i for i in range(1, 6)])]
  82. read_cols = list(df.columns)
  83. read_cols.remove('Time')
  84. read_cols.remove('风机号')
  85. wind_names = df['风机号'].unique()
  86. with multiprocessing.Pool(5) as pool:
  87. dfs = pool.starmap(read_and_select,
  88. [(df[df['风机号'] == wind_name], wind_name, read_cols) for wind_name in wind_names])
  89. resu_df = pd.concat(dfs, ignore_index=True)
  90. print(resu_df.columns)
  91. resu_df.sort_values(by=['风机号'], inplace=True)
  92. resu_df.to_csv("太平里-1秒.csv", encoding='utf8', index=False)