use joblib.dump and load for trials
This commit is contained in:
parent
c4a8435e00
commit
96bb2efe69
@ -7,7 +7,6 @@ This module contains the hyperopt logic
|
|||||||
import logging
|
import logging
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
import os
|
||||||
import pickle
|
|
||||||
import sys
|
import sys
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@ -99,14 +98,14 @@ class Hyperopt(Backtesting):
|
|||||||
"""
|
"""
|
||||||
if self.trials:
|
if self.trials:
|
||||||
logger.info('Saving %d evaluations to \'%s\'', len(self.trials), self.trials_file)
|
logger.info('Saving %d evaluations to \'%s\'', len(self.trials), self.trials_file)
|
||||||
pickle.dump(self.trials, open(self.trials_file, 'wb'))
|
dump(self.trials, self.trials_file)
|
||||||
|
|
||||||
def read_trials(self) -> List:
|
def read_trials(self) -> List:
|
||||||
"""
|
"""
|
||||||
Read hyperopt trials file
|
Read hyperopt trials file
|
||||||
"""
|
"""
|
||||||
logger.info('Reading Trials from \'%s\'', self.trials_file)
|
logger.info('Reading Trials from \'%s\'', self.trials_file)
|
||||||
trials = pickle.load(open(self.trials_file, 'rb'))
|
trials = load(self.trials_file)
|
||||||
os.remove(self.trials_file)
|
os.remove(self.trials_file)
|
||||||
return trials
|
return trials
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ def create_trials(mocker) -> None:
|
|||||||
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=False)
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=False)
|
||||||
mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1)
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1)
|
||||||
mocker.patch('freqtrade.optimize.hyperopt.os.remove', return_value=True)
|
mocker.patch('freqtrade.optimize.hyperopt.os.remove', return_value=True)
|
||||||
mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
||||||
|
|
||||||
return [{'loss': 1, 'result': 'foo', 'params': {}}]
|
return [{'loss': 1, 'result': 'foo', 'params': {}}]
|
||||||
|
|
||||||
@ -139,10 +139,9 @@ def test_no_log_if_loss_does_not_improve(init_hyperopt, caplog) -> None:
|
|||||||
|
|
||||||
def test_save_trials_saves_trials(mocker, init_hyperopt, caplog) -> None:
|
def test_save_trials_saves_trials(mocker, init_hyperopt, caplog) -> None:
|
||||||
trials = create_trials(mocker)
|
trials = create_trials(mocker)
|
||||||
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
||||||
|
|
||||||
hyperopt = _HYPEROPT
|
hyperopt = _HYPEROPT
|
||||||
mocker.patch('freqtrade.optimize.hyperopt.open', return_value=hyperopt.trials_file)
|
|
||||||
_HYPEROPT.trials = trials
|
_HYPEROPT.trials = trials
|
||||||
|
|
||||||
hyperopt.save_trials()
|
hyperopt.save_trials()
|
||||||
@ -157,8 +156,7 @@ def test_save_trials_saves_trials(mocker, init_hyperopt, caplog) -> None:
|
|||||||
|
|
||||||
def test_read_trials_returns_trials_file(mocker, init_hyperopt, caplog) -> None:
|
def test_read_trials_returns_trials_file(mocker, init_hyperopt, caplog) -> None:
|
||||||
trials = create_trials(mocker)
|
trials = create_trials(mocker)
|
||||||
mock_load = mocker.patch('freqtrade.optimize.hyperopt.pickle.load', return_value=trials)
|
mock_load = mocker.patch('freqtrade.optimize.hyperopt.load', return_value=trials)
|
||||||
mock_open = mocker.patch('freqtrade.optimize.hyperopt.open', return_value=mock_load)
|
|
||||||
|
|
||||||
hyperopt = _HYPEROPT
|
hyperopt = _HYPEROPT
|
||||||
hyperopt_trial = hyperopt.read_trials()
|
hyperopt_trial = hyperopt.read_trials()
|
||||||
@ -168,7 +166,6 @@ def test_read_trials_returns_trials_file(mocker, init_hyperopt, caplog) -> None:
|
|||||||
caplog.record_tuples
|
caplog.record_tuples
|
||||||
)
|
)
|
||||||
assert hyperopt_trial == trials
|
assert hyperopt_trial == trials
|
||||||
mock_open.assert_called_once()
|
|
||||||
mock_load.assert_called_once()
|
mock_load.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user