coco_pipe.utils#

Shared package utilities.

This module holds small helpers that are not specific to one subpackage.

Attributes#

Functions#

balanced_sample_weights(y[, groups])

Return sample weights that give each class, and each group in it, one share.

stable_hash(value, *[, length])

Return a deterministic SHA-256 prefix for a JSON-compatible value.

import_optional_dependency(loader, feature, dependency)

Lazily import an optional dependency with clearer failure modes.

get_git_revision_hash([cwd])

Return the current short git hash, or "Unknown" when unavailable.

get_package_version(package_name)

Return an installed distribution version, or "Unknown".

get_environment_info([cwd, packages])

Capture runtime provenance metadata for reports and experiment results.

slug(value, *[, max_len])

Return a filesystem-safe slug from an arbitrary value.

resolve_n_jobs(n_jobs)

Resolve n_jobs to a concrete positive integer.

run_task_batch(tasks, worker_fn, max_workers)

Execute tasks with worker_fn, optionally in parallel.

permute_labels_within_strata(y, strata, *[, blocks, ...])

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. Without groups, the weights equal scikit-learn’s class_weight="balanced".

Parameters:
  • y (sequence of shape (n_samples,)) – Class labels.

  • groups (sequence of shape (n_samples,), optional) – The group of each sample.

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.

Parameters:
  • value (Any)

  • length (int)

Return type:

str

coco_pipe.utils.import_optional_dependency(loader, feature, dependency, install_hint=None)#

Lazily import an optional dependency with clearer failure modes.

Parameters:
  • loader (callable) – Zero-argument callable returning the imported dependency.

  • feature (str) – Feature or component name using the dependency.

  • dependency (str) – Human-readable dependency name.

  • install_hint (str, optional) – Installation hint shown on ImportError.

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:

str

coco_pipe.utils.get_package_version(package_name)#

Return an installed distribution version, or "Unknown".

Parameters:

package_name (str)

Return type:

str

coco_pipe.utils.get_environment_info(cwd=None, packages=None)#

Capture runtime provenance metadata for reports and experiment results.

Parameters:
Return type:

dict[str, Any]

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.

Parameters:
  • value (object) – Any object; str(value) is used as the source text.

  • max_len (int) – Maximum character length of the returned slug.

Return type:

str

coco_pipe.utils.resolve_n_jobs(n_jobs)#

Resolve n_jobs to a concrete positive integer.

-1 maps to os.cpu_count() (minimum 1). Any other value must already be a positive integer, or ValueError is raised.

Parameters:

n_jobs (int)

Return type:

int

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 with n_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 1 for serial execution.

Returns:

Results in the same order as tasks.

Return type:

list

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)