{ "metadata": { "name": "", "signature": "sha256:f666f2c941c4cda539e80f9c93a9c8a259b080cb672c58735c09100b36cb563f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# - compatibility with Python 3\n", "from __future__ import print_function # print('me') instead of print 'me'\n", "from __future__ import division # 1/2 == 0.5, not 0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# - show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# - import common modules\n", "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# - set gray colormap and nearest neighbor interpolation by default\n", "plt.rcParams['image.cmap'] = 'gray'\n", "plt.rcParams['image.interpolation'] = 'nearest'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# - import numpy.linalg with a shorter name\n", "import numpy.linalg as npl" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Saving images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to workon on `ds114_sub009_t2r1.nii`. This is a 4D functional image." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For our processing stream, we want to start by removing the first functional volume from this 4D image. We previously found that this volume was very different from the following volumes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load the functional image\n", "# Get the 4D array from the image\n", "# Delete the first volume by slicing the data\n", "# Save the resulting image as `fds114_sub009_t2r1.nii`\n", "import nibabel as nib\n", "img = nib.load('ds114_sub009_t2r1.nii')\n", "data = img.get_data()\n", "fixed_img = nib.Nifti1Image(data[..., 1:], img.affine, img.header)\n", "nib.save(fixed_img, 'fds114_sub009_t2r1.nii')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course we want to check what we did!\n", "\n", "Load the new `fds114_sub009_t2r1.nii` and check that the data for this array really is equal to the data from the original image, with the first volume removed." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load `fds114_sub009_t2r1.nii`\n", "# Use `assert` to raise an error unless new image array \n", "# data is equal to original data with first volume dropped\n", "new_img = nib.load('fds114_sub009_t2r1.nii')\n", "assert np.all(new_img.get_data() == img.get_data()[..., 1:])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 } ], "metadata": {} } ] }