{ "cells": [ { "cell_type": "markdown", "id": "96ac7d07", "metadata": {}, "source": [ "# Creating tract density maps of callosal bundles\n", "\n", "This example shows how to use the AFQ API to delineate the callosal bundles and\n", "then compute tract density / visitation maps at both the individual and\n", "group level." ] }, { "cell_type": "code", "execution_count": null, "id": "e7fdef44", "metadata": {}, "outputs": [], "source": [ "import os.path as op\n", "import matplotlib.pyplot as plt\n", "import nibabel as nib\n", "\n", "import plotly\n", "\n", "from AFQ.api.group import GroupAFQ\n", "import AFQ.api.bundle_dict as abd\n", "from AFQ.definitions.image import RoiImage\n", "import AFQ.data.fetch as afd" ] }, { "cell_type": "markdown", "id": "5fd04593", "metadata": {}, "source": [ "## Get some example data\n", "\n", "Retrieves [Stanford HARDI dataset](https://purl.stanford.edu/ng782rw8378)." ] }, { "cell_type": "code", "execution_count": null, "id": "7ed511c1", "metadata": {}, "outputs": [], "source": [ "afd.organize_stanford_data(clear_previous_afq=\"track\")" ] }, { "cell_type": "markdown", "id": "3051e886", "metadata": {}, "source": [ "## Set segmentation parameters (optional)\n", "We make this segmentation_params which we will pass to the GroupAFQ object\n", "which specifies that we want to clip the extracted tract profiles\n", "to only be between the two ROIs.\n", "\n", "We do this because tract profiles become less reliable as the bundles\n", "approach the gray matter-white matter boundary. On some of the non-callosal\n", "bundles, ROIs are not in a good position to clip edges. In these cases,\n", "one can remove the first and last nodes in a tract profile." ] }, { "cell_type": "code", "execution_count": null, "id": "da21b448", "metadata": {}, "outputs": [], "source": [ "segmentation_params = {\"clip_edges\": True}" ] }, { "cell_type": "markdown", "id": "849aac05", "metadata": {}, "source": [ "## Initialize a GroupAFQ object:\n", "\n", "We specify bundle_info as the callosal bundles only\n", "(`abd.callosal_bd`). If we want to segment both the callosum\n", "and the other bundles, we would pass\n", "`abd.callosal_bd() + abd.default_bd()`\n", "instead. This would tell the GroupAFQ object to use bundles from both\n", "the standard and callosal templates." ] }, { "cell_type": "code", "execution_count": null, "id": "cfd59f1e", "metadata": {}, "outputs": [], "source": [ "myafq = GroupAFQ(\n", " bids_path=op.join(afd.afq_home, 'stanford_hardi'),\n", " dwi_preproc_pipeline='vistasoft',\n", " t1_preproc_pipeline='freesurfer',\n", " bundle_info=abd.callosal_bd(),\n", " segmentation_params=segmentation_params,\n", " viz_backend_spec='plotly_no_mp4')\n", "\n", "# Calling export all produces all of the outputs of processing, including\n", "# tractography, scalar maps, tract profiles and visualizations:\n", "myafq.export_all()" ] }, { "cell_type": "markdown", "id": "3eeb2ff9", "metadata": {}, "source": [ "## Create tract density maps:\n", "\n", "pyAFQ can make density maps of streamline counts per subject/session\n", "by calling `myafq.export(\"density_map\")`. When using `GroupAFQ`, you can also\n", "combine these into one file by calling `myafq.export_group_density()`." ] }, { "cell_type": "code", "execution_count": null, "id": "367086e4", "metadata": {}, "outputs": [], "source": [ "group_density = myafq.export_group_density()\n", "group_density = nib.load(group_density).get_fdata()\n", "fig, ax = plt.subplots(1)\n", "ax.matshow(\n", " group_density[:, :, group_density.shape[-1] // 2, 0],\n", " cmap='viridis')\n", "ax.axis(\"off\")" ] }, { "cell_type": "markdown", "id": "6771ba4a", "metadata": {}, "source": [ "## Visualizing bundles and tract profiles:\n", "This would run the script and visualize the bundles using the plotly\n", "interactive visualization, which should automatically open in a\n", "new browser window." ] }, { "cell_type": "code", "execution_count": null, "id": "c3747999", "metadata": {}, "outputs": [], "source": [ "bundle_html = myafq.export(\"all_bundles_figure\")\n", "plotly.io.show(bundle_html[\"01\"][0])" ] }, { "cell_type": "markdown", "id": "9a5c1b51", "metadata": {}, "source": [ ":::{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 }