{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Callosal bundles using AFQ API\nAn example using the AFQ API to find callosal bundles using the templates from:\nhttp://hdl.handle.net/1773/34926\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os.path as op\nimport matplotlib.pyplot as plt\nimport nibabel as nib\n\nimport plotly\n\nfrom AFQ.api.group import GroupAFQ\nimport AFQ.api.bundle_dict as abd\nfrom AFQ.definitions.image import RoiImage\nimport AFQ.data.fetch as afd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get some example data\n\nRetrieves [Stanford HARDI dataset](https://purl.stanford.edu/ng782rw8378).\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "afd.organize_stanford_data(clear_previous_afq=\"track\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set tractography parameters (optional)\nWe make this tracking_params which we will pass to the GroupAFQ object\nwhich specifies that we want 100,000 seeds randomly distributed\nin the ROIs of every bundle.\n\nWe only do this to make this example faster and consume less space.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tracking_params = dict(seed_mask=RoiImage(),\n n_seeds=25000,\n random_seeds=True,\n rng_seed=42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set segmentation parameters (optional)\nWe make this segmentation_params which we will pass to the GroupAFQ object\nwhich specifies that we want to clip the extracted tract profiles\nto only be between the two ROIs.\n\nWe do this because tract profiles become less reliable as the bundles\napproach the gray matter-white matter boundary. On some of the non-callosal\nbundles, ROIs are not in a good position to clip edges. In these cases,\none can remove the first and last nodes in a tract profile.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "segmentation_params = {\"clip_edges\": True}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize a GroupAFQ object:\n\nWe specify bundle_info as the callosal bundles only\n(`abd.callosal_bd`). If we want to segment both the callosum\nand the other bundles, we would pass\n`abd.callosal_bd() + abd.default_bd()`\ninstead. This would tell the GroupAFQ object to use bundles from both\nthe standard and callosal templates.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "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 tracking_params=tracking_params,\n segmentation_params=segmentation_params,\n viz_backend_spec='plotly_no_gif')\n\n# Calling export all produces all of the outputs of processing, including\n# tractography, scalar maps, tract profiles and visualizations:\nmyafq.export_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Group Density Maps:\n\npyAFQ can make density maps of streamline counts per subject/session\nby calling `myafq.export(\"density_map\")`. When using `GroupAFQ`, you can also\ncombine these into one file by calling `myafq.export_group_density()`.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "group_density = myafq.export_group_density()\ngroup_density = nib.load(group_density).get_fdata()\nfig, ax = plt.subplots(1)\nax.matshow(\n group_density[:, :, group_density.shape[-1] // 2, 0],\n cmap='viridis')\nax.axis(\"off\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing bundles and tract profiles:\nThis would run the script and visualize the bundles using the plotly\ninteractive visualization, which should automatically open in a\nnew browser window.\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[\"01\"][0])" ] } ], "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 }