coco_pipe.decoding.permute_labels_within_strata#

coco_pipe.decoding.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)