{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# White matter challenge prediction\n", "\n", "This notebook generates a prediction for the [ISBI2015 White Matter Challenge](http://cmic.cs.ucl.ac.uk/wmmchallenge/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import all the necessary components from other libraries, including `numpy`, a module from `scikit-learn` and functions from `dipy` for managing gradient tables:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import sklearn.linear_model as lm\n", "from dipy.core import gradients as grad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following imports are from our current implementation of the RS-SFM model" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import utils\n", "from model import Model, BiExponentialIsotropicModel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A utility function reads the data from the files. We read in the 'seen' data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = utils.read_data()\n", "seen_bvals = data['seen']['bvals']\n", "seen_bvecs = data['seen']['bvecs']\n", "seen_delta = data['seen']['delta']\n", "seen_Delta = data['seen']['Delta']\n", "seen_te = data['seen']['TE']\n", "seen_g = data['seen']['g']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And create a gradient table to describe the conditions used to collect the seen data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "seen_gtab = grad.gradient_table(seen_bvals, \n", " seen_bvecs, \n", " big_delta=seen_Delta, \n", " small_delta=seen_delta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, a gradient table is created for the unseen data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "unseen_bvals = data['unseen']['bvals']\n", "unseen_bvecs = data['unseen']['bvecs']\n", "unseen_delta = data['unseen']['delta']\n", "unseen_Delta = data['unseen']['Delta']\n", "unseen_te = data['unseen']['TE']\n", "unseen_g = data['unseen']['g']\n", "\n", "unseen_gtab = grad.gradient_table(unseen_bvals, \n", " unseen_bvecs, \n", " big_delta=unseen_Delta, \n", " small_delta=unseen_delta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model object used to fit the data is initialized with the gradient table for the seen data" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "model = Model(seen_gtab, \n", " isotropic=BiExponentialIsotropicModel, \n", " solver=lm.ElasticNet(l1_ratio=0.4, \n", " alpha=5e-7, \n", " positive=True, \n", " warm_start=True, \n", " fit_intercept=True, \n", " normalize=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We initialize a list to contain the predictions" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "predict = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each iteration of the following loop, the model is fit to the seen data from a different voxel, and a prediction is made for this voxel in the unseen conditions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/arokem/anaconda/lib/python3.5/site-packages/sklearn/linear_model/coordinate_descent.py:466: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations\n", " ConvergenceWarning)\n" ] } ], "source": [ "for vox_idx in range(6):\n", " signal = data['seen']['signal'][:, vox_idx]\n", " fit = model.fit(signal, seen_te, seen_g)\n", " predict.append(fit.predict(unseen_gtab, unseen_te))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The prediction is saved in the expected format" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.savetxt('unseenSignal.txt', np.array(predict).T, fmt='%.4f', header='\\%-voxel1---voxel2---voxel3---voxel4---voxel5---voxel6')" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }