{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aefc4b07",
   "metadata": {},
   "source": [
    "# Using RecoBundles for tract delineation\n",
    "\n",
    "To delineate the major white matter pathways, pyAFQ defaults to use the waypoint ROI approach described in [Yeatman, 2012](https://doi.org/10.1371/journal.pone.0049790). However, as an alternative approach, pyAFQ also supports using the RecoBundles algorithm [Garyfallidis, 2018](https://doi.org/10.1016/j.neuroimage.2017.07.015),which uses an atlas of bundles in streamlines. \n",
    "\n",
    ":::{tip}\n",
    "This section shows how to use RecoBundles for bundle recognition.\n",
    ":::"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f87dd5-d2a2-4b42-9bb4-43f0e37da057",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path as op\n",
    "from paths import afq_home"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f15a0704",
   "metadata": {},
   "outputs": [],
   "source": [
    "from AFQ.api.group import GroupAFQ\n",
    "import AFQ.api.bundle_dict as abd\n",
    "import plotly"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e636240",
   "metadata": {},
   "source": [
    "## Defining the segmentation params\n",
    "\n",
    "We also refer to bundle recognition as the \"segmentation\" of the tractogram.\n",
    "Parameters of this process are set through a dictionary input to the\n",
    "`segmentation_params` argument of the GroupAFQ object. In this case, we\n",
    "use `abd.reco_bd(16)`, which tells pyAFQ to use the RecoBundles\n",
    "algorithm for bundle recognition.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38a0c123",
   "metadata": {},
   "outputs": [],
   "source": [
    "myafq = GroupAFQ(\n",
    "    output_dir=op.join(afq_home, 'stanford_hardi', 'derivatives',\n",
    "                       'recobundles'),\n",
    "    bids_path=op.join(afq_home, 'stanford_hardi'),\n",
    "    # Set the algorithm to use RecoBundles for bundle recognition:\n",
    "    bundle_info=abd.reco_bd(16),\n",
    "    preproc_pipeline='vistasoft')\n",
    "\n",
    "myafq.export(\"profiles\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7979bf44",
   "metadata": {},
   "outputs": [],
   "source": [
    "plotly.io.show(myafq.export(\"all_bundles_figure\")[\"01\"][0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
