coco_pipe.dim_reduction.artifacts#

Artifact persistence helpers for dimensionality-reduction runs.

This module owns the save/load/update layer that the dim-reduction pipeline uses to checkpoint fit results to disk; eval results live only in the runs inventory. Keeping these functions in coco-pipe (rather than in a consumer project) means any project that runs the dim-reduction pipeline can share the same artifact format without duplicating the serialization logic.

Public API#

save_fit_artifact

Write embedding, ids, fit payload, metrics, and diagnostics to one .npz file.

load_fit_artifact

Read a fit artifact back into a dict.

slice_fit_artifact

One dimension of a fit artifact that holds a nested reducer’s sweep.

load_fit_runs

Read a JSON runs inventory file back into a list of dicts.

update_runs

Upsert a record into a JSON runs inventory, keeping it sorted.

Constants#

SEPARATION_METRIC_KEY

Canonical key for the logistic-regression separation metric returned by coco_pipe.dim_reduction.evaluation.core.evaluate_embedding().

SEPARATION_RF_METRIC_KEY

Canonical key for the random-forest separation metric.

FIT_METRIC_COLUMNS

Ordered list of geometry quality metric names written by fit artifacts.

EVAL_METRIC_COLUMNS

Ordered list of eval metric names in eval inventory records.

FIT_RUN_KEY_FIELDS

Fields that uniquely identify a fit run in the runs inventory.

EVAL_RUN_KEY_FIELDS

Fields that uniquely identify an eval run in the runs inventory.

Functions#

save_fit_artifact(path, embedding, ids, fit_payload, ...)

Write a fit artifact: one .npz file at path.

load_fit_artifact(path[, n_components])

Load a fit artifact written by save_fit_artifact().

slice_fit_artifact(artifact, n_components)

One dimension of a loaded fit artifact that holds a sweep.

load_fit_runs(path)

Load a fit-runs inventory JSON file into a list of dicts.

update_runs(path, record, key_fields)

Upsert record into a JSON runs inventory at path.

build_record(payload, artifact_path, output_root, ...)

Build a flat run-inventory record from an artifact payload dict.

write_run_status(output_root, fit_runs_path[, ...])

Write run_summary.json and a run-marker sentinel to output_root.

build_availability_record(*, scope, condition, ...)

Build a data-availability record from container shape information.

Module Contents#

coco_pipe.dim_reduction.artifacts.save_fit_artifact(path, embedding, ids, fit_payload, metrics_payload, diagnostics, sweep=None)#

Write a fit artifact: one .npz file at path.

The file holds the embedding, the ids, the reducer diagnostics and, as JSON, the fit payload and its metrics. It is written atomically, so a file at path is a complete artifact: one file per fit keeps the file count low for analysis modes with many units.

Parameters:
  • path (pathlib.Path) – Target .npz file. Parent directories are created.

  • embedding (numpy.ndarray) – Array of shape (n_obs, n_dims).

  • ids (numpy.ndarray) – 1-D array of observation identifiers aligned with embedding.

  • fit_payload (dict[str, Any]) – Serialisable dict describing the fit (reducer, config, provenance, …). The key "artifact_stem" is recorded for provenance.

  • metrics_payload (dict[str, Any]) – Serialisable dict of geometry quality metrics (trustworthiness, …).

  • diagnostics (dict[str, Any]) – Reducer-internal diagnostics. May be empty.

  • sweep (dict[int, dict[str, Any]] | None) – For a nested reducer fitted once at its largest dimension, each swept n_components -> {"fit": payload, "metrics": metrics}; the file then holds the whole sweep, read one dimension at a time with slice_fit_artifact().

Return type:

None

coco_pipe.dim_reduction.artifacts.load_fit_artifact(path, n_components=None)#

Load a fit artifact written by save_fit_artifact().

Parameters:
  • path (pathlib.Path) – The artifact’s .npz file.

  • n_components (int | None) – For a file holding a sweep, the dimension to return (see slice_fit_artifact()); None returns the whole file.

Returns:

embedding, ids, fit, metrics, diagnostics, manifest, path, and embedding_container. The embedding_container is a DataContainer view of the embedding (or None for non-2D embeddings); the embedding and ids arrays remain for direct array access.

Return type:

dict with keys

coco_pipe.dim_reduction.artifacts.slice_fit_artifact(artifact, n_components)#

One dimension of a loaded fit artifact that holds a sweep.

The embedding, components and explained-variance ratios are cut to their first n_components, and the fit payload and metrics are that dimension’s. An artifact without a sweep is returned unchanged.

Parameters:
Return type:

dict[str, Any]

coco_pipe.dim_reduction.artifacts.load_fit_runs(path)#

Load a fit-runs inventory JSON file into a list of dicts.

Parameters:

path (pathlib.Path) – Path to the JSON file written by update_runs().

Return type:

list of dict

Raises:
coco_pipe.dim_reduction.artifacts.update_runs(path, record, key_fields)#

Upsert record into a JSON runs inventory at path.

If path already exists, the list is loaded and the record whose key_fields values match record’s is replaced (upsert semantics). If no match is found, record is appended. The list is then sorted by key_fields followed by common run-taxonomy fields and written back.

Parameters:
  • path (pathlib.Path) – Target JSON file. The parent directory is created if necessary.

  • record (dict[str, Any]) – Dict to upsert. Must contain all fields listed in key_fields.

  • key_fields (collections.abc.Sequence[str]) – Ordered sequence of field names that uniquely identify a run.

Return type:

None

coco_pipe.dim_reduction.artifacts.build_record(payload, artifact_path, output_root, metric_columns, metrics_payload=None, error=None)#

Build a flat run-inventory record from an artifact payload dict.

Used for both fit and eval records — pass FIT_METRIC_COLUMNS or EVAL_METRIC_COLUMNS as metric_columns.

Strips bulky sub-dicts (metrics, records, metadata, artifacts) from payload, adds a relative artifact_path, promotes each metric in metric_columns to a top-level float (or nan), and stamps the record with a status of "success" or "failed".

Parameters:
Return type:

dict[str, Any]

coco_pipe.dim_reduction.artifacts.write_run_status(output_root, fit_runs_path, eval_runs_path=None, *, run_summary_path=None, fatal_error=None, report_path=None, run_metadata=None)#

Write run_summary.json and a run-marker sentinel to output_root.

The marker file is one of _RUN_SUCCESS, _RUN_PARTIAL, or _RUN_FAILED. Any pre-existing marker files are removed first so only one is present at a time.

Run status logic:

  • success — at least one successful fit or eval, no failures

  • partial — both successes and failures present

  • failed — only failures, or fatal_error is set with no successes

Parameters:
  • output_root (pathlib.Path) – Directory that receives run_summary.json and the marker file.

  • fit_runs_path (pathlib.Path) – Path to the fit runs JSON inventory (may not exist yet).

  • eval_runs_path (pathlib.Path | None) – Path to the eval runs JSON inventory (may not exist yet; None for a run without evals).

  • run_summary_path (pathlib.Path | None) – Optional explicit path for the summary JSON. Defaults to output_root / "run_summary.json".

  • fatal_error (str | None) – If set, the run is marked as at least partially failed.

  • report_path (pathlib.Path | None) – Path to the generated HTML report, if any.

  • run_metadata (dict[str, Any] | None) – Extra key/value pairs merged into the summary payload.

Returns:

The summary payload written to disk.

Return type:

dict

coco_pipe.dim_reduction.artifacts.build_availability_record(*, scope, condition, unit_spec, container, requested_components, valid_components)#

Build a data-availability record from container shape information.

Captures matrix dimensions and which n_components values are feasible vs skipped, for inclusion in run-level provenance metadata.

Parameters:
Return type:

dict[str, Any]