11
11
from ..language import Language # noqa: F401
12
12
13
13
14
+ def setup_table (
15
+ * , cols : List [str ], widths : List [int ], max_width : int = 13
16
+ ) -> Tuple [List [str ], List [int ], List [str ]]:
17
+ final_cols = []
18
+ final_widths = []
19
+ for col , width in zip (cols , widths ):
20
+ if len (col ) > max_width :
21
+ col = col [: max_width - 3 ] + "..." # shorten column if too long
22
+ final_cols .append (col .upper ())
23
+ final_widths .append (max (len (col ), width ))
24
+ return final_cols , final_widths , ["r" for _ in final_widths ]
25
+
26
+
14
27
@registry .loggers ("spacy.ConsoleLogger.v1" )
15
28
def console_logger (progress_bar : bool = False ):
16
29
def setup_printer (
17
30
nlp : "Language" , stdout : IO = sys .stdout , stderr : IO = sys .stderr
18
31
) -> Tuple [Callable [[Optional [Dict [str , Any ]]], None ], Callable [[], None ]]:
32
+ write = lambda text : stdout .write (f"{ text } \n " )
19
33
msg = Printer (no_print = True )
20
34
# ensure that only trainable components are logged
21
35
logged_pipes = [
@@ -26,15 +40,14 @@ def setup_printer(
26
40
eval_frequency = nlp .config ["training" ]["eval_frequency" ]
27
41
score_weights = nlp .config ["training" ]["score_weights" ]
28
42
score_cols = [col for col , value in score_weights .items () if value is not None ]
29
- score_widths = [max (len (col ), 6 ) for col in score_cols ]
30
43
loss_cols = [f"Loss { pipe } " for pipe in logged_pipes ]
31
- loss_widths = [ max ( len ( col ), 8 ) for col in loss_cols ]
32
- table_header = [ "E" , "#" ] + loss_cols + score_cols + [ "Score" ]
33
- table_header = [ col . upper () for col in table_header ]
34
- table_widths = [3 , 6 ] + loss_widths + score_widths + [6 ]
35
- table_aligns = [ "r" for _ in table_widths ]
36
- stdout . write (msg .row (table_header , widths = table_widths ) + " \n " )
37
- stdout . write (msg .row (["-" * width for width in table_widths ]) + " \n " )
44
+ spacing = 2
45
+ table_header , table_widths , table_aligns = setup_table (
46
+ cols = [ "E" , "#" ] + loss_cols + score_cols + [ "Score" ],
47
+ widths = [3 , 6 ] + [ 8 for _ in loss_cols ] + [ 6 for _ in score_cols ] + [6 ],
48
+ )
49
+ write (msg .row (table_header , widths = table_widths , spacing = spacing ) )
50
+ write (msg .row (["-" * width for width in table_widths ], spacing = spacing ) )
38
51
progress = None
39
52
40
53
def log_step (info : Optional [Dict [str , Any ]]) -> None :
@@ -70,7 +83,9 @@ def log_step(info: Optional[Dict[str, Any]]) -> None:
70
83
)
71
84
if progress is not None :
72
85
progress .close ()
73
- stdout .write (msg .row (data , widths = table_widths , aligns = table_aligns ) + "\n " )
86
+ write (
87
+ msg .row (data , widths = table_widths , aligns = table_aligns , spacing = spacing )
88
+ )
74
89
if progress_bar :
75
90
# Set disable=None, so that it disables on non-TTY
76
91
progress = tqdm .tqdm (
0 commit comments