Skip to content

Commit

Permalink
[python] added f-strings to python-package/lightgbm/dask.py (#4144)
Browse files Browse the repository at this point in the history
* added f-string to dask.py

* Update python-package/lightgbm/dask.py

Co-authored-by: James Lamb <jaylamb20@gmail.com>

* Update python-package/lightgbm/dask.py

Co-authored-by: James Lamb <jaylamb20@gmail.com>

* Updated branch

* Updated file as per specifications

* Removed warning as per specification

* update other places

* Apply suggestions from code review

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

* revert unnecessary change

* Apply suggestions from code review

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>

Co-authored-by: James Lamb <jaylamb20@gmail.com>
Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
  • Loading branch information
3 people authored May 15, 2021
1 parent c629cb0 commit 3a657c8
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions python-package/lightgbm/dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _concat(seq: List[_DaskPart]) -> _DaskPart:
elif isinstance(seq[0], ss.spmatrix):
return ss.vstack(seq, format='csr')
else:
raise TypeError('Data must be one of: numpy arrays, pandas dataframes, sparse matrices (from scipy). Got %s.' % str(type(seq[0])))
raise TypeError(f'Data must be one of: numpy arrays, pandas dataframes, sparse matrices (from scipy). Got {type(seq[0])}.')


def _train_part(
Expand Down Expand Up @@ -306,7 +306,7 @@ def _train(
'voting_parallel'
}
if params["tree_learner"] not in allowed_tree_learners:
_log_warning('Parameter tree_learner set to %s, which is not allowed. Using "data" as default' % params['tree_learner'])
_log_warning(f'Parameter tree_learner set to {params["tree_learner"]}, which is not allowed. Using "data" as default')
params['tree_learner'] = 'data'

# Some passed-in parameters can be removed:
Expand Down Expand Up @@ -413,7 +413,7 @@ def _train(
)

machines = ','.join([
'%s:%d' % (urlparse(worker_address).hostname, port)
f'{urlparse(worker_address).hostname}:{port}'
for worker_address, port
in worker_address_to_port.items()
])
Expand Down Expand Up @@ -572,7 +572,7 @@ def _predict(
drop_axis=1
)
else:
raise TypeError('Data must be either Dask Array or Dask DataFrame. Got %s.' % str(type(data)))
raise TypeError(f'Data must be either Dask Array or Dask DataFrame. Got {type(data)}.')


class _DaskLGBMModel:
Expand Down Expand Up @@ -704,12 +704,11 @@ def __init__(

_base_doc = LGBMClassifier.__init__.__doc__
_before_kwargs, _kwargs, _after_kwargs = _base_doc.partition('**kwargs')
_base_doc = (
_before_kwargs
+ 'client : dask.distributed.Client or None, optional (default=None)\n'
+ ' ' * 12 + 'Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.\n'
+ ' ' * 8 + _kwargs + _after_kwargs
)
_base_doc = f"""
{_before_kwargs}client : dask.distributed.Client or None, optional (default=None)
{' ':4}Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.
{_kwargs}{_after_kwargs}
"""

# the note on custom objective functions in LGBMModel.__init__ is not
# currently relevant for the Dask estimators
Expand Down Expand Up @@ -749,11 +748,9 @@ def fit(
+ _base_doc[_base_doc.find('verbose :'):])

# DaskLGBMClassifier support for callbacks and init_model is not tested
fit.__doc__ = (
_base_doc[:_base_doc.find('callbacks :')]
+ '**kwargs\n'
+ ' ' * 12 + 'Other parameters passed through to ``LGBMClassifier.fit()``.\n'
)
fit.__doc__ = f"""{_base_doc[:_base_doc.find('callbacks :')]}**kwargs
Other parameters passed through to ``LGBMClassifier.fit()``.
"""

def predict(self, X: _DaskMatrixLike, **kwargs: Any) -> dask_Array:
"""Docstring is inherited from the lightgbm.LGBMClassifier.predict."""
Expand Down Expand Up @@ -858,13 +855,11 @@ def __init__(

_base_doc = LGBMRegressor.__init__.__doc__
_before_kwargs, _kwargs, _after_kwargs = _base_doc.partition('**kwargs')
_base_doc = (
_before_kwargs
+ 'client : dask.distributed.Client or None, optional (default=None)\n'
+ ' ' * 12 + 'Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.\n'
+ ' ' * 8 + _kwargs + _after_kwargs
)

_base_doc = f"""
{_before_kwargs}client : dask.distributed.Client or None, optional (default=None)
{' ':4}Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.
{_kwargs}{_after_kwargs}
"""
# the note on custom objective functions in LGBMModel.__init__ is not
# currently relevant for the Dask estimators
__init__.__doc__ = _base_doc[:_base_doc.find('Note\n')]
Expand Down Expand Up @@ -903,11 +898,9 @@ def fit(
+ _base_doc[_base_doc.find('verbose :'):])

# DaskLGBMRegressor support for callbacks and init_model is not tested
fit.__doc__ = (
_base_doc[:_base_doc.find('callbacks :')]
+ '**kwargs\n'
+ ' ' * 12 + 'Other parameters passed through to ``LGBMRegressor.fit()``.\n'
)
fit.__doc__ = f"""{_base_doc[:_base_doc.find('callbacks :')]}**kwargs
Other parameters passed through to ``LGBMRegressor.fit()``.
"""

def predict(self, X: _DaskMatrixLike, **kwargs) -> dask_Array:
"""Docstring is inherited from the lightgbm.LGBMRegressor.predict."""
Expand Down Expand Up @@ -993,12 +986,11 @@ def __init__(

_base_doc = LGBMRanker.__init__.__doc__
_before_kwargs, _kwargs, _after_kwargs = _base_doc.partition('**kwargs')
_base_doc = (
_before_kwargs
+ 'client : dask.distributed.Client or None, optional (default=None)\n'
+ ' ' * 12 + 'Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.\n'
+ ' ' * 8 + _kwargs + _after_kwargs
)
_base_doc = f"""
{_before_kwargs}client : dask.distributed.Client or None, optional (default=None)
{' ':4}Dask client. If ``None``, ``distributed.default_client()`` will be used at runtime. The Dask client used by this class will not be saved if the model object is pickled.
{_kwargs}{_after_kwargs}
"""

# the note on custom objective functions in LGBMModel.__init__ is not
# currently relevant for the Dask estimators
Expand Down Expand Up @@ -1040,11 +1032,9 @@ def fit(
+ _base_doc[_base_doc.find('verbose :'):])

# DaskLGBMRanker support for callbacks and init_model is not tested
fit.__doc__ = (
_base_doc[:_base_doc.find('callbacks :')]
+ '**kwargs\n'
+ ' ' * 12 + 'Other parameters passed through to ``LGBMRanker.fit()``.\n'
)
fit.__doc__ = f"""{_base_doc[:_base_doc.find('callbacks :')]}**kwargs
Other parameters passed through to ``LGBMRanker.fit()``.
"""

def predict(self, X: _DaskMatrixLike, **kwargs: Any) -> dask_Array:
"""Docstring is inherited from the lightgbm.LGBMRanker.predict."""
Expand Down

0 comments on commit 3a657c8

Please sign in to comment.