Skip to content

ENH ascii visualisation for metadata routing - #31535

Draft
adrinjalali wants to merge 63 commits into
scikit-learn:mainfrom
adrinjalali:slep6/visualise-cleaned
Draft

adrinjalali wants to merge 63 commits into
scikit-learn:mainfrom
adrinjalali:slep6/visualise-cleaned

Conversation

@adrinjalali

@adrinjalali adrinjalali commented Jun 12, 2025 •

Copy link
Copy Markdown
Member

Adding visualisation for metadata routing.

Right now it gives such output:

def run_test_1():
    numeric_features = ["age", "fare"]
    numeric_transformer = Pipeline(
        steps=[
            ("imputer", SimpleImputer(strategy="median")),
            (
                "scaler",
                StandardScaler()
                .set_fit_request(sample_weight="inner_weights")
                .set_transform_request(copy=True),
            ),
        ]
    )

    categorical_features = ["embarked", "sex", "pclass"]
    categorical_transformer = Pipeline(
        steps=[
            ("encoder", OneHotEncoder(handle_unknown="ignore")),
            ("selector", SelectPercentile(chi2, percentile=50)),
        ]
    )
    preprocessor = ColumnTransformer(
        transformers=[
            ("num", numeric_transformer, numeric_features),
            ("cat", categorical_transformer, categorical_features),
        ]
    )

    # %%
    # Append classifier to preprocessing pipeline.
    # Now we have a full prediction pipeline.
    clf = Pipeline(
        steps=[
            ("preprocessor", preprocessor),
            ("classifier", LogisticRegression().set_fit_request(sample_weight=False)),
        ]
    )

    param_grid = {
        "preprocessor__num__imputer__strategy": ["mean", "median"],
        "preprocessor__cat__selector__percentile": [10, 30, 50, 70],
        "classifier__C": [0.1, 1.0, 10, 100],
    }

    scorer = get_scorer("accuracy").set_score_request(sample_weight=True)

    search_cv = RandomizedSearchCV(
        clf, param_grid, cv=GroupKFold(), scoring=scorer, random_state=0
    )

    # Get the routing information
    test = get_routing_for_object(search_cv)

    visualise_routing(test)


def run_test_2():
    est = make_pipeline(
        make_pipeline(StandardScaler().set_fit_request(sample_weight=True)),
        make_pipeline(StandardScaler().set_fit_request(sample_weight=False)),
        make_pipeline(StandardScaler()),
        make_pipeline(StandardScaler().set_fit_request(sample_weight=WARN)),
    )

    visualise_routing(get_routing_for_object(est))


def run_test_3():
    est = RandomizedSearchCV(estimator=LogisticRegression(), param_distributions={})
    visualise_routing(get_routing_for_object(est))

    est = RandomizedSearchCV(
        estimator=LogisticRegression(),
        param_distributions={},
        scoring=get_scorer("accuracy").set_score_request(sample_weight=True),
    )
    visualise_routing(get_routing_for_object(est))


if __name__ == "__main__":
    # Enable metadata routing
    set_config(enable_metadata_routing=True)
    run_test_1()
    run_test_2()
    run_test_3()
$ python sklearn/utils/tests/test_metadata_routing_visualise.py

=== METADATA ROUTING TREE ===
RandomizedSearchCV
├── estimator (Pipeline)
│   ├── preprocessor (ColumnTransformer)
│   │   ├── num (Pipeline)
│   │   │   ├── imputer (SimpleImputer)
│   │   │   └── scaler (StandardScaler)
│   │   │           ➤ copy[transform✓]
│   │   │           ➤ inner_weights→sample_weight[fit↗]
│   │   └── cat (Pipeline)
│   │       ├── encoder (OneHotEncoder)
│   │       └── selector (SelectPercentile)
│   └── classifier (LogisticRegression)
│           ➤ sample_weight[fit✗]
├── scorer (make_scorer(accuracy_score, response_method='predict'))
│       ➤ sample_weight[score✓]
└── splitter (GroupKFold)
        ➤ groups[split✓]

Parameter summary:
fit
 ├─ ✓ copy
 │   • ✓ requested:
 │       - RandomizedSearchCV/estimator/preprocessor/num/scaler.transform
 ├─ ✓ groups
 │   • ✓ requested:
 │       - RandomizedSearchCV/splitter.split
 ├─ ✓ inner_weights
 │   • ✓ requested:
 │       - RandomizedSearchCV/estimator/preprocessor/num/scaler.fit
 ├─ ✓ sample_weight
 │   • ✗ ignored:
 │       - RandomizedSearchCV/estimator/classifier.fit
 │   • ✓ requested:
 │       - RandomizedSearchCV/scorer.score

score
 ├─ ✓ sample_weight
 │   • ✓ requested:
 │       - RandomizedSearchCV/scorer.score


=== METADATA ROUTING TREE ===
Pipeline
├── pipeline-1 (Pipeline)
│   └── standardscaler (StandardScaler)
│           ➤ copy[transform⛔,inverse_transform⛔]
│           ➤ sample_weight[fit✓]
├── pipeline-2 (Pipeline)
│   └── standardscaler (StandardScaler)
│           ➤ copy[transform⛔,inverse_transform⛔]
│           ➤ sample_weight[fit✗]
├── pipeline-3 (Pipeline)
│   └── standardscaler (StandardScaler)
│           ➤ copy[transform⛔,inverse_transform⛔]
│           ➤ sample_weight[fit⛔]
└── pipeline-4 (Pipeline)
    └── standardscaler (StandardScaler)
            ➤ copy[transform⛔,inverse_transform⛔]
            ➤ sample_weight[fit⚠]

Parameter summary:
fit
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform
 ├─ ⛔ sample_weight
 │   • ⛔ errors:
 │       - Pipeline/pipeline-3/standardscaler.fit
 │   • ⚠ warns:
 │       - Pipeline/pipeline-4/standardscaler.fit
 │   • ✗ ignored:
 │       - Pipeline/pipeline-2/standardscaler.fit
 │   • ✓ requested:
 │       - Pipeline/pipeline-1/standardscaler.fit

predict
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform

predict_proba
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform

predict_log_proba
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform

decision_function
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform

score
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform

transform
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.transform
 │       - Pipeline/pipeline-2/standardscaler.transform
 │       - Pipeline/pipeline-3/standardscaler.transform
 │       - Pipeline/pipeline-4/standardscaler.transform

inverse_transform
 ├─ ⛔ copy
 │   • ⛔ errors:
 │       - Pipeline/pipeline-1/standardscaler.inverse_transform
 │       - Pipeline/pipeline-2/standardscaler.inverse_transform
 │       - Pipeline/pipeline-3/standardscaler.inverse_transform
 │       - Pipeline/pipeline-4/standardscaler.inverse_transform


=== METADATA ROUTING TREE ===
RandomizedSearchCV
├── estimator (LogisticRegression)
│       ➤ sample_weight[fit⛔]
├── scorer (LogisticRegression.score)
│       ➤ sample_weight[score⛔]
└── splitter (NoneType)

Parameter summary:
fit
 ├─ ⛔ sample_weight
 │   • ⛔ errors:
 │       - RandomizedSearchCV/estimator.fit
 │       - RandomizedSearchCV/scorer.score

score
 ├─ ⛔ sample_weight
 │   • ⛔ errors:
 │       - RandomizedSearchCV/scorer.score


=== METADATA ROUTING TREE ===
RandomizedSearchCV
├── estimator (LogisticRegression)
│       ➤ sample_weight[fit⛔]
├── scorer (make_scorer(accuracy_score, response_method='predict'))
│       ➤ sample_weight[score✓]
└── splitter (NoneType)

Parameter summary:
fit
 ├─ ⛔ sample_weight
 │   • ⛔ errors:
 │       - RandomizedSearchCV/estimator.fit
 │   • ✓ requested:
 │       - RandomizedSearchCV/scorer.score

score
 ├─ ✓ sample_weight
 │   • ✓ requested:
 │       - RandomizedSearchCV/scorer.score

@StefanieSenger

StefanieSenger commented Jun 13, 2025 •

Copy link
Copy Markdown
Member

I've already gotten a demonstration, and had expressed by appreciation. I think that will be very useful. Now I have some suggestions:

  1. Is it possible to make that colourful? Maybe colours could even be used instead of ✓, ✗, ⛔, ⚠, it it is somehow possible to kind of force a colourful terminal output, also if users don't use shells like zsh?

  2. I am not sure about ⛔, since it is the most pointy and colourful symbol just means: implicitly not requested (set_fit_request(metadata=None)). But from the symbol it looks a bit like explicitly not requested (set_fit_request(metadata=False)).

  3. I also wonder if ✓, ✗, ⛔, ⚠ are safe to be used for any users or if they could look cryptic for some users? These are ascii and thus available for any user?

@adrinjalali

Copy link
Copy Markdown
Member Author
  • Legend: yes, it should have a small one, and a link to a page in the docs where things are better explained.
  • Filtering: yep, easy to implement, and makes sense to have.
  • Symbols: I'm happy to have alternative suggestions for symbols, and I'll try to improve what we have. The ⛔ sign is pretty nice since it's the only case where it results in an actual error. But I'm also not happy with these signs and what users understand intuitively from them.
  • Colors: This one is very tricky and I rather not have it to start with. It's relatively easy to have colors in a white background with black font color, but terminals have all sorts of color themes. Mine is green on black, others have white/bright gray on black/dark gray, some use the black on white, etc. Having a color setting for all of them which works and is easily visible to a half colorblind person like me is pretty tricky. So I usually tend to avoid colors in my own work. But I'd be happy to review such suggestions in another PR.

@virchan virchan added the Metadata Routing all issues related to metadata routing, slep006, sample props label Jun 13, 2025
@StefanieSenger

Copy link
Copy Markdown
Member

Symbols: I'm happy to have alternative suggestions for symbols, and I'll try to improve what we have.

I am not really sure which symbols we can pick from? I have found this list on all 256 ascii codes, but it seems the ones used here are not among them?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants