coco_pipe.dim_reduction.evaluation.stats#

Statistical tests for trajectory scalar metrics.

Functions#

paired_condition_stats

Paired t-test across all condition pairs, with FDR correction.

grouped_condition_stats

Paired t-test across grouped condition sets, with FDR correction.

permutation_null_separation_auc

Label-permutation test of the separation AUC of one contrast.

permutation_familywise_separation_auc

Max-statistic permutation test over several results and contrasts.

Functions#

paired_condition_stats(scalar_df, conditions[, ...])

Run a paired t-test for every (metric, condition pair) combination.

grouped_condition_stats(scalar_df, condition_sets[, ...])

Run paired t-tests across grouped condition sets with FDR correction.

permutation_null_separation_auc(result, group_a, group_b)

Label-permutation test of between-group separation AUC.

permutation_familywise_separation_auc(results, ...[, ...])

Family-wise max-statistic test of separation AUC over results and contrasts.

Module Contents#

coco_pipe.dim_reduction.evaluation.stats.paired_condition_stats(scalar_df, conditions, metric_col='metric', condition_col='condition', subject_col='subject', value_col='value', min_pairs=3)#

Run a paired t-test for every (metric, condition pair) combination.

Parameters:
  • scalar_df (pd.DataFrame) – Long-format table of scalar trajectory metrics. Must contain at least metric_col, condition_col, subject_col, and value_col.

  • conditions (list[str]) – Ordered list of condition labels to include in the comparisons.

  • metric_col (str, default="metric")

  • condition_col (str, default="condition")

  • subject_col (str, default="subject")

  • value_col (str, default="value")

  • min_pairs (int, default=3) – Minimum number of matched subject pairs required to run a t-test.

Returns:

Columns: metric, comparison, n, mean_diff, t, p_uncorrected, p_fdr. Empty DataFrame if no valid pairs are found.

Return type:

pd.DataFrame

coco_pipe.dim_reduction.evaluation.stats.grouped_condition_stats(scalar_df, condition_sets, metric_col='metric', condition_col='condition', subject_col='subject', value_col='value', min_pairs=3)#

Run paired t-tests across grouped condition sets with FDR correction.

Conditions within each group are averaged per subject before testing, allowing multi-condition groups (e.g. “Positive = [happy, excited]”).

Parameters:
  • scalar_df (pd.DataFrame) – Long-format table of scalar trajectory metrics.

  • condition_sets (dict[str, dict[str, list[str]]]) – Mapping {set_name: {group_label: [condition, ...]}} defining how conditions are grouped for each comparison set.

  • metric_col (str) – Column name overrides.

  • condition_col (str) – Column name overrides.

  • subject_col (str) – Column name overrides.

  • value_col (str) – Column name overrides.

  • min_pairs (int, default=3) – Minimum number of matched subject pairs required to run a t-test.

Returns:

Columns: set, metric, comparison, n, mean_diff, t, p_uncorrected, p_fdr. Empty DataFrame if no valid pairs are found.

Return type:

pd.DataFrame

coco_pipe.dim_reduction.evaluation.stats.permutation_null_separation_auc(result, group_a, group_b, n_perm=200, rng=None, method='centroid', window=None, strata=None, per_subject=False)#

Label-permutation test of between-group separation AUC.

Parameters:
  • result (TrajectoryResult) – The TrajectoryResult container holding trajectories and metadata.

  • group_a (Sequence) – The condition labels to compare, as stored in result.conditions (names or integers).

  • group_b (Sequence) – The condition labels to compare, as stored in result.conditions (names or integers).

  • n_perm (int, default=200)

  • rng (np.random.Generator, optional)

  • method (str, default="centroid") – Separation method passed to trajectory_separation(). Any method that function supports may be used, e.g. "mahalanobis".

  • window (tuple of float, optional) – Time window (tmin, tmax) to restrict the AUC calculation.

  • strata (sequence, optional) – One stratum per trajectory; labels are permuted only inside a stratum (permute_labels_within_strata()). Choose them so that only the tested factor moves: result.subjects for a within-subject test, or subject x class x side to test speed among speed x class x side conditions. By default labels are shuffled across all trajectories.

  • per_subject (bool, default=False) – Average the separation curves of the subjects instead of separating the pooled trials. Needs strata nested in subjects.

Returns:

  • observed_auc (float)

  • null_aucs (np.ndarray of shape (n_perm,)) – AUC values under n_perm random label permutations.

  • p_value (float) – (1 + #(null_aucs >= observed_auc)) / (n_perm + 1).

Raises:

ValueError – If a label in group_a or group_b is not a condition of result, strata does not fit or leaves trials of the contrast unable to change group, an AUC is NaN, or the null has zero spread.

Return type:

tuple[float, numpy.ndarray, float]

See also

permutation_familywise_separation_auc

Max-statistic correction over several results and contrasts.

Notes

By default the statistic is the separation of the pooled trials, for example the distance between the centroids of all group-a and all group-b trials. With per_subject=True it is the mean of the subjects’ own separation curves, the subject as the unit; with few trials per subject that mean is biased upward by noise. Both are valid under permutation but measure different things.

coco_pipe.dim_reduction.evaluation.stats.permutation_familywise_separation_auc(results, contrasts, strata, n_perm=1000, rng=None, method='centroid', window=None, per_subject=False, standardize=True)#

Family-wise max-statistic test of separation AUC over results and contrasts.

Each permutation relabels the trials inside strata and applies the same relabelling to every result. The null of the family is the maximum statistic over all results and contrasts per permutation, and each observed statistic gets a p-value corrected for the family-wise error rate. The trajectories are used as given: a reducer that produced them is not refit.

Parameters:
  • results (mapping of key to TrajectoryResult) – The family, such as one result per frequency band. Every result holds the same subjects and conditions in the same row order.

  • contrasts (sequence of (group_a, group_b)) – Pairs of condition groups, each a label or a list of labels as stored in conditions, such as (["famous", "unfamiliar"], "scrambled").

  • strata (array-like of shape (n_trials,) or (n_contrasts, n_trials)) – Labels are permuted only inside a stratum. Choose them so that only the tested factor moves: subject x class x side to test speed among speed x class x side conditions. One row is shared by every contrast, and each permutation draws one relabelling for all of them. One row per contrast lets contrasts of different factors use their own strata; each permutation then draws one relabelling per contrast, which still controls the family-wise error, more conservatively.

  • n_perm (int, default=1000) – Number of permutations.

  • rng (np.random.Generator, optional) – Generator for the relabellings. Defaults to default_rng(0).

  • method (str, default="centroid") – Separation method passed to trajectory_separation().

  • window (tuple of float, optional) – Time window (tmin, tmax) of the AUC.

  • per_subject (bool, default=False) – Average the separation curves of the subjects instead of separating the pooled trials. Needs strata nested in subjects.

  • standardize (bool, default=True) – Take the maximum over z = (AUC - null mean) / null sd, each (result, contrast) standardised by its own null, so results on different scales, such as one PCA per band, weigh equally. With False the maximum is over raw AUCs, which suits results on one scale.

Returns:

  • table (pd.DataFrame) – One row per (key, contrast): key, contrast ("A_vs_B", the labels of a group joined by "+"), observed_auc, null_mean, null_sd, z and p_fwer, (1 + #(max_null >= statistic)) / (n_perm + 1) with the statistic z or observed_auc.

  • max_null (np.ndarray of shape (n_perm,)) – Maximum statistic over the family under each permutation.

  • null_aucs (np.ndarray of shape (n_perm, n_results, n_contrasts)) – AUC of every result and contrast under each permutation.

Raises:

ValueError – If results or contrasts is empty, the results do not hold the same subjects and conditions in the same order, strata does not fit or leaves trials of a contrast unable to change group, a contrast names an unknown condition, an AUC is NaN, or a null has zero spread.

Return type:

tuple[pandas.DataFrame, numpy.ndarray, numpy.ndarray]

See also

permutation_null_separation_auc

One result and one contrast.

Notes

The statistic is that of permutation_null_separation_auc(): the separation of the pooled trials by default, the mean of the subjects’ separation curves with per_subject=True.