04 · Selecting and reshaping¶
You will learn how to read a study back as DataFrames, and how to keep part of it: some variables, some units, some coordinates. You will also rename variables and combine studies.
You need 03 · Adding values.
import pandas as pd
import synaptome as syn
We use a small toy study (tutorial 07 presents them all): six subjects in two groups, weighed every two weeks from week 0 to week 12.
study = syn.datasets.longitudinal(n_per_group=3, missing=0.0, seed=1)
study
Study('Toy longitudinal study', completed; levels: subject(6); variables: 2)
Views¶
to_df gives one variable as a long table. with_=True joins every subject-level variable at once.
study.to_df("weight", with_=True).head()
| subject | week | weight | group | |
|---|---|---|---|---|
| 0 | s01 | 0 | 80.991467 | control |
| 1 | s01 | 2 | 81.457620 | control |
| 2 | s01 | 4 | 81.031020 | control |
| 3 | s01 | 6 | 82.004595 | control |
| 4 | s01 | 8 | 81.338080 | control |
table gives a wide table with one row per unit, for the variables without dimensions:
study.table("subject")
| subject | group | |
|---|---|---|
| 0 | s01 | control |
| 1 | s02 | control |
| 2 | s03 | control |
| 3 | s04 | treated |
| 4 | s05 | treated |
| 5 | s06 | treated |
Taking part of a study¶
select returns a new study; the original is never changed. Keep units by id, by the value of a
subject variable, or keep coordinates along a dimension (slice bounds are inclusive):
treated = study.select(where={"group": "treated"})
treated.table("subject")
| subject | group | |
|---|---|---|
| 0 | s04 | treated |
| 1 | s05 | treated |
| 2 | s06 | treated |
early = study.select(coords={"week": slice(0, 6)})
early.to_df("weight").head()
| subject | week | weight | |
|---|---|---|---|
| 0 | s01 | 0 | 80.991467 |
| 1 | s01 | 2 | 81.457620 |
| 2 | s01 | 4 | 81.031020 |
| 3 | s01 | 6 | 82.004595 |
| 4 | s02 | 0 | 83.850898 |
two = study.select(variables=["weight", "group"], units={"subject": ["s01", "s04"]})
two.describe()
| variable | level | dims | dtype | measurement_unit | units_with_values | units_at_level | values | missing | inferred | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | group | subject | category | NaN | 2 | 2 | 2 | 0 | False | |
| 1 | weight | subject | week(7) | float | kg | 2 | 2 | 14 | 0 | False |
drop is the opposite of select:
study.drop(variables="group").describe()
| variable | level | dims | dtype | measurement_unit | units_with_values | units_at_level | values | missing | inferred | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | weight | subject | week(7) | float | kg | 6 | 6 | 42 | 0 | False |
study.describe()
| variable | level | dims | dtype | measurement_unit | units_with_values | units_at_level | values | missing | inferred | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | group | subject | category | NaN | 6 | 6 | 6 | 0 | False | |
| 1 | weight | subject | week(7) | float | kg | 6 | 6 | 42 | 0 | False |
rename changes variable or dimension names, again in a new study:
renamed = study.rename(variables={"weight": "body_mass"}, dims={"week": "time"})
renamed.to_df("body_mass").head(3)
| subject | time | body_mass | |
|---|---|---|---|
| 0 | s01 | 0 | 80.991467 |
| 1 | s01 | 2 | 81.457620 |
| 2 | s01 | 4 | 81.031020 |
copy gives an independent study you can modify freely.
draft = study.copy()
draft.variables.remove("group")
"group" in study.variables, "group" in draft.variables
(True, False)
Combining studies¶
Studies with the same structure, for instance one per site, combine with syn.concat. Definitions must
agree, and the same value can't appear twice.
site_a = study.select(units={"subject": ["s01", "s02", "s04"]})
site_b = study.select(units={"subject": ["s03", "s05", "s06"]})
both = syn.concat([site_a, site_b], title="Weight trial, both sites")
both
Study('Weight trial, both sites', completed; levels: subject(6); variables: 2)
Reducing before viewing¶
A test compares one number per subject, so curves often have to be summarized first. reduce names
how to collapse a dimension: mean, median, sum, count, slope, auc (area under the curve),
first, last, or ("at", coordinate) for the value at one coordinate. Missing values are skipped.
The weight at week 12, one row per subject:
study.to_df("weight", reduce={"week": ("at", 12)})
| subject | weight | |
|---|---|---|
| 0 | s01 | 80.301669 |
| 1 | s02 | 82.916419 |
| 2 | s03 | 81.712067 |
| 3 | s04 | 65.946187 |
| 4 | s05 | 76.596472 |
| 5 | s06 | 74.386488 |
table puts reduced variables next to the subject variables. Here, each subject's weight slope (kg per
week) beside their group, ready for a group comparison:
study.table("subject", ["group", "weight"], reduce={"week": "slope"})
| subject | group | weight | |
|---|---|---|---|
| 0 | s01 | control | -0.051438 |
| 1 | s02 | control | -0.103526 |
| 2 | s03 | control | 0.034958 |
| 3 | s04 | treated | -0.846853 |
| 4 | s05 | treated | -0.616443 |
| 5 | s06 | treated | -0.684954 |
Repetitions are unit levels (tutorial 02), and they are reduced the same way. Below, each subject did three reaction-time trials. Asking for one value per subject without saying what to do with the trials is refused, because the trials would otherwise count as independent subjects:
rt = syn.Study("Reaction times")
rt.units.define("subject > trial")
rt.add(
"rt",
pd.DataFrame(
{
"subject": ["s01"] * 3 + ["s02"] * 3,
"trial": [1, 2, 3] * 2,
"rt": [0.41, 0.38, 0.45, 0.52, 0.49, 0.55],
}
),
)
try:
rt.table("subject", ["rt"])
except ValueError as err:
print(err)
'rt' is at level 'trial', below 'subject': it has several values per subject. Reduce level(s) ['trial'] (e.g. reduce={'trial': 'mean'}).
/tmp/ipykernel_2646/915932948.py:3: InferenceNotice: Inferred definition for 'rt': rt: float @ trial. Review it with study.variables['rt'], or define the variable explicitly. rt.add(
rt.table("subject", ["rt"], reduce={"trial": "median"})
| subject | rt | |
|---|---|---|
| 0 | s01 | 0.41 |
| 1 | s02 | 0.52 |
Next: 05 · Save and share shows how to document a study and save it for someone else.