{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# How to add new bundles into pyAFQ (Acoustic Radiations Example)\n\npyAFQ is designed to be customizable and extensible. This example shows how you\ncan customize it to define a new bundle based on a definition of waypoint and\nendpoint ROIs of your design. In this case, we add the acoustic radiations.\n\nWe start by importing some of the components that we need for this example and\nfixing the random seed for reproducibility\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os.path as op\nimport plotly\nimport numpy as np\n\nfrom AFQ.api.group import GroupAFQ\nimport AFQ.api.bundle_dict as abd\nimport AFQ.data.fetch as afd\nfrom AFQ.definitions.image import ImageFile, RoiImage\nnp.random.seed(1234)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get dMRI data\nWe will analyze one subject from the Healthy Brain Network Processed Open\nDiffusion Derivatives dataset (HBN-POD2) [1]_, [2]_. We'll use a fetcher to\nget preprocessed dMRI data for one of the >2,000 subjects in that study. The\ndata gets organized into a BIDS-compatible format in the `~/AFQ_data/HBN`\nfolder:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "study_dir = afd.fetch_hbn_preproc([\"NDARAA948VFH\"])[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define custom `BundleDict` object\nThe `BundleDict` object holds information about \"include\" and \"exclude\" ROIs,\nas well as endpoint ROIS, and whether the bundle crosses the midline. In this\ncase, the ROIs are all defined in the MNI template space that is used as the\ndefault template space in pyAFQ, but, in principle, other template spaces\ncould be used.\n\nThe ROIs for the case can be downloaded using a custom fetcher which saves\nthe ROIs to a folder and creates a dictionary of paths to the ROIs:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ar_rois = afd.read_ar_templates()\n\nbundles = abd.BundleDict({\n \"Left Acoustic Radiation\": {\n \"start\": ar_rois[\"AAL_Thal_L\"],\n \"end\": ar_rois[\"AAL_TempSup_L\"],\n \"cross_midline\": False,\n },\n \"Right Acoustic Radiation\": {\n \"start\": ar_rois[\"AAL_Thal_R\"],\n \"end\": ar_rois[\"AAL_TempSup_R\"],\n \"cross_midline\": False\n }\n})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define GroupAFQ object\nHBN POD2 have been processed with qsiprep [3]_. This means that a brain mask\nhas already been computer for them. As you can see in other examples, these\ndata also have a mapping calculated for them, which can also be incorporated\ninto processing. However, in this case, we will let pyAFQ calculate its own\nSyN-based mapping so that the `combine_bundle` method can be used below to\ncreate a montage visualization.\n\nFor tractography, we use CSD-based probabilistic tractography seeding\nextensively (`n_seeds=4` means 81 seeds per voxel!), but only within the ROIs\nand not throughout the white matter. This is controlled by passing\n`\"seed_mask\": RoiImage()` in the `tracking_params` dict. The custom bundles\nare passed as `bundle_info=bundles`. The call to `my_afq.export_all()`\ninitiates the pipeline.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "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_ar\"),\n tracking_params={\"n_seeds\": 4,\n \"directions\": \"prob\",\n \"odf_model\": \"CSD\",\n \"seed_mask\": RoiImage(use_endpoints=True)},\n bundle_info=bundles)\n\nmy_afq.export_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive bundle visualization\nAnother way to examine the outputs is to export the individual bundle\nfigures, which show the streamlines, as well as the ROIs used to define the\nbundle. This is an html file, which contains an interactive figure that can\nbe navigated, zoomed, rotated, etc.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bundle_html = my_afq.export(\"indiv_bundles_figures\")\nplotly.io.show(bundle_html[\"NDARAA948VFH\"][\"Left Acoustic Radiation\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\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" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.13" } }, "nbformat": 4, "nbformat_minor": 0 }