Getting started programming with pyAFQ#

There are two ways to use pyAFQ: through the command line interface, and by writing Python code. This tutorial will walk you through the basics of the latter, using pyAFQ’s Python Application Programming Interface (API).

import os.path as op

import matplotlib.pyplot as plt
import nibabel as nib
import plotly
import pandas as pd

from AFQ.api.group import GroupAFQ
import AFQ.data.fetch as afd
import AFQ.viz.altair as ava

Example data#

pyAFQ assumes that the data is organized in a BIDS compliant directory. To get users started with this tutorial, we will download some example data and organize it in a BIDS compliant way (for more details on how BIDS is used in pyAFQ, refer to bids_tutorial).

The following call dowloads a dataset that contains a single subject’s high angular resolution diffusion imaging (HARDI) data, collected at the Stanford Vista Lab

Note

See https://purl.stanford.edu/ng782rw8378 for details on dataset.

The data are downloaded and organized locally into a BIDS compliant anatomical data folder (anat) and a diffusion-weighted imaging data (dwi) folder, which are both placed in the user’s home directory under:

``~/AFQ_data/stanford_hardi/``

The data is also placed in a derivatives directory, signifying that it has already undergone the required preprocessing necessary for pyAFQ to run.

The clear_previous_afq is used to remove any previous runs of the afq object stored in the ~/AFQ_data/stanford_hardi/ BIDS directory. Set it to None if you want to use the results of previous runs.

afd.organize_stanford_data(clear_previous_afq="track")

Set tractography parameters (optional)#

We make create a tracking_params variable, which we will pass to the GroupAFQ object which specifies that we want 25,000 seeds randomly distributed in the white matter.

We only do this to make this example faster and consume less space.

tracking_params = dict(n_seeds=25000,
                       random_seeds=True,
                       rng_seed=2022,
                       trx=True)

Initialize a GroupAFQ object:#

Creates a GroupAFQ object, that encapsulates tractometry. This object can be used to manage the entire AFQ pipeline, including:

  • Tractography

  • Registration

  • Segmentation

  • Cleaning

  • Profiling

  • Visualization

This will also create an output folder for the corresponding AFQ derivatives in the AFQ data directory: AFQ_data/stanford_hardi/derivatives/afq/

To initialize this object we will pass in the path location to our BIDS compliant data, the name of the preprocessing pipeline we want to use, and the tracking parameters we defined above. We will also specify the visualization backend we want to use (see below for more details). We will also be using plotly to generate an interactive visualization. The value plotly_no_gif indicates that interactive visualizations will be generated as html web-pages that can be opened in a browser, but not as static gif files.

myafq = GroupAFQ(
    bids_path=op.join(afd.afq_home, 'stanford_hardi'),
    preproc_pipeline='vistasoft',
    tracking_params=tracking_params,
    viz_backend_spec='plotly_no_gif')

Calculating DTI FA (Diffusion Tensor Imaging Fractional Anisotropy)#

The GroupAFQ object has a method called export, which allows the user to calculate various derived quantities from the data.

For example, FA can be computed using the DTI model, by explicitly calling myafq.export(“dti_fa”). This triggers the computation of DTI parameters for all subjects in the dataset, and stores the results in the AFQ derivatives directory. In addition, it calculates the FA from these parameters and stores it in a different file in the same directory.

Note

The AFQ API computes quantities lazily. This means that DTI parameters are not computed until they are required. This means that the first line below is the one that requires time.

The result of the call to export is a dictionary, with the subject IDs as keys, and the filenames of the corresponding files as values. This means that to extract the filename corresponding to the FA of the first subject, we can do:

FA_fname = myafq.export("dti_fa")["01"]

# We will then use `nibabel` to load the deriviative file and retrieve the
# data array.

FA_img = nib.load(FA_fname)
FA = FA_img.get_fdata()

Visualize the result with Matplotlib#

At this point FA is an array, and we can use standard Python tools to visualize it or perform additional computations with it.

In this case we are going to take an axial slice halfway through the FA data array and plot using a sequential color map.

Note

The data array is structured as a xyz coordinate system.

fig, ax = plt.subplots(1)
ax.matshow(FA[:, :, FA.shape[-1] // 2], cmap='viridis')
ax.axis("off")
plot 001 afq api
(-0.5, 105.5, 80.5, -0.5)

Recognizing the bundles and calculating act profiles:#

Typically, users of pyAFQ are interested in calculating not only an overall map of the FA, but also the major white matter pathways (or bundles) and tract profiles of tissue properties along their length. To trigger the pyAFQ pipeline that calculates the profiles, users can call the export(‘profiles’) method:

Note

Running the code below triggers the full pipeline of operations leading to the computation of the tract profiles. Therefore, it takes a little while to run (about 40 minutes, typically).

myafq.export('profiles')
0it [00:00, ?it/s]
21it [00:00, 194.79it/s]
45it [00:00, 219.83it/s]
70it [00:00, 228.75it/s]
99it [00:00, 250.58it/s]
127it [00:00, 259.24it/s]
153it [00:00, 218.52it/s]
177it [00:00, 223.40it/s]
203it [00:00, 233.73it/s]
227it [00:00, 233.14it/s]
251it [00:01, 225.99it/s]
274it [00:01, 226.01it/s]
298it [00:01, 229.20it/s]
322it [00:01, 224.07it/s]
347it [00:01, 228.34it/s]
370it [00:01, 116.12it/s]
395it [00:02, 138.66it/s]
421it [00:02, 161.26it/s]
443it [00:02, 171.05it/s]
465it [00:02, 181.85it/s]
487it [00:02, 187.19it/s]
508it [00:02, 191.15it/s]
530it [00:02, 196.43it/s]
555it [00:02, 210.57it/s]
579it [00:02, 217.76it/s]
602it [00:02, 220.50it/s]
628it [00:03, 230.51it/s]
652it [00:03, 214.07it/s]
677it [00:03, 220.57it/s]
702it [00:03, 228.23it/s]
727it [00:03, 232.94it/s]
754it [00:03, 242.67it/s]
779it [00:03, 231.00it/s]
806it [00:03, 241.52it/s]
831it [00:03, 240.14it/s]
857it [00:04, 245.78it/s]
883it [00:04, 247.01it/s]
908it [00:04, 235.85it/s]
932it [00:04, 232.34it/s]
959it [00:04, 240.64it/s]
984it [00:04, 237.16it/s]
1008it [00:04, 219.97it/s]
1032it [00:04, 224.21it/s]
1057it [00:04, 229.22it/s]
1081it [00:05, 227.39it/s]
1106it [00:05, 230.84it/s]
1130it [00:05, 228.17it/s]
1153it [00:05, 211.44it/s]
1178it [00:05, 218.97it/s]
1201it [00:05, 221.85it/s]
1227it [00:05, 232.13it/s]
1251it [00:05, 232.26it/s]
1275it [00:06, 137.54it/s]
1299it [00:06, 156.97it/s]
1323it [00:06, 172.52it/s]
1345it [00:06, 181.46it/s]
1369it [00:06, 195.15it/s]
1395it [00:06, 211.74it/s]
1418it [00:06, 206.18it/s]
1440it [00:06, 204.91it/s]
1464it [00:06, 213.45it/s]
1488it [00:07, 213.11it/s]
1510it [00:07, 201.98it/s]
1535it [00:07, 214.33it/s]
1557it [00:07, 203.32it/s]
1583it [00:07, 216.59it/s]
1608it [00:07, 225.81it/s]
1635it [00:07, 235.83it/s]
1659it [00:07, 233.37it/s]
1683it [00:07, 232.17it/s]
1707it [00:08, 227.39it/s]
1730it [00:08, 227.17it/s]
1754it [00:08, 229.35it/s]
1781it [00:08, 239.17it/s]
1806it [00:08, 238.54it/s]
1830it [00:08, 236.13it/s]
1854it [00:08, 235.65it/s]
1878it [00:08, 222.41it/s]
1901it [00:08, 220.37it/s]
1924it [00:09, 219.58it/s]
1951it [00:09, 231.92it/s]
1975it [00:09, 232.31it/s]
1999it [00:09, 222.97it/s]
2025it [00:09, 230.97it/s]
2049it [00:09, 231.50it/s]
2073it [00:09, 230.34it/s]
2097it [00:09, 231.19it/s]
2121it [00:09, 232.11it/s]
2145it [00:09, 233.77it/s]
2169it [00:10, 217.92it/s]
2192it [00:10, 212.45it/s]
2218it [00:10, 224.60it/s]
2241it [00:10, 199.50it/s]
2264it [00:10, 205.80it/s]
2286it [00:10, 207.31it/s]
2314it [00:10, 227.40it/s]
2338it [00:10, 215.68it/s]
2360it [00:10, 216.35it/s]
2382it [00:11, 122.50it/s]
2405it [00:11, 139.66it/s]
2434it [00:11, 169.65it/s]
2456it [00:11, 179.46it/s]
2482it [00:11, 197.41it/s]
2507it [00:11, 210.12it/s]
2531it [00:11, 212.14it/s]
2557it [00:12, 219.79it/s]
2583it [00:12, 230.00it/s]
2607it [00:12, 216.43it/s]
2633it [00:12, 225.52it/s]
2657it [00:12, 209.65it/s]
2682it [00:12, 219.55it/s]
2708it [00:12, 229.69it/s]
2732it [00:12, 230.85it/s]
2756it [00:12, 227.72it/s]
2780it [00:13, 228.10it/s]
2803it [00:13, 218.27it/s]
2829it [00:13, 228.59it/s]
2856it [00:13, 239.68it/s]
2881it [00:13, 234.38it/s]
2908it [00:13, 244.05it/s]
2933it [00:13, 242.73it/s]
2958it [00:13, 241.37it/s]
2983it [00:13, 232.82it/s]
3007it [00:14, 213.70it/s]
3030it [00:14, 217.04it/s]
3055it [00:14, 224.10it/s]
3080it [00:14, 230.48it/s]
3104it [00:14, 233.10it/s]
3130it [00:14, 239.55it/s]
3157it [00:14, 247.30it/s]
3184it [00:14, 253.32it/s]
3210it [00:14, 241.66it/s]
3235it [00:15, 239.66it/s]
3260it [00:15, 231.34it/s]
3284it [00:15, 229.47it/s]
3308it [00:15, 228.16it/s]
3331it [00:15, 221.90it/s]
3355it [00:15, 226.73it/s]
3378it [00:15, 223.42it/s]
3401it [00:15, 217.39it/s]
3425it [00:15, 220.87it/s]
3454it [00:15, 237.34it/s]
3478it [00:16, 230.18it/s]
3502it [00:16, 219.78it/s]
3527it [00:16, 226.62it/s]
3550it [00:16, 222.55it/s]
3573it [00:16, 221.59it/s]
3596it [00:16, 220.17it/s]
3625it [00:16, 238.55it/s]
3651it [00:16, 244.68it/s]
3676it [00:16, 234.59it/s]
3700it [00:17, 230.94it/s]
3724it [00:17, 225.52it/s]
3747it [00:17, 222.37it/s]
3771it [00:17, 224.42it/s]
3795it [00:17, 227.80it/s]
3818it [00:17, 120.87it/s]
3842it [00:18, 141.88it/s]
3863it [00:18, 154.79it/s]
3885it [00:18, 167.89it/s]
3910it [00:18, 187.46it/s]
3935it [00:18, 201.30it/s]
3958it [00:18, 208.43it/s]
3983it [00:18, 218.94it/s]
4007it [00:18, 221.04it/s]
4030it [00:18, 221.39it/s]
4053it [00:18, 217.44it/s]
4076it [00:19, 217.92it/s]
4099it [00:19, 215.64it/s]
4121it [00:19, 208.81it/s]
4144it [00:19, 214.46it/s]
4166it [00:19, 210.08it/s]
4189it [00:19, 213.00it/s]
4214it [00:19, 220.85it/s]
4237it [00:19, 218.71it/s]
4259it [00:19, 204.70it/s]
4280it [00:20, 205.70it/s]
4306it [00:20, 216.28it/s]
4331it [00:20, 224.94it/s]
4355it [00:20, 226.82it/s]
4378it [00:20, 222.36it/s]
4404it [00:20, 232.74it/s]
4428it [00:20, 223.21it/s]
4455it [00:20, 232.19it/s]
4484it [00:20, 244.62it/s]
4512it [00:20, 254.37it/s]
4538it [00:21, 250.37it/s]
4566it [00:21, 257.58it/s]
4592it [00:21, 238.02it/s]
4617it [00:21, 238.95it/s]
4642it [00:21, 224.61it/s]
4667it [00:21, 230.26it/s]
4693it [00:21, 233.53it/s]
4717it [00:21, 232.19it/s]
4741it [00:21, 227.90it/s]
4764it [00:22, 218.92it/s]
4791it [00:22, 232.35it/s]
4815it [00:22, 229.09it/s]
4839it [00:22, 217.77it/s]
4867it [00:22, 230.75it/s]
4891it [00:22, 227.07it/s]
4914it [00:22, 227.51it/s]
4937it [00:22, 224.44it/s]
4963it [00:22, 231.76it/s]
4987it [00:23, 225.67it/s]
5012it [00:23, 230.67it/s]
5036it [00:23, 223.93it/s]
5059it [00:23, 219.00it/s]
5082it [00:23, 220.32it/s]
5105it [00:23, 221.51it/s]
5130it [00:23, 229.39it/s]
5157it [00:23, 240.37it/s]
5182it [00:23, 241.14it/s]
5208it [00:24, 245.29it/s]
5234it [00:24, 248.02it/s]
5259it [00:24, 239.79it/s]
5286it [00:24, 247.23it/s]
5311it [00:24, 238.02it/s]
5335it [00:24, 236.55it/s]
5363it [00:24, 248.21it/s]
5388it [00:24, 242.02it/s]
5413it [00:24, 228.41it/s]
5438it [00:24, 231.45it/s]
5466it [00:25, 244.79it/s]
5492it [00:25, 247.71it/s]
5518it [00:25, 250.35it/s]
5544it [00:25, 234.88it/s]
5568it [00:25, 233.44it/s]
5593it [00:25, 125.48it/s]
5619it [00:26, 146.41it/s]
5639it [00:26, 153.98it/s]
5662it [00:26, 169.24it/s]
5687it [00:26, 188.17it/s]
5714it [00:26, 207.85it/s]
5738it [00:26, 213.11it/s]
5762it [00:26, 217.63it/s]
5785it [00:26, 220.29it/s]
5808it [00:26, 210.94it/s]
5830it [00:26, 208.82it/s]
5852it [00:27, 211.15it/s]
5879it [00:27, 227.38it/s]
5903it [00:27, 226.21it/s]
5931it [00:27, 238.32it/s]
5956it [00:27, 225.81it/s]
5979it [00:27, 202.16it/s]
6004it [00:27, 208.58it/s]
6026it [00:27, 210.61it/s]
6048it [00:28, 206.03it/s]
6069it [00:28, 206.07it/s]
6094it [00:28, 217.15it/s]
6121it [00:28, 231.56it/s]
6146it [00:28, 230.22it/s]
6170it [00:28, 231.51it/s]
6194it [00:28, 220.20it/s]
6219it [00:28, 226.70it/s]
6242it [00:28, 225.78it/s]
6268it [00:28, 233.07it/s]
6293it [00:29, 235.55it/s]
6317it [00:29, 217.42it/s]
6341it [00:29, 221.35it/s]
6364it [00:29, 216.16it/s]
6388it [00:29, 222.65it/s]
6411it [00:29, 217.96it/s]
6434it [00:29, 218.77it/s]
6456it [00:29, 217.52it/s]
6478it [00:29, 214.75it/s]
6501it [00:30, 217.88it/s]
6523it [00:30, 213.89it/s]
6545it [00:30, 213.73it/s]
6569it [00:30, 219.22it/s]
6593it [00:30, 223.92it/s]
6616it [00:30, 204.51it/s]
6639it [00:30, 208.67it/s]
6664it [00:30, 218.35it/s]
6687it [00:30, 212.45it/s]
6711it [00:31, 216.98it/s]
6733it [00:31, 213.50it/s]
6755it [00:31, 209.50it/s]
6781it [00:31, 223.58it/s]
6805it [00:31, 227.73it/s]
6831it [00:31, 236.82it/s]
6859it [00:31, 249.25it/s]
6885it [00:31, 246.14it/s]
6910it [00:31, 230.10it/s]
6934it [00:31, 232.83it/s]
6960it [00:32, 237.03it/s]
6984it [00:32, 231.80it/s]
7008it [00:32, 224.08it/s]
7033it [00:32, 230.64it/s]
7058it [00:32, 234.23it/s]
7082it [00:32, 230.86it/s]
7106it [00:32, 220.00it/s]
7131it [00:32, 224.63it/s]
7154it [00:32, 224.64it/s]
7177it [00:33, 204.79it/s]
7202it [00:33, 216.08it/s]
7231it [00:33, 233.13it/s]
7255it [00:33, 223.58it/s]
7280it [00:33, 229.18it/s]
7307it [00:33, 240.62it/s]
7332it [00:33, 240.91it/s]
7357it [00:33, 241.87it/s]
7384it [00:33, 248.41it/s]
7410it [00:34, 249.72it/s]
7436it [00:34, 235.35it/s]
7460it [00:34, 234.29it/s]
7484it [00:34, 233.38it/s]
7509it [00:34, 236.25it/s]
7533it [00:34, 235.97it/s]
7557it [00:34, 228.35it/s]
7580it [00:34, 219.39it/s]
7605it [00:34, 227.87it/s]
7630it [00:34, 224.77it/s]
7655it [00:35, 231.86it/s]
7679it [00:35, 227.08it/s]
7702it [00:35, 214.56it/s]
7724it [00:35, 207.11it/s]
7752it [00:35, 222.27it/s]
7775it [00:35, 218.12it/s]
7797it [00:36, 108.41it/s]
7824it [00:36, 133.05it/s]
7847it [00:36, 151.13it/s]
7871it [00:36, 169.72it/s]
7898it [00:36, 192.31it/s]
7921it [00:36, 196.96it/s]
7944it [00:36, 190.44it/s]
7965it [00:36, 190.05it/s]
7992it [00:36, 207.97it/s]
8015it [00:37, 212.90it/s]
8038it [00:37, 213.21it/s]
8062it [00:37, 219.07it/s]
8085it [00:37, 219.49it/s]
8112it [00:37, 231.33it/s]
8139it [00:37, 241.15it/s]
8164it [00:37, 235.18it/s]
8188it [00:37, 226.32it/s]
8211it [00:37, 225.78it/s]
8236it [00:38, 228.87it/s]
8263it [00:38, 239.18it/s]
8288it [00:38, 229.52it/s]
8312it [00:38, 225.82it/s]
8340it [00:38, 239.76it/s]
8365it [00:38, 240.73it/s]
8393it [00:38, 250.27it/s]
8420it [00:38, 253.11it/s]
8448it [00:38, 259.96it/s]
8475it [00:39, 250.99it/s]
8501it [00:39, 247.40it/s]
8526it [00:39, 242.77it/s]
8551it [00:39, 232.35it/s]
8578it [00:39, 240.29it/s]
8603it [00:39, 233.07it/s]
8629it [00:39, 239.28it/s]
8657it [00:39, 249.37it/s]
8683it [00:39, 239.71it/s]
8708it [00:39, 234.75it/s]
8732it [00:40, 229.98it/s]
8756it [00:40, 223.93it/s]
8779it [00:40, 209.11it/s]
8801it [00:40, 207.63it/s]
8825it [00:40, 215.24it/s]
8847it [00:40, 216.08it/s]
8873it [00:40, 227.54it/s]
8896it [00:40, 215.28it/s]
8921it [00:40, 223.10it/s]
8945it [00:41, 224.59it/s]
8968it [00:41, 223.11it/s]
8992it [00:41, 226.97it/s]
9017it [00:41, 232.75it/s]
9043it [00:41, 240.32it/s]
9068it [00:41, 235.49it/s]
9092it [00:41, 224.25it/s]
9116it [00:41, 226.92it/s]
9139it [00:41, 217.33it/s]
9161it [00:42, 215.21it/s]
9186it [00:42, 222.95it/s]
9213it [00:42, 234.58it/s]
9237it [00:42, 232.21it/s]
9263it [00:42, 238.07it/s]
9287it [00:42, 230.45it/s]
9313it [00:42, 238.06it/s]
9337it [00:42, 229.05it/s]
9362it [00:42, 233.06it/s]
9386it [00:43, 231.05it/s]
9410it [00:43, 217.73it/s]
9433it [00:43, 220.43it/s]
9456it [00:43, 217.24it/s]
9481it [00:43, 226.29it/s]
9504it [00:43, 223.87it/s]
9528it [00:43, 227.61it/s]
9555it [00:43, 238.01it/s]
9582it [00:43, 247.06it/s]
9607it [00:43, 245.77it/s]
9632it [00:44, 243.23it/s]
9657it [00:44, 237.60it/s]
9681it [00:44, 230.71it/s]
9705it [00:44, 232.50it/s]
9729it [00:44, 229.74it/s]
9755it [00:44, 236.40it/s]
9779it [00:44, 234.40it/s]
9803it [00:44, 235.28it/s]
9827it [00:44, 224.26it/s]
9850it [00:45, 224.57it/s]
9874it [00:45, 228.48it/s]
9897it [00:45, 222.54it/s]
9921it [00:45, 224.66it/s]
9946it [00:45, 229.89it/s]
9970it [00:45, 229.70it/s]
9994it [00:45, 209.85it/s]
10016it [00:46, 67.85it/s]
10040it [00:46, 86.76it/s]
10063it [00:46, 106.24it/s]
10086it [00:46, 125.73it/s]
10108it [00:46, 142.47it/s]
10130it [00:47, 158.69it/s]
10153it [00:47, 175.00it/s]
10175it [00:47, 182.30it/s]
10198it [00:47, 193.69it/s]
10223it [00:47, 206.40it/s]
10249it [00:47, 218.80it/s]
10276it [00:47, 230.71it/s]
10302it [00:47, 237.02it/s]
10328it [00:47, 242.99it/s]
10353it [00:48, 243.17it/s]
10378it [00:48, 242.00it/s]
10403it [00:48, 229.47it/s]
10427it [00:48, 220.70it/s]
10451it [00:48, 224.53it/s]
10478it [00:48, 235.87it/s]
10502it [00:48, 232.07it/s]
10526it [00:48, 233.27it/s]
10550it [00:48, 230.03it/s]
10575it [00:48, 235.23it/s]
10599it [00:49, 141.18it/s]
10618it [00:49, 145.42it/s]
10640it [00:49, 160.96it/s]
10663it [00:49, 176.15it/s]
10691it [00:49, 201.05it/s]
10714it [00:49, 198.51it/s]
10741it [00:49, 216.36it/s]
10768it [00:50, 230.23it/s]
10793it [00:50, 226.48it/s]
10817it [00:50, 227.99it/s]
10841it [00:50, 227.68it/s]
10865it [00:50, 213.96it/s]
10889it [00:50, 218.61it/s]
10916it [00:50, 230.94it/s]
10941it [00:50, 231.32it/s]
10970it [00:50, 244.42it/s]
10996it [00:51, 245.07it/s]
11021it [00:51, 230.37it/s]
11048it [00:51, 239.77it/s]
11073it [00:51, 241.51it/s]
11098it [00:51, 238.30it/s]
11124it [00:51, 242.91it/s]
11149it [00:51, 244.22it/s]
11174it [00:51, 235.09it/s]
11198it [00:51, 217.14it/s]
11223it [00:52, 224.15it/s]
11246it [00:52, 217.77it/s]
11269it [00:52, 217.26it/s]
11296it [00:52, 231.48it/s]
11320it [00:52, 227.51it/s]
11343it [00:52, 214.69it/s]
11365it [00:52, 212.95it/s]
11388it [00:52, 216.31it/s]
11413it [00:52, 225.86it/s]
11437it [00:52, 229.17it/s]
11461it [00:53, 229.66it/s]
11485it [00:53, 224.17it/s]
11508it [00:53, 217.15it/s]
11531it [00:53, 219.14it/s]
11553it [00:53, 209.02it/s]
11575it [00:53, 124.71it/s]
11594it [00:53, 136.38it/s]
11623it [00:54, 167.39it/s]
11644it [00:54, 168.72it/s]
11674it [00:54, 199.93it/s]
11697it [00:54, 206.05it/s]
11720it [00:54, 208.11it/s]
11743it [00:54, 206.77it/s]
11770it [00:54, 219.85it/s]
11793it [00:54, 218.91it/s]
11816it [00:54, 216.18it/s]
11838it [00:55, 214.34it/s]
11862it [00:55, 219.52it/s]
11886it [00:55, 225.33it/s]
11909it [00:55, 225.80it/s]
11934it [00:55, 229.59it/s]
11959it [00:55, 230.34it/s]
11984it [00:55, 234.32it/s]
12008it [00:55, 234.17it/s]
12034it [00:55, 238.07it/s]
12058it [00:55, 238.10it/s]
12082it [00:56, 231.08it/s]
12106it [00:56, 225.28it/s]
12129it [00:56, 224.36it/s]
12152it [00:56, 225.38it/s]
12175it [00:56, 226.36it/s]
12200it [00:56, 230.63it/s]
12224it [00:56, 221.59it/s]
12250it [00:56, 232.17it/s]
12274it [00:56, 231.08it/s]
12298it [00:57, 231.80it/s]
12322it [00:57, 225.14it/s]
12345it [00:57, 215.20it/s]
12376it [00:57, 240.05it/s]
12402it [00:57, 245.27it/s]
12427it [00:57, 237.17it/s]
12451it [00:57, 219.40it/s]
12474it [00:57, 218.34it/s]
12497it [00:57, 202.66it/s]
12522it [00:58, 211.52it/s]
12544it [00:58, 208.53it/s]
12567it [00:58, 210.41it/s]
12589it [00:58, 212.75it/s]
12616it [00:58, 223.97it/s]
12639it [00:58, 212.88it/s]
12662it [00:58, 211.50it/s]
12686it [00:58, 218.64it/s]
12712it [00:58, 228.64it/s]
12736it [00:59, 231.23it/s]
12762it [00:59, 239.15it/s]
12787it [00:59, 127.20it/s]
12816it [00:59, 155.86it/s]
12838it [00:59, 161.58it/s]
12864it [00:59, 182.84it/s]
12887it [00:59, 189.38it/s]
12911it [01:00, 200.47it/s]
12937it [01:00, 215.85it/s]
12961it [01:00, 214.84it/s]
12986it [01:00, 223.40it/s]
13014it [01:00, 238.73it/s]
13039it [01:00, 236.07it/s]
13064it [01:00, 239.46it/s]
13090it [01:00, 245.08it/s]
13115it [01:00, 241.17it/s]
13142it [01:01, 237.79it/s]
13166it [01:01, 226.41it/s]
13189it [01:01, 223.09it/s]
13216it [01:01, 233.50it/s]
13240it [01:01, 221.30it/s]
13264it [01:01, 224.42it/s]
13287it [01:01, 217.89it/s]
13312it [01:01, 224.69it/s]
13337it [01:01, 231.82it/s]
13361it [01:02, 228.01it/s]
13384it [01:02, 224.97it/s]
13407it [01:02, 224.49it/s]
13430it [01:02, 219.80it/s]
13455it [01:02, 226.96it/s]
13478it [01:02, 217.50it/s]
13504it [01:02, 229.38it/s]
13528it [01:02, 228.25it/s]
13551it [01:02, 215.47it/s]
13574it [01:02, 219.09it/s]
13599it [01:03, 223.55it/s]
13622it [01:03, 220.45it/s]
13648it [01:03, 230.12it/s]
13672it [01:03, 229.40it/s]
13696it [01:03, 230.48it/s]
13720it [01:03, 223.51it/s]
13743it [01:03, 222.93it/s]
13766it [01:03, 220.70it/s]
13789it [01:03, 213.94it/s]
13811it [01:04, 212.05it/s]
13836it [01:04, 221.16it/s]
13859it [01:04, 221.95it/s]
13888it [01:04, 241.18it/s]
13913it [01:04, 238.55it/s]
13937it [01:04, 229.52it/s]
13962it [01:04, 233.32it/s]
13989it [01:04, 242.86it/s]
14014it [01:04, 243.11it/s]
14039it [01:04, 240.46it/s]
14064it [01:05, 231.90it/s]
14088it [01:05, 218.73it/s]
14113it [01:05, 227.17it/s]
14137it [01:05, 229.27it/s]
14161it [01:05, 227.03it/s]
14184it [01:05, 223.97it/s]
14208it [01:05, 227.32it/s]
14235it [01:05, 234.07it/s]
14259it [01:05, 217.18it/s]
14281it [01:06, 208.97it/s]
14305it [01:06, 215.68it/s]
14327it [01:06, 216.30it/s]
14349it [01:06, 114.79it/s]
14378it [01:06, 144.61it/s]
14404it [01:06, 166.13it/s]
14426it [01:07, 175.47it/s]
14451it [01:07, 192.26it/s]
14474it [01:07, 194.56it/s]
14496it [01:07, 197.82it/s]
14518it [01:07, 199.08it/s]
14539it [01:07, 200.03it/s]
14563it [01:07, 208.81it/s]
14585it [01:07, 210.64it/s]
14609it [01:07, 217.86it/s]
14634it [01:07, 223.27it/s]
14658it [01:08, 226.96it/s]
14686it [01:08, 239.31it/s]
14711it [01:08, 232.38it/s]
14737it [01:08, 239.74it/s]
14764it [01:08, 245.13it/s]
14789it [01:08, 235.51it/s]
14814it [01:08, 237.87it/s]
14838it [01:08, 229.68it/s]
14862it [01:08, 226.05it/s]
14889it [01:09, 237.01it/s]
14915it [01:09, 239.71it/s]
14940it [01:09, 232.53it/s]
14967it [01:09, 242.72it/s]
14992it [01:09, 237.46it/s]
15016it [01:09, 230.75it/s]
15040it [01:09, 231.27it/s]
15066it [01:09, 238.74it/s]
15090it [01:09, 238.22it/s]
15114it [01:10, 235.57it/s]
15138it [01:10, 232.36it/s]
15162it [01:10, 227.13it/s]
15185it [01:10, 226.06it/s]
15209it [01:10, 228.99it/s]
15232it [01:10, 226.49it/s]
15255it [01:10, 226.74it/s]
15280it [01:10, 233.07it/s]
15304it [01:10, 218.94it/s]
15328it [01:10, 222.89it/s]
15355it [01:11, 235.39it/s]
15379it [01:11, 223.85it/s]
15402it [01:11, 223.16it/s]
15425it [01:11, 209.04it/s]
15447it [01:11, 208.68it/s]
15469it [01:11, 211.54it/s]
15496it [01:11, 226.43it/s]
15519it [01:11, 227.13it/s]
15547it [01:11, 241.87it/s]
15572it [01:12, 237.28it/s]
15600it [01:12, 248.07it/s]
15625it [01:12, 226.60it/s]
15650it [01:12, 232.05it/s]
15674it [01:12, 207.02it/s]
15696it [01:12, 209.67it/s]
15718it [01:12, 208.06it/s]
15741it [01:12, 212.98it/s]
15763it [01:12, 205.91it/s]
15784it [01:13, 200.89it/s]
15816it [01:13, 231.88it/s]
15840it [01:13, 225.69it/s]
15863it [01:13, 212.67it/s]
15885it [01:13, 211.53it/s]
15907it [01:13, 213.04it/s]
15930it [01:13, 216.83it/s]
15953it [01:13, 213.83it/s]
15980it [01:13, 229.04it/s]
16004it [01:14, 231.72it/s]
16028it [01:14, 232.36it/s]
16052it [01:14, 217.20it/s]
16074it [01:14, 209.19it/s]
16096it [01:14, 211.70it/s]
16118it [01:14, 213.72it/s]
16143it [01:14, 223.81it/s]
16166it [01:14, 222.50it/s]
16193it [01:14, 234.22it/s]
16217it [01:15, 113.81it/s]
16239it [01:15, 131.30it/s]
16263it [01:15, 150.99it/s]
16286it [01:15, 164.27it/s]
16312it [01:15, 185.03it/s]
16337it [01:15, 195.20it/s]
16360it [01:15, 202.89it/s]
16385it [01:16, 215.29it/s]
16409it [01:16, 221.86it/s]
16433it [01:16, 224.19it/s]
16457it [01:16, 225.61it/s]
16482it [01:16, 226.75it/s]
16508it [01:16, 234.84it/s]
16532it [01:16, 231.05it/s]
16557it [01:16, 233.03it/s]
16581it [01:16, 232.51it/s]
16605it [01:17, 222.37it/s]
16628it [01:17, 222.72it/s]
16654it [01:17, 232.07it/s]
16678it [01:17, 218.41it/s]
16704it [01:17, 228.40it/s]
16728it [01:17, 220.60it/s]
16753it [01:17, 227.55it/s]
16776it [01:17, 224.41it/s]
16799it [01:17, 222.46it/s]
16825it [01:18, 231.31it/s]
16849it [01:18, 232.33it/s]
16873it [01:18, 221.79it/s]
16896it [01:18, 219.26it/s]
16923it [01:18, 230.64it/s]
16949it [01:18, 235.94it/s]
16973it [01:18, 228.25it/s]
16996it [01:18, 221.91it/s]
17021it [01:18, 228.03it/s]
17044it [01:18, 222.11it/s]
17071it [01:19, 233.62it/s]
17095it [01:19, 228.95it/s]
17121it [01:19, 235.75it/s]
17145it [01:19, 230.65it/s]
17170it [01:19, 235.66it/s]
17194it [01:19, 224.14it/s]
17217it [01:19, 221.88it/s]
17240it [01:19, 203.58it/s]
17262it [01:19, 206.58it/s]
17283it [01:20, 204.48it/s]
17310it [01:20, 222.12it/s]
17333it [01:20, 223.65it/s]
17356it [01:20, 222.52it/s]
17379it [01:20, 217.68it/s]
17401it [01:20, 210.59it/s]
17426it [01:20, 220.14it/s]
17449it [01:20, 216.05it/s]
17472it [01:20, 219.34it/s]
17495it [01:21, 218.91it/s]
17520it [01:21, 225.16it/s]
17545it [01:21, 230.74it/s]
17569it [01:21, 222.59it/s]
17592it [01:21, 224.28it/s]
17615it [01:21, 224.46it/s]
17638it [01:21, 210.63it/s]
17662it [01:21, 217.08it/s]
17684it [01:21, 207.60it/s]
17707it [01:22, 213.59it/s]
17731it [01:22, 218.51it/s]
17757it [01:22, 229.98it/s]
17783it [01:22, 238.01it/s]
17808it [01:22, 237.26it/s]
17832it [01:22, 224.23it/s]
17855it [01:22, 211.51it/s]
17884it [01:22, 230.16it/s]
17908it [01:22, 230.30it/s]
17932it [01:22, 231.88it/s]
17949it [01:23, 216.15it/s]

{'01': '/home/runner/AFQ_data/stanford_hardi/derivatives/afq/sub-01/ses-01/sub-01_ses-01_coordsys-RASMM_trkmethod-probCSD_recogmethod-AFQ_desc-profiles_dwi.csv'}

Visualizing the bundles and calculating act profiles:#

The pyAFQ API provides several ways to visualize bundles and profiles.

First, we will run a function that exports an html file that contains an interactive visualization of the bundles that are segmented.

Note

By default we resample a 100 points within a bundle, however to reduce processing time we will only resample 50 points.

Once it is done running, it should pop a browser window open and let you interact with the bundles.

Note

You can hide or show a bundle by clicking the legend, or select a single bundle by double clicking the legend. The interactive visualization will also all you to pan, zoom, and rotate.

bundle_html = myafq.export("all_bundles_figure")
plotly.io.show(bundle_html["01"][0])

We can also visualize the tract profiles in all of the bundles. These plots show both FA (left) and MD (right) layed out anatomically. To make this plot, it is required that you install with pip install pyAFQ[plot] so that you have the necessary dependencies.

fig_files = myafq.export("tract_profile_plots")["01"]
  0%|          | 0/28 [00:00<?, ?it/s]
  4%|▎         | 1/28 [00:00<00:07,  3.63it/s]
  7%|▋         | 2/28 [00:00<00:06,  4.32it/s]
 11%|█         | 3/28 [00:00<00:05,  4.39it/s]
 14%|█▍        | 4/28 [00:00<00:05,  4.61it/s]
 18%|█▊        | 5/28 [00:01<00:04,  4.74it/s]
 21%|██▏       | 6/28 [00:01<00:04,  4.54it/s]
 25%|██▌       | 7/28 [00:01<00:04,  4.51it/s]
 29%|██▊       | 8/28 [00:01<00:04,  4.49it/s]
 46%|████▋     | 13/28 [00:01<00:01, 12.03it/s]
 54%|█████▎    | 15/28 [00:02<00:01, 10.81it/s]
 64%|██████▍   | 18/28 [00:02<00:00, 10.98it/s]
 82%|████████▏ | 23/28 [00:02<00:00, 17.19it/s]
 93%|█████████▎| 26/28 [00:02<00:00, 15.18it/s]
100%|██████████| 28/28 [00:02<00:00, 13.11it/s]
100%|██████████| 28/28 [00:02<00:00,  9.38it/s]

  0%|          | 0/28 [00:00<?, ?it/s]
  4%|▎         | 1/28 [00:00<00:08,  3.09it/s]
  7%|▋         | 2/28 [00:00<00:08,  3.21it/s]
 11%|█         | 3/28 [00:00<00:08,  3.09it/s]
 14%|█▍        | 4/28 [00:01<00:07,  3.16it/s]
 18%|█▊        | 5/28 [00:01<00:07,  3.24it/s]
 21%|██▏       | 6/28 [00:01<00:06,  3.28it/s]
 25%|██▌       | 7/28 [00:02<00:06,  3.32it/s]
 29%|██▊       | 8/28 [00:02<00:05,  3.43it/s]
 43%|████▎     | 12/28 [00:02<00:01,  8.12it/s]
 54%|█████▎    | 15/28 [00:02<00:01,  8.50it/s]
 64%|██████▍   | 18/28 [00:03<00:01,  6.42it/s]
 82%|████████▏ | 23/28 [00:03<00:00, 10.78it/s]
 93%|█████████▎| 26/28 [00:03<00:00, 10.22it/s]
100%|██████████| 28/28 [00:04<00:00,  9.14it/s]
100%|██████████| 28/28 [00:04<00:00,  6.54it/s]
tutorials/tutorial_examples/{{fig_files[0]}}

We can even use altair to visualize the tract profiles in all of the bundles. We provide a more customizable interface for visualizing the tract profiles using altair. Again, to make this plot, it is required that you install with pip install pyAFQ[plot] so that you have the necessary dependencies.

profiles_df = myafq.combine_profiles()
altair_df = ava.combined_profiles_df_to_altair_df(profiles_df)
altair_chart = ava.altair_df_to_chart(altair_df)
altair_chart.display()
/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/scipy/stats/_distn_infrastructure.py:2246: RuntimeWarning:

invalid value encountered in multiply

/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/scipy/stats/_distn_infrastructure.py:2247: RuntimeWarning:

invalid value encountered in multiply

alt.VConcatChart(...)

We can check the number of streamlines per bundle, to make sure every bundle is found with a reasonable amount of streamlines.

bundle_counts = pd.read_csv(myafq.export("sl_counts")["01"], index_col=[0])
for ind in bundle_counts.index:
    if ind == "Total Recognized":
        threshold = 1000
    else:
        threshold = 10
    if bundle_counts["n_streamlines"][ind] < threshold:
        raise ValueError((
            "Small number of streamlines found "
            f"for bundle(s):\n{bundle_counts}"))

Total running time of the script: (6 minutes 7.432 seconds)

Estimated memory usage: 1415 MB

Gallery generated by Sphinx-Gallery