Skip to contents

This vignette describes the public API of {toad} and the internal design of the package. It is aimed at developers who want to extend toad with a new dataset or reuse its internal machinery.

Public API

The public API consists of two discovery functions, one importing function, and one convenience accessor per bundle.

Dataset and bundle discovery

available_datasets() returns the names of all datasets that can be accessed via import_bundle().

available_datasets()
#> [1] "HCP1065"  "TractSeg"

available_bundles(dataset) returns the full names of all bundles available in a dataset. These names are the values to supply to the bundle argument of import_bundle():

length(available_bundles("HCP1065"))
#> [1] 87
length(available_bundles("TractSeg"))
#> [1] 72

The allowed names are the internal named vectors HCP1065_ACRONYM_LIST (87 entries; full bundle name → short acronym used in the file names) and TractSeg_BUNDLE_LIST (72 entries; full bundle name → TractSeg trk file stem):

head(toad:::HCP1065_ACRONYM_LIST)
#>            Anterior Commissure        Left Arcuate Fasciculus 
#>                           "AC"                         "AF_L" 
#>       Right Arcuate Fasciculus        Left Acoustic Radiation 
#>                         "AF_R"                         "AR_L" 
#>       Right Acoustic Radiation Left Frontal Parietal Cingulum 
#>                         "AR_R"                       "C_FP_L"
head(toad:::TractSeg_BUNDLE_LIST)
#>           Left Arcuate Fasciculus          Right Arcuate Fasciculus 
#>                         "AF_left"                        "AF_right" 
#>  Left Anterior Thalamic Radiation Right Anterior Thalamic Radiation 
#>                        "ATR_left"                       "ATR_right" 
#>               Commissure Anterior                   Corpus Callosum 
#>                              "CA"                              "CC"

import_bundle() — importing bundles

import_bundle(dataset, bundle, subjects = NULL) downloads the requested bundle from GitHub releases and returns a fiber::bundle, or a fiber::bundle_set when several subjects are loaded.

b <- import_bundle("HCP1065", "Anterior Commissure")
class(b)

For the "TractSeg" dataset, subjects controls which subjects are loaded (see the getting-started vignette):

  • NULL — all available subjects (a bundle_set);
  • an integer scalar n — n random subjects without replacement (a bundle_set);
  • a character vector of length > 1 — the bundles for those subject IDs (a bundle_set);
  • a character scalar — the bundle for that single subject (a bundle).

Valid subject IDs for a bundle are discovered with a single GitHub Releases API call (see below); invalid IDs, out-of-range counts, and unsupported types raise errors. For "HCP1065" the subjects argument is ignored.

Convenience accessors

Each bundle also ships an exported convenience function, e.g. HCP1065_CST_L() and TractSeg_AF_left(subjects = NULL). The accessors are named <dataset>_<bundle_identifier>: for HCP1065 the short acronym of the bundle (CST_L for the Left Corticospinal Tract), for TractSeg the trk file stem (AF_left for the Left Arcuate Fasciculus). They are generated by the module in R/TractSeg.R / R/HCP1065.R and simply forward to import_bundle().

Internal functions

The internal (non-exported) API is the small machinery behind import_bundle().

Downloading a release asset

import_bundle() resolves the URL

https://github.com/tractoverse/toad/releases/download/<release>/<asset>.rds

where <release> is hcp1065 for the HCP1065 atlas and tractseg-<bundle_stem> for TractSeg, and <asset> is constructed from the dataset-specific naming convention:

  • HCP1065_<acronym>.rds for HCP1065;
  • TractSeg_<subject>_<bundle_stem>.rds for TractSeg.

The download itself goes through the internal .toad_download_file() wrapper around utils::download.file(). It is a separate function so that tests can mock it: tests replace it in the namespace with a function that copies a local .rds fixture, so the whole download → readRDS() → assemble pipeline is exercised without touching the network.

Discovering TractSeg subjects

For the TractSeg dataset, import_bundle() must know which subject assets exist for a bundle. This is resolved without per-subject URL probing by a single GitHub Releases API call per bundle:

cat(substr(
  toad:::.tractseg_api_url("AF_left"),
  1,
  120
))
#> https://api.github.com/repos/tractoverse/toad/releases/tags/tractseg-AF_left

The pipeline is split into four functions, each unit-testable offline:

  • .tractseg_api_url(bundle_stem) builds the Releases API URL for the tractseg-<bundle_stem> tag;
  • .tractseg_release_json(bundle_stem, api_url = NULL) fetches the JSON payload, returning NULL on any connection/HTTP error (e.g. a release not yet published). The default-URL branch delegates the actual reading to the mockable seam .tractseg_read_release(api_url);
  • .tractseg_release_assets(json_text, bundle_stem) extracts matching RDS asset names from the JSON with a targeted regular expression (deliberately no jsonlite dependency);
  • .tractseg_subject_ids(asset_names, bundle_stem) parses subject IDs from the TractSeg_<subject>_<bundle_stem>.rds asset names.
toad:::.tractseg_subject_ids(
  c(
    "TractSeg_100307_AF_left.rds",
    "TractSeg_100408_AF_left.rds",
    "not-a-subject-file.rds"
  ),
  "AF_left"
)
#> [1] "100307" "100408"

.tractseg_subject_list(bundle_stem) orchestrates the whole discovery, returning a character vector of subject IDs (or character(0) when the release is not yet published).

Error handling without a hard cli dependency

cli is only a suggested package. All error paths therefore go through the internal .toad_abort() helper, which uses cli::cli_abort() when cli is installed (passing the caller’s environment so that {...} interpolation works) and otherwise falls back to .toad_abort_base(), a pure-base-R stop() that applies close-enough {expr} / {.val {expr}} interpolation via .toad_interp(). This keeps the dependency graph light while giving nicely formatted messages in interactive use.

The GitHub-releases data architecture

toad ships no data files. All atlas data are published as GitHub releases in the tractoverse/toad repository:

Dataset Releases Assets per release Total assets
HCP1065 1 (hcp1065) 87 (HCP1065_<acronym>.rds) 87
TractSeg 72 (tractseg-<stem>) 105 (TractSeg_<subject>_<stem>.rds) 7,560

Users download only the assets they need, on demand, through import_bundle(). This keeps the package itself tiny and avoids re-bundling data that already exists elsewhere.

The HCP1065 release assets are generated by data-raw/hcp1065.R. The TractSeg releases are generated by a GitHub Actions workflow (data-raw/tractseg.R and the Python helpers data-raw/zip_index.py / data-raw/fetch_bundle.py) that converts the original Zenodo .trk files into .rds fiber::bundle objects, one release per bundle. Neither data-raw/ nor the release assets are part of the R package build.

Adding a new dataset

To add a new atlas:

  1. Decide on a release strategy: one release for population-averaged atlases (like hcp1065), or one release per bundle for per-subject data (like tractseg-<stem>). Assets must be .rds fiber::bundle objects.
  2. Add a dataset name to available_datasets() and a named bundle vector to R/utils.R (mirroring HCP1065_ACRONYM_LIST / TractSeg_BUNDLE_LIST).
  3. Add a branch in import_bundle() that resolves the release URL from the dataset and bundle, downloads via .toad_download_file(), and assembles the return value.
  4. Generate the exported convenience accessors in R/<dataset>.R and document the dataset with a roxygen block following HCP1065 / TractSeg.
  5. Add tests in inst/tinytest/; the existing test_import_bundle.R, test_TractSeg_bundles.R, and test_utils.R files show how to mock the network seams with assignInNamespace() and local .rds fixtures.

The key design principle is that every network or filesystem seam is a separate function (.toad_download_file(), .tractseg_release_json(), .tractseg_read_release()), so the surrounding logic can be tested hermetically at 100 % coverage.