Low-level Internals
This page documents the low-level helper classes that connect processed tabular columns to backend variables and tree encodings.
The notation is the same as in Mathematical Model:
\(x\) is the counterfactual explanation,
\(\hat{x}\) is the query instance,
\(f\) is the ensemble decision function,
\(p_{t,\ell}\) denotes the active leaf or path variable,
\(\varepsilon_c\) is the tie-breaking margin.
Mapper utilities
- class Indexer(*getters)[source]
Bases:
GenericMemoized dispatcher used to resolve column positions efficiently.
Store the lookup functions used for each supported key arity.
- class Mapper(mapping=None, *, columns=None, validate=True)[source]
Bases:
Mapping[Key,V],GenericMap original feature names to transformed columns.
Both the counterfactual point \(x\) and the query \(\hat{x}\) are represented in this transformed coordinate system. The mapper is the low-level bridge between original feature semantics and processed columns.
Initialize a mapper from feature metadata and transformed columns.
- Parameters:
mapping – Mapping from original feature names to metadata objects.
columns – Processed pandas index describing the transformed coordinates.
validate – Whether to verify that
mappingandcolumnsare consistent.
- __init__(mapping=None, *, columns=None, validate=True)[source]
Initialize a mapper from feature metadata and transformed columns.
- Parameters:
mapping – Mapping from original feature names to metadata objects.
columns – Processed pandas index describing the transformed coordinates.
validate – Whether to verify that
mappingandcolumnsare consistent.
- reduce(reducer)[source]
Apply
reducerto each mapped value and keep the original keys.- Returns:
Reduced mapping with the same keys.
- Return type:
Mapping[Key, S]
- apply(func)[source]
Transform the mapped values while preserving the column structure.
- Returns:
New mapper with transformed values and the same columns.
- Return type:
Mapper[U]
- __len__()[source]
Return the number of original features tracked by the mapper.
- Returns:
Number of original features.
- Return type:
NonNegativeInt
- __iter__()[source]
Iterate over original feature names.
- Returns:
Iterator over mapper keys.
- Return type:
Iterator[Key]
- __getitem__(key)[source]
Return the mapped value associated with feature name
key.- Returns:
Value associated with
key.- Return type:
V
- static _get_args(mapping, columns)[source]
Normalize constructor arguments into a mapping and a column index.
- Returns:
Pair
(mapping, columns)ready for validation.- Return type:
Args[V]
- _validate_args(mapping, columns, *, validate=True)[source]
Check that the mapping metadata matches the transformed columns.
- Raises:
ValueError – If feature names or one-hot codes do not match the column index.
- _add_indexer()[source]
Build the memoized index dispatcher used by
idx.- Returns:
Index dispatcher for transformed columns.
- Return type:
Indexer[Key, NonNegativeInt]
- _add_getter(n)[source]
Return the lookup function matching a given number of keys.
- Returns:
Getter matching the requested key arity.
- Return type:
Getter[Key, NonNegativeInt]
- Raises:
ValueError – If the requested key arity is unsupported.
- _get_with_name(name)[source]
Resolve the transformed column index for a non-encoded feature name.
- Returns:
Column position associated with
name.- Return type:
NonNegativeInt
- Raises:
KeyError – If
nameis not present in the mapper.
- _get_with_code(name, code)[source]
Resolve the transformed column index for a one-hot encoded feature code.
- Returns:
Column position associated with
(name, code).- Return type:
NonNegativeInt
- Raises:
KeyError – If
nameorcodeis not present in the mapper.
- static _repr(mapping)[source]
Return a column-aligned string representation of a small mapping.
- Returns:
Multi-line aligned representation of
mapping.- Return type:
str
MIP managers
- class FeatureManager(mapper)[source]
Bases:
objectManage MIP feature variables for the counterfactual point \(x\).
This manager owns the backend-specific feature variables that represent the processed coordinates of the counterfactual explanation. The query \(\hat{x}\) only appears later when the model objective is built.
Wrap the parsed feature mapper in MIP-specific feature variables.
- class TreeManager(trees, *, weights=None, n_isolators=0, max_samples=0, flow_type=FlowType.CONTINUOUS)
Bases:
objectManage MIP tree variables and the ensemble decision function \(f\).
Each
TreeVarencodes the active leaf or path decisions \(p_{t,\ell}\) for one tree. Aggregating those variables yields the backend expression for \(f(x)\).Initialize the tree manager and record ensemble metadata.
- build_trees(model)
Create the backend tree variables and cache the derived expressions.
After this call, the manager stores the aggregate path-length expression and the MIP representation of \(f(x)\).
- weighted_function(weights)
Return the weighted ensemble decision function \(f(x)\).
- Returns:
MIP expression representing \(f(x)\).
- Return type:
gp.MLinExpr
- xgb_margin_function(weights)
Return the XGBoost margin form of \(f(x)\).
This includes the base logit term in addition to the weighted tree contributions.
- Returns:
Margin expression representing \(f(x)\).
- Return type:
gp.MLinExpr
- _set_trees(trees, *, flow_type)
Wrap parsed trees in backend-specific
TreeVarobjects.- Raises:
ValueError – If no tree is provided.
- _set_weights(weights=None)
Validate and store the ensemble weights.
- Raises:
ValueError – If the number of weights does not match the number of estimators.
- _get_length()
Return the total isolation-path length expression.
- Returns:
Sum of the isolation-tree path lengths.
- Return type:
gp.LinExpr
- _get_function()
Return the backend expression representing \(f(x)\).
- Returns:
Cached MIP expression for \(f(x)\).
- Return type:
gp.MLinExpr
CP managers
- class FeatureManager(mapper)[source]
Bases:
objectManage CP feature variables for the counterfactual point \(x\).
This manager owns the finite-domain variables representing the processed coordinates of the counterfactual explanation. The query \(\hat{x}\) only appears later in the objective terms.
Wrap the parsed feature mapper in CP-specific feature variables.
- class TreeManager(trees, *, weights=None, scale=10000000000)
Bases:
objectManage CP tree variables and the scaled decision function \(f\).
Each
TreeVarencodes the active leaf decisions \(p_{t,\ell}\) of one tree. Aggregating those variables yields the integer-scaled CP representation of \(f(x)\).Initialize the tree manager and the score-scaling factor.
- build_trees(model)
Create the CP tree variables and cache \(f(x)\).
- weighted_function(weights)
Return the integer-scaled CP representation of \(f(x)\).
The returned dictionary maps each output-class pair
(op, c)to the corresponding linear expression for \(f_c(x)\).- Returns:
Dictionary of scaled class-score expressions.
- Return type:
dict[tuple[NonNegativeInt, NonNegativeInt], cp.LinearExpr]
- _set_trees(trees)
Wrap parsed trees in CP-specific
TreeVarobjects.- Raises:
ValueError – If no tree is provided.
- _set_weights(weights=None)
Validate and store the ensemble weights.
- Raises:
ValueError – If the number of weights does not match the number of estimators.
- _get_function()
Return the cached CP representation of \(f(x)\).
- Returns:
Cached class-score expressions.
- Return type:
dict[tuple[NonNegativeInt, NonNegativeInt], cp.LinearExpr]
MaxSAT managers
- class FeatureManager(mapper)[source]
Bases:
objectManage MaxSAT feature variables for the counterfactual point \(x\).
This manager owns the Boolean or interval-selector variables representing the processed coordinates of the counterfactual explanation. The query \(\hat{x}\) only appears later in the soft objective clauses.
Wrap parsed features in MaxSAT-specific backend variables.
- class TreeManager(trees, *, weights=None)
Bases:
objectManage MaxSAT tree variables and the support encoding of \(f\).
Each
TreeVarencodes the active leaf decisions \(p_{t,\ell}\) of one tree. The stored function representation is a Boolean support view of \(f(x)\) used by the MaxSAT encoding.Initialize the tree manager and validate the estimator weights.
- build_trees(model)
Create the MaxSAT tree variables and cache \(f(x)\) support.
- weighted_function()
Return the Boolean support representation used for \(f(x)\).
Each entry
(op, c)stores the leaf literals whose active leaf supports class \(c\) for outputop.- Returns:
Boolean support literals grouped by output and class.
- Return type:
dict[tuple[NonNegativeInt, NonNegativeInt], list[int]]
- _set_trees(trees)
Wrap parsed trees in MaxSAT-specific
TreeVarobjects.- Raises:
ValueError – If no tree is provided.
- _set_weights(weights=None)
Validate and store the ensemble weights.
- Raises:
ValueError – If the number of weights does not match the number of estimators.
- _get_function()
Return the cached MaxSAT support representation.
- Returns:
Cached Boolean support representation.
- Return type:
dict[tuple[NonNegativeInt, NonNegativeInt], list[int]]