{ "metadata": { "name": "", "signature": "sha256:eaf5d18001dd13ae573a75945dd42a10ff31de5d6643a67c041f55b2358b7871" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modules and scripts" ] }, { "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import nibabel as nib" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say I have a function that loads a 4D image, and a list with mean values across all voxels in each volume:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def vol_means(image_fname):\n", " img = nib.load(image_fname)\n", " data = img.get_data()\n", " means = []\n", " for i in range(data.shape[-1]):\n", " vol = data[..., i]\n", " means.append(np.mean(vol))\n", " return means" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that works:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_means = vol_means('ds107_sub012_t1r2.nii')\n", "plt.plot(my_means)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This seems like a useful function to me. I want to make a module called `mymodule` so I can do this:\n", "\n", "```\n", "import mymodule\n", "means = mymodule.vol_means('ds107_sub012_t1r2.nii')\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact, what I would really like to do, is to be able to have a script called `my_script.py` that prints the scan means out to the terminal window like this:\n", "\n", "```\n", "import mymodule\n", "means = mymodule.vol_means('ds107_sub012_t1r2.nii')\n", "print(means)\n", "```\n", "\n", "And I want to be able to run it like this from the command line:\n", "\n", "```\n", "python my_script.py\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Python *module* is a Python file with functions (and other things) that other modules and scripts may use. These other modules and scripts will `import` the module.\n", "\n", "A script is a file that you can run with `python my_script.py` that will do some processing, and usually print something to the screen, put up a plot or save some data to the filesystem.\n", "\n", "You store modules in files ending in `.py`. For example, the module `mymodule` would be in a file called `mymodule.py`.\n", "\n", "A module needs to 'import' everything it uses." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can store scripts with any extension, but it is common to use `.py` extensions for Python scripts as well." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's go to the text editor and the terminal, and create the module `mymodule.py` and the script `my_script.py`." ] } ], "metadata": {} } ] }