{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Getting started with pyAFQ - ParticipantAFQ\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport os.path as op\n\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport plotly\n\nfrom AFQ.api.participant import ParticipantAFQ\nimport AFQ.data.fetch as afd\nimport AFQ.definitions.image as afm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example data\nThe following call downloads a a single subject's data from the Healthy Brain\nNetwork Processed Open Diffusion Derivatives dataset (HBN-POD2) [1]_, [2]_\nand organizes it in BIDS in the user's home directory under::\n\n ``~/AFQ_data/HBN/``\n\nThe data is also placed in a derivatives directory, signifying that it has\nalready undergone the required preprocessing necessary for pyAFQ to run.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "afd.fetch_hbn_preproc([\"NDARAA948VFH\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining data files\nIf your data is not in BIDS format, you can still use pyAFQ. If you have BIDS\ncompliant dataset, you can use ``GroupAFQ`` instead (:doc:`plot_001_group_afq_api`).\nOtherwise, You will need to define the data files that you want to use. In\nthis case, we will define the data files for the subject we downloaded above.\nThe data files are located in the ``~/AFQ_data/HBN/derivatives/qsiprep``\ndirectory, and are organized into a BIDS compliant directory structure. The\ndata files are located in the ``dwi`` directories. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sub_dir = op.join(afd.afq_home, \"HBN\", \"derivatives\", \"qsiprep\",\n \"sub-NDARAA948VFH\")\ndwi_data_file = op.join(sub_dir, \"ses-HBNsiteRU\", \"dwi\", (\n \"sub-NDARAA948VFH_\"\n \"ses-HBNsiteRU_\"\n \"acq-64dir_space-T1w_desc-preproc_dwi.nii.gz\"))\nbval_file = op.join(sub_dir, \"ses-HBNsiteRU\", \"dwi\", (\n \"sub-NDARAA948VFH_\"\n \"ses-HBNsiteRU_\"\n \"acq-64dir_space-T1w_desc-preproc_dwi.bval\"))\nbvec_file = op.join(sub_dir, \"ses-HBNsiteRU\", \"dwi\", (\n \"sub-NDARAA948VFH_\"\n \"ses-HBNsiteRU_\"\n \"acq-64dir_space-T1w_desc-preproc_dwi.bvec\"))\nt1_file = op.join(sub_dir, \"anat\",\n \"sub-NDARAA948VFH_desc-preproc_T1w.nii.gz\")\n\n# You will also need to define the output directory where you want to store the\n# results. The output directory needs to exist before exporting ParticipantAFQ\n# results.\n\noutput_dir = op.join(afd.afq_home, \"HBN\",\n \"derivatives\", \"afq\", \"sub-NDARAA948VFH\",\n \"ses-HBNsiteRU\", \"dwi\")\nos.makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set tractography parameters (optional)\nWe make create a `tracking_params` variable, which we will pass to the\nParticipantAFQ object which specifies that we want 100,000 seeds randomly\ndistributed in the white matter. We only do this to make this example faster\nand consume less space; normally, we use more seeds.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tracking_params = dict(n_seeds=1e5,\n random_seeds=True,\n rng_seed=2025,\n trx=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define PVE images (optional)\nTo improve segmentation and tractography results, we can provide\npartial volume estimate (PVE) images for the cerebrospinal fluid (CSF),\ngray matter (GM), and white matter (WM). Here, we define these images\nusing the AFQ.definitions.image.PVEImages class, which takes as input\nthree AFQ.definitions.image.ImageFile objects, one for each tissue type.\nOne can also provide a single PVE image with all three tissue types\nusing the AFQ.definitions.image.PVEImage class. Finally, by default,\nif no PVE images are provided, pyAFQ will use SynthSeg2 to compute\nthese images.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pve = afm.PVEImages(\n afm.ImageFile(\n path=op.join(sub_dir, \"anat\", \n \"sub-NDARAA948VFH_label-CSF_probseg.nii.gz\")),\n afm.ImageFile(\n path=op.join(sub_dir, \"anat\", \n \"sub-NDARAA948VFH_label-GM_probseg.nii.gz\")),\n afm.ImageFile(\n path=op.join(sub_dir, \"anat\", \n \"sub-NDARAA948VFH_label-WM_probseg.nii.gz\")))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Brain Mask Definition (optional)\n\nBy default, pyAFQ will compute a brain mask from the T1. However,\nthis requires onnxruntime to be installed. If you do not have onnxruntime\ninstalled, or if you want to use a different brain mask, you can specify\nit here.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "brain_mask_definition = afm.ImageFile(\n path=op.join(sub_dir, \"anat\", \"sub-NDARAA948VFH_desc-brain_mask.nii.gz\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize a ParticipantAFQ object:\n\nCreates a ParticipantAFQ object, that encapsulates tractometry. This object\ncan be used to manage the entire :doc:`/explanations/index`, including:\n\n- Tractography\n- Registration\n- Segmentation\n- Cleaning\n- Profiling\n- Visualization\n\nTo initialize the object, we will pass in the diffusion data files and specify\nthe output directory where we want to store the results. We will also\npass in the tracking parameters we defined above.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myafq = ParticipantAFQ(\n dwi_data_file=dwi_data_file,\n bval_file=bval_file,\n bvec_file=bvec_file,\n t1_file=t1_file,\n output_dir=output_dir,\n tracking_params=tracking_params,\n pve=pve,\n brain_mask_definition=brain_mask_definition,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculating DKI FA (Diffusion Tensor Imaging Fractional Anisotropy)\nThe ParticipantAFQ object has a method called ``export``, which allows the user\nto calculate various derived quantities from the data.\n\nFor example, FA can be computed using the DKI model, by explicitly\ncalling ``myafq.export(\"dki_fa\")``. This triggers the computation of DKI\nparameters, and stores the results in the AFQ derivatives directory.\nIn addition, it calculates the FA from these parameters and stores it in a\ndifferent file in the same directory.\n\n

Note

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

\n\nThe result of the call to ``export`` is the filename of the corresponding FA\nfiles.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FA_fname = myafq.export(\"dki_fa\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will then use ``nibabel`` to load the deriviative file and retrieve the\ndata array.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FA_img = nib.load(FA_fname)\nFA = FA_img.get_fdata()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize the result with Matplotlib\nAt this point ``FA`` is an array, and we can use standard Python tools to\nvisualize it or perform additional computations with it.\n\nIn this case we are going to take an axial slice halfway through the\nFA data array and plot using a sequential color map.\n\n

Note

The data array is structured as a xyz coordinate system.

\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1)\nax.matshow(FA[:, :, FA.shape[-1] // 2], cmap=\"viridis\")\nax.axis(\"off\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recognizing the bundles and calculating tract profiles:\nTypically, users of pyAFQ are interested in calculating not only an overall\nmap of the FA, but also the major white matter pathways (or bundles) and\ntract profiles of tissue properties along their length. To trigger the\npyAFQ pipeline that calculates the profiles, users can call the\n``export(\"profiles\")`` method:\n\n

Note

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\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myafq.export(\"profiles\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing the bundles and calculating tract profiles:\nThe pyAFQ API provides several ways to visualize bundles and profiles.\n\nFirst, we will run a function that exports an html file that contains\nan interactive visualization of the bundles that are segmented.\n\n

Note

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

\n\nOnce it is done running, it should pop a browser window open and let you\ninteract with the bundles.\n\n

Note

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 all you to pan, zoom, and rotate.

\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bundle_html = myafq.export(\"all_bundles_figure\")\nplotly.io.show(bundle_html[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also visualize the tract profiles in all of the bundles. These\nplots show both FA (left) and MD (right) laid out anatomically.\nTo make this plot, it is required that you install with\n``pip install pyAFQ[plot]`` so that you have the necessary dependencies.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig_files = myafq.export(\"tract_profile_plots\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: {{ fig_files[0] }}\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting citations\nFinally, we can export the citations for the some of methods used in this\nanalysis. These are not guaranteed to be comprehensive, but they\nshould be a good starting point.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "myafq.export(\"citations\")" ] }, { "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 }