Skip to content

add .skb.get_vars()#1646

Merged
rcap107 merged 6 commits into
skrub-data:mainfrom
jeromedockes:get-vars
Oct 9, 2025
Merged

add .skb.get_vars()#1646
rcap107 merged 6 commits into
skrub-data:mainfrom
jeromedockes:get-vars

Conversation

@jeromedockes

Copy link
Copy Markdown
Member

This pr adds a function to inspect all the variables in a dataop. It can also return all the nodes that have a name (ie for which a value can be passed in the environment)

>>> import skrub

>>> a = skrub.var("a")
>>> b = skrub.var("b")
>>> c = (a + b).skb.set_name("c")
>>> d = c + c
>>> d
<BinOp: add>

Our DataOp, `d`, contains 2 variables: "a" and "b":

>>> d.skb.get_vars()
{'a': <Var 'a'>, 'b': <Var 'b'>}

Those are the keys for which we need to provide values in the environment when
evaluating `d`:

>>> d.skb.eval({"a": 10, "b": 3}) # (10 + 3) + (10 + 3) = 26
26

In addition, we set a name on the internal node `c`. It is not a variable, and
normally it is computed as `(a + b)`. But as it has a name, we can override its
output by passing a value for "c" in the environment. When we do, the
computation of `c` never happens (nor of `a` or `b`, here, because they are only
used to compute `c`) -- it is bypassed and the provided value is used instead.

>>> d.skb.eval({"c": 7}) # 7 + 7 = 14
14

If we want ``get_vars`` to also list nodes like our example ``c`` which have a
name and can be passed in the environment, we pass ``all_named_ops=True``:

>>> d.skb.get_vars(all_named_ops=True)
{'a': <Var 'a'>, 'b': <Var 'b'>, 'c': <c | BinOp: add>}

Note ``get_vars`` can be particularly useful when we have a learner (e.g. loaded
from a pickle file) and we want to check what inputs we should pass to its
methods such as ``fit`` and ``transform``:

>>> learner = d.skb.make_learner()
>>> list(learner.data_op.skb.get_vars().keys())
['a', 'b']

The output above tells us what keys the dict we pass to ``learner.fit()`` should
contain:

>>> learner.fit({'a': 2, 'b': 3})
SkrubLearner(data_op=<BinOp: add>)

@jeromedockes jeromedockes added the data_ops Something related to the skrub DataOps label Oct 2, 2025
@rcap107 rcap107 added this to the 0.7.0 milestone Oct 7, 2025
@rcap107

rcap107 commented Oct 8, 2025

Copy link
Copy Markdown
Member

This is a small but very useful change.

Could you add a small test for this @jeromedockes? Apart from that, it looks good

@jeromedockes

Copy link
Copy Markdown
Member Author

thanks @rcap107 :)

@rcap107 rcap107 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks @jeromedockes

@rcap107 rcap107 merged commit c0eb23d into skrub-data:main Oct 9, 2025
29 checks passed
@rcap107 rcap107 mentioned this pull request Oct 9, 2025
dierickxsimon pushed a commit to dierickxsimon/skrub that referenced this pull request Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data_ops Something related to the skrub DataOps

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants