{ "cells": [ { "cell_type": "markdown", "id": "fdb4099c", "metadata": {}, "source": [ "# Getting started with pyAFQ - GroupAFQ\n", "\n", "There are two ways to [use pyAFQ](/tutorials/index): through the\n", "command line interface, and by writing Python code. This tutorial will walk you\n", "through the basics of the latter, using pyAFQ's Python Application Programming\n", "Interface (API)." ] }, { "cell_type": "code", "execution_count": null, "id": "30db853b", "metadata": {}, "outputs": [], "source": [ "import os.path as op\n", "\n", "import matplotlib.pyplot as plt\n", "import nibabel as nib\n", "import plotly\n", "import pandas as pd\n", "\n", "from AFQ.api.group import GroupAFQ\n", "import AFQ.data.fetch as afd\n", "import AFQ.viz.altair as ava\n", "import AFQ.definitions.image as afm" ] }, { "cell_type": "markdown", "id": "56e29628", "metadata": {}, "source": [ "## Example data\n", "\n", "pyAFQ can be called using GroupAFQ to handle data\n", "organized in a BIDS compliant directory.\n", "If this is not the case, refer to the Participant AFQ example.\n", "To get users started with this tutorial, we will download some example\n", "data and organize it in a BIDS compliant way (for more details on how\n", "BIDS is used in pyAFQ, refer to [plot_006_bids_layout](plot_006_bids_layout)).\n", "\n", "The following call downloads a a single subject's data from the Healthy Brain\n", "Network Processed Open Diffusion Derivatives dataset (HBN-POD2) [^1], [^2]\n", "and organizes it in BIDS in the user's home directory under:\n", "\n", "`~/AFQ_data/HBN/`\n", "\n", "The data is also placed in a derivatives directory, signifying that it has\n", "already undergone the required preprocessing necessary for pyAFQ to run.\n", "\n", "The clear_previous_afq is used to remove any previous runs of the afq object\n", "stored in the `~/AFQ_data/HBN/` BIDS directory. Set it to None if\n", "you want to use the results of previous runs." ] }, { "cell_type": "code", "execution_count": null, "id": "7f45f192", "metadata": {}, "outputs": [], "source": [ "bids_path = afd.fetch_hbn_preproc(\n", " [\"NDARAA948VFH\"],\n", " clear_previous_afq=\"all\")[1]" ] }, { "cell_type": "markdown", "id": "eb4eaf47", "metadata": {}, "source": [ "## Set tractography parameters (optional)\n", "We make create a `tracking_params` variable, which we will pass to the\n", "GroupAFQ object which specifies that we want 100,000 seeds randomly\n", "distributed in the white matter. We only do this to make this example faster\n", "and consume less space; normally, we use more seeds." ] }, { "cell_type": "code", "execution_count": null, "id": "72943684", "metadata": {}, "outputs": [], "source": [ "tracking_params = dict(n_seeds=1e5,\n", " random_seeds=True,\n", " rng_seed=2025,\n", " trx=True)" ] }, { "cell_type": "markdown", "id": "97688842", "metadata": {}, "source": [ "## Define PVE images (optional)\n", "To improve segmentation and tractography results, we can provide\n", "partial volume estimate (PVE) images for the cerebrospinal fluid (CSF),\n", "gray matter (GM), and white matter (WM). Here, we define these images\n", "using the AFQ.definitions.image.PVEImages class, which takes as input\n", "three AFQ.definitions.image.ImageFile objects, one for each tissue type.\n", "One can also provide a single PVE image with all three tissue types\n", "using the AFQ.definitions.image.PVEImage class. Finally, by default,\n", "if no PVE images are provided, pyAFQ will use SynthSeg2 to compute\n", "these images." ] }, { "cell_type": "code", "execution_count": null, "id": "c3b6c0d0", "metadata": {}, "outputs": [], "source": [ "pve = afm.PVEImages(\n", " afm.ImageFile(\n", " suffix=\"probseg\", filters={\"scope\": \"qsiprep\", \"label\": \"CSF\"}),\n", " afm.ImageFile(\n", " suffix=\"probseg\", filters={\"scope\": \"qsiprep\", \"label\": \"GM\"}),\n", " afm.ImageFile(\n", " suffix=\"probseg\", filters={\"scope\": \"qsiprep\", \"label\": \"WM\"}))" ] }, { "cell_type": "markdown", "id": "8da6ec5e", "metadata": {}, "source": [ "## Brain Mask Definition (optional)\n", "\n", "By default, pyAFQ will compute a brain mask from the T1. However,\n", "this requires onnxruntime to be installed. If you do not have onnxruntime\n", "installed, or if you want to use a different brain mask, you can specify\n", "it here." ] }, { "cell_type": "code", "execution_count": null, "id": "2fd7b1a3", "metadata": {}, "outputs": [], "source": [ "brain_mask_definition = afm.ImageFile(\n", " suffix=\"mask\", filters={\"desc\": \"brain\", \"scope\": \"qsiprep\"})" ] }, { "cell_type": "markdown", "id": "e21176bc", "metadata": {}, "source": [ "## Initialize a GroupAFQ object:\n", "\n", "Creates a GroupAFQ object, that encapsulates tractometry. This object can be\n", "used to manage the entire [tractometry pipeline](/explanations/index), including:\n", "\n", "- Tractography\n", "- Registration\n", "- Segmentation\n", "- Cleaning\n", "- Profiling\n", "- Visualization\n", "\n", "This will also create an output folder for the corresponding AFQ derivatives\n", "in the AFQ data directory: `AFQ_data/HBN/derivatives/afq/`\n", "\n", "To initialize this object we will pass in the path location to our BIDS\n", "compliant data, the name of the preprocessing pipeline we want to use, \n", "the name of the t1 preprocessing pipeline we want to use (in this case,\n", "its the same, qsiprep [^3]), the participant labels we want to process\n", "(in this case, just a single subject), the PVE images we defined above, and\n", "the tracking parameters we defined above." ] }, { "cell_type": "code", "execution_count": null, "id": "d197d626", "metadata": {}, "outputs": [], "source": [ "myafq = GroupAFQ(\n", " bids_path=op.join(afd.afq_home, 'HBN'),\n", " dwi_preproc_pipeline='qsiprep',\n", " t1_preproc_pipeline='qsiprep',\n", " participant_labels=['NDARAA948VFH'],\n", " pve=pve,\n", " brain_mask_definition=brain_mask_definition,\n", " tracking_params=tracking_params)" ] }, { "cell_type": "markdown", "id": "b4d3125a", "metadata": {}, "source": [ "## Calculating DTI FA (Diffusion Tensor Imaging Fractional Anisotropy)\n", "\n", "The GroupAFQ object has a method called `export`, which allows the user\n", "to calculate various derived quantities from the data.\n", "\n", "For example, FA can be computed using the DTI model, by explicitly\n", "calling `myafq.export(\"dti_fa\")`. This triggers the computation of DTI\n", "parameters for all subjects in the dataset, and stores the results in\n", "the AFQ derivatives directory. In addition, it calculates the FA\n", "from these parameters and stores it in a different file in the same\n", "directory.\n", "\n", ":::{note}\n", "The AFQ API computes quantities lazily. This means that DTI parameters\n", "are not computed until they are required. This means that the first\n", "line below is the one that requires time.\n", ":::\n", "\n", "The result of the call to `export` is a dictionary, with the subject\n", "IDs as keys, and the filenames of the corresponding files as values.\n", "This means that to extract the filename corresponding to the FA of the first\n", "subject, we can do:" ] }, { "cell_type": "code", "execution_count": null, "id": "7418268a", "metadata": {}, "outputs": [], "source": [ "FA_fname = myafq.export(\"dti_fa\", collapse=False)[\"NDARAA948VFH\"][\"HBNsiteRU\"]\n", "\n", "# We will then use `nibabel` to load the deriviative file and retrieve the\n", "# data array.\n", "\n", "FA_img = nib.load(FA_fname)\n", "FA = FA_img.get_fdata()" ] }, { "cell_type": "markdown", "id": "d1a5bc1d", "metadata": {}, "source": [ "## Visualize the result with Matplotlib\n", "At this point `FA` is an array, and we can use standard Python tools to\n", "visualize it or perform additional computations with it.\n", "\n", "In this case we are going to take an axial slice halfway through the\n", "FA data array and plot using a sequential color map.\n", "\n", ":::{note}\n", "The data array is structured as a xyz coordinate system.\n", ":::" ] }, { "cell_type": "code", "execution_count": null, "id": "81fadddc", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1)\n", "ax.matshow(FA[:, :, FA.shape[-1] // 2], cmap='viridis')\n", "ax.axis(\"off\")\n", "plt.savefig(\"FA.png\")" ] }, { "cell_type": "code", "execution_count": null, "id": "fe739a5f", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "from AFQ.utils.docs import embed_html, embed_image\n", "\n", "embed_image(\"FA.png\")" ] }, { "cell_type": "markdown", "id": "729c67bf", "metadata": {}, "source": [ "## Recognizing the bundles and calculating tract profiles:\n", "\n", "Typically, users of pyAFQ are interested in calculating not only an overall\n", "map of the FA, but also the major white matter pathways (or bundles) and\n", "tract profiles of tissue properties along their length. To trigger the\n", "pyAFQ pipeline that calculates the profiles, users can call the\n", "`export('profiles')` method:\n", "\n", ":::{note}\n", "Running the code below triggers the full pipeline of operations\n", "leading to the computation of the tract profiles. Therefore, it\n", "takes a little while to run (about 40 minutes, typically).\n", ":::" ] }, { "cell_type": "code", "execution_count": null, "id": "38139a5e", "metadata": {}, "outputs": [], "source": [ "myafq.export('profiles')" ] }, { "cell_type": "markdown", "id": "c5d41d9a", "metadata": {}, "source": [ "## Visualizing the bundles and calculating tract profiles:\n", "\n", "The pyAFQ API provides several ways to visualize bundles and profiles.\n", "\n", "First, we will run a function that exports an html file that contains\n", "an interactive visualization of the bundles that are segmented.\n", "\n", ":::{note}\n", "By default we resample a 100 points within a bundle, however to reduce\n", "processing time we will only resample 50 points.\n", ":::\n", "\n", "Once it is done running, it should pop a browser window open and let you\n", "interact with the bundles.\n", "\n", ":::{note}\n", "You can hide or show a bundle by clicking the legend, or select a\n", "single bundle by double clicking the legend. The interactive\n", "visualization will also allow you to pan, zoom, and rotate.\n", ":::" ] }, { "cell_type": "code", "execution_count": null, "id": "ed2c3eb2", "metadata": { "tags": [ "remove-execution" ] }, "outputs": [], "source": [ "bundle_html = myafq.export(\"all_bundles_figure\", collapse=False)\n", "plotly.io.show(bundle_html[\"NDARAA948VFH\"][\"HBNsiteRU\"][0])" ] }, { "cell_type": "code", "execution_count": null, "id": "748d77b1", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "bundle_html = myafq.export(\"all_bundles_figure\", collapse=False)\n", "embed_html(bundle_html[\"NDARAA948VFH\"][\"HBNsiteRU\"][1])" ] }, { "cell_type": "markdown", "id": "9402a9a2", "metadata": {}, "source": [ "We can also visualize the tract profiles in all of the bundles. These\n", "plots show both FA (left) and MD (right) laid out anatomically.\n", "To make this plot, it is required that you install with\n", "`pip install pyAFQ[plot]` so that you have the necessary dependencies." ] }, { "cell_type": "code", "execution_count": null, "id": "05f87c68", "metadata": {}, "outputs": [], "source": [ "fig_files = myafq.export(\"tract_profile_plots\", collapse=False)[\n", " \"NDARAA948VFH\"][\"HBNsiteRU\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "524c18bb", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "embed_image(fig_files[0] + \".png\")" ] }, { "cell_type": "markdown", "id": "8397c8b0", "metadata": {}, "source": [ "We can even use altair to visualize the tract profiles in all\n", "of the bundles. We provide a more customizable interface for visualizing\n", "the tract profiles using altair.\n", "Again, to make this plot, it is required that you install with\n", "`pip install pyAFQ[plot]` so that you have the necessary dependencies." ] }, { "cell_type": "code", "execution_count": null, "id": "870dc221", "metadata": {}, "outputs": [], "source": [ "profiles_df = myafq.combine_profiles()\n", "altair_df = ava.combined_profiles_df_to_altair_df(\n", " profiles_df,\n", " tissue_properties=[\n", " \"dti_fa\",\n", " \"dti_md\",\n", " \"t1w_over_b0\",\n", " \"msdki_msd\",\n", " \"msdki_msk\"])\n", "altair_chart = ava.altair_df_to_chart(altair_df)\n", "altair_chart.save(\"profiles_chart.html\")" ] }, { "cell_type": "code", "execution_count": null, "id": "74928b34", "metadata": { "tags": [ "remove-input" ] }, "outputs": [], "source": [ "embed_html(\"profiles_chart.html\")" ] }, { "cell_type": "markdown", "id": "6f751417", "metadata": {}, "source": [ "## Exporting citations\n", "Finally, we can export the citations for the some of methods used in this\n", "analysis. These are not guaranteed to be comprehensive, but they\n", "should be a good starting point." ] }, { "cell_type": "code", "execution_count": null, "id": "2e7af4a7", "metadata": {}, "outputs": [], "source": [ "myafq.export(\"citations\")" ] }, { "cell_type": "markdown", "id": "0d815d2a", "metadata": {}, "source": [ "We can check the number of streamlines per bundle, to make sure\n", "every bundle is found with a reasonable amount of streamlines." ] }, { "cell_type": "code", "execution_count": null, "id": "ee6a7610", "metadata": {}, "outputs": [], "source": [ "bundle_counts = pd.read_csv(\n", " myafq.export(\"sl_counts\", collapse=False)[\n", " \"NDARAA948VFH\"][\"HBNsiteRU\"], index_col=[0])\n", "for ind in bundle_counts.index:\n", " if ind == \"Total Recognized\":\n", " threshold = 15000\n", " elif \"Temporo-parietal\" in ind:\n", " threshold = 15\n", " else:\n", " threshold = 20\n", " if bundle_counts[\"n_streamlines\"][ind] < threshold:\n", " raise ValueError((\n", " \"Small number of streamlines found \"\n", " f\"for bundle(s):\\n{bundle_counts}\"))" ] }, { "cell_type": "markdown", "id": "97ab3ff2", "metadata": {}, "source": [ "## Alternative way to initialize GroupAFQ when using QSIPrep data\n", "As a final note, if you are using QSIPrep preprocessed data,\n", "you can also initialize the GroupAFQ object using the\n", "`from_qsiprep` class method. This method will automatically set\n", "the appropriate BIDS filters to find the preprocessed DWI data.\n", "Additionally, it will find and use the brain masks and PVE images\n", "that QSIPrep generates. Outside of BIDS filters, the arguments\n", "are the same as those used when initializing the GroupAFQ object\n", "directly." ] }, { "cell_type": "code", "execution_count": null, "id": "20a40943", "metadata": {}, "outputs": [], "source": [ "myafq = GroupAFQ.from_qsiprep(\n", " qsi_dir=op.join(afd.afq_home, 'HBN'),\n", " participant_labels=['NDARAA948VFH'],\n", " tracking_params=tracking_params)" ] }, { "cell_type": "markdown", "id": "deb7a07e", "metadata": {}, "source": [ "## References\n", "\n", "[^1]: Alexander LM, Escalera J, Ai L, et al. An open resource for\n", " transdiagnostic research in pediatric mental health and learning\n", " disorders. Sci Data. 2017;4:170181.\n", "\n", "[^2]: Richie-Halford A, Cieslak M, Ai L, et al. An analysis-ready and quality\n", " controlled resource for pediatric brain white-matter research. Scientific\n", " Data. 2022;9(1):1-27.\n", "\n", "[^3]: Cieslak M, Cook PA, He X, et al. QSIPrep: an integrative platform for\n", " preprocessing and reconstructing diffusion MRI data. Nat Methods.\n", " 2021;18(7):775-778.\n", "\n", ":::{only} html\n", "{download}`Download as Jupyter Notebook `\n", ":::" ] } ], "metadata": { "file_format": "mystnb", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }