{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n#  Run a meta-analytic coactivation modeling analysis\n\nMeta-analytic coactivation modeling (MACM) is a common coordinate-based\nanalysis in which task-independent \"connectivity\" is assessed by selecting\nstudies within a larger database based on locations of report coordinates.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport numpy as np\nimport nibabel as nib\nfrom nilearn import plotting, image, datasets\n\nimport nimare\nfrom nimare.tests.utils import get_test_data_path"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load Dataset\n--------------------------------------------------\nWe will assume that the Neurosynth database has already been downloaded and\nconverted to a NiMARE dataset.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dset_file = 'neurosynth_nimare_with_abstracts.pkl.gz'\ndset = nimare.dataset.Dataset.load(dset_file)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define a region of interest\n--------------------------------------------------\nWe'll use the right amygdala from the Harvard-Oxford atlas\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "atlas = datasets.fetch_atlas_harvard_oxford('sub-maxprob-thr50-2mm')\nimg = nib.load(atlas['maps'])\n\nroi_idx = atlas['labels'].index('Right Amygdala')\nimg_vals = np.unique(img.get_fdata())\nroi_val = img_vals[roi_idx]\nroi_img = image.math_img('img1 == {}'.format(roi_val), img1=img)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Select studies with a reported coordinate in the ROI\n----------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "roi_ids = dset.get_studies_by_mask(roi_img)\ndset_sel = dset.slice(roi_ids)\nprint('{}/{} studies report at least one coordinate in the '\n      'ROI'.format(len(roi_ids), len(dset.ids)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Select studies with *no* reported coordinates in the ROI\n--------------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "no_roi_ids = list(set(dset.ids).difference(roi_ids))\ndset_unsel = dset.slice(no_roi_ids)\nprint('{}/{} studies report zero coordinates in the '\n      'ROI'.format(len(no_roi_ids), len(dset.ids)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "MKDA Chi2 with FWE correction\n--------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mkda = nimare.meta.cbma.mkda.MKDAChi2(kernel__r=10)\nmkda.fit(dset_sel, dset_unsel)\n\ncorr = nimare.correct.FWECorrector(method='montecarlo', n_iters=10000)\ncres = corr.transform(mkda.results)\n\n# We want the \"specificity\" map (2-way chi-square between sel and unsel)\nplotting.plot_stat_map(\n    cres.get_map('logp_level-cluster_corr-FWE_method-montecarlo'),\n    threshold=3., draw_cross=False, cmap='RdBu_r'\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "SCALE\n--------------------------------------------------\nAnother good option for a MACM analysis is the SCALE algorithm, which was\ndesigned specifically for MACM. Unfortunately, SCALE does not support\nmultiple-comparisons correction.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# First, we must define our null model of reported coordinates in the literature.\n# We will use the IJK coordinates in Neurosynth\nijk = dset.coordinates[['i', 'j', 'k']].values\nscale = nimare.meta.cbma.ale.SCALE(ijk=ijk, n_iters=10000, kernel__n=20)\nscale.fit(dset_sel)\nplotting.plot_stat_map(\n    scale.results.get_map('z_vthresh'),\n    draw_cross=False, cmap='RdBu_r'\n)"
      ]
    }
  ],
  "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}