Skip to content

synaptome.questions

synaptome.questions

Questions: what is compared on which data. A question resolves its design and analysis table from the study; tests, estimates and plots all read from it (DESIGN.md section 5).

Create questions from a study: study.compare(...), study.compare_along(...), study.distribution(...); rebuild a recorded one with study.question(spec).

Question

A question asked of a study. Create one with study.compare, study.compare_along or study.distribution, not directly.

A question is read-only. Its design, data and data_key are resolved from the study on first access and then kept: if the study changes afterwards, ask the question again (study.question(q.to_dict())).

Parameters:

Name Type Description Default
study Study
required
outcome str

The variable compared.

required
subset Mapping[str, Any] | None

Filters applied first. Keys: a dimension of the outcome (coordinates), a level at or above the outcome's (unit ids), or a scalar variable at or above it (units whose value is among these). Values: one value, a list, or slice(low, high) (inclusive), also written {"between": [low, high]}.

None
reduce Mapping[str, Any] | None

Reductions of dimensions and levels the question doesn't use, e.g. {"source": "median"} or {"trial": "mean"} (see synaptome.study.reduce). Never guessed: a dimension with several coordinates or a level below the unit left unreduced is an error.

None
unit str | None

The unit of independence: a level (default: the outcome's level, or the level its reductions lead to), or an ordered dimension of the outcome to pair on its coordinates within one unit.

None
title str | None

Short label; default built from the roles.

None
description str

The interpretation, in words.

''
status str

"exploratory" (default), "primary" or "secondary".

'exploratory'
specified_at datetime | str | None

When the question was asked (default: now, UTC).

None

design property

What is compared: paired or independent, groups, pairs, unit, n (see Design).

data property

The analysis table: unit id columns, the factor by, the along coordinate (if any) and the outcome; one row per observed value, after subset and reduce. A copy: changing it changes nothing.

data_key property

Fingerprint of data and of the definitions it was read from (see fingerprint.data_key).

pair_data(a, b)

The complete cases of the pair a - b. Paired designs: one row per unit with both values, in columns a and b (with along: one row per unit and coordinate, keeping units observed in both groups at every coordinate). Independent designs: the rows of the two groups, in long form (with along, of the units observed at every coordinate).

estimate(name, ci='analytic', confidence=0.95, n_boot=2000, seed=0, interval='bca', replicates=None, assume_independent=False)

How large? Computes estimator name (see synaptome.estimates.ESTIMATORS) per group or per pair, with a confidence interval. Refuses an estimator the design doesn't suit, naming those that do.

Parameters:

Name Type Description Default
name str

Per group: mean, median, proportion. Per pair (a - b): mean_diff, hodges_lehmann, prop_diff, odds_ratio, cohens_d, hedges_g, cohens_dz, hedges_gz, rank_biserial, cles.

required
ci str | None

"analytic" (default; an error names the alternative when the estimator has none), "bootstrap", or None (the estimate alone).

'analytic'
confidence float

Coverage of the interval (default 0.95).

0.95
n_boot int

Bootstrap resamples (default 2000).

2000
seed int | None

Bootstrap seed (default 0, so the same call gives the same interval); recorded in the result.

0
interval str

Bootstrap interval: "bca" (default), "percentile" or "basic".

'bca'
replicates str | bool | None

Upstream bootstrap draws to read instead of resampling: default, the outcome's replicate variable if it has exactly one; a name picks one; False resamples.

None
assume_independent bool

Only with an ordered unit (unit="time"): treat the positions as independent observations, allowing every interval (default False: only mean / mean_diff, which use the effective sample size, and no bootstrap). Recorded in the result.

False

Returns:

Type Description
EstimateResult

test(name, alternative='two-sided', offset=None, effect_sizes=None, correction=None, alpha=0.05, assume_independent=False, **params)

Is there a difference? Runs test name (see synaptome.hypotheses.TESTS) with effect sizes and corrected p values. Refuses a test the design doesn't suit, naming those that do.

Parameters:

Name Type Description Default
name str

Two groups, once per pair of the question on its own complete cases: paired wilcoxon, paired_ttest, ttest_ess (ordered unit), mcnemar (binary); independent mannwhitneyu, ttest, welch; either bootstrap. All groups at once (omnibus): paired friedman (3+), rm_anova; independent kruskal, anova. Curves (compare_along): cluster_permutation. Normality (distribution per group; compare: per pair of differences when paired, per group when independent): shapiro, normaltest.

required
alternative str

"two-sided" (default), "greater" (H1: a - b > offset) or "less". Pairwise and cluster tests only.

'two-sided'
offset float | None

The difference tested against (default 0; for bootstrap, the estimator's null: 0, 0.5 for CLES, 1 for an odds ratio). offset=0.1, alternative="greater": does a beat b by more than 0.1? Effect sizes always describe the raw difference; only the p value moves.

None
effect_sizes str | tuple[str, ...] | list[str] | None

Pair (or, for omnibus tests, omnibus) estimators to report, each with its analytic interval when it has one (coverage 1 - alpha). Default: the test's own (wilcoxon: rank-biserial and Hodges-Lehmann; t-tests: mean difference and Hedges' g / gz; mcnemar: odds ratio; friedman: Kendall's W; rm_anova: partial eta-squared; kruskal: epsilon-squared; anova: eta-squared), those the design allows. (): none. They equal q.estimate(name).

None
correction str | None

Across the result's rows: "holm", "bonferroni", "fdr_bh", "fdr_by", "none". Default: "holm" (nothing changes with one row), "none" for normality tests (a diagnostic); cluster tests pool every cluster (result.correct(family="pair") for one p per pair). Across several results: synaptome.correct.

None
alpha float

Threshold of significant (default 0.05).

0.05
assume_independent bool

Only with an ordered unit (unit="time"): treat the positions as independent observations. Refused by default, since serially correlated observations make p values too small; recorded in the result. ttest_ess needs no override.

False
**params Any

Test parameters: zero_method (wilcoxon: "wilcox", "pratt", "zsplit"); estimator (default "mean_diff"), n_boot, seed, interval, replicates (bootstrap, as in estimate); n_permutations (1000), cluster_alpha (0.05, sets the cluster-forming threshold), seed (0) (cluster_permutation).

{}

Returns:

Type Description
PairwiseResult | OmnibusResult | ClusterResult | NormalityResult

describe()

Summary statistics of the outcome, per group (and per along coordinate), from q.data: n, missing, mean, sd, median, q1, q3, min, max for interval or ratio values; no mean or SD for ordinal ones; proportion for binary ones.

Returns:

Type Description
DescribeResult

to_dict()

The question as JSON-safe data (no values), to record it. Rebuild with study.question(spec).

from_dict(spec, study) classmethod

Rebuilds a question from to_dict output on study.

Compare

Bases: Question

An outcome compared across the levels of a factor: study.compare(outcome, by=...).

Whether the comparison is paired or independent is read from where by lives: a dimension of the outcome, or a variable at a level crossed with the unit, varies within each unit (paired); a variable at the unit's level is constant within it (independent).

Its roles, besides those of every Question (outcome, subset, reduce, unit, title, ...):

  • by (str): the factor, a dimension of the outcome or a categorical variable.
  • reference (optional): a level of by that every other level is compared with; pairs are then (reference, other).
  • pairs (list of (a, b), optional): the pairs to compare, each as a - b. Default: every pair. Give reference or pairs, not both.
  • higher_is_better (bool, optional): orients answers (results say "positive = reference better"). Default True.

CompareAlong

Bases: Compare

Curves compared across the levels of a factor along an ordered dimension kept unreduced: study.compare_along(outcome, by=..., along=...). Answered by cluster permutation and by estimates per coordinate.

Its roles are those of Compare (by, reference, pairs, higher_is_better) and:

  • along (str): an ordered dimension of the outcome (ordinal, interval or ratio scale), kept unreduced.

Design dataclass

What a question compares, once resolved on a study. Built by the question (q.design); read-only.

Attributes:

Name Type Description
kind str

"compare", "compare_along" or "distribution".

outcome str

The variable compared.

by str | None

The factor compared across (None for a distribution without groups).

factor_role str | None

Where by lives: "dimension" (a dimension of the outcome: a within-unit factor), "between" (a variable at the unit's level: a between-unit factor) or "crossed" (a variable at a level crossed with the unit: a within-unit factor).

by_level str | None

The level of by when it is a variable.

paired bool | None

True when by varies within the unit (paired / repeated measures), False when it is constant within it (independent samples); None for a distribution.

groups tuple

The levels of by present after subset, in order.

reference Any

The reference group, if any.

pairs tuple[tuple[Any, Any], ...]

The pairs compared; (a, b) means a - b.

higher_is_better bool | None
unit str

The unit of independence: a level, or a dimension of the outcome (unit_is_dimension).

unit_is_dimension bool
ordered_unit bool

True when the observations are positions along an ordered dimension (serially correlated).

n_units int

Units with at least one observed value.

n_per_group dict

Units with an observed value, per group.

n_per_pair dict

Units in the pair's complete cases: paired, units with both values; independent, units in either group. With along, only units observed at every coordinate.

along str | None

The ordered dimension kept unreduced (compare_along).

along_coordinates tuple
scale str

The scale of the analysed values: the outcome's, or the one its reductions give (a count or a proportion of booleans is ratio, a slope interval). scale_assumed is True when the outcome's scale wasn't declared (numbers are taken as interval).

scale_assumed bool
binary bool

True when the analysed values are binary: a bool outcome (not reduced to proportions), or a category with two categories.

replicates tuple[str, ...]

Replicate variables of the outcome (upstream bootstrap or jackknife draws).

k property

Number of groups.

factor_description()

by in statistical words, e.g. "a within-unit factor (dimension of 'score')".

Distribution

Bases: Question

How an outcome is distributed, overall or per level of a factor: study.distribution(outcome, by=None). Answered by descriptive statistics and normality tests.

Its role, besides those of every Question:

  • by (str, optional): a factor (a dimension of the outcome or a categorical variable) to describe each level separately.

question_from_dict(spec, study)

Rebuilds a question of any kind from Question.to_dict output, on study.

data_key(table, definitions)

A hash of an analysis table and of the definitions it was read from.

The table is sorted on all its columns first, so row order never matters. Definitions contribute only their meaning-bearing fields (definition_fingerprint): editing a description leaves the key unchanged; changing a range, a scale or a category does not.

Parameters:

Name Type Description Default
table DataFrame
required
definitions Iterable[Variable]
required

Returns:

Type Description
str

"v1:<sha256 hex>".