{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4fd11f92",
   "metadata": {},
   "source": [
    "# BabyAFQ : tractometry for infant dMRI data\n",
    "\n",
    "Infant brains are by no means just smaller versions of a grownup brain.\n",
    "Although many of the structures -- including major white matter tracts -- are\n",
    "present at birth, the infant brain differs substantially in its anatomy and\n",
    "physical composition relative to the adult brain. For example, the degree of\n",
    "myelination in the newborn brain is much smaller than in the adult brain. This\n",
    "is apparent when examining a newborn infants T1-weighted MRI, where gray matter\n",
    "appears _brighter_ than the white matter. As the brain white matter myelinates\n",
    "in the first few months of life, this contrast inverts with a point of roughly\n",
    "equal brightness around 6 months of age (for a review see\n",
    "[Gilmore, 2018](https://doi.org/10.1038/nrn.2018.1)).\n",
    "\n",
    "Therefore, tractometry of neonates requires methods that are substantially \n",
    "different than the methods that are used for adult brains. The principles are \n",
    "similar, but the details vary. For example, instead of using the MNI152 template \n",
    "that is used in adults, we use an infant-oriented template [Shi et al, 2021](https://doi.org/10.1371/journal.pone.0018746). \n",
    "\n",
    "Furthermore, the waypoint ROIs that are used in infant data differ in their\n",
    "position from the adult. These waypoint ROIs were first defined and integrated\n",
    "into the Matlab version of AFQ in [Grotheer, 2022](https://doi.org/10.1038/s41467-022-28326-4), and and validated\n",
    "against expert-delineated tracts. The infant template and ROIs were\n",
    "subsequently integrated into pyAFQ in [Grotheer, 2023](https://doi.org/10.1073/pnas.2303491120) and the Python\n",
    "implementation was again validated with respect to these expert-delineated\n",
    "manual tracts.\n",
    "\n",
    "In this example, an example of babyAFQ is presented with data from one of the \n",
    "subjects whose data was first presented in [Grotheer, 2022](https://doi.org/10.1038/s41467-022-28326-4). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7062eb28",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path as op\n",
    "from paths import afq_home"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d93a2f79",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly\n",
    "from AFQ.api.group import GroupAFQ\n",
    "import AFQ.api.bundle_dict as abd\n",
    "import AFQ.data.fetch as afd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be9a9849",
   "metadata": {},
   "outputs": [],
   "source": [
    "baby_example_folder = op.join(afq_home, \"baby_example\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aef4ad61",
   "metadata": {},
   "source": [
    "## Initialize a GroupAFQ object\n",
    "\n",
    "Now that the data is downloaded and organized in a BIDS-compliant structure,\n",
    "we can start running pyAFQ on it. We start by initializing a GroupAFQ object.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83204a93",
   "metadata": {},
   "outputs": [],
   "source": [
    "myafq = GroupAFQ(\n",
    "    bids_path=op.join(baby_example_folder,\n",
    "                      \"example_bids_subject\"),\n",
    "    preproc_pipeline=\"vistasoft\", # These data were preprocessed with the vistasoft pipeline\n",
    "    reg_template_spec=afd.read_pediatric_templates(\n",
    "    )[\"UNCNeo-withCerebellum-for-babyAFQ\"], # Note the use of a different template\n",
    "    reg_subject_spec=\"b0\",\n",
    "    bundle_info=abd.baby_bd(), # Note use of the baby BundleDict object, which over-rides the default\n",
    "    import_tract={\n",
    "        \"suffix\": \"tractography\", \"scope\": \"mrtrix\"}, #Tractography was executed in advance with the mrtrix software\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce302544",
   "metadata": {},
   "source": [
    "## Running the pipeline\n",
    "\n",
    "A call to the `export` function will trigger the pyAFQ pipeline. This will\n",
    "run tractography, bundle segmentation, and bundle cleaning.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "519ba24e",
   "metadata": {},
   "outputs": [],
   "source": [
    "viz = myafq.export(\"all_bundles_figure\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29472950",
   "metadata": {},
   "source": [
    "## Viewing the results\n",
    "\n",
    "One way to view the results is to open the file named\n",
    "`sub-01_ses-01_dwi_space-RASMM_model-probCSD_algo-AFQ_desc-viz_dwi.html`\n",
    "in your browser.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42c767fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "plotly.io.show(viz[\"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
}
