From 1ade11f00b4cabf6fef4be71c38ff0e908136bdd Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 May 2024 19:17:18 +0200 Subject: [PATCH 1/5] chore: Fix a few freqAI mutable defaults --- freqtrade/freqai/RL/BaseEnvironment.py | 11 ++++++----- freqtrade/freqai/RL/BaseReinforcementLearningModel.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqai/RL/BaseEnvironment.py b/freqtrade/freqai/RL/BaseEnvironment.py index 5ddfdeb68..63fca6224 100644 --- a/freqtrade/freqai/RL/BaseEnvironment.py +++ b/freqtrade/freqai/RL/BaseEnvironment.py @@ -47,19 +47,20 @@ class BaseEnvironment(gym.Env): def __init__( self, - df: DataFrame = DataFrame(), - prices: DataFrame = DataFrame(), - reward_kwargs: dict = {}, + *, + df: DataFrame, + prices: DataFrame, + reward_kwargs: dict, window_size=10, starting_point=True, id: str = "baseenv-1", # noqa: A002 seed: int = 1, - config: dict = {}, + config: dict, live: bool = False, fee: float = 0.0015, can_short: bool = False, pair: str = "", - df_raw: DataFrame = DataFrame(), + df_raw: DataFrame, ): """ Initializes the training/eval environment. diff --git a/freqtrade/freqai/RL/BaseReinforcementLearningModel.py b/freqtrade/freqai/RL/BaseReinforcementLearningModel.py index 225ed3d50..05ab5be55 100644 --- a/freqtrade/freqai/RL/BaseReinforcementLearningModel.py +++ b/freqtrade/freqai/RL/BaseReinforcementLearningModel.py @@ -488,7 +488,7 @@ def make_env( seed: int, train_df: DataFrame, price: DataFrame, - env_info: Dict[str, Any] = {}, + env_info: Dict[str, Any], ) -> Callable: """ Utility function for multiprocessed env. From 6b889814ad975ab4e159823b0e0402f4ba178627 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 May 2024 19:26:14 +0200 Subject: [PATCH 2/5] chore: Fix further "mutable arguments" call --- freqtrade/freqai/torch/PyTorchModelTrainer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqai/torch/PyTorchModelTrainer.py b/freqtrade/freqai/torch/PyTorchModelTrainer.py index 54c42a284..b10de906c 100644 --- a/freqtrade/freqai/torch/PyTorchModelTrainer.py +++ b/freqtrade/freqai/torch/PyTorchModelTrainer.py @@ -25,7 +25,7 @@ class PyTorchModelTrainer(PyTorchTrainerInterface): criterion: nn.Module, device: str, data_convertor: PyTorchDataConvertor, - model_meta_data: Dict[str, Any] = {}, + model_meta_data: Optional[Dict[str, Any]] = None, window_size: int = 1, tb_logger: Any = None, **kwargs, @@ -45,6 +45,8 @@ class PyTorchModelTrainer(PyTorchTrainerInterface): :param n_epochs: The maximum number batches to use for evaluation. :param batch_size: The size of the batches to use during training. """ + if model_meta_data is None: + model_meta_data = {} self.model = model self.optimizer = optimizer self.criterion = criterion From aa81c75bef72b5261b221d0309835b066fb2d2fd Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 May 2024 19:42:03 +0200 Subject: [PATCH 3/5] chore: Further reduce mutable default usage --- freqtrade/freqai/data_kitchen.py | 8 ++++---- tests/freqai/test_freqai_datakitchen.py | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index d43f569d8..04f7322dc 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -214,7 +214,7 @@ class FreqaiDataKitchen: self, unfiltered_df: DataFrame, training_feature_list: List, - label_list: List = list(), + label_list: Optional[List] = None, training_filter: bool = True, ) -> Tuple[DataFrame, DataFrame]: """ @@ -244,7 +244,7 @@ class FreqaiDataKitchen: # we don't care about total row number (total no. datapoints) in training, we only care # about removing any row with NaNs # if labels has multiple columns (user wants to train multiple modelEs), we detect here - labels = unfiltered_df.filter(label_list, axis=1) + labels = unfiltered_df.filter(label_list or [], axis=1) drop_index_labels = pd.isnull(labels).any(axis=1) drop_index_labels = ( drop_index_labels.replace(True, 1).replace(False, 0).infer_objects(copy=False) @@ -654,8 +654,8 @@ class FreqaiDataKitchen: pair: str, tf: str, strategy: IStrategy, - corr_dataframes: dict = {}, - base_dataframes: dict = {}, + corr_dataframes: dict, + base_dataframes: dict, is_corr_pairs: bool = False, ) -> DataFrame: """ diff --git a/tests/freqai/test_freqai_datakitchen.py b/tests/freqai/test_freqai_datakitchen.py index 27efc3a66..5b7ec3ef1 100644 --- a/tests/freqai/test_freqai_datakitchen.py +++ b/tests/freqai/test_freqai_datakitchen.py @@ -151,7 +151,9 @@ def test_get_pair_data_for_features_with_prealoaded_data(mocker, freqai_conf): freqai.dd.load_all_pair_histories(timerange, freqai.dk) _, base_df = freqai.dd.get_base_and_corr_dataframes(timerange, "LTC/BTC", freqai.dk) - df = freqai.dk.get_pair_data_for_features("LTC/BTC", "5m", strategy, base_dataframes=base_df) + df = freqai.dk.get_pair_data_for_features( + "LTC/BTC", "5m", strategy, {}, base_dataframes=base_df + ) assert df is base_df["5m"] assert not df.empty @@ -171,7 +173,9 @@ def test_get_pair_data_for_features_without_preloaded_data(mocker, freqai_conf): freqai.dd.load_all_pair_histories(timerange, freqai.dk) base_df = {"5m": pd.DataFrame()} - df = freqai.dk.get_pair_data_for_features("LTC/BTC", "5m", strategy, base_dataframes=base_df) + df = freqai.dk.get_pair_data_for_features( + "LTC/BTC", "5m", strategy, {}, base_dataframes=base_df + ) assert df is not base_df["5m"] assert not df.empty From 5b3f348bbb5df530f6700f017d90430bb933baef Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 May 2024 20:01:49 +0200 Subject: [PATCH 4/5] chore: Don't use method call in function header --- freqtrade/freqai/data_kitchen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 04f7322dc..903f3c222 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -776,7 +776,7 @@ class FreqaiDataKitchen: corr_dataframes: dict = {}, base_dataframes: dict = {}, pair: str = "", - prediction_dataframe: DataFrame = pd.DataFrame(), + prediction_dataframe: Optional[DataFrame] = None, do_corr_pairs: bool = True, ) -> DataFrame: """ @@ -822,7 +822,7 @@ class FreqaiDataKitchen: if tf not in corr_dataframes[p]: corr_dataframes[p][tf] = pd.DataFrame() - if not prediction_dataframe.empty: + if prediction_dataframe is not None and not prediction_dataframe.empty: dataframe = prediction_dataframe.copy() base_dataframes[self.config["timeframe"]] = dataframe.copy() else: From 705d1e4cc02a28e05526da4e4dbe038064957c37 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 1 Sep 2024 08:34:28 +0200 Subject: [PATCH 5/5] chore: remove freqAI per-line-ignores --- pyproject.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bd36d15c6..f3d1bc5ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -168,8 +168,6 @@ max-complexity = 12 [tool.ruff.lint.per-file-ignores] "freqtrade/freqai/**/*.py" = [ "S311", # Standard pseudo-random generators are not suitable for cryptographic purposes - "B006", # Bugbear - mutable default argument - "B008", # bugbear - Do not perform function calls in argument defaults ] "tests/**/*.py" = [ "S101", # allow assert in tests