CohortPreprocessingChain

Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the Habitat Guide and Python API guide (v2.0) for usage.

class CohortPreprocessingChain(methods: Sequence[Any])[source]

Bases: object

Stateful preprocessing of the pooled cohort feature matrix.

Learns its statistics ONCE from the training cohort and applies that frozen state to every later matrix, which is what makes units from different subjects comparable and therefore what makes a habitat definition transferable. It is also the single place where habitat definition can leak test information, so fit must see training data only.

The fitted state is exposed via state and restorable via from_state(), because it has to travel inside the published HabitatModel: applying a habitat definition to a new cohort without its cohort-level preprocessing would silently place that cohort in a different feature space.

Parameters:

methods – Ordered methods to apply. Must be non-empty. Imputation is prepended when not named explicitly.

__init__(methods: Sequence[Any]) → None[source]
property methods: Tuple[Any, ...]

Return the ordered methods, including any inserted imputation.

property is_fitted: bool

Return whether the chain has learned its state.

property fit_columns: Tuple[str, ...]

Return the feature columns the chain was fitted on.

property output_columns: Tuple[str, ...]

Return the feature columns surviving the fitted chain.

property spec: Spec

Return the composed specification of every method.

set_random_state(seed: int) → None[source]

Seed every stochastic method in the chain.

Parameters:

seed – Seed forwarded to methods exposing set_random_state.

fit(block: DataFrame) → CohortPreprocessingChain[source]

Learn every method’s state from the TRAINING matrix.

Parameters:

block – Pooled training matrix (rows = units from every training subject).

Returns:

self, fitted.

Raises:

HABITAPIError – If block has no rows or a method produces non-finite values.

transform(block: DataFrame) → DataFrame[source]

Apply the fitted state to a matrix.

Parameters:

block – Matrix carrying the feature columns seen at fit time.

Returns:

The preprocessed matrix.

Raises:

HABITAPIError – If the chain is unfitted, the matrix lacks fitted columns, or a method produces non-finite values.

fit_transform(block: DataFrame) → DataFrame[source]

Fit on a matrix and return its transformation.

Parameters:

block – Pooled training matrix.

Returns:

The transformed training matrix.

property state: Dict[str, Any]

Return the fitted state for storage inside a habitat model.

Returns:

A mapping holding the chain specification, each method’s state and the fitted/output column schemas. Method states may contain fitted scikit-learn objects, so the payload is pickle-serialisable rather than JSON-serialisable – the same contract save() already uses for model payloads.

Raises:

HABITAPIError – If the chain is not fitted.

classmethod from_state(state: Mapping[str, Any]) → CohortPreprocessingChain[source]

Restore a fitted chain from state.

Parameters:

state – Payload previously produced by state.

Returns:

The restored, fitted chain.

Raises:

HABITAPIError – If the payload is not a cohort chain state or its method count disagrees with its specification.

Examples using habit.feature_preprocessing.CohortPreprocessingChain

Preprocessing voxel texture before clustering

Preprocessing voxel texture before clustering

Preprocessing features before clustering

Preprocessing features before clustering