Skip to content

Commit

Permalink
iss #317 update doc strings and add tests for ti_by_speed
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaMorandi committed Sep 22, 2022
1 parent deed02d commit b5739a6
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 70 deletions.
160 changes: 105 additions & 55 deletions brightwind/analyse/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,35 +1050,81 @@ def calc(wspd, wspd_std):
def by_speed(wspd, wspd_std, speed_bin_array=np.arange(-0.5, 41, 1), speed_bin_labels=range(0, 41),
percentile=90, IEC_class=None, return_data=False):
"""
Accepts a wind speed series and its standard deviation, calculates turbulence intensity (TI) and returns the
distribution by of TI by speed bins
:param wspd: Wind speed data series
:type wspd: pandas.Series
:param wspd_std: Wind speed standard deviation data series
:type wspd_std: pandas.Series
:param speed_bin_array: (Optional) Array of wind speeds where adjacent elements of array form a bin
:type speed_bin_array: List or array
:param speed_bin_labels: (Optional) Labels to use for speed bins, 0, 1, 2, 3 .. and so on by default
:type speed_bin_labels: List, range or array
:param percentile: The percentile representative of TI (see return for more information)
:type percentile: float, int
:param IEC_class: By default IEC class 2005 is used for custom class pass a DataFrame. Note we have removed
option to include IEC Class 1999 as no longer appropriate.
This may need to be placed in a separate function when updated IEC standard is released
:param return_data: Set to True if you want the data returned.
:type return_data: bool
:return: TI distribution with columns names as:
* Mean_TI (average TI for a speed bin),
* TI_Count ( number of data points in the bin),
* Rep_TI (Representative TI set at 90 percentile by default,
* TI_2Sigma (2 sigma TI),
* Char_TI (characteristic TI)
:rtype: pandas.DataFrame
Accepts a wind speed series and its standard deviation, calculates turbulence intensity (TI) and returns a
scatter plot of TI versus speed and the distribution of TI by speed bins. Note that speed values lower than
3 m/s are filtered out when deriving the TI values.
:param wspd: Wind speed data series
:type wspd: pandas.Series
:param wspd_std: Wind speed standard deviation data series
:type wspd_std: pandas.Series
:param speed_bin_array: (Optional) Array of numbers where adjacent elements of array form a speed bin.
Default is numpy.arange(-0.5, 41, 1), this is an array of speed values
from -0.5 to 40.5 with a 1 m/s bin interval (ie first bin is -0.5 to 0.5)
:type speed_bin_array: list or numpy.array
:param speed_bin_labels: (Optional) Labels to use for speed bins in the output TI distribution table.
Note that labels correspond with the central bins of each adjacent element
of the input speed_bin_array. Default is an array of values from 0 to 40 with an
interval equal to 1 (ie 0, 1, 2, 3 ..). The length of the speed_bin_labels array
must be equal to len(speed_bin_array) - 1
:type speed_bin_labels: list, range or numpy.array
:param percentile: The percentile representative of TI (see return for more information)
:type percentile: float, int
:param IEC_class: Default value is None, this means that default IEC class 2005 is used. For custom
class pass a DataFrame. Note: we have removed option to include IEC Class 1999 as
no longer appropriate. This may need to be placed in a separate function when
updated IEC standard is released
:type IEC_class: None or pandas.DataFrame
:param return_data: Set to True if you want the data returned.
:type return_data: bool
:return: Return plot of TI distribution by speed bins and table with TI distribution values
for statistics below when return_data is True:
* Mean_TI (average TI for a speed bin),
* TI_Count (number of data points in the bin),
* Rep_TI (representative TI set at 90 percentile by default,
* TI_2Sigma (2 sigma TI),
* Char_TI (characteristic TI)
:rtype: matplotlib.figure.Figure, pandas.DataFrame
**Example usage**
::
import brightwind as bw
data = bw.load_csv(bw.demo_datasets.demo_data)
# Plot TI distribution by speed bins using default inputs
fig_ti_dist = bw.TI.by_speed(data[['Spd80mN']], data[['Spd80mNStd']])
display(fig_ti_dist)
# Plot TI distribution by speed bins giving as input speed_bin_array and speed_bin_labels and return TI
# distribution table
fig_ti_dist, ti_dist = bw.TI.by_speed(data.Spd80mN, data.Spd80mNStd, speed_bin_array=[1,3,6,9,15],
speed_bin_labels=[1.5,4.5,7.5,12], return_data=True)
display(fig_ti_dist)
display(ti_dist)
# Plot TI distribution by speed bins considering a 60 percentile representative of TI and return TI
# distribution table
fig_ti_dist, ti_dist = bw.TI.by_speed(data.Spd80mN, data.Spd80mNStd, percentile=60, return_data=True)
display(fig_ti_dist)
display(ti_dist)
# Plot TI distribution by speed bins and give as input custom IEC_class pandas.DataFrame
IEC_class = pd.DataFrame({'windspeed': list(range(0,26)),
'IEC Class A': list(0.16 * (0.75 + (5.6 / np.array(range(0,26)))))}
).replace(np.inf, 0)
bw.TI.by_speed(data.Spd80mN, data.Spd80mNStd, IEC_class=IEC_class)
"""

if not (len(speed_bin_array) - 1) == len(speed_bin_labels):
raise ValueError('The length of the input `speed_bin_labels` array must be equal to '
'len(`speed_bin_array`) - 1. Speed bin labels correspond with the central '
'bins of each adjacent element of the input `speed_bin_array`.')

wspd = _convert_df_to_series(wspd)
wspd_std = _convert_df_to_series(wspd_std)
ti = pd.concat([wspd.rename('wspd'), wspd_std.rename('wspd_std')], axis=1, join='inner')
Expand Down Expand Up @@ -1119,31 +1165,34 @@ def by_sector(wspd, wspd_std, wdir, min_speed=0, sectors=12, direction_bin_array
direction_bin_labels=None, return_data=False):
"""
Accepts a wind speed series, its standard deviation and a direction series, calculates turbulence intensity (TI)
and returns the distribution by of TI by sector
:param wspd: Wind speed data series
:type wspd: pandas.Series
:param wspd_std: Wind speed standard deviation data series
:type wspd_std: pandas.Series
:param wdir: Wind direction series
:type wdir: pandas.Series
:param min_speed: Set the minimum wind speed.
:type min_speed: float
:param sectors: Set the number of direction sectors. Usually 12, 16, 24, 36 or 72.
:type sectors: int
:param direction_bin_array: (Optional) Array of wind speeds where adjacent elements of array form a bin
:param direction_bin_array: (Optional) To change default behaviour of first sector centered at 0 assign an
array of bins to this
:param direction_bin_labels: (Optional) you can specify an array of labels to be used for the bins. uses string
labels of the format '30-90' by default
:param return_data: Set to True if you want the data returned.
:type return_data: bool
:return: TI distribution with columns names as:
* Mean_TI (average TI for a speed bin),
* TI_Count ( number of data points in the bin)
:rtype: pandas.DataFrame
and returns the distribution of TI by sector
:param wspd: Wind speed data series
:type wspd: pandas.Series
:param wspd_std: Wind speed standard deviation data series
:type wspd_std: pandas.Series
:param wdir: Wind direction series
:type wdir: pandas.Series
:param min_speed: Set the minimum wind speed.
:type min_speed: float
:param sectors: Set the number of direction sectors. Usually 12, 16, 24, 36 or 72.
:type sectors: int
:param direction_bin_array: (Optional) Array of wind speeds where adjacent elements of array form a bin.
To change default behaviour of first sector centered at 0 assign an
array of bins to this
:type direction_bin_array: numpy.array or list
:param direction_bin_labels: (Optional) you can specify an array of labels to be used for the bins.
Default is using string labels of format '30-90'
:type direction_bin_labels: numpy.array or list
:param return_data: Set to True if you want the data returned.
:type return_data: bool
:return: Return plot of TI distribution by sector and table with TI distribution values
for statistics below when return_data is True:
* Mean_TI (average TI for a speed bin),
* TI_Count ( number of data points in the bin)
:rtype: matplotlib.figure.Figure, pandas.DataFrame
"""
ti = pd.concat([wspd.rename('wspd'), wspd_std.rename('wspd_std'), wdir.rename('wdir')], axis=1,
Expand All @@ -1160,8 +1209,8 @@ def by_sector(wspd, wspd_std, wdir, min_speed=0, sectors=12, direction_bin_array
direction_series=ti['wdir'],
sectors=sectors, direction_bin_array=direction_bin_array,
direction_bin_labels=direction_bin_labels,
aggregation_method='count', return_data=True)[-1].rename("TI_Count")],
axis=1, join='outer')
aggregation_method='count', return_data=True)[-1].rename("TI_Count")
], axis=1, join='outer')

ti_dist.index.rename('Direction Bin', inplace=True)
if return_data:
Expand Down Expand Up @@ -1209,9 +1258,10 @@ def sector_ratio(wspd_1, wspd_2, wdir, sectors=72, min_wspd=3, direction_bin_arr
:param sectors: Set the number of direction sectors. Usually 12, 16, 24, 36 or 72.
:type sectors: int
:param min_wspd: Minimum wind speed to be used.
:type: min_wpd: float
:type min_wpd: float
:param direction_bin_array: (Optional) Array of numbers where adjacent elements of array form a bin. This
overwrites the sectors.
:type direction_bin_array: numpy.array or list
:param boom_dir_1: Boom orientation in degrees of wspd_1. If top mounted leave default as -1. One or more
boom orientations can be accepted. If multiple orientations, number of orientations must
equal number of wspd_1 timeseries.
Expand Down
42 changes: 34 additions & 8 deletions brightwind/analyse/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,19 +1399,45 @@ def plot_rose_with_gradient(freq_table, percent_symbol=True, plot_bins=None, plo
def plot_TI_by_speed(wspd, wspd_std, ti, IEC_class=None):
"""
Plot turbulence intensity graphs alongside with IEC standards
:param wspd:
:param wspd_std:
:param ti: DataFrame returned from TI.by_speed() in analyse
:param IEC_class: By default IEC class 2005 is used for custom class pass a DataFrame. Note we have removed
option to include IEC Class 1999 as no longer appropriate.
This may need to be placed in a separate function when updated IEC standard is released
:return: Plots turbulence intensity distribution by wind speed
:param wspd: Wind speed data series
:type wspd: pandas.Series
:param wspd_std: Wind speed standard deviation data series
:type wspd_std: pandas.Series
:param ti: DataFrame returned from bw.TI.by_speed()
:type ti: pandas.DataFrame
:param IEC_class: Default value is None, this means that default IEC class 2005 is used. For custom class pass
a DataFrame. Note: we have removed option to include IEC Class 1999 as no longer appropriate.
This may need to be placed in a separate function when updated IEC standard is released
:type IEC_class: None or pandas.DataFrame
:return: Plots scatter plot of turbulence intensity (TI) & distribution of TI by speed bins
derived as for statistics below and the IEC Class curves defined as for IEC_class input.
* Mean_TI (average TI for a speed bin),
* Rep_TI (representative TI set at a certain percentile and derived from bw.TI.by_speed())
**Example usage**
::
import brightwind as bw
data = bw.load_csv(bw.demo_datasets.demo_data)
# Plots scatter plot of turbulence intensity (TI) and distribution of TI by speed bins and
# IEC Class curves
_ , ti_dist = bw.TI.by_speed(data.Spd80mN, data.Spd80mNStd, return_data=True)
bw.analyse.plot.plot_TI_by_speed(data.Spd80mN, data.Spd80mNStd, ti_dist, IEC_class=None)
# Plot TI distribution by speed bins and give as input custom IEC_class pandas.DataFrame
IEC_class = pd.DataFrame({'windspeed': list(range(0,26)),
'IEC Class A': list(0.16 * (0.75 + (5.6 / np.array(range(0,26)))))}
).replace(np.inf, 0)
bw.analyse.plot.plot_TI_by_speed(data.Spd80mN, data.Spd80mNStd, ti_dist, IEC_class=IEC_class)
"""

# IEC Class 2005

if IEC_class is None:
IEC_class = pd.DataFrame(np.zeros([26, 4]), columns=['Windspeed', 'IEC Class A', 'IEC Class B', 'IEC Class C'])
IEC_class = pd.DataFrame(np.zeros([26, 4]), columns=['windspeed', 'IEC Class A', 'IEC Class B', 'IEC Class C'])
for n in range(1, 26):
IEC_class.iloc[n, 0] = n
IEC_class.iloc[n, 1] = 0.16 * (0.75 + (5.6 / n))
Expand Down

0 comments on commit b5739a6

Please sign in to comment.