01 · Quickstart¶
You will learn how to turn a pandas DataFrame into a synaptome Study, look at what it holds, and save
it as a single file you can send to a collaborator.
You need basic pandas. Nothing else: this is the first tutorial.
We start from data as it often arrives: one table describing the subjects, and one long table of measurements (one row per subject and time point).
import tempfile
from pathlib import Path
import pandas as pd
import synaptome as syn
subjects = pd.DataFrame(
{
"subject": ["s01", "s02", "s03", "s04", "s05", "s06"],
"group": ["control", "control", "control", "treated", "treated", "treated"],
"age": [34, 41, 29, 38, 45, 31],
}
)
weights = pd.DataFrame(
{
"subject": ["s01"] * 3 + ["s02"] * 3 + ["s03"] * 3 + ["s04"] * 3 + ["s05"] * 3 + ["s06"] * 3,
"time": [0, 6, 12] * 6,
"weight": [
82.1,
81.9,
82.3,
75.4,
75.0,
75.6,
90.2,
89.8,
90.5,
79.0,
77.2,
75.9,
85.3,
83.0,
81.4,
72.8,
71.1,
70.2,
],
}
)
weights.head()
| subject | time | weight | |
|---|---|---|---|
| 0 | s01 | 0 | 82.1 |
| 1 | s01 | 6 | 81.9 |
| 2 | s01 | 12 | 82.3 |
| 3 | s02 | 0 | 75.4 |
| 4 | s02 | 6 | 75.0 |
A study starts empty. add_units registers the subjects: the subject column gives their ids, and every
other column (group, age) becomes a variable describing each subject.
study = syn.Study("Weight trial")
study.add_units("subject", subjects)
/tmp/ipykernel_2557/4059168707.py:2: InferenceNotice: Inferred definitions for subject variables: group: category @ subject; age: int @ subject. Review them with study.variables, or define them explicitly.
study.add_units("subject", subjects)
6
add stores the measurements. synaptome recognises the subject column as the unit ids and treats the
remaining column, time, as a dimension of weight. Because we never described weight, synaptome
guesses its definition and tells you what it guessed (the InferenceNotice below). Tutorial 02 shows how
to write definitions yourself. add returns the definition, printed here in short form.
weight = study.add("weight", weights)
print(weight)
weight[time]: float @ subject
/tmp/ipykernel_2557/2131077891.py:1: InferenceNotice: Inferred definition for 'weight': weight[time]: float @ subject. Review it with study.variables['weight'], or define the variable explicitly.
weight = study.add("weight", weights)
describe gives one row per variable: where it lives, its shape, and how complete it is.
study.describe()
| variable | level | dims | dtype | measurement_unit | units_with_values | units_at_level | values | missing | inferred | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | group | subject | category | None | 6 | 6 | 6 | 0 | True | |
| 1 | age | subject | int | None | 6 | 6 | 6 | 0 | True | |
| 2 | weight | subject | time(3) | float | None | 6 | 6 | 18 | 0 | True |
to_df gives a variable back as a long DataFrame, ready for pandas or plotting. with_ joins
subject-level variables onto it, so each measurement carries its subject's group.
study.to_df("weight", with_=["group"]).head(6)
| subject | time | weight | group | |
|---|---|---|---|---|
| 0 | s01 | 0 | 82.1 | control |
| 1 | s01 | 6 | 81.9 | control |
| 2 | s01 | 12 | 82.3 | control |
| 3 | s02 | 0 | 75.4 | control |
| 4 | s02 | 6 | 75.0 | control |
| 5 | s02 | 12 | 75.6 | control |
A first question¶
A question says what to compare, on which data. Here: does the weight slope (kg per week) differ between
groups? reduce turns each subject's curve into its slope first. No test is run yet; the question
tells you what would be compared:
q = study.compare("weight", by="group", reduce={"time": "slope"})
q.design
Design: compare 'weight' by 'group' (independent) factor: 'group', a between-subject factor (a variable at level 'subject'); 2 levels: control, treated unit: subject, 6 observed pairs: control - treated (n=6) outcome: 'weight', interval scale
q.data
| subject | group | weight | |
|---|---|---|---|
| 0 | s01 | control | 0.016667 |
| 1 | s02 | control | 0.016667 |
| 2 | s03 | control | 0.025000 |
| 3 | s04 | treated | -0.258333 |
| 4 | s05 | treated | -0.325000 |
| 5 | s06 | treated | -0.216667 |
Asking for a test gives the answer: a Welch t-test of the slopes, with the mean difference and its interval. synaptome picked nothing for you: the question said what to compare, and the test is named.
q.test("welch").table
| a | b | statistic | df | p | p_corrected | significant | n_a | n_b | mean_diff | mean_diff_ci_low | mean_diff_ci_high | hedges_g | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | control | treated | 9.033698 | 2.031006 | 0.011464 | 0.011464 | True | 3 | 3 | 0.286111 | 0.151814 | 0.420408 | 5.885183 |
Every result draws itself with plot(): here the slope of each subject per group, with a bracket
when the test finds a difference.
fig, ax = q.test("welch").plot()
Saving to a path ending in .syn writes one file holding everything: units, definitions and values.
syn.load reads it back. (We save to a temporary folder so this tutorial leaves nothing behind.)
tmp = tempfile.TemporaryDirectory()
path = study.save(Path(tmp.name) / "weight_trial.syn")
again = syn.load(path)
again
Study('Weight trial'; levels: subject(6); variables: 3)
again.to_df("weight").equals(study.to_df("weight"))
True
tmp.cleanup()
Next: 02 · Units and variables explains how to describe the structure of a study: who was measured, how units nest, and what each variable means. Questions are covered in 08 · Questions, estimates in 09 and tests in 10 · Tests and corrections.