Study
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 Study(spec: HabitatSpec, design: str | None = None)[source]
Bases:
objectA habitat analysis declared independently of any cohort.
The lifecycle mirrors a sklearn estimator:
fit()learns the cohort-level habitat definition and returnsself;predict()projects that definition onto a new cohort;fit_predict()fits and hands back the fullStudyResultin one call. Fitted state is exposed through the trailing-underscore attributesmodel_andfit_result_.- spec
The analysis to run.
- design
Optional declared intent (
"two_step","one_step"or"direct_pooling"). When set,fitvalidates the spec against the design’s guards before running, so a mismatched spec fails loudly instead of silently running a different dataflow. WhenNone, the dataflow declared by the spec itself (pooling/ stage list) decides what runs.- Type:
str | None
- model_
The fitted
HabitatModel;Noneuntil fitted, andNoneafter fitting aone_stepstudy (that design defines habitats per subject, so there is no cohort-level model to publish).- Type:
- fit_result_
The
StudyResultproduced by the latestfit();Noneuntil fitted.- Type:
See also
habit.spec.HabitatSpecFrozen analysis declaration used by this study.
habit.contracts.HabitatModelFitted cohort habitat definition.
habit.recipes.StudyResultIn-memory artefacts from
fit_predict.habit.recipes.two_step_habitatFactory for the classical two-step design.
- fit(cohort: Cohort, *, backend: ExecutionBackend | None = None, checkpoint: CheckpointStore | None = None, seed: int | None = None, inspect: StepObserver | None = None, report: Any | None = None, writer: ResultWriter | None = None, retain: str = 'all', on_subject_complete: Callable[[Subject, HabitatMap, HabitatModel], None] | None = None, persist_subject_models: bool = True) Study[source]
Learn the habitat definition on a cohort; return
self.- Parameters:
cohort – Subjects to analyse.
backend – Optional execution backend (parallelism, resume policy).
checkpoint – Optional checkpoint store forwarded to per-subject stages.
seed – Optional override of
spec.random_seed.inspect – Optional step observer for in-memory debugging / QA. Unsupported with the process backend.
report – Optional
Reportdeclaring what to persist and draw as each subject completes. This is the primary streaming API.writer/retain/on_subject_completeremain as shorthands that fill an implicit report.writer – Optional streaming writer (
one_stepdesign only): each subject’s habitat map is persisted the moment the backend yields it, so a crashed run keeps completed subjects.retain –
"all"(default) keeps every artefact in memory;"maps"drops voxel-level clustering units (the memory-dominant payload of voxel-level designs);"tables"additionally drops habitat maps and requireswriter.on_subject_complete – Optional parent-process callback
(subject, habitat_map, model)fired once per completed subject – including checkpoint-resumed ones – before retention stripping. Prefer a figure atom onreport.persist_subject_models – With a streaming
writerand no explicitreport.persist, also write<subject_id>.habitatmodelfor each subject.
- Returns:
model_holds the cohort-level definition (except for theone_stepdesign) andfit_result_the full study result.- Return type:
self, fitted
- predict(cohort: Cohort, *, backend: ExecutionBackend | None = None, checkpoint: CheckpointStore | None = None, seed: int | None = None, inspect: StepObserver | None = None) StudyResult[source]
Apply the fitted habitat definition to a (new) cohort.
The model’s own cohort-level preprocessing state is restored and re-applied, because centroids only mean something in the feature space they were computed in.
- Parameters:
cohort – Subjects to label.
backend – Optional execution backend. Serial when omitted.
checkpoint – Optional store enabling per-subject resume; keys scope on
model_.model_id.seed – Optional override of
spec.random_seed.inspect – Optional step observer for in-memory debugging / QA. Unsupported with the process backend.
- Returns:
habitat maps, the habitat feature table and the run manifest, all in memory.
- Return type:
The study result for the projected cohort
- Raises:
NotFittedError – If the study has no fitted model yet.
HABITAPIError – If the study ran a
one_stepfit, which defines habitats per subject and therefore has no cohort-level model to apply.
- fit_predict(cohort: Cohort, *, backend: ExecutionBackend | None = None, checkpoint: CheckpointStore | None = None, seed: int | None = None, inspect: StepObserver | None = None, report: Any | None = None, writer: ResultWriter | None = None, retain: str = 'all', on_subject_complete: Callable[[Subject, HabitatMap, HabitatModel], None] | None = None, persist_subject_models: bool = True) StudyResult[source]
Fit on a cohort and return the full study result.
Equivalent to
fit(cohort).fit_result_, provided for the common case where the training-cohort artefacts (maps, features, manifest) are wanted immediately.- Parameters:
cohort – Subjects to analyse.
backend – Optional execution backend (parallelism, resume policy).
checkpoint – Optional checkpoint store forwarded to per-subject stages.
seed – Optional override of
spec.random_seed.inspect – Optional step observer for in-memory debugging / QA.
writer – Optional streaming writer (
one_stepdesign only); seefit().retain – In-memory retention mode; see
fit().on_subject_complete – Optional per-subject completion callback; see
fit().persist_subject_models – Write per-subject models when streaming; see
fit().
- Returns:
The completed study result.
- classmethod from_model(model: HabitatModel | str | Path, spec: HabitatSpec | None = None) Study[source]
Build a fitted study from a published habitat model.
This is the external-validation entry point: load a
.habitatmodelartefact (or pass an in-memoryHabitatModel) and callpredict()on the new cohort.- Parameters:
model – A fitted model, or a path to a
.habitatmodelarchive.spec – The analysis declaration whose upstream stages must match the model’s training spec. When
None, the spec embedded in the model archive is used.
- Returns:
A study whose
model_is already fitted, ready forpredict().
- __init__(spec: HabitatSpec, design: str | None = None) None