Fix issue with colring enabled + styling

This commit is contained in:
Fredrik81 2020-04-09 11:42:13 +02:00
parent cdc774549e
commit 4707484a4c

View File

@ -7,7 +7,6 @@ This module contains the hyperopt logic
import locale import locale
import logging import logging
import random import random
import sys
import warnings import warnings
from math import ceil from math import ceil
from collections import OrderedDict from collections import OrderedDict
@ -18,7 +17,6 @@ from typing import Any, Dict, List, Optional
import rapidjson import rapidjson
from colorama import Fore, Style from colorama import Fore, Style
from colorama import init as colorama_init
from joblib import (Parallel, cpu_count, delayed, dump, load, from joblib import (Parallel, cpu_count, delayed, dump, load,
wrap_non_picklable_objects) wrap_non_picklable_objects)
from pandas import DataFrame, json_normalize, isna from pandas import DataFrame, json_normalize, isna
@ -268,17 +266,10 @@ class Hyperopt:
Log results if it is better than any previous evaluation Log results if it is better than any previous evaluation
""" """
is_best = results['is_best'] is_best = results['is_best']
if not self.print_all:
# Print '\n' after each 100th epoch to separate dots from the log messages.
# Otherwise output is messy on a terminal.
print('.', end='' if results['current_epoch'] % 100 != 0 else None) # type: ignore
sys.stdout.flush()
if self.print_all or is_best: if self.print_all or is_best:
if not self.print_all: print(
# Separate the results explanation string from dots self.get_result_table(
print("\n")
print(self.get_result_table(
self.config, results, self.total_epochs, self.config, results, self.total_epochs,
self.print_all, self.print_colorized, self.print_all, self.print_colorized,
self.hyperopt_table_header self.hyperopt_table_header
@ -675,9 +666,6 @@ class Hyperopt:
self.dimensions: List[Dimension] = self.hyperopt_space() self.dimensions: List[Dimension] = self.hyperopt_space()
self.opt = self.get_optimizer(self.dimensions, config_jobs) self.opt = self.get_optimizer(self.dimensions, config_jobs)
if self.print_colorized:
colorama_init(autoreset=True)
try: try:
with Parallel(n_jobs=config_jobs) as parallel: with Parallel(n_jobs=config_jobs) as parallel:
jobs = parallel._effective_n_jobs() jobs = parallel._effective_n_jobs()
@ -690,15 +678,18 @@ class Hyperopt:
' (', progressbar.Percentage(), ')] ', ' (', progressbar.Percentage(), ')] ',
progressbar.Bar(marker=progressbar.AnimatedMarker( progressbar.Bar(marker=progressbar.AnimatedMarker(
fill='', fill='',
fill_wrap='\x1b[32m{}\x1b[39m', fill_wrap=Fore.GREEN + '{}' + Fore.RESET,
marker_wrap='\x1b[31m{}\x1b[39m', marker_wrap=Style.BRIGHT + '{}' + Style.RESET_ALL,
)), )),
' [', progressbar.ETA(), ', ', progressbar.Timer(), ']', ' [', progressbar.ETA(), ', ', progressbar.Timer(), ']',
] ]
else: else:
widgets = [ widgets = [
' [Epoch ', progressbar.Counter(), ' of ', str(self.total_epochs), '] ', ' [Epoch ', progressbar.Counter(), ' of ', str(self.total_epochs),
progressbar.Bar(marker=''), ' (', progressbar.Percentage(), ')] ',
progressbar.Bar(marker=progressbar.AnimatedMarker(
fill='',
)),
' [', progressbar.ETA(), ', ', progressbar.Timer(), ']', ' [', progressbar.ETA(), ', ', progressbar.Timer(), ']',
] ]
with progressbar.ProgressBar( with progressbar.ProgressBar(