Minyus / pipelinex

PipelineX: Python package to build ML pipelines for experimentation with Kedro, MLflow, and more
https://pipelinex.readthedocs.io/
Other
223 stars 11 forks source link

Q: How to bind positional arguments in HatchDict? #17

Closed npow closed 1 year ago

npow commented 1 year ago

I'm trying to use functools.partial in HatchDict, but it seems to always require one argument which is not named. Is there any way to work around this?

(Pdb) config.get("metrics")
*** TypeError: type 'partial' takes at least one argument

config:

metrics:
  - =: functools.partial
    func:
      =: sklearn.metrics.roc_auc_score
    multiclass: ovr
Minyus commented 1 year ago

HI @npow ,

You can use _ key to specify positional (not named) arguments if any.

from pipelinex import HatchDict
import yaml
from pprint import pprint  # pretty-print for clearer look

params_yaml = """
metrics:
  - =: functools.partial
    _:
      =: sklearn.metrics.roc_auc_score
    multiclass: ovr
"""
parameters = yaml.safe_load(params_yaml)

metrics_dict = parameters.get("metrics")

print("### Before ###")
pprint(metrics_dict)

metrics = HatchDict(parameters).get("metrics")

print("\n### After ###")
print(metrics)
### Before ###
[{'=': 'functools.partial',
  '_': {'=': 'sklearn.metrics.roc_auc_score'},
  'multiclass': 'ovr'}]

### After ###
[functools.partial(<function roc_auc_score at 0x16bcf19d0>, multiclass='ovr')]
npow commented 1 year ago

Awesome, that did it!