Skip to content

Long-Toolkit Your Hub, via API or CLI, of Toolkits for Longitudinal Data

Longitudinal studies invest heavily in collecting and curating their data, yet machine learning is either left aside or applied as if every wave were an unrelated feature. We built Long-Toolkit so that experts of longitudinal studies can get their data ready, and measure fairly whether conventional or longitudinal machine learning is worth it.

Simon Provost, Postdoctoral Research @ McGill University (MEDomicsLab) & Dr Alex Kwong, LEAP lab (University of Edinburgh)

Four toolkits, one workflow

We need more tools and primitives: come help us decide which ones
  1. Generation of synthetic longitudinal cohorts with known trends, shocks or missingness, or conversion of your own study files between CSV, Parquet and Stata.

    Open the toolkit
  2. Cleaning, imputation, category harmonisation, and long to wide reshaping of repeated measures.

    Open the toolkit
  3. Standard and longitudinal estimators ranked all against all, under one shared protocol; or train a single estimator out of conventional or longitudinal-data-aware ones.

    Open the toolkit
  4. The predictions of a saved model, e.g., the best estimator of the benchmark, explained with SHAP values.

    Open the toolkit
prepare.py
from long_toolkit.data_preparation import (
    StataToCsv, TrendPatterns,
)

# (1) Generate a synthetic cohort, in long format
TrendPatterns(
    n_samples=300, n_waves=4, random_state=7
).prepare().to_csv("data/cohort_long.csv", index=False)

# (2) Or convert your own study from Stata to CSV
StataToCsv().prepare(
    input_path="data/study.dta",
    output_path="data/study.csv",
)
from long_toolkit.data_preprocessing import PivotLongToWide

# One row per participant: mood_score_w1 to _w4
PivotLongToWide().fit_preprocess(
    input_path="data/cohort_long.csv",
    output_path="data/cohort_wide.csv",
    id_cols=["subject_id"], time_col="wave",
    longitudinal_columns=["mood_score"],
    time_label="w", separator="_",
)
# Then add the outcome to predict, e.g., outcome_class
from long_toolkit.machine_learning import (
    BenchStandardAndLongitudinalML,
)

# Every estimator ranked against every other one
bench = BenchStandardAndLongitudinalML().fit_predict(
    technique="run_benchmark",
    input_path="data/cohort_wide.csv",
    target_column="outcome_class",
    feature_columns="mood_score_w1,mood_score_w2,mood_score_w3",
    feature_groups_mode="suffix",
    feature_groups_suffix="_w",
    metric_keys="f1_macro,auroc", n_splits=5,
)
from long_toolkit.explainability import SHAPAnalysis

# The estimator ranked first by the benchmark
best = bench["runs"][0]["ranked_estimators"][0]

SHAPAnalysis().fit_explain(
    model_path=best["model_artifact"],
    input_path="data/cohort_wide.csv",
    target_column="outcome_class",
    feature_columns="mood_score_w1,mood_score_w2,mood_score_w3",
)
# Which waves drive its predictions

In code or in the terminal

Every tool is available in two modes, which run the same Python code and write the same files: in code, through the application programming interface (API), or without code, through the command-line interface (CLI).

Python · API

In code

Import a tool, pass its parameters as keyword arguments, and chain tools in a script or a Jupyter notebook. Each tool reads and writes files on disk, so that any step can be rerun on its own.

impute.py
from long_toolkit.data_preprocessing import (
    MissingImputation,
)

MissingImputation().fit_preprocess(
    technique="median_imputation",
    input_path="data/cohort_clean.csv",
    output_path="data/cohort_imputed.csv",
)
Open the API tutorial
Terminal · CLI

Without code

Launch long-toolkit, open a stage, and fill in the form of any tool, with its defaults and hints. The command palette (:) finds any tool by name.

~ long-toolkit
Machine Learning
Select one to configure and run.

│ Standard Machine Learning
  Longitudinal Machine Learning
  Benchmark Standard ML
  Benchmark Longitudinal ML

Command Palette
⌁ shap analysis
Open the CLI tutorial

50+ Estimators

one shared protocol

Several non-longitudinal conventional machine learning and longitudinal-data-aware primitives are trained on the same splits, with the same metrics and seed, and ranked all against all: every estimator is compared with every other one, not only with a single baseline such as logistic regression.

Get Started → Long-Toolkit