{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spherical Mean Representation of any Compartment Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The idea of Spherical Mean models is to fit the spherical mean of a model to the spherical mean of the data, rather than fitting the separate DWIs per shell. As such, spherical mean models have no concept of orientation, dispersion or crossings. *(Kaden et al. 2015)* proposed the spherical mean technique by providing the analytic expression of the Zeppelin model. Later, *(Kaden et al. 2016)* combined the spherical mean model of a Zeppelin and Stick model to create his multi-compartment spherical mean technique.\n",
    "\n",
    "Taking a step forward, Dmipy allows to estimate the spherical mean representation of any Compartment Model, and combine them in a MultiCompartmentSphericalMeanModel in the same way as a standard MultiCompartmentModel. The spherical mean for a model is estimated as follows:\n",
    "- If available, the spherical mean for a model is estimated using an analytic expression (e.g. for C1Stick, G2Zeppelin and G3RestrictedZeppelin).\n",
    "- For round models such as G1Ball or restricted sphere models, the spherical mean is just a value for the given shell parameters.\n",
    "- For cylinder models without analytic expressions, we estimate the spherical mean using the its rotational_harmonics_representation. In short, the spherical mean of any spherical function (if sufficiently expanded) is equal to $c_{m=0}^{l=0} / (2 \\sqrt{\\pi})$, where $c_{m=0}^{l=0}$ is the zeroth order spherical harmonics coefficient.\n",
    "\n",
    "A model's spherical mean for a given acquisition scheme can be called using model.spherical_mean(acquisition_scheme, **parameters). To illustrate, we show the composition of the well-known Wu-Minn HCP acquisition scheme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Acquisition scheme summary\n",
      "\n",
      "total number of measurements: 288\n",
      "number of b0 measurements: 18\n",
      "number of DWI shells: 3\n",
      "\n",
      "shell_index |# of DWIs |bvalue [s/mm^2] |gradient strength [mT/m] |delta [ms] |Delta[ms] |TE[ms]\n",
      "0           |18        |0               |0                        |10.6       |43.1      |N/A  \n",
      "1           |90        |1000            |56                       |10.6       |43.1      |N/A  \n",
      "2           |90        |2000            |79                       |10.6       |43.1      |N/A  \n",
      "3           |90        |3000            |97                       |10.6       |43.1      |N/A  \n"
     ]
    }
   ],
   "source": [
    "# as an example we estimate the spherical mean of the Gaussian phase cylinder model.\n",
    "from dmipy.data.saved_acquisition_schemes import wu_minn_hcp_acquisition_scheme\n",
    "scheme = wu_minn_hcp_acquisition_scheme()\n",
    "scheme.print_acquisition_info"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can be seen that this scheme has 4 measured shells (including the b0 measurements). This means that the spherical mean for any of dmipy's models will also only have 4 values. To illustrate, we estimate the spherical mean of the Gaussian phase cylinder approximation (which does not have an analytical representation):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  0.63540094,  0.47616228,  0.3917526 ])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dmipy.signal_models import cylinder_models\n",
    "cylinder = cylinder_models.C4CylinderGaussianPhaseApproximation()\n",
    "\n",
    "diameter = 2e-6  # diameter of 2 microns\n",
    "lambda_par = 1.7e-9  # parallel intra-axonal diffusivity\n",
    "cylinder.spherical_mean(scheme, diameter=diameter, lambda_par=lambda_par)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that for the spherical mean respresentation it is not necessary to give the model a value for its orientation 'mu'. This is because a spherical mean model has no concept of orientation (as it has been integrated out of the model representation). Currently, the dmipy models that have a spherical mean representation are the following:\n",
    "- cylinder_models.C1Stick\n",
    "- cylinder_models.C2CylinderStejskalTannerApproximation\n",
    "- cylinder_models.C3CylinderCallaghanApproximation\n",
    "- cylinder_models.C4CylinderGaussianPhaseApproximation\n",
    "- gaussian_models.G1Ball\n",
    "- gaussian_models.G2Zeppelin\n",
    "- gaussian_models.G3TemporalZeppelin\n",
    "- sphere_models.S1Dot\n",
    "- sphere_models.S2SphereStejskalTannerApproximation\n",
    "- sphere_models.S4SphereGaussianPhaseApproximation\n",
    "\n",
    "In addition, the spherical mean is also available for any GammaDistributed model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Constructing a MultiCompartmentSphericalMeanModel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A MultiCompartmentModel for spherical mean representations of models is done by calling MultiCompartmentSphericalMeanModel with the same parameters as the MultiCompartmentModel. For example, making a spherical mean representation of the Multi Compartment Microscopic Diffusion Imaging (MC-MDI) *(Kaden et al. 2016)* model is done as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['G2Zeppelin_1_lambda_perp',\n",
       " 'C1Stick_1_lambda_par',\n",
       " 'G2Zeppelin_1_lambda_par',\n",
       " 'partial_volume_0',\n",
       " 'partial_volume_1']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dmipy.signal_models import cylinder_models, gaussian_models\n",
    "from dmipy.core.modeling_framework import MultiCompartmentSphericalMeanModel\n",
    "stick = cylinder_models.C1Stick()\n",
    "zeppelin = gaussian_models.G2Zeppelin()\n",
    "mc_mdi_model = MultiCompartmentSphericalMeanModel(\n",
    "    models=[stick, zeppelin])\n",
    "mc_mdi_model.parameter_names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the spherical mean representation has no orientation parameters 'mu'. Adding tortuosity and equality constraints on the model parameters is done the same as before:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['G2Zeppelin_1_lambda_par', 'partial_volume_0', 'partial_volume_1']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mc_mdi_model.set_tortuous_parameter('G2Zeppelin_1_lambda_perp',\n",
    "                                    'C1Stick_1_lambda_par',\n",
    "                                    'partial_volume_0',\n",
    "                                    'partial_volume_1')\n",
    "mc_mdi_model.set_equal_parameter('G2Zeppelin_1_lambda_par',\n",
    "                                 'C1Stick_1_lambda_par')\n",
    "mc_mdi_model.parameter_names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With this the spherical mean model can be fitted to dMRI data as mc_mdi_model.fit(scheme, data). The application of this model on HCP data is given in the [MC-MDI model example](http://nbviewer.jupyter.org/github/AthenaEPI/mipy/blob/master/examples/example_multi_compartment_spherical_mean_technique.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "- Kaden, Enrico, Frithjof Kruggel, and Daniel C. Alexander. \"Quantitative mapping of the per‐axon diffusion coefficients in brain white matter.\" Magnetic resonance in medicine 75.4 (2016): 1752-1763.\n",
    "- Kaden, Enrico, et al. \"Multi-compartment microscopic diffusion imaging.\" NeuroImage 139 (2016): 346-359."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}