From 5a10c35d9baf1e49173ee906b13954ccc89eee73 Mon Sep 17 00:00:00 2001 From: Jaswinder Singh Date: Sun, 31 Oct 2021 11:56:25 +0530 Subject: [PATCH] black formatting --- .../_AbsolutePriceOscillator.py | 3 +- .../_AverageDirectionalMovementIndex.py | 26 ++++----- .../indicator_features/_AverageTrueRange.py | 24 ++++---- .../_ElasticSeriesWeightedAverage.py | 16 +++--- .../indicator_features/_KeltnerChannel.py | 1 + .../_MovingAverageConvergenceDivergence.py | 7 ++- .../_PercentageValueOscillator.py | 7 ++- .../_RelativeStrengthIndex.py | 57 +++++++++++-------- .../_SeriesWeightedAverage.py | 26 +++++++-- .../_SeriesWeightedMovingFeature.py | 7 +-- .../indicator_features/_TypicalValue.py | 14 ++--- .../_ZeroLagExponentialMovingFeature.py | 4 +- .../moving_average_features.py | 40 ++++++------- .../weighted_window_features.py | 49 ++++++++-------- 14 files changed, 150 insertions(+), 131 deletions(-) diff --git a/NitroFE/time_based_features/indicator_features/_AbsolutePriceOscillator.py b/NitroFE/time_based_features/indicator_features/_AbsolutePriceOscillator.py index bbe9823f..6cf7a00a 100644 --- a/NitroFE/time_based_features/indicator_features/_AbsolutePriceOscillator.py +++ b/NitroFE/time_based_features/indicator_features/_AbsolutePriceOscillator.py @@ -9,9 +9,10 @@ class AbsolutePriceOscillator: - """ + """ Provided dataframe must be in ascending order. """ + def __init__( self, fast_period: int = 5, diff --git a/NitroFE/time_based_features/indicator_features/_AverageDirectionalMovementIndex.py b/NitroFE/time_based_features/indicator_features/_AverageDirectionalMovementIndex.py index 99b19b0b..86c66b34 100644 --- a/NitroFE/time_based_features/indicator_features/_AverageDirectionalMovementIndex.py +++ b/NitroFE/time_based_features/indicator_features/_AverageDirectionalMovementIndex.py @@ -19,8 +19,9 @@ class AverageDirectionalMovementIndex: - def __init__(self, - directional_movement_lookback_period: int = 4, + def __init__( + self, + directional_movement_lookback_period: int = 4, directional_movement_min_periods: int = None, directional_movement_smoothing_period: int = 14, directional_movement_smoothing_min_periods: int = 0, @@ -29,7 +30,8 @@ def __init__(self, true_range_lookback: int = 4, average_true_range_span: int = 6, true_range_min_periods: int = None, - average_true_range_periods: int = 1): + average_true_range_periods: int = 1, + ): """ Parameters ---------- @@ -54,9 +56,7 @@ def __init__(self, average_true_range_periods : int, optional Minimum number of observations in window required to have a value for average true range calculation , by default 1 """ - self.directional_movement_lookback_period = ( - directional_movement_lookback_period - ) + self.directional_movement_lookback_period = directional_movement_lookback_period self.directional_movement_min_periods = directional_movement_min_periods self.directional_movement_smoothing_period = ( directional_movement_smoothing_period @@ -78,13 +78,13 @@ def __init__(self, self.average_true_range_periods = average_true_range_periods def _plus_dm(self, x, look_back_period): - look_back_period=int(look_back_period/2) + look_back_period = int(look_back_period / 2) return np.max(x.iloc[look_back_period:]) - np.max( x.iloc[0 : look_back_period - 1] ) def _minus_dm(self, x, look_back_period): - look_back_period=int(look_back_period/2) + look_back_period = int(look_back_period / 2) return np.min(x.iloc[0 : look_back_period - 1]) - np.min( x.iloc[look_back_period:] ) @@ -136,8 +136,6 @@ def fit( operation="mean", ) - - plus_dma = self._plus_dma_object._template_feature_calculation( function_name="plus_dma", win_function=_identity_window, @@ -191,7 +189,11 @@ def fit( dataframe=dataframe, first_fit=first_fit, ) - average_true_range=average_true_range.to_frame() if isinstance(average_true_range,pd.Series) else average_true_range + average_true_range = ( + average_true_range.to_frame() + if isinstance(average_true_range, pd.Series) + else average_true_range + ) plus_directional_index = 100 * (smoothed_plus_dma.div(average_true_range)) minus_directional_index = 100 * (smoothed_minus_dma.div(average_true_range)) @@ -206,5 +208,3 @@ def fit( first_fit=first_fit, ) return adx - - diff --git a/NitroFE/time_based_features/indicator_features/_AverageTrueRange.py b/NitroFE/time_based_features/indicator_features/_AverageTrueRange.py index a88a4210..46590bce 100644 --- a/NitroFE/time_based_features/indicator_features/_AverageTrueRange.py +++ b/NitroFE/time_based_features/indicator_features/_AverageTrueRange.py @@ -2,18 +2,24 @@ import pandas as pd from typing import Union, Callable -from NitroFE.time_based_features.weighted_window_features.weighted_windows import _equal_window, _identity_window +from NitroFE.time_based_features.weighted_window_features.weighted_windows import ( + _equal_window, + _identity_window, +) from NitroFE.time_based_features.weighted_window_features.weighted_window_features import ( weighted_window_features, ) + class AverageTrueRange: - def __init__(self, + def __init__( + self, true_range_lookback: int = 4, average_true_range_span: int = 6, true_range_min_periods: int = None, average_true_range_periods: int = 1, - return_true_range:bool=False): + return_true_range: bool = False, + ): """ Parameters ---------- @@ -28,13 +34,13 @@ def __init__(self, return_true_range : bool, optional If true, True range is returned instead of Average True range """ - + self.true_range_lookback = true_range_lookback self.average_true_range_span = average_true_range_span self.true_range_min_periods = true_range_min_periods self.average_true_range_periods = average_true_range_periods - self.return_true_range=return_true_range + self.return_true_range = return_true_range def true_range(self, x): return np.max( @@ -45,11 +51,7 @@ def true_range(self, x): ] ) - def fit( - self, - dataframe: Union[pd.DataFrame, pd.Series], - first_fit: bool = True - ): + def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): """ For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False @@ -96,4 +98,4 @@ def fit( operation_args=(), ) - return average_true_range \ No newline at end of file + return average_true_range diff --git a/NitroFE/time_based_features/indicator_features/_ElasticSeriesWeightedAverage.py b/NitroFE/time_based_features/indicator_features/_ElasticSeriesWeightedAverage.py index 40ae9f22..5b047eaa 100644 --- a/NitroFE/time_based_features/indicator_features/_ElasticSeriesWeightedAverage.py +++ b/NitroFE/time_based_features/indicator_features/_ElasticSeriesWeightedAverage.py @@ -11,8 +11,7 @@ class ElasticSeriesWeightedAverage: - def __init__(self, - weight_sum_lookback: int = 4): + def __init__(self, weight_sum_lookback: int = 4): """ Parameters ---------- @@ -25,7 +24,8 @@ def fit( self, dataframe: Union[pd.DataFrame, pd.Series], dataframe_for_weight: Union[pd.DataFrame, pd.Series], - first_fit: bool = True): + first_fit: bool = True, + ): """ elastic_series_weighted_average This feature is an implementation of elastic volume weighted moving average @@ -55,7 +55,6 @@ def fit( if first_fit: self._object = weighted_window_features() - rolling_sum = self._object._template_feature_calculation( function_name="_lag_object", win_function=_identity_window, @@ -70,7 +69,7 @@ def fit( if isinstance(dataframe, pd.Series): dataframe = dataframe.to_frame() - + eswa = pd.DataFrame(np.zeros(dataframe.shape), columns=dataframe.columns) print(dataframe.shape) print(eswa) @@ -95,8 +94,7 @@ def fit( + ((r1[1] - previous_eswa) * (r3[1] / r4[1])).fillna(0) ).values[0] - - res=eswa.iloc[1:][ll] - + res = eswa.iloc[1:][ll] + self.values_from_last_run = eswa.iloc[-1:] - return res \ No newline at end of file + return res diff --git a/NitroFE/time_based_features/indicator_features/_KeltnerChannel.py b/NitroFE/time_based_features/indicator_features/_KeltnerChannel.py index 916d17fb..20581b50 100644 --- a/NitroFE/time_based_features/indicator_features/_KeltnerChannel.py +++ b/NitroFE/time_based_features/indicator_features/_KeltnerChannel.py @@ -15,6 +15,7 @@ ExponentialMovingFeature, ) + class KeltnerChannel: def __init__( self, diff --git a/NitroFE/time_based_features/indicator_features/_MovingAverageConvergenceDivergence.py b/NitroFE/time_based_features/indicator_features/_MovingAverageConvergenceDivergence.py index 5fc2f7fa..ddece017 100644 --- a/NitroFE/time_based_features/indicator_features/_MovingAverageConvergenceDivergence.py +++ b/NitroFE/time_based_features/indicator_features/_MovingAverageConvergenceDivergence.py @@ -12,9 +12,10 @@ class MovingAverageConvergenceDivergence: - """ + """ Provided dataframe must be in ascending order. """ + def __init__( self, fast_period: int = 26, @@ -32,7 +33,7 @@ def __init__( return_histogram=False, ): """ - + Parameters ---------- fast_period : int, optional @@ -86,7 +87,7 @@ def fit( For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False Returns --> Smoothed signal line , macd histogram - + Parameters ---------- dataframe : Union[pd.DataFrame, pd.Series] diff --git a/NitroFE/time_based_features/indicator_features/_PercentageValueOscillator.py b/NitroFE/time_based_features/indicator_features/_PercentageValueOscillator.py index 480bc58e..dbff1dc7 100644 --- a/NitroFE/time_based_features/indicator_features/_PercentageValueOscillator.py +++ b/NitroFE/time_based_features/indicator_features/_PercentageValueOscillator.py @@ -9,14 +9,15 @@ class PercentageValueOscillator: - """ + """ Provided dataframe must be in ascending order. """ + def __init__( self, fast_period: int = 4, slow_period: int = 8, - smoothing_period:int = 9, + smoothing_period: int = 9, fast_operation: str = "mean", slow_operation: str = "mean", initialize_using_operation: bool = False, @@ -52,7 +53,7 @@ def __init__( """ self.span_fast = fast_period self.span_slow = slow_period - self.span_smoothing=smoothing_period + self.span_smoothing = smoothing_period self.min_periods = min_periods diff --git a/NitroFE/time_based_features/indicator_features/_RelativeStrengthIndex.py b/NitroFE/time_based_features/indicator_features/_RelativeStrengthIndex.py index 85e5a3d2..607efb6c 100644 --- a/NitroFE/time_based_features/indicator_features/_RelativeStrengthIndex.py +++ b/NitroFE/time_based_features/indicator_features/_RelativeStrengthIndex.py @@ -1,5 +1,3 @@ - - import numpy as np import pandas as pd from typing import Union @@ -10,13 +8,18 @@ _equal_window, _identity_window, ) -from NitroFE.time_based_features.moving_average_features.moving_average_features import ExponentialMovingFeature,HullMovingFeature,\ - KaufmanAdaptiveMovingAverage,FractalAdaptiveMovingAverage,TripleExponentialMovingFeature,SmoothedMovingAverage +from NitroFE.time_based_features.moving_average_features.moving_average_features import ( + ExponentialMovingFeature, + HullMovingFeature, + KaufmanAdaptiveMovingAverage, + FractalAdaptiveMovingAverage, + TripleExponentialMovingFeature, + SmoothedMovingAverage, +) class RelativeStrengthIndex: - def __init__(self, - lookback_period:int=8): + def __init__(self, lookback_period: int = 8): """ Parameters ---------- @@ -25,19 +28,16 @@ def __init__(self, """ self.lookback_period = lookback_period - def _diff_pos(self,x): - diff_val=x.iloc[-1]-x.iloc[-2] - return diff_val if diff_val>0 else 0 + def _diff_pos(self, x): + diff_val = x.iloc[-1] - x.iloc[-2] + return diff_val if diff_val > 0 else 0 - def _diff_neg(self,x): - diff_val=x.iloc[-1]-x.iloc[-2] - res=diff_val if diff_val<0 else 0 + def _diff_neg(self, x): + diff_val = x.iloc[-1] - x.iloc[-2] + res = diff_val if diff_val < 0 else 0 return -res - def fit( - self, - dataframe: Union[pd.DataFrame, pd.Series], - first_fit: bool = True): + def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): """ Provided dataframe must be in ascending order. @@ -59,10 +59,13 @@ def fit( self._up_object = weighted_window_features() self._down_object = weighted_window_features() - self._up_smoothed=SmoothedMovingAverage(lookback_period=self.lookback_period) - self._down_smoothed=SmoothedMovingAverage(lookback_period=self.lookback_period) + self._up_smoothed = SmoothedMovingAverage( + lookback_period=self.lookback_period + ) + self._down_smoothed = SmoothedMovingAverage( + lookback_period=self.lookback_period + ) - if isinstance(dataframe, pd.Series): dataframe = dataframe.to_frame() @@ -71,7 +74,7 @@ def fit( win_function=_identity_window, first_fit=first_fit, dataframe=dataframe, - window=2 , + window=2, min_periods=None, symmetric=None, operation=self._diff_pos, @@ -83,15 +86,19 @@ def fit( win_function=_identity_window, first_fit=first_fit, dataframe=dataframe, - window=2 , + window=2, min_periods=None, symmetric=None, operation=self._diff_neg, operation_args=(), ) - smoothed_up_value=self._up_smoothed.fit(dataframe=up_value,first_fit=first_fit) - smoothed_down_value=self._down_smoothed.fit(dataframe=down_value,first_fit=first_fit) + smoothed_up_value = self._up_smoothed.fit( + dataframe=up_value, first_fit=first_fit + ) + smoothed_down_value = self._down_smoothed.fit( + dataframe=down_value, first_fit=first_fit + ) - rsi = 100 -100/(1+(smoothed_up_value/smoothed_down_value)) - return rsi \ No newline at end of file + rsi = 100 - 100 / (1 + (smoothed_up_value / smoothed_down_value)) + return rsi diff --git a/NitroFE/time_based_features/indicator_features/_SeriesWeightedAverage.py b/NitroFE/time_based_features/indicator_features/_SeriesWeightedAverage.py index a333495d..317e0678 100644 --- a/NitroFE/time_based_features/indicator_features/_SeriesWeightedAverage.py +++ b/NitroFE/time_based_features/indicator_features/_SeriesWeightedAverage.py @@ -8,8 +8,14 @@ _equal_window, _identity_window, ) -from NitroFE.time_based_features.moving_average_features.moving_average_features import ExponentialMovingFeature,HullMovingFeature,\ - KaufmanAdaptiveMovingAverage,FractalAdaptiveMovingAverage,TripleExponentialMovingFeature,SmoothedMovingAverage +from NitroFE.time_based_features.moving_average_features.moving_average_features import ( + ExponentialMovingFeature, + HullMovingFeature, + KaufmanAdaptiveMovingAverage, + FractalAdaptiveMovingAverage, + TripleExponentialMovingFeature, + SmoothedMovingAverage, +) class SeriesWeightedAverage: @@ -43,12 +49,18 @@ def fit( dataframe_for_weight = dataframe_for_weight.to_frame() multiplication_res = ( - pd.DataFrame(np.multiply(dataframe.values,dataframe_for_weight.values),columns=dataframe.columns) + pd.DataFrame( + np.multiply(dataframe.values, dataframe_for_weight.values), + columns=dataframe.columns, + ) if first_fit else pd.concat( [ self.multiplication_values_from_last_run, - pd.DataFrame(np.multiply(dataframe.values,dataframe_for_weight.values),columns=dataframe.columns), + pd.DataFrame( + np.multiply(dataframe.values, dataframe_for_weight.values), + columns=dataframe.columns, + ), ] ) ) @@ -75,7 +87,9 @@ def fit( dataframe_for_weight.iloc[1:] if (not first_fit) else dataframe_for_weight ) - res = pd.DataFrame(cumilative_multiplication_res.values/(cumilative_res.values)) + res = pd.DataFrame( + cumilative_multiplication_res.values / (cumilative_res.values) + ) self.values_from_last_run = cumilative_res.iloc[-1:] - return res \ No newline at end of file + return res diff --git a/NitroFE/time_based_features/indicator_features/_SeriesWeightedMovingFeature.py b/NitroFE/time_based_features/indicator_features/_SeriesWeightedMovingFeature.py index 80a22032..af8e3e28 100644 --- a/NitroFE/time_based_features/indicator_features/_SeriesWeightedMovingFeature.py +++ b/NitroFE/time_based_features/indicator_features/_SeriesWeightedMovingFeature.py @@ -32,16 +32,15 @@ def __init__( """ self.lookback_period = lookback_period self.min_periods = min_periods - self.operation = np.mean if operation==None else operation + self.operation = np.mean if operation == None else operation self.operation_args = operation_args - - def fit( self, dataframe: Union[pd.DataFrame, pd.Series], dataframe_for_weight: Union[pd.DataFrame, pd.Series], - first_fit: bool = True): + first_fit: bool = True, + ): """ For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False diff --git a/NitroFE/time_based_features/indicator_features/_TypicalValue.py b/NitroFE/time_based_features/indicator_features/_TypicalValue.py index 18ccad29..07b4564b 100644 --- a/NitroFE/time_based_features/indicator_features/_TypicalValue.py +++ b/NitroFE/time_based_features/indicator_features/_TypicalValue.py @@ -1,4 +1,3 @@ - import numpy as np import pandas as pd from typing import Union, Callable @@ -10,10 +9,9 @@ _identity_window, ) + class TypicalValue: - def __init__(self, - lookback_period: int = 6, - min_periods: int = None): + def __init__(self, lookback_period: int = 6, min_periods: int = None): """ Parameters ---------- @@ -28,10 +26,7 @@ def __init__(self, def _calculate_typical_value(self, x): return (np.max(x) + np.min(x) + x.iloc[-1:]) / 3 - def fit( - self, - dataframe: Union[pd.DataFrame, pd.Series], - first_fit: bool = True): + def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): """ For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False @@ -50,7 +45,6 @@ def fit( if first_fit: self._typical_value_object = weighted_window_features() - _typical_value = self._typical_value_object._template_feature_calculation( function_name="typical_value", win_function=_identity_window, @@ -63,4 +57,4 @@ def fit( operation_args=(), ) - return _typical_value \ No newline at end of file + return _typical_value diff --git a/NitroFE/time_based_features/indicator_features/_ZeroLagExponentialMovingFeature.py b/NitroFE/time_based_features/indicator_features/_ZeroLagExponentialMovingFeature.py index 03e8e67f..d6a9be47 100644 --- a/NitroFE/time_based_features/indicator_features/_ZeroLagExponentialMovingFeature.py +++ b/NitroFE/time_based_features/indicator_features/_ZeroLagExponentialMovingFeature.py @@ -21,9 +21,9 @@ def __init__( operation: str = "mean", initialize_using_operation: bool = False, initialize_span: int = None, - com: float = None, + com: float = None, span: float = None, - halflife: float = None, + halflife: float = None, min_periods: int = 0, ignore_na: bool = False, axis: int = 0, diff --git a/NitroFE/time_based_features/moving_average_features/moving_average_features.py b/NitroFE/time_based_features/moving_average_features/moving_average_features.py index 7c29208d..f187f30e 100644 --- a/NitroFE/time_based_features/moving_average_features/moving_average_features.py +++ b/NitroFE/time_based_features/moving_average_features/moving_average_features.py @@ -61,7 +61,7 @@ def __init__( self.span = span self.halflife = halflife self.alpha = alpha - self.min_periods = min_periods if min_periods!=None else 0 + self.min_periods = min_periods if min_periods != None else 0 self.adjust = False self.ignore_na = ignore_na self.axis = axis @@ -72,7 +72,7 @@ def __init__( self.initialize_span = initialize_span def _perform_temp_operation(self, x): - + if self.operation == "mean": _return = x.mean() elif self.operation == "var": @@ -81,7 +81,7 @@ def _perform_temp_operation(self, x): _return = x.std() else: raise ValueError(f"Operation {self.operation} not supported") - + return _return def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): @@ -150,6 +150,7 @@ def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True) self.last_values_from_previous_run = _return.iloc[-1:] return _return + class HullMovingFeature: """ Provided dataframe must be in ascending order. @@ -309,7 +310,6 @@ def __init__( self.fast_ema_span = fast_ema_span self.slow_ema_span = slow_ema_span - def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): """ For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False @@ -338,16 +338,16 @@ def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True) kma = pd.concat( [pd.DataFrame(np.zeros((1, kma.shape[1])), columns=kma.columns), kma] ) - if self.kaufman_efficiency_min_periods==None: - _first_pervious=(self.kaufman_efficiency_lookback_period-2) - elif self.kaufman_efficiency_min_periods>1: - _first_pervious=(self.kaufman_efficiency_min_periods-2) + if self.kaufman_efficiency_min_periods == None: + _first_pervious = self.kaufman_efficiency_lookback_period - 2 + elif self.kaufman_efficiency_min_periods > 1: + _first_pervious = self.kaufman_efficiency_min_periods - 2 else: - _first_pervious=0 + _first_pervious = 0 else: kma = pd.concat([self.values_from_last_run, kma]) - _first_pervious=-1 + _first_pervious = -1 kma["_iloc"] = np.arange(len(kma)) _kaufman_efficiency = self._kaufman_object.fit( @@ -362,12 +362,16 @@ def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True) _kaufman_efficiency.copy() * (2 / (self.fast_ema_span + 1) - 2 / (self.slow_ema_span + 1)) + 2 / (self.slow_ema_span + 1) - )** 2 + ) ** 2 if first_fit: - SC.iloc[_first_pervious]=[0]*SC.shape[1] - - for r1, r2, r3 in zip(dataframe[(_first_pervious+1):].iterrows(), kma[(1+_first_pervious+1):].iterrows(), SC[(_first_pervious+1):].iterrows()): + SC.iloc[_first_pervious] = [0] * SC.shape[1] + + for r1, r2, r3 in zip( + dataframe[(_first_pervious + 1) :].iterrows(), + kma[(1 + _first_pervious + 1) :].iterrows(), + SC[(_first_pervious + 1) :].iterrows(), + ): previous_kama = kma[kma["_iloc"] == (r2[1]["_iloc"] - 1)][ll] @@ -376,9 +380,8 @@ def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True) + np.multiply(r3[1].values, (r1[1].values - previous_kama.values)) ).values[0] - res = kma.iloc[1:][ll] - + self.values_from_last_run = res.iloc[-1:] return res @@ -565,7 +568,6 @@ def __init__( self.initialize_using_operation = initialize_using_operation self.initialize_span = initialize_span - def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True): """ @@ -645,8 +647,7 @@ class SmoothedMovingAverage: Provided dataframe must be in ascending order. """ - def __init__(self, - lookback_period: int = 4): + def __init__(self, lookback_period: int = 4): """ Parameters ---------- @@ -677,7 +678,6 @@ def fit( if first_fit: self._first_object = weighted_window_features() - if isinstance(dataframe, pd.Series): dataframe = dataframe.to_frame() diff --git a/NitroFE/time_based_features/weighted_window_features/weighted_window_features.py b/NitroFE/time_based_features/weighted_window_features/weighted_window_features.py index 4b3e492a..4770f7a6 100644 --- a/NitroFE/time_based_features/weighted_window_features/weighted_window_features.py +++ b/NitroFE/time_based_features/weighted_window_features/weighted_window_features.py @@ -48,7 +48,7 @@ def _template_feature_calculation( symmetric: bool = False, operation: Callable = np.mean, operation_args: tuple = (), - last_values_from_calculated:bool=False, + last_values_from_calculated: bool = False, **kwargs ): _function_name = function_name @@ -64,7 +64,9 @@ def _template_feature_calculation( self.params[_function_name]["symmetric"] = symmetric self.params[_function_name]["operation"] = operation self.params[_function_name]["operation_args"] = operation_args - self.params[_function_name]["last_values_from_calculated"] = last_values_from_calculated + self.params[_function_name][ + "last_values_from_calculated" + ] = last_values_from_calculated self.first_fit_params_save(_function_name, kwargs=kwargs) @@ -100,9 +102,9 @@ def _template_feature_calculation( ) if not first_fit: _return = _return.iloc[ - self.params[_function_name]["len_last_values_from_previous_run"] :] + self.params[_function_name]["len_last_values_from_previous_run"] : + ] - if not self.params[_function_name]["last_values_from_calculated"]: _last_values_from_previous_run = ( dataframe.iloc[1 - self.params[_function_name]["window"] :] @@ -136,7 +138,7 @@ def caluclate_weighted_moving_window_feature( Create weighted moving window feature A weighted average is an average that has multiplying factors to give different weights to data at different positions in the sample window. - Mathematically, the weighted moving average is the convolution of the data with a fixed weighting function. + Mathematically, the weighted moving average is the convolution of the data with a fixed weighting function. In an n-day WMA the latest day has weight n, the second latest n-1, etc, down to one Parameters @@ -158,8 +160,8 @@ def caluclate_weighted_moving_window_feature( additional agrument values to be sent for self defined operation function """ - - operation=np.sum if operation==None else operation + + operation = np.sum if operation == None else operation _function_name = "caluclate_weighted_moving_window_feature" return self._template_feature_calculation( function_name=_function_name, @@ -208,7 +210,7 @@ def caluclate_barthann_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_barthann_feature" return self._template_feature_calculation( function_name=_function_name, @@ -255,8 +257,8 @@ def caluclate_bartlett_feature( operation to perform over the weighted rolling window values, when None is passed, np.mean is used operation_args : tuple, optional additional agrument values to be sent for operation function - """ - operation=np.mean if operation==None else operation + """ + operation = np.mean if operation == None else operation _function_name = "caluclate_bartlett_feature" return self._template_feature_calculation( function_name=_function_name, @@ -302,7 +304,7 @@ def caluclate_equal_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_equal_feature" return self._template_feature_calculation( function_name=_function_name, @@ -350,7 +352,7 @@ def caluclate_blackman_feature( operation_args : tuple, optional additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_blackman_feature" return self._template_feature_calculation( function_name=_function_name, @@ -398,7 +400,7 @@ def caluclate_blackmanharris_feature( operation_args : tuple, optional additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_blackmanharris_feature" return self._template_feature_calculation( function_name=_function_name, @@ -446,7 +448,7 @@ def caluclate_bohman_feature( operation_args : tuple, optional additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_bohman_feature" return self._template_feature_calculation( function_name=_function_name, @@ -495,7 +497,7 @@ def caluclate_cosine_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_cosine_feature" return self._template_feature_calculation( function_name=_function_name, @@ -551,7 +553,7 @@ def caluclate_exponential_feature( Parameter defining the decay. For center = 0 use tau = -(M-1) / ln(x) if x is the fraction of the window remaining at the end, by default 1 """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_exponential_feature" return self._template_feature_calculation( function_name=_function_name, @@ -602,7 +604,7 @@ def caluclate_flattop_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_flattop_feature" return self._template_feature_calculation( function_name=_function_name, @@ -654,7 +656,7 @@ def caluclate_gaussian_feature( The standard deviation, sigma. """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_gaussian_feature" return self._template_feature_calculation( function_name=_function_name, @@ -704,7 +706,7 @@ def caluclate_hamming_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_hamming_feature" return self._template_feature_calculation( function_name=_function_name, @@ -753,7 +755,7 @@ def caluclate_hann_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_hann_feature" return self._template_feature_calculation( function_name=_function_name, @@ -805,7 +807,7 @@ def caluclate_kaiser_feature( operation_args : tuple, optional additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_kaiser_feature" return self._template_feature_calculation( function_name=_function_name, @@ -855,7 +857,7 @@ def caluclate_parzen_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_parzen_feature" return self._template_feature_calculation( function_name=_function_name, @@ -904,7 +906,7 @@ def caluclate_triang_feature( additional agrument values to be sent for operation function """ - operation=np.mean if operation==None else operation + operation = np.mean if operation == None else operation _function_name = "caluclate_triang_feature" return self._template_feature_calculation( function_name=_function_name, @@ -917,4 +919,3 @@ def caluclate_triang_feature( operation=operation, operation_args=operation_args, ) -