Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e5b6cc

Browse files
committed
Add message_with_time helper
1 parent efb4aac commit 2e5b6cc

5 files changed

Lines changed: 66 additions & 56 deletions

File tree

sklearn/cross_validation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import scipy.sparse as sp
2323

2424
from .base import is_classifier, clone
25-
from .utils import indexable, check_random_state, safe_indexing
25+
from .utils import (indexable, check_random_state, safe_indexing,
26+
message_with_time)
2627
from .utils.validation import (_is_arraylike, _num_samples,
2728
column_or_1d)
2829
from .utils.multiclass import type_of_target
@@ -1700,8 +1701,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
17001701
if verbose > 2:
17011702
msg += ", score=%f" % test_score
17021703
if verbose > 1:
1703-
end_msg = "%s -%s" % (msg, logger.short_format_time(scoring_time))
1704-
print("[CV] %s %s" % ((64 - len(end_msg)) * '.', end_msg))
1704+
print(message_with_time('CV', msg, scoring_time))
17051705

17061706
ret = [train_score] if return_train_score else []
17071707
ret.extend([test_score, _num_samples(X_test), scoring_time])

sklearn/model_selection/_validation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import scipy.sparse as sp
2121

2222
from ..base import is_classifier, clone
23-
from ..utils import indexable, check_random_state, safe_indexing
23+
from ..utils import (indexable, check_random_state, safe_indexing,
24+
message_with_time)
2425
from ..utils.validation import _is_arraylike, _num_samples
2526
from ..utils.metaestimators import _safe_split
26-
from ..externals.joblib import Parallel, delayed, logger
27+
from ..externals.joblib import Parallel, delayed
2728
from ..externals.six.moves import zip
2829
from ..metrics.scorer import check_scoring, _check_multimetric_scoring
2930
from ..exceptions import FitFailedWarning
@@ -480,8 +481,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
480481
msg += ", score=%s" % test_scores
481482
if verbose > 1:
482483
total_time = score_time + fit_time
483-
end_msg = "%s, total=%s" % (msg, logger.short_format_time(total_time))
484-
print("[CV] %s %s" % ((64 - len(end_msg)) * '.', end_msg))
484+
print(message_with_time('CV', msg, total_time))
485485

486486
ret = [train_scores, test_scores] if return_train_score else [test_scores]
487487

sklearn/pipeline.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,14 @@
1919
from .externals.joblib import Parallel, delayed
2020
from .externals import six
2121
from .utils.metaestimators import if_delegate_has_method
22-
from .utils import Bunch
22+
from .utils import Bunch, message_with_time
2323
from .utils.validation import check_memory
2424

2525
from .utils.metaestimators import _BaseComposition
2626

2727
__all__ = ['Pipeline', 'FeatureUnion']
2828

2929

30-
def _pretty_print(step_info):
31-
"""Helper method to print the information about execution of a particular
32-
step of Pipeline / FeatureUnion (if verbosity is enabled). It receives a
33-
string having information about current step and prints it in such a way
34-
that its length is 70 characters.
35-
36-
Parameters
37-
----------
38-
step_info : str
39-
String of form '[ClassName] (step x of y) step_name ... time_elapsed'
40-
41-
"""
42-
name, elapsed = step_info.split('...')
43-
print('%s%s%s' % (name, '.' * (70 - len(name + elapsed)), elapsed))
44-
45-
4630
class Pipeline(_BaseComposition):
4731
"""Pipeline of transforms with a final estimator.
4832
@@ -190,11 +174,11 @@ def _validate_steps(self):
190174
% (estimator, type(estimator)))
191175

192176
def _print_final_step(self, final_step_time_elapsed, time_elapsed_so_far):
193-
_pretty_print('[Pipeline] (step %d of %d) %s ... %.5fs' %
194-
(len(self.steps), len(self.steps), self.steps[-1][0],
195-
final_step_time_elapsed))
196-
_pretty_print('[Pipeline] Total time elapsed: ... %.5fs' %
197-
time_elapsed_so_far)
177+
message = '(step %d of %d) %s' % (
178+
len(self.steps), len(self.steps), self.steps[-1][0])
179+
print(message_with_time('Pipeline', message, final_step_time_elapsed))
180+
print(message_with_time(
181+
'Pipeline', 'Total time elapsed:', time_elapsed_so_far))
198182

199183
@property
200184
def _estimator_type(self):
@@ -250,9 +234,9 @@ def _fit(self, X, y=None, **fit_params):
250234
time_elapsed_so_far += step_time_elapsed
251235
# Logging time elapsed for current step to stdout
252236
if self.verbose:
253-
_pretty_print('[Pipeline] (step %d of %d) %s ... %.5fs' %
254-
(step_idx + 1, len(self.steps), name,
255-
step_time_elapsed))
237+
message = '(step %d of %d) %s' % (
238+
step_idx + 1, len(self.steps), name)
239+
print(message_with_time('Pipeline', message, step_time_elapsed))
256240
if self._final_estimator is None:
257241
return Xt, {}, time_elapsed_so_far
258242
return Xt, fit_params_steps[self.steps[-1][0]], time_elapsed_so_far
@@ -326,10 +310,10 @@ def fit_transform(self, X, y=None, **fit_params):
326310
final_step_start_time = time.time()
327311
if last_step is None:
328312
if self.verbose:
329-
_pretty_print('[Pipeline] Step %s is NoneType ...' %
330-
self.steps[-1][0])
331-
_pretty_print('[Pipeline] Total time elapsed: ... %.5fs' %
332-
time_elapsed_so_far)
313+
message = 'Step %s is NoneType' % (self.steps[-1][0],)
314+
print(message_with_time('Pipeline', message, 0))
315+
print(message_with_time(
316+
'Pipeline', 'Total time elapsed', time_elapsed_so_far))
333317
return Xt
334318
elif hasattr(last_step, 'fit_transform'):
335319
Xt = last_step.fit_transform(Xt, y, **fit_params)
@@ -635,8 +619,8 @@ def _fit_one_transformer(transformer, X, y, verbose=False, idx=None,
635619
transformer = transformer.fit(X, y)
636620
step_time_elapsed = time.time() - step_start_time
637621
if verbose:
638-
_pretty_print('[FeatureUnion] (step %d of %d) %s ... %.5fs' %
639-
(idx + 1, total_steps, name, step_time_elapsed))
622+
message = '(step %d of %d) %s' % (idx + 1, total_steps, name)
623+
print(message_with_time('FeatureUnion', message, step_time_elapsed))
640624
return transformer
641625

642626

@@ -658,8 +642,8 @@ def _fit_transform_one(transformer, weight, X, y, verbose=False, idx=None,
658642
res = transformer.fit(X, y, **fit_params).transform(X)
659643
step_time_elapsed = time.time() - step_start_time
660644
if verbose:
661-
_pretty_print('[FeatureUnion] (step %d of %d) %s ... %.5fs' %
662-
(idx + 1, total_steps, name, step_time_elapsed))
645+
message = '(step %d of %d) %s' % (idx + 1, total_steps, name)
646+
print(message_with_time('FeatureUnion', message, step_time_elapsed))
663647
# if we have a weight for this transformer, multiply output
664648
if weight is None:
665649
return res, transformer
@@ -803,8 +787,9 @@ def fit(self, X, y=None):
803787
for idx, (name, transformer, _) in enumerate(all_transformers))
804788
time_elapsed = time.time() - start_time
805789
if self.verbose:
806-
_pretty_print(
807-
'[FeatureUnion] Total time elapsed: ... %.5fs' % time_elapsed)
790+
print(message_with_time(
791+
'FeatureUnion', 'Total time elapsed:', time_elapsed))
792+
808793
self._update_transformer_list(transformers)
809794
return self
810795

@@ -837,8 +822,8 @@ def fit_transform(self, X, y=None, **fit_params):
837822
for idx, (name, transformer, weight) in enumerate(all_transformers))
838823
time_elapsed = time.time() - start_time
839824
if self.verbose:
840-
_pretty_print(
841-
'[FeatureUnion] Total time elapsed: ... %.5fs' % time_elapsed)
825+
print(message_with_time(
826+
'FeatureUnion', 'Total time elapsed:', time_elapsed))
842827

843828
if not result:
844829
# All transformers are None

sklearn/tests/test_pipeline.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,11 @@ def check_pipeline_verbosity_fit_predict(pipe_method):
983983
# check output
984984
verbose_output.seek(0)
985985
lines = verbose_output.readlines()
986-
assert_true('[Pipeline] (step 1 of 2) transf ...' in lines[0])
987-
assert_true('[Pipeline] (step 2 of 2) clf ...' in lines[1])
988-
assert_true('[Pipeline] Total time elapsed: ' in lines[2])
989-
986+
assert_true('(step 1 of 2) transf' in lines[0])
987+
assert_true('(step 2 of 2) clf' in lines[1])
988+
assert_true('Total time elapsed' in lines[2])
989+
for line in lines:
990+
assert line.startswith('[Pipeline]')
990991

991992
def test_pipeline_fit_verbosity():
992993
pipe = Pipeline([('transf', Transf()), ('clf', FitParamT())], verbose=True)
@@ -1007,12 +1008,13 @@ def check_pipeline_verbosity_fit_transform(pipe_method, last_was_none=False):
10071008
# check output
10081009
verbose_output.seek(0)
10091010
lines = verbose_output.readlines()
1010-
assert_true('[Pipeline] (step 1 of 2) mult1 ...' in lines[0])
1011+
assert_true('(step 1 of 2) mult1' in lines[0])
1012+
assert_true(lines[0].startswith('[Pipeline]'))
10111013
if last_was_none:
1012-
assert_true('[Pipeline] Step mult2 is NoneType ...' in lines[1])
1014+
assert_true('Step mult2 is NoneType' in lines[1])
10131015
else:
1014-
assert_true('[Pipeline] (step 2 of 2) mult2 ...' in lines[1])
1015-
assert_true('[Pipeline] Total time elapsed: ' in lines[2])
1016+
assert_true('(step 2 of 2) mult2' in lines[1])
1017+
assert_true('Total time elapsed' in lines[2])
10161018

10171019

10181020
def test_pipeline_verbosity_fit_transform():
@@ -1037,9 +1039,12 @@ def check_feature_union_verbosity(feature_union_method):
10371039
# check output
10381040
verbose_output.seek(0)
10391041
lines = verbose_output.readlines()
1040-
assert_true('[FeatureUnion] (step 1 of 2) mult1 ...' in lines[0])
1041-
assert_true('[FeatureUnion] (step 2 of 2) mult2 ...' in lines[1])
1042-
assert_true('[FeatureUnion] Total time elapsed: ' in lines[2])
1042+
assert_true('(step 1 of 2) mult1' in lines[0])
1043+
assert_true('(step 2 of 2) mult2' in lines[1])
1044+
assert_true('Total time elapsed' in lines[2])
1045+
assert_true(lines[0].startswith('[FeatureUnion]'))
1046+
assert_true(lines[1].startswith('[FeatureUnion]'))
1047+
assert_true(lines[2].startswith('[FeatureUnion]'))
10431048

10441049

10451050
def test_feature_union_verbosity():

sklearn/utils/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
check_consistent_length, check_X_y, indexable,
1515
check_symmetric)
1616
from .class_weight import compute_class_weight, compute_sample_weight
17-
from ..externals.joblib import cpu_count
17+
from ..externals.joblib import cpu_count, logger
1818
from ..exceptions import DataConversionWarning
1919
from .deprecation import deprecated
2020

@@ -506,3 +506,23 @@ def indices_to_mask(indices, mask_length):
506506
mask[indices] = True
507507

508508
return mask
509+
510+
511+
def message_with_time(source, message, time_):
512+
"""Create one line message for logging purposes
513+
514+
Parameters
515+
----------
516+
source: str
517+
String indicating the source or the reference of the message
518+
519+
message: str
520+
Short message
521+
522+
time_: int
523+
Time in seconds
524+
"""
525+
start_message = '[%s]' % (source,)
526+
end_message = "%s, total=%s" % (message, logger.short_format_time(time_))
527+
dots_len = (68 - len(start_message) - len(end_message))
528+
return ("%s %s %s" % (start_message, dots_len * '.', end_message))

0 commit comments

Comments
 (0)