pitchGeneratorSpeedAnalyst.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. from datetime import datetime
  3. import numpy as np
  4. import pandas as pd
  5. import plotly.graph_objects as go
  6. from algorithmContract.confBusiness import *
  7. from algorithmContract.contract import Contract
  8. from behavior.analystWithGoodBadPoint import AnalystWithGoodBadPoint
  9. class PitchGeneratorSpeedAnalyst(AnalystWithGoodBadPoint):
  10. """
  11. 风电机组变桨-发电机转速分析
  12. """
  13. def typeAnalyst(self):
  14. return "pitch_generator_speed"
  15. # def recalculation(self, turbineModelInfo: pd.Series, dataFrame: pd.DataFrame):
  16. # return self.recalculationOfGeneratorSpeed(
  17. # dataFrame, Field_RotorSpeed, Field_GeneratorSpeed, self.turbineModelInfo[Field_RSR].iloc[0])
  18. def selectColumns(self):
  19. return [Field_DeviceCode, Field_Time,Field_WindSpeed,Field_ActiverPower,Field_PitchAngel1, Field_GeneratorSpeed]
  20. def turbinesAnalysis(self, outputAnalysisDir, conf: Contract, turbineCodes):
  21. dictionary = self.processTurbineData(turbineCodes, conf,self.selectColumns())
  22. dataFrame = self.userDataFrame(dictionary,conf.dataContract.configAnalysis,self)
  23. return self.plot_speed_pitch_angle(dataFrame,outputAnalysisDir, conf, )
  24. def plot_speed_pitch_angle(self, dataFrameMerge, outputAnalysisDir, conf: Contract):
  25. # 按设备名分组数据
  26. dataFrameMerge = dataFrameMerge[(dataFrameMerge[Field_ActiverPower] > 0)].sort_values(by=Field_YearMonth)
  27. grouped = dataFrameMerge.groupby([Field_NameOfTurbine, Field_CodeOfTurbine])
  28. # 遍历每个设备并绘制散点图
  29. result_rows = []
  30. for name, group in grouped:
  31. groupNew = group.copy()
  32. # 处理直驱转速与非直驱的差异
  33. # if not self.common.isNone(conf.value_gen_speed_multiple):
  34. # groupNew[fieldGeneratorSpeed] = group[fieldGeneratorSpeed] * \
  35. # conf.value_gen_speed_multiple
  36. # 创建图形
  37. fig = go.Figure()
  38. # 添加散点图
  39. fig.add_trace(go.Scatter(
  40. x=groupNew[Field_GeneratorSpeed],
  41. y=groupNew[Field_PitchAngel1],
  42. mode='markers',
  43. # marker=dict(color='blue', size=3.5)
  44. marker=dict(
  45. color=group[Field_UnixYearMonth],
  46. colorscale='Rainbow',
  47. size=3,
  48. opacity=0.7,
  49. colorbar=dict(
  50. tickvals=np.linspace(
  51. group[Field_UnixYearMonth].min(), group[Field_UnixYearMonth].max(), 6),
  52. ticktext=[datetime.fromtimestamp(ts).strftime('%Y-%m') for ts in np.linspace(
  53. group[Field_UnixYearMonth].min(), group[Field_UnixYearMonth].max(), 6)],
  54. thickness=18,
  55. len=1, # 设置颜色条的长度,使其占据整个图的高度
  56. outlinecolor='rgba(255,255,255,0)'
  57. ),
  58. showscale=True
  59. ),
  60. showlegend=False
  61. ))
  62. # 设置图形布局
  63. fig.update_layout(
  64. title=f'机组: {name[0]}',
  65. xaxis=dict(
  66. title='发电机转速',
  67. range=[self.axisLowerLimitGeneratorSpeed, self.axisUpperLimitGeneratorSpeed],
  68. dtick=self.axisStepGeneratorSpeed,
  69. tickangle=-45 # 设置x轴刻度值旋转角度为45度,如果需要
  70. ),
  71. yaxis=dict(
  72. title='桨距角',
  73. range=[self.axisLowerLimitPitchAngle, self.axisUpperLimitPitchAngle],
  74. dtick=self.axisStepPitchAngle
  75. ),
  76. coloraxis=dict(
  77. colorbar=dict(
  78. title="时间",
  79. ticks="outside",
  80. len=1, # 设置颜色条的长度,使其占据整个图的高度
  81. thickness=20, # 调整颜色条的宽度
  82. orientation='v', # 设置颜色条为垂直方向
  83. tickmode='array', # 确保刻度按顺序排列
  84. tickvals=dataFrameMerge[Field_YearMonth].unique(
  85. ).tolist(), # 确保刻度为唯一的年月
  86. ticktext=dataFrameMerge[Field_YearMonth].unique(
  87. ).tolist() # 以%Y-%m格式显示标签
  88. )
  89. )
  90. )
  91. # Save plot
  92. filePathOfImage = os.path.join(outputAnalysisDir, f"{name[0]}.png")
  93. fig.write_image(filePathOfImage, scale=3)
  94. filePathOfHtml = os.path.join(outputAnalysisDir, f"{name[0]}.html")
  95. fig.write_html(filePathOfHtml)
  96. result_rows.append({
  97. Field_Return_TypeAnalyst: self.typeAnalyst(),
  98. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  99. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  100. Field_CodeOfTurbine: name[1],
  101. Field_Return_FilePath: filePathOfImage,
  102. Field_Return_IsSaveDatabase: False
  103. })
  104. result_rows.append({
  105. Field_Return_TypeAnalyst: self.typeAnalyst(),
  106. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  107. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  108. Field_CodeOfTurbine: name[1],
  109. Field_Return_FilePath: filePathOfHtml,
  110. Field_Return_IsSaveDatabase: True
  111. })
  112. result_df = pd.DataFrame(result_rows)
  113. return result_df