Skip to content

Allow a metadata-aware splitter as cv_class (used directly, not wrapped as the inner groups-CV) #1104

Closed

Description

@bruAristimunha

Summary

CrossSubjectEvaluation (and the CrossSubjectSplitter/CrossSessionSplitter wrappers)
always treat cv_class as the inner CV: they instantiate it and call
split(X=index, y, [groups=subject]), forwarding only the subject column. There is no
public way to plug in a metadata-aware top-level splitter — one that needs split(y, metadata)
and other columns (e.g. session) to build the fold.

Why this matters

We want a single-target cross-subject fold that restricts the test set to one session while
excluding the target's other sessions from training (test = target@session, train = other
subjects). That decision needs the session column, which the inner-CV path never passes.

A splitter that already follows the moabb convention (subclasses BaseCrossValidator, declares
metadata_columns = ("subject", "session"), implements split(self, y, metadata)) cannot be
passed via cv_class:

import pandas as pd
from moabb.evaluations.splitters import CrossSubjectSplitter
# MySplitter: BaseCrossValidator with metadata_columns=("subject","session") and split(self, y, metadata)
md = pd.DataFrame({"subject": [1, 1, 2, 2], "session": ["0", "1", "0", "1"]})
list(CrossSubjectSplitter(cv_class=MySplitter, target=2, test_session="1").split(y=None, metadata=md))
# TypeError: MySplitter.split() got an unexpected keyword argument 'X'

Current workaround

Subclass the evaluation and override _create_splitter to use cv_class directly:

class TargetSubjectEvaluation(CrossSubjectEvaluation):
    def _create_splitter(self):
        return self.cv_class(**self.cv_kwargs) if self.cv_class else super()._create_splitter()

Proposed

The evaluation already inspects cv_class.metadata_columns in __init__ (to extend
additional_columns). Extend that: when cv_class declares metadata_columns, use it directly
as the top-level splitter — call split(y, metadata) — instead of wrapping it as the inner
groups-CV. Backward compatible (group CVs without metadata_columns keep wrapping), and it lets
metadata-driven folds be configured through the public cv_class / cv_kwargs API with no subclass.

Happy to open a PR if this direction sounds good.

Activity

  1. bruAristimunha commented on Jun 28, 2026

    @bruAristimunha
    CollaboratorAuthor

    Status after #1105 (groups=) — the core need is still open

    #1105 lets CrossSubjectSplitter key folds on a compound metadata column (groups=["subject", "session"]) through the stock inner CV. That's useful, but it doesn't cover this issue, because the inner CV runs over all rows and can only partition them (train ⊎ test). The single-target case here needs a 3-way split — train / test / excluded — where the held-out subject's non-test sessions are dropped from both sets.

    Verified against our workaround splitter (TargetSubjectSplitter, which declares metadata_columns=("subject","session") and does split(self, y, metadata)):

    # subjects 1–3, sessions {0train, 1test}; target=2, test_session="1test"
    #   train : subjects 1, 3  (all their sessions)
    #   test  : subject 2 @ 1test
    #   -> subject 2 @ 0train is DROPPED — not in train, not in test

    No stock scikit-learn CV expresses that drop:

    • PredefinedSplit(test_fold=-1) puts the excluded rows in train → the held-out subject leaks into training.
    • LeaveOneGroupOut never excludes rows.

    The keep-filter that makes this correct lives inside the splitter (subset the metadata to the kept rows, then PredefinedSplit over those). That's exactly why this issue's proposal — use a metadata_columns-declaring splitter directly as cv_class, calling split(y, metadata) instead of wrapping it as the inner groups-CV — is the clean fix; it lets the splitter own both the fold and the exclusion. (A narrower alternative would be a drop/exclude callable on CrossSubjectSplitter, metadata -> bool mask, applied before the inner CV.)

    Concretely it's the only thing still keeping a small shim (TargetSubjectEvaluation + TargetSubjectSplitter) in our downstream code after #1105/#1107/#1109 retired the others. Happy to PR the metadata_columns-as-cv_class direction if that's the preferred shape.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions