StudyResult
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 StudyResult(habitat_model: ~habit.contracts.habitat.HabitatModel | None, pipeline: ~typing.Any, features: ~habit.contracts.table.FeatureTable, habitat_maps: ~typing.Tuple[~habit.contracts.habitat.HabitatMap, ...], manifest: ~habit.contracts.manifest.RunManifest, subject_models: ~typing.Mapping[str, ~habit.contracts.habitat.HabitatModel] = <factory>, units: ~typing.Tuple[~habit.contracts.habitat.Supervoxelization, ...] = (), units_rows: ~typing.Tuple[~pandas.core.frame.DataFrame, ...] = (), maps_persisted: bool = False, inspection: ~typing.Any | None = None)[source]
Bases:
objectWhat a fitted study hands back, entirely in memory.
Nothing here has touched the filesystem. Writing is a separate, explicit act via
write()(anyResultWriter) orsave()(the conventional directory layout), which is what allows the identical code to run inside someone else’s service where there is no output directory at all.- habitat_model
The population-level habitat definition. Named in full rather than
modelbecausemodelalready means a trained classifier elsewhere in HABIT.Nonefor designs that define habitats per subject rather than across the cohort – the one-step design in particular, where “the definition” is not one object (seesubject_models).- Type:
- pipeline
The subject-level procedure that applies that definition, so that model and procedure can be shipped together for external validation.
Nonewhen no single procedure applies.- Type:
Any
- features
Habitat-level features for the fitted cohort.
- habitat_maps
Per-subject habitat label images, in cohort order.
- Type:
Tuple[habit.contracts.habitat.HabitatMap, …]
- manifest
Provenance and reporting for this run.
- subject_models
Per-subject habitat definitions, for designs that cluster each subject independently. Empty for cohort-level designs. Held in memory only: the writer protocol persists one habitat model per study, and inventing a per-subject file naming convention here would fix a layout no caller has asked for yet.
- Type:
Mapping[str, habit.contracts.habitat.HabitatModel]
- units
Per-subject clustering units the habitat maps were labelled from, in cohort order, aligned with
habitat_maps. This is a REPORTING payload, not part of the scientific result: the v0.1habitats.parquetunit table and*_supervoxel.nrrdmaps are derived views of it, assembled by the directory writer. Empty when the caller only needs the label maps. A design with no supervoxel step stores one-voxel units (seevoxel_units()), so the field has one uniform type regardless of design. Streaming retention modes (retain="maps"/"tables") drop this memory-dominant payload and keepunits_rowsinstead.- Type:
- units_rows
Pre-aggregated per-subject rows of the v0.1 units table, produced inside the workers by streaming runs. When present,
save()writes the units table from these frames instead of deriving it fromunits.- Type:
Tuple[pandas.core.frame.DataFrame, …]
- maps_persisted
Truewhen a streaming writer already persisted every habitat map during the fit;save()then skips map writing (and the map overwrite probe) entirely.- Type:
- inspection
Optional step observer / recorder passed as
inspect=to a recipe. DefaultNone. Writers ignore this field; it is for in-memory debugging only and is never part of fingerprints.- Type:
Any | None
See also
habit.recipes.StudyProduces this result via
fit_predict.habit.contracts.HabitatModelCohort-level definition on
habitat_model.habit.spec.HabitatSpecAnalysis declaration recorded in the manifest.
- write(writer: ResultWriter) None[source]
Hand every artefact to a writer.
The result decides WHAT is persisted; the writer decides WHERE and in what format. That split is what makes an S3 writer, a DICOM-SEG writer or a no-op writer possible without touching this class.
- Parameters:
writer – Destination implementing
ResultWriter.
- save(out_dir: str | Path, *, table_format: str = 'parquet', map_format: str = 'nrrd', write_maps: bool = True, write_units_table: bool = True, write_cluster_plots: bool = False, write_cluster_plots_3d: bool = False, write_interactive_cluster_plots: bool = False) Path[source]
Write the artefacts of this study to a directory.
Convenience sugar over
write()with the conventional directory writer; the layout itself belongs toDirectoryResultWriter. Unlikewrite(), which hands over everything unconditionally (the protocol semantics), this entry point honours the two v0.1 reporting switches so the CLI can keep them meaningful:write_maps=Falseskips every label map (v0.1save_images: false) andwrite_units_table=Falseskips the units table (v0.1save_results_csv: false).Beyond the protocol artefacts, when the study collected its clustering
unitsthis also persists the derived v0.1 reporting views of them: thehabitats.parquet/habitats.csvunit table (row granularity follows the recipe design) and, for the two-step training design, one<subject_id>_supervoxel.<ext>per subject. v0.1 wrote supervoxel maps during training only – its predict path read them back rather than rewriting them – so the apply design writes none either.When
write_cluster_plots=Trueand a cohort-levelhabitat_modelis present, a population-level 2D PCA habitat scatter is written undervisualizations/habitat_clustering/, mirroring the v0.1ClusteringService.visualize_habitat_clusteringlayout for the static PNG only (interactive 3D HTML remains in the legacy stack).When the fit ran with a streaming writer (
maps_persisted), the habitat maps are already on disk and are neither probed nor rewritten here;savethen persists only the cohort-level artefacts (feature table, manifest, units table from the retained per-subject rows).- Parameters:
out_dir – Destination directory, created when missing.
table_format – On-disk format of the units table,
"parquet"(v0.1 default) or"csv".map_format – On-disk format of habitat / supervoxel label maps.
"nrrd"(v0.1 default),"nii","nii.gz","mha", or"mhd".write_maps – Write habitat maps (and, for the two-step design, supervoxel maps) using
map_format.write_units_table – Write the
habitatsunits table.write_cluster_plots – Write the population-level 2D PCA clustering scatter when cohort-level units and a habitat model exist.
write_cluster_plots_3d – Also write a static 3D PCA scatter PNG.
write_interactive_cluster_plots – Also write a rotatable plotly HTML file when plotly is installed.
- Returns:
The directory written to.
- __init__(habitat_model: ~habit.contracts.habitat.HabitatModel | None, pipeline: ~typing.Any, features: ~habit.contracts.table.FeatureTable, habitat_maps: ~typing.Tuple[~habit.contracts.habitat.HabitatMap, ...], manifest: ~habit.contracts.manifest.RunManifest, subject_models: ~typing.Mapping[str, ~habit.contracts.habitat.HabitatModel] = <factory>, units: ~typing.Tuple[~habit.contracts.habitat.Supervoxelization, ...] = (), units_rows: ~typing.Tuple[~pandas.core.frame.DataFrame, ...] = (), maps_persisted: bool = False, inspection: ~typing.Any | None = None) None