generatorSpeedTorqueAnalyst.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import os
  2. from datetime import datetime
  3. import numpy as np
  4. import pandas as pd
  5. import plotly.express as px
  6. import plotly.graph_objects as go
  7. from algorithmContract.confBusiness import *
  8. from algorithmContract.contract import Contract
  9. from behavior.analystWithGoodPoint import AnalystWithGoodPoint
  10. class GeneratorSpeedTorqueAnalyst(AnalystWithGoodPoint):
  11. """
  12. 风电机组发电机转速-转矩分析
  13. """
  14. def typeAnalyst(self):
  15. return "speed_torque"
  16. def turbinesAnalysis(self, outputAnalysisDir, conf: Contract, turbineCodes):
  17. dictionary = self.processTurbineData(turbineCodes, conf, [
  18. Field_DeviceCode, Field_Time,Field_RotorSpeed,Field_GeneratorSpeed,Field_GeneratorTorque, Field_WindSpeed, Field_ActiverPower])
  19. dataFrameOfTurbines = self.userDataFrame(
  20. dictionary, conf.dataContract.configAnalysis, self)
  21. # 检查所需列是否存在
  22. required_columns = {Field_CodeOfTurbine,Field_RotorSpeed,Field_GeneratorSpeed,Field_GeneratorTorque}
  23. if not required_columns.issubset(dataFrameOfTurbines.columns):
  24. raise ValueError(f"DataFrame缺少必要的列。需要的列有: {required_columns}")
  25. turbrineInfos = self.common.getTurbineInfos(
  26. conf.dataContract.dataFilter.powerFarmID, turbineCodes, self.turbineInfo)
  27. groupedOfTurbineModel = turbrineInfos.groupby(Field_MillTypeCode)
  28. returnDatas = []
  29. for turbineModelCode, group in groupedOfTurbineModel:
  30. currTurbineCodes = group[Field_CodeOfTurbine].unique().tolist()
  31. currTurbineModeInfo = self.common.getTurbineModelByCode(
  32. turbineModelCode, self.turbineModelInfo)
  33. currDataFrameOfTurbines = dataFrameOfTurbines[dataFrameOfTurbines[Field_CodeOfTurbine].isin(
  34. currTurbineCodes)]
  35. # 将 currTurbineInfos 转换为字典
  36. currTurbineInfos_dict = turbrineInfos.set_index(Field_CodeOfTurbine)[Field_NameOfTurbine].to_dict()
  37. # 使用 map 函数来填充 Field_NameOfTurbine 列
  38. currDataFrameOfTurbines[Field_NameOfTurbine] = currDataFrameOfTurbines[Field_CodeOfTurbine].map(currTurbineInfos_dict).fillna("")
  39. result2D = self.drawScatter2DMonthly(
  40. currDataFrameOfTurbines, outputAnalysisDir, conf)
  41. returnDatas.extend(result2D)
  42. result3D = self.drawScatterGraph(
  43. currDataFrameOfTurbines, outputAnalysisDir, conf)
  44. returnDatas.extend(result3D)
  45. resultTotal = self.drawScatterGraphForTurbines(
  46. currDataFrameOfTurbines, outputAnalysisDir, conf, currTurbineModeInfo)
  47. returnDatas.extend(resultTotal)
  48. returnDataFrame = pd.DataFrame(returnDatas)
  49. return returnDataFrame
  50. def drawScatter2DMonthlyOfTurbine(self, dataFrame: pd.DataFrame, outputAnalysisDir: str, conf: Contract, turbineName: str):
  51. # 设置颜色条参数
  52. dataFrame = dataFrame.sort_values(by=Field_YearMonth)
  53. # 绘制 Plotly 散点图
  54. fig = go.Figure(data=go.Scatter(
  55. x=dataFrame[Field_GeneratorSpeed],
  56. y=dataFrame[Field_GeneratorTorque],
  57. # color=Field_YearMonth,
  58. # color_continuous_scale='Rainbow', # 颜色条样式
  59. mode='markers',
  60. marker=dict(
  61. color=dataFrame['monthIntTime'],
  62. colorscale='Rainbow',
  63. size=3,
  64. opacity=0.7,
  65. colorbar=dict(
  66. tickvals=np.linspace(
  67. dataFrame['monthIntTime'].min(), dataFrame['monthIntTime'].max(), 6),
  68. ticktext=[datetime.fromtimestamp(ts).strftime('%Y-%m') for ts in np.linspace(
  69. dataFrame['monthIntTime'].min(), dataFrame['monthIntTime'].max(), 6)],
  70. thickness=18,
  71. len=1, # 设置颜色条的长度,使其占据整个图的高度
  72. outlinecolor='rgba(255,255,255,0)'
  73. ),
  74. showscale=True
  75. ),
  76. # labels={Field_GeneratorSpeed: 'Generator Speed',
  77. # Field_YearMonth: 'Time', Field_GeneratorTorque: 'Torque'},
  78. showlegend=False
  79. ))
  80. # # 设置固定散点大小
  81. # fig.update_traces(marker=dict(size=3))
  82. # 如果需要颜色轴的刻度和标签
  83. # 以下是以比例方式进行色彩的可视化处理
  84. fig.update_layout(
  85. title={
  86. "text": f'月度发电机转速扭矩散点图: {turbineName}',
  87. # "x": 0.5
  88. },
  89. xaxis=dict(
  90. title='发电机转速',
  91. dtick=self.axisStepGeneratorSpeed,
  92. range=[self.axisLowerLimitGeneratorSpeed,
  93. self.axisUpperLimitGeneratorSpeed],
  94. tickangle=-45
  95. ),
  96. yaxis=dict(
  97. title='扭矩',
  98. dtick=self.axisStepGeneratorTorque,
  99. range=[self.axisLowerLimitGeneratorTorque,
  100. self.axisUpperLimitGeneratorTorque],
  101. )
  102. # coloraxis=dict(
  103. # colorbar=dict(
  104. # title="Time",
  105. # ticks="outside",
  106. # len=1, # 设置颜色条的长度,使其占据整个图的高度
  107. # thickness=20, # 调整颜色条的宽度
  108. # orientation='v', # 设置颜色条为垂直方向
  109. # tickmode='array', # 确保刻度按顺序排列
  110. # tickvals=dataFrame[Field_YearMonth].unique(
  111. # ).tolist(), # 确保刻度为唯一的年月
  112. # ticktext=dataFrame[Field_YearMonth].unique(
  113. # ).tolist() # 以%Y-%m格式显示标签
  114. # )
  115. # )
  116. )
  117. # 保存图片
  118. outputFilePathPNG = os.path.join(
  119. outputAnalysisDir, f"{turbineName}.png")
  120. fig.write_image(outputFilePathPNG, width=800, height=600, scale=3)
  121. # 保存html
  122. outputFileHtml = os.path.join(outputAnalysisDir, f"{turbineName}.html")
  123. fig.write_html(outputFileHtml)
  124. result = []
  125. result.append({
  126. Field_Return_TypeAnalyst: self.typeAnalyst(),
  127. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  128. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  129. Field_CodeOfTurbine: dataFrame[Field_CodeOfTurbine].iloc[0],
  130. Field_Return_FilePath: outputFilePathPNG,
  131. Field_Return_IsSaveDatabase: False
  132. })
  133. result.append({
  134. Field_Return_TypeAnalyst: self.typeAnalyst(),
  135. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  136. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  137. Field_CodeOfTurbine: dataFrame[Field_CodeOfTurbine].iloc[0],
  138. Field_Return_FilePath: outputFileHtml,
  139. Field_Return_IsSaveDatabase: True
  140. })
  141. return result
  142. def drawScatterGraphOfTurbine(self, dataFrame: pd.DataFrame, outputAnalysisDir: str, conf: Contract, turbineName: str):
  143. # 创建3D散点图
  144. fig = px.scatter_3d(dataFrame,
  145. x=Field_GeneratorSpeed,
  146. y=Field_YearMonth,
  147. z=Field_GeneratorTorque,
  148. color=Field_YearMonth,
  149. labels={Field_GeneratorSpeed: '发电机转速',
  150. Field_YearMonth: '时间', Field_GeneratorTorque: '扭矩'}
  151. )
  152. # 设置固定散点大小
  153. fig.update_traces(marker=dict(size=1.5))
  154. # 更新图形的布局
  155. fig.update_layout(
  156. title={
  157. "text": f'月度发电机转速扭矩3D散点图: {turbineName}',
  158. "x": 0.5
  159. },
  160. scene=dict(
  161. xaxis=dict(
  162. title='发电机转速',
  163. dtick=self.axisStepGeneratorSpeed, # 设置y轴刻度间隔
  164. range=[self.axisLowerLimitGeneratorSpeed,
  165. self.axisUpperLimitGeneratorSpeed], # 设置y轴的范围
  166. showgrid=True, # 显示网格线
  167. ),
  168. yaxis=dict(
  169. title='时间',
  170. tickformat='%Y-%m', # 日期格式,
  171. dtick='M1', # 每月一个刻度
  172. showgrid=True, # 显示网格线
  173. ),
  174. zaxis=dict(
  175. title='扭矩',
  176. dtick=self.axisStepGeneratorTorque,
  177. range=[self.axisLowerLimitGeneratorTorque,
  178. self.axisUpperLimitGeneratorTorque],
  179. showgrid=True, # 显示网格线
  180. )
  181. ),
  182. scene_camera=dict(
  183. up=dict(x=0, y=0, z=1), # 保持相机向上方向不变
  184. center=dict(x=0, y=0, z=0), # 保持相机中心位置不变
  185. eye=dict(x=-1.8, y=-1.8, z=1.2) # 调整eye属性以实现水平旋转180°
  186. ),
  187. # 设置图例标题
  188. # legend_title_text='Time',
  189. legend=dict(
  190. orientation="h",
  191. itemsizing="constant", # Use constant size for legend items
  192. itemwidth=80 # Set the width of legend items to 50 pixels
  193. )
  194. )
  195. # 保存图像
  196. outputFileHtml = os.path.join(
  197. outputAnalysisDir, "{}_3D.html".format(turbineName))
  198. fig.write_html(outputFileHtml)
  199. result = []
  200. result.append({
  201. Field_Return_TypeAnalyst: self.typeAnalyst(),
  202. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  203. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  204. Field_CodeOfTurbine: dataFrame[Field_CodeOfTurbine].iloc[0],
  205. Field_Return_FilePath: outputFileHtml,
  206. Field_Return_IsSaveDatabase: True
  207. })
  208. return result
  209. def drawScatter2DMonthly(self, dataFrameMerge: pd.DataFrame, outputAnalysisDir, conf: Contract):
  210. results = []
  211. grouped = dataFrameMerge.groupby(Field_NameOfTurbine)
  212. for name, group in grouped:
  213. result = self.drawScatter2DMonthlyOfTurbine(
  214. group, outputAnalysisDir, conf, name)
  215. results.extend(result)
  216. return results
  217. def drawScatterGraph(self, dataFrame: pd.DataFrame, outputAnalysisDir, conf: Contract):
  218. """
  219. 绘制风速-功率分布图并保存为文件。
  220. 参数:
  221. dataFrameMerge (pd.DataFrame): 包含数据的DataFrame,需要包含设备名、风速和功率列。
  222. outputAnalysisDir (str): 分析输出目录。
  223. confData (Contract): 配置
  224. """
  225. results = []
  226. dataFrame = dataFrame[(dataFrame[Field_GeneratorTorque] > 0)].sort_values(
  227. by=Field_YearMonth)
  228. grouped = dataFrame.groupby(Field_NameOfTurbine)
  229. # 遍历每个设备的数据
  230. for name, group in grouped:
  231. if len(group[Field_YearMonth].unique()) > 1:
  232. result = self.drawScatterGraphOfTurbine(
  233. group, outputAnalysisDir, conf, name)
  234. results.extend(result)
  235. return results
  236. def drawScatterGraphForTurbines(self, dataFrame: pd.DataFrame, outputAnalysisDir, conf: Contract, turbineModelInfo: pd.Series):
  237. """
  238. 绘制风速-功率分布图并保存为文件。
  239. 参数:
  240. dataFrameMerge (pd.DataFrame): 包含数据的DataFrame,需要包含设备名、风速和功率列。
  241. outputAnalysisDir (str): 分析输出目录。
  242. confData (Contract): 配置
  243. """
  244. dataFrame = dataFrame[(dataFrame[Field_GeneratorTorque] > 0)].sort_values(
  245. by=Field_NameOfTurbine)
  246. # 创建3D散点图
  247. fig = px.scatter_3d(dataFrame,
  248. x=Field_GeneratorSpeed,
  249. y=Field_NameOfTurbine,
  250. z=Field_GeneratorTorque,
  251. color=Field_NameOfTurbine,
  252. labels={Field_GeneratorSpeed: '发电机转速',
  253. Field_NameOfTurbine: '机组', Field_GeneratorTorque: '实际扭矩'}
  254. )
  255. # 设置固定散点大小
  256. fig.update_traces(marker=dict(size=1.5))
  257. # 更新图形的布局
  258. fig.update_layout(
  259. title={
  260. "text": f'发电机转速扭矩3D散点图-{turbineModelInfo[Field_MachineTypeCode]}',
  261. "x": 0.5
  262. },
  263. scene=dict(
  264. xaxis=dict(
  265. title='发电机转速',
  266. dtick=self.axisStepGeneratorSpeed, # 设置y轴刻度间隔
  267. range=[self.axisLowerLimitGeneratorSpeed,
  268. self.axisUpperLimitGeneratorSpeed], # 设置y轴的范围
  269. showgrid=True, # 显示网格线
  270. ),
  271. yaxis=dict(
  272. title='机组',
  273. showgrid=True, # 显示网格线
  274. ),
  275. zaxis=dict(
  276. title='实际扭矩',
  277. dtick=self.axisStepGeneratorTorque,
  278. range=[self.axisLowerLimitGeneratorTorque,
  279. self.axisUpperLimitGeneratorTorque],
  280. )
  281. ),
  282. scene_camera=dict(
  283. up=dict(x=0, y=0, z=1), # 保持相机向上方向不变
  284. center=dict(x=0, y=0, z=0), # 保持相机中心位置不变
  285. eye=dict(x=-1.8, y=-1.8, z=1.2) # 调整eye属性以实现水平旋转180°
  286. ),
  287. # 设置图例标题
  288. # legend_title_text='Turbine',
  289. # margin=dict(t=50, b=10) # t为顶部(top)间距,b为底部(bottom)间距
  290. legend=dict(
  291. orientation="h",
  292. itemsizing="constant", # Use constant size for legend items
  293. itemwidth=80 # Set the width of legend items to 50 pixels
  294. )
  295. )
  296. # 保存图像
  297. outputFileHtml = os.path.join(
  298. outputAnalysisDir, "{}-{}.html".format(self.typeAnalyst(),turbineModelInfo[Field_MillTypeCode]))
  299. fig.write_html(outputFileHtml)
  300. result = []
  301. result.append({
  302. Field_Return_TypeAnalyst: self.typeAnalyst(),
  303. Field_PowerFarmCode: conf.dataContract.dataFilter.powerFarmID,
  304. Field_Return_BatchCode: conf.dataContract.dataFilter.dataBatchNum,
  305. Field_CodeOfTurbine: Const_Output_Total,
  306. Field_Return_FilePath: outputFileHtml,
  307. Field_Return_IsSaveDatabase: True
  308. })
  309. return result