coco_pipe.utils#
Shared package utilities.
This module holds small helpers that are not specific to one subpackage.
Attributes#
Functions#
|
Return sample weights that give each class, and each group in it, one share. |
|
Return a deterministic SHA-256 prefix for a JSON-compatible value. |
|
Lazily import an optional dependency with clearer failure modes. |
|
Return the current short git hash, or |
|
Return an installed distribution version, or |
|
Capture runtime provenance metadata for reports and experiment results. |
|
Return a filesystem-safe slug from an arbitrary value. |
|
Resolve |
|
Execute tasks with worker_fn, optionally in parallel. |
|
Permute labels without moving any of them across a stratum. |
Module Contents#
- coco_pipe.utils.PACKAGE_VERSIONS: collections.abc.Mapping[str, str]#
- coco_pipe.utils.balanced_sample_weights(y, groups=None)#
Return sample weights that give each class, and each group in it, one share.
Each sample weighs one over the number of samples of its class or, with
groups, of its group within its class; each class is then scaled to the same total. A group (e.g. a subject) thus counts once within each class, however many samples (e.g. windows) it has there. Withoutgroups, the weights equal scikit-learn’sclass_weight="balanced".- Parameters:
- Returns:
The weights, averaging 1, so that an estimator’s regularisation keeps its scale.
- Return type:
numpy.ndarray of shape (n_samples,)
- coco_pipe.utils.stable_hash(value, *, length=64)#
Return a deterministic SHA-256 prefix for a JSON-compatible value.
Dictionaries are serialized with sorted keys and compact separators. Values such as paths that are not directly JSON serializable fall back to
str.
- coco_pipe.utils.import_optional_dependency(loader, feature, dependency, install_hint=None)#
Lazily import an optional dependency with clearer failure modes.
- Parameters:
- Returns:
Imported dependency returned by
loader.- Return type:
Any
- Raises:
ImportError – If the dependency is not installed.
RuntimeError – If the dependency is installed but fails during initialization.
- coco_pipe.utils.get_git_revision_hash(cwd=None)#
Return the current short git hash, or
"Unknown"when unavailable.- Parameters:
cwd (str | os.PathLike[str] | None)
- Return type:
- coco_pipe.utils.get_package_version(package_name)#
Return an installed distribution version, or
"Unknown".
- coco_pipe.utils.get_environment_info(cwd=None, packages=None)#
Capture runtime provenance metadata for reports and experiment results.
- Parameters:
cwd (str | os.PathLike[str] | None)
packages (collections.abc.Mapping[str, str] | None)
- Return type:
- coco_pipe.utils.slug(value, *, max_len=80)#
Return a filesystem-safe slug from an arbitrary value.
Collapses runs of non-alphanumeric characters (except
.,_,=,-) into a single-, strips leading/trailing punctuation, and truncates at max_len.
- coco_pipe.utils.resolve_n_jobs(n_jobs)#
Resolve
n_jobsto a concrete positive integer.-1maps toos.cpu_count()(minimum 1). Any other value must already be a positive integer, orValueErroris raised.
- coco_pipe.utils.run_task_batch(tasks, worker_fn, max_workers)#
Execute tasks with worker_fn, optionally in parallel.
When max_workers is 1 the tasks are run serially in the current process. For any larger value
joblib.Parallel()is used withn_jobs=min(max_workers, len(tasks))so the pool size never exceeds the actual work to do.- Parameters:
tasks (collections.abc.Sequence[Any]) – Sequence of opaque task objects passed one-by-one to worker_fn.
worker_fn (collections.abc.Callable[[Any], Any]) – Single-argument callable that processes one task and returns a result.
max_workers (int) – Maximum number of parallel workers. Pass
1for serial execution.
- Returns:
Results in the same order as tasks.
- Return type:
- coco_pipe.utils.permute_labels_within_strata(y, strata, *, blocks=None, random_state=None)#
Permute labels without moving any of them across a stratum.
Without blocks, sample labels are shuffled inside each stratum, which keeps each stratum’s class counts. With blocks, each block (one value of blocks within one stratum) must carry a single label, and block labels are shuffled between the blocks of each stratum. This is the draw the decoding full-pipeline permutation test makes for
permutation_scheme="within_strata"and"blocks_within_strata", and the one the trajectory separation tests of dim_reduction make within subjects.- Parameters:
y (sequence) – One label per sample.
strata (sequence) – One stratum per sample (e.g. the subject).
blocks (sequence, optional) – One block per sample (e.g. the run), for a blocked design.
random_state (int or numpy.random.Generator, optional) – Seed or generator for the draw.
- Returns:
y_perm – The permuted labels, aligned with y.
- Return type:
np.ndarray
- Raises:
ValueError – If a block holds more than one label, or no stratum holds two exchangeable units with different labels.
Examples
>>> from coco_pipe.utils import permute_labels_within_strata >>> y = [0, 1, 0, 1, 0, 1] >>> subjects = ["S1", "S1", "S1", "S2", "S2", "S2"] >>> y_perm = permute_labels_within_strata(y, subjects, random_state=0)