{ "metadata": { "name": "classify_regions_using_features" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Classifying brain regions based on neurosynth features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we will classify two regions of the brain (ROIs) based on neurosynth features (i.e. words) using a linear support vector machine.\n", "In particular, we'll classify studies that activate medial motor regions versus those that activate ventromedial PFC. \n", "\n", "Using Nifti masks, we will select studies that activiate only each of the two regions, train a classifier to classify between the two sets of studies, and use cross-validation to estimate the classifier's performance.\n", "\n", "First, we need to load a dataset. Let's assume you have already generated one and saved it. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "from neurosynth.base.dataset import Dataset\n", "dataset = Dataset.load('../data/pickled.pkl')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using the function \"classify_regions\" in neurosynth.analysis. Let's take a look at the argument structure:\n", "\n", "classify_regions only has two required arguments: a neurosynth dataset and a list of Nifit binary masks to use for selecting studies to classify.\n", "Let's give it our dataset and the path to two masks:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from neurosynth.analysis import classify\n", "results = classify.classify_regions(dataset, [\"../neurosynth/tests/data/medial_motor.nii.gz\", \"../neurosynth/tests/data/vmPFC.nii.gz\"])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "classify_regions will by default return a Python dictionary with various elements of the classification results.\n", "\n", "First, we can look at the number of studies selected for each mask:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "results['n']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "{0: 2164, 1: 388}" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means there are 2164 studies in the first class (medial_motor) and 388 in the second (vmPFC).\n", "Next, lets see how well we classifier by looking at the score, which is by default overall accuracy." ] }, { "cell_type": "code", "collapsed": false, "input": [ "results['score']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "0.74216300940438873" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hey! Not bad, pretty high accuracy. Before we get too excited, let's look a little deeper into classify_region's default behavior by looking at the argument structure:\n", "\n", "classify_regions(dataset, masks, method='SVM', threshold=0.001, remove_overlap=True, regularization='scale', output='summary', studies=None, features=None, class_weight=True, classifier=None, cross_val=None):\n", "\n", "Let's focus on the first few arguments: by default, a support vector machine is used as the classifier method, studies that activate more than 0.1% of voxels within a mask are included, studies that activate both regions are removed, the feature vectors are regularized to having unit variance and a summary dictionary is returned.\n", "\n", "Let's try changing the method. Importantly, we need a baseline to compare to. For that, we can use a *Dummy classifier*.\n", "Dummy classifier use very simple strategies to classify (such as picking the most frequent class) and serve as a good baseline that is specific to your data. We can try this type of classifier by specifying `method = \"Dummy\"`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "results = classify.classify_regions(dataset, [\"../neurosynth/tests/data/medial_motor.nii.gz\", \"../neurosynth/tests/data/vmPFC.nii.gz\"], method=\"Dummy\")\n", "results['score']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "0.74020376175548597" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uh oh, well now it looks like our classifier is not doing much better than our dummy classifier. This means we're not getting much meaningful signal over and above that one class appears more often than the other. \n", "\n", "Maybe we need to be more selective in selecting studies by raising the threshold:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "classify.classify_regions(dataset, [\"../neurosynth/tests/data/medial_motor.nii.gz\", \"../neurosynth/tests/data/vmPFC.nii.gz\"], threshold=0.1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "{'n': {0: 875, 1: 262}, 'score': 0.69570051890289097}" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our accuracy is now 69% and we only have around 1100 total observations. Let's compare to a Dummy classifier to make sense of this" ] }, { "cell_type": "code", "collapsed": false, "input": [ "classify.classify_regions(dataset, [\"../neurosynth/tests/data/medial_motor.nii.gz\", \"../neurosynth/tests/data/vmPFC.nii.gz\"], threshold=0.1, method=\"Dummy\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 7, "text": [ "{'n': {0: 875, 1: 262}, 'score': 0.57870953792933033}" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like now we're classifier a bit better with a SVM than with a Dummy classifier. Great!\n", "To really get at this we'd need to run a permutation test to see if they are significantly different but for now, let's rejoice!" ] } ], "metadata": {} } ] }