From 98a620ba2e2b370856a5edf532e33ae99881552d Mon Sep 17 00:00:00 2001 From: code_reformat <> Date: Fri, 30 Aug 2024 11:40:52 +0000 Subject: [PATCH] Automatic reformat of code Signed-off-by: code_reformat <> --- src/pyelq/meteorology.py | 20 ++++++++++---------- tests/test_meteorology.py | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/pyelq/meteorology.py b/src/pyelq/meteorology.py index a1aeb2a..093699b 100644 --- a/src/pyelq/meteorology.py +++ b/src/pyelq/meteorology.py @@ -102,28 +102,28 @@ def calculate_uv_from_wind_speed_direction(self) -> None: def calculate_wind_turbulence_horizontal(self, window: str) -> None: """Calculate the horizontal wind turbulence values from the wind direction attribute. - Wind turbulence values are calculated as the circular standard deviation defined as - sqrt(-2 log |1 / n sum_{i=1}^{n}(e^(ix_k) | ) + Wind turbulence values are calculated as the circular standard deviation defined as + sqrt(-2 log |1 / n sum_{i=1}^{n}(e^(ix_k) | ) (https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.circstd.html) - - where i is the imaginary unit, x_k - is the wind direction, n is the number of observations in the window and |.| denotes + + where i is the imaginary unit, x_k + is the wind direction, n is the number of observations in the window and |.| denotes the length of the complex value. Using Euler formula e^(ix_k) can be exressed as cos(x_k) + i sin(x_k) and the The term in the || expresses the mean of the complex values over the rolling window. Therefore, rolling mean can be applied to the sin and cos values of the wind direction. The length of the complex value is then calculated as the square root of the sum of the squares of - the sin and cos values (hypotenuse). and the wind turbulence is calculated as - sqrt(-2 log(hypotenuse)). + the sin and cos values (hypotenuse). and the wind turbulence is calculated as + sqrt(-2 log(hypotenuse)). This calculation is equivalent to using the circstd function from scipy.stats as an apply function on a rolling window. However, using the rolling mean on sin and cos speeds up - the calculation by a factor of 100. + the calculation by a factor of 100. Outputted values are calculated at the center of the window and at least 3 observations are required in a window for the calculation. If the window contains less values the result will be np.nan. The result of the calculation will be stored as the wind_turbulence_horizontal attribute. - + Args: window (str): The size of the window in which values are aggregated specified as an offset alias: https://pandas.pydata.org/docs/user_guide/timeseries.html#timeseries-offset-aliases @@ -135,7 +135,7 @@ def calculate_wind_turbulence_horizontal(self, window: str) -> None: cos_data_series = np.cos(data_series) sin_rolling = sin_data_series.rolling(window=window, center=True, min_periods=3).mean() cos_rolling = cos_data_series.rolling(window=window, center=True, min_periods=3).mean() - hypotenuse = (sin_rolling ** 2 + cos_rolling ** 2) ** 0.5 + hypotenuse = (sin_rolling**2 + cos_rolling**2) ** 0.5 aggregated_data = np.sqrt(-2 * np.log(hypotenuse)) * 180 / np.pi self.wind_turbulence_horizontal = aggregated_data.values diff --git a/tests/test_meteorology.py b/tests/test_meteorology.py index 0ea9ce1..fcb2920 100644 --- a/tests/test_meteorology.py +++ b/tests/test_meteorology.py @@ -13,12 +13,12 @@ import numpy as np import pandas as pd -from scipy.stats import circstd import pytest +from pytictoc import TicToc +from scipy.stats import circstd from pyelq.coordinate_system import LLA from pyelq.meteorology import Meteorology, MeteorologyGroup -from pytictoc import TicToc @pytest.mark.parametrize( @@ -231,14 +231,14 @@ def test_calculate_wind_turbulence_horizontal(): data_series = pd.Series(data=met.wind_direction, index=met.time) time_circstd.tic() wind_turbulance_circstd = data_series.rolling(window="300s", center=True, min_periods=3).apply( - circstd, kwargs={"low": 0, "high": 360}) + circstd, kwargs={"low": 0, "high": 360} + ) time_circstd.toc() time_elapsed_circstd = time_circstd.elapsed - tolerance = 3 * np.std(met.wind_turbulence_horizontal) mean_turbulence = np.mean(met.wind_turbulence_horizontal) mean_turbilance_circstd = np.mean(wind_turbulance_circstd) - assert time_elapsed_met < 1/100 * time_elapsed_circstd + assert time_elapsed_met < 1 / 100 * time_elapsed_circstd assert (mean_turbulence - tolerance) < sigma < (mean_turbulence + tolerance) assert np.isclose(mean_turbulence, mean_turbilance_circstd)