{ "cells": [ { "cell_type": "markdown", "id": "01a2a046", "metadata": {}, "source": [ "# How to add new bundles into pyAFQ (Optic Radiations Example)\n", "\n", "pyAFQ is designed to be customizable and extensible. This example shows how you\n", "can customize it to define a new bundle based on a definition of waypoint and\n", "endpoint ROIs of your design.\n", "\n", "In this case, we add the optic radiations, based on work by Caffara et al. [^1],\n", "[^2]. The optic radiations (OR) are the primary projection of visual information\n", "from the lateral geniculate nucleus of the thalamus to the primary visual\n", "cortex. Studying the optic radiations with dMRI provides a linkage between white\n", "matter tissue properties, visual perception and behavior, and physiological\n", "responses of the visual cortex to visual stimulation.\n", "\n", "We start by importing some of the components that we need for this example and\n", "fixing the random seed for reproducibility" ] }, { "cell_type": "code", "execution_count": null, "id": "63172253", "metadata": {}, "outputs": [], "source": [ "import os.path as op\n", "import plotly\n", "import numpy as np\n", "import shutil\n", "\n", "from AFQ.api.group import GroupAFQ\n", "import AFQ.api.bundle_dict as abd\n", "import AFQ.data.fetch as afd\n", "from AFQ.definitions.image import ImageFile, RoiImage\n", "import AFQ.utils.streamlines as aus\n", "np.random.seed(1234)" ] }, { "cell_type": "markdown", "id": "1c6be45f", "metadata": {}, "source": [ "## Get dMRI data\n", "\n", "We will analyze one subject from the Healthy Brain Network Processed Open\n", "Diffusion Derivatives dataset (HBN-POD2) [^3], [^4]. We'll use a fetcher to\n", "get preprocessed dMRI data for one of the >2,000 subjects in that study. The\n", "data gets organized into a BIDS-compatible format in the `~/AFQ_data/HBN`\n", "folder:" ] }, { "cell_type": "code", "execution_count": null, "id": "524a575f", "metadata": {}, "outputs": [], "source": [ "study_dir = afd.fetch_hbn_preproc([\"NDARAA948VFH\"])[1]" ] }, { "cell_type": "markdown", "id": "fb87fedb", "metadata": {}, "source": [ "## Define custom `BundleDict` object\n", "The `BundleDict` object holds information about \"include\" and \"exclude\" ROIs,\n", "as well as endpoint ROIS, and whether the bundle crosses the midline. In this\n", "case, the ROIs are all defined in the MNI template space that is used as the\n", "default template space in pyAFQ, but, in principle, other template spaces\n", "could be used.\n", "\n", "The ROIs for the case can be downloaded using a custom fetcher which saves\n", "the ROIs to a folder and creates a dictionary of paths to the ROIs:" ] }, { "cell_type": "code", "execution_count": null, "id": "3401fc88", "metadata": {}, "outputs": [], "source": [ "or_rois = afd.read_or_templates()\n", "\n", "bundles = abd.OR_bd()" ] }, { "cell_type": "markdown", "id": "28654fef", "metadata": {}, "source": [ "Custom bundle definitions such as the OR, and the standard BundleDict can be\n", "combined through addition. To get both the OR and the standard bundles, we\n", "would execute the following code:\n", "\n", "```python\n", "bundles = bundles + abd.default_bd()\n", "```\n", "\n", "In this case, we will skip this and generate just the OR." ] }, { "cell_type": "code", "execution_count": null, "id": "ba2ccbb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4bad215f", "metadata": {}, "source": [ "## Define GroupAFQ object\n", "\n", "HBN POD2 have been processed with qsiprep [^5]. This means that a brain mask\n", "has already been computer for them. As you can see in other examples, these\n", "data also have a mapping calculated for them, which can also be incorporated\n", "into processing. However, in this case, we will let pyAFQ calculate its own\n", "SyN-based mapping so that the `combine_bundle` method can be used below to\n", "create a montage visualization.\n", "\n", "For tractography, we use CSD-based probabilistic tractography seeding\n", "extensively (`n_seeds=4` means 81 seeds per voxel!), but only within the ROIs\n", "and not throughout the white matter. This is controlled by passing\n", "`\"seed_mask\": RoiImage()` in the `tracking_params` dict. The custom bundles\n", "are passed as `bundle_info=bundles`. The call to `my_afq.export_all()`\n", "initiates the pipeline." ] }, { "cell_type": "code", "execution_count": null, "id": "7112b2a5", "metadata": {}, "outputs": [], "source": [ "my_afq = GroupAFQ(\n", " bids_path=study_dir,\n", " dwi_preproc_pipeline=\"qsiprep\",\n", " participant_labels=[\"NDARAA948VFH\"],\n", " output_dir=op.join(study_dir, \"derivatives\", \"afq_or\"),\n", " tracking_params={\"n_seeds\": 4,\n", " \"random_seeds\": False,\n", " \"directions\": \"prob\",\n", " \"odf_model\": \"CSD\",\n", " \"seed_mask\": RoiImage()},\n", " bundle_info=bundles)\n", "\n", "my_afq.export_all()" ] }, { "cell_type": "markdown", "id": "52fdd8f4", "metadata": {}, "source": [ "## Visualize a montage\n", "\n", "One way to examine the output of the pyAFQ pipeline is by creating a montage\n", "of images of a particular bundle across a group of participants (or, in this\n", "case, the one participant that was analyzed).\n", "\n", ":::{note}\n", "The montage file is copied to the present working directory so that it gets\n", "properly rendered into the web-page containing this example. It is not\n", "necessary to do this when running this type of analysis.\n", ":::" ] }, { "cell_type": "code", "execution_count": null, "id": "a98e0c12", "metadata": {}, "outputs": [], "source": [ "my_afq.combine_bundle(\"Left Optic Radiation\")\n", "montage = my_afq.group_montage(\n", " \"Left Optic Radiation\",\n", " (1, 1), \"Axial\", \"left\")\n", "shutil.copy(montage[0], op.split(montage[0])[-1])" ] }, { "cell_type": "markdown", "id": "985a79e8", "metadata": {}, "source": [ "## Interactive bundle visualization\n", "Another way to examine the outputs is to export the individual bundle\n", "figures, which show the streamlines, as well as the ROIs used to define the\n", "bundle. This is an html file, which contains an interactive figure that can\n", "be navigated, zoomed, rotated, etc." ] }, { "cell_type": "code", "execution_count": null, "id": "8c06b446", "metadata": {}, "outputs": [], "source": [ "bundle_html = my_afq.export(\"indiv_bundles_figures\")\n", "plotly.io.show(bundle_html[\"NDARAA948VFH\"][\"Left Optic Radiation\"])" ] }, { "cell_type": "markdown", "id": "dae2a2cb", "metadata": {}, "source": [ "## References\n", "\n", "[^1]: Caffarra S, Joo SJ, Bloom D, Kruper J, Rokem A, Yeatman JD. Development\n", " of the visual white matter pathways mediates development of\n", " electrophysiological responses in visual cortex. Hum Brain Mapp.\n", " 2021;42(17):5785-5797.\n", "\n", "[^2]: Caffarra S, Kanopka K, Kruper J, Richie-Halford A, Roy E, Rokem A,\n", " Yeatman JD. Development of the alpha rhythm is linked to visual white\n", " matter pathways and visual detection performance. bioRxiv.\n", " doi:10.1101/2022.09.03.506461\n", "\n", "[^3]: 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", "[^4]: 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", "[^5]: 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" }, "mystnb": { "execution_mode": "off" } }, "nbformat": 4, "nbformat_minor": 5 }