| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import os
- import pandas as pd
- import numpy as np
- import plotly.graph_objects as go
- import plotly.express as px
- from plotly.subplots import make_subplots
- from behavior.analyst import Analyst
- from utils.directoryUtil import DirectoryUtil as dir
- import matplotlib.pyplot as plt
- from algorithmContract.confBusiness import *
- class WindSpeedFrequencyAnalyst(Analyst):
- def typeAnalyst(self):
- return "wind_speed_frequency"
-
- def filterCommon(self,dataFrame:pd.DataFrame, confData:ConfBusiness):
- return dataFrame
- def turbinesAnalysis(self, dataFrameMerge, outputAnalysisDir, confData: ConfBusiness):
- self.windRoseAnalysis(dataFrameMerge, outputAnalysisDir, confData)
- def windRoseAnalysis(self, dataFrameMerge: pd.DataFrame, outputAnalysisDir, confData: ConfBusiness):
- # 检查所需列是否存在
- required_columns = {Field_NameOfTurbine, confData.field_wind_speed}
- if not required_columns.issubset(dataFrameMerge.columns):
- raise ValueError(f"DataFrame缺少必要的列。需要的列有: {required_columns}")
- wind_speed_bins = np.arange(0, 26, 0.5) # x轴风速范围 ,间隔0.5
- # 按设备名分组数据
- grouped = dataFrameMerge.groupby(Field_NameOfTurbine)
- for name, group in grouped:
- # 2. 计算风速频率
- # 首先,我们需要确定风速的范围并计算每个风速的频数
- wind_speeds = group[confData.field_wind_speed].unique()
- # 计算风速频率,确保频率没有零值(用很小的数代替零)
- wind_speed_freq = np.histogram(wind_speeds, bins=wind_speed_bins)[0] / len(wind_speeds) * 100
- # 3. & 4. 确定y轴风速频率的范围和间隔(这里直接计算了频率,所以不需要手动设置间隔)
- # 我们已经计算了风速频率,因此不需要再手动设置y轴的间隔和范围
- # 5. 使用plotly绘制风速频率分布柱状图
- # 为了使用plotly绘制柱状图,我们需要将风速范围的中点作为x轴的值
- x_values = (wind_speed_bins[:-1] + wind_speed_bins[1:]) / 2
- # 创建柱状图
- fig = px.bar(x=x_values, y=wind_speed_freq)
- # 更新图形的布局
- fig.update_layout(
- title={
- 'text': f'Wind Speed Frequency {name}',
- # 'y': 0.95,
- 'x': 0.5,
- 'xanchor': 'center',
- 'yanchor': 'top'
- },
- xaxis=dict(
- title='Wind Speed (m/s)',
- showgrid=True,
- range=[0, 26],
- dtick=1,
- tickangle=-45
- ),
- yaxis=dict(
- title='Frequency (%)',
- showgrid=True,
- # range=[0, 1],
- ),
- margin=dict(t=50, b=10) # t为顶部(top)间距,b为底部(bottom)间距
- )
- # # 更新x轴和y轴的范围和标签
- # fig.update_yaxes(range=[0, max(wind_speed_freq) * 1.1 if max(wind_speed_freq) > 0 else 0.2], title='Frequency')
-
- # # 保存html
- outputFileHtml = os.path.join(outputAnalysisDir, f"{name}.html")
- fig.write_html(outputFileHtml)
- # 保存图像
- # output_file = os.path.join(outputAnalysisDir, f"{name}.png")
- # fig.write_image(output_file, scale=2)
|