{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Introduction to Scientific Computing\n", "### Lecture 07: Core modules in Python and Excercises\n", "#### J.R. Gladden, Spring 2018, Univ. of Mississippi" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "There are **many** modules that come with every Python distribution - so called \"core modules\". These library provide tools for all kinds of tasks such as working with files on the computer, establishing a network connection, get time information, and many other things.\n", "\n", "We'll explore the modules\n", "- os\n", "- sys\n", "- time\n", "- glob\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "The **os** module provides access to information to the local operating system" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/joshgladden/Box Sync/ScientificComputing/Spring2018/lecs_code\n", "posix\n" ] } ], "source": [ "print(os.getcwd())\n", "print(os.name)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "The **sys** module has information about the python system" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.7.13 |Enthought, Inc. (x86_64)| (default, Mar 2 2017, 08:20:50) \n", "[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]\n", "\n", "\n", "['', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python27.zip', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-darwin', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-mac', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-tk', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-old', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/lib-dynload', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages', '/Users/joshgladden/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages/IPython/extensions', '/Users/joshgladden/.ipython']\n" ] } ], "source": [ "print(sys.version)\n", "print('\\n')\n", "print(sys.path)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "The **time** module provides access to time information." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1518383221.33\n", "time.struct_time(tm_year=2018, tm_mon=2, tm_mday=11, tm_hour=21, tm_min=7, tm_sec=1, tm_wday=6, tm_yday=42, tm_isdst=0)\n", "The elapsed time is 3.001 seconds\n" ] } ], "source": [ "print(time.time())\n", "print(time.gmtime())\n", "startTime = time.time()\n", "time.sleep(3)\n", "endTime = time.time()\n", "print('The elapsed time is %2.3f seconds'%(endTime-startTime))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "The **glob** module is useful for dealing with directory contents like listing files of a certain type." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "import glob" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['newdata.dat',\n", " 'mynewfile.dat',\n", " 'mydata.dat',\n", " 'testdata2.dat',\n", " 'data.dat',\n", " 'oldfile.dat',\n", " 'speedup_PI.dat',\n", " 'zunzunData.dat',\n", " 'newfile.dat']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydatFiles = glob.glob('*.dat')\n", "mydatFiles" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "**Excercise 1:** Time stamper should do the following:\n", "- Take in a file extension as an argument, and path as optional 2nd argument (default to current directory)\n", "- Read in a list of those files in the current directory\n", "- Add a time stamp to their names\n", "- Make a new directory with the current date as name \n", "- copy all the files into that directory with the time stamped names.\n", "\n", "** Note: ** Because it is not possible to pass command line arguments to the program in a Jupyter notebook, I am just listing the code in the cell below. It'll produce an error if you run it as an iPython notebook. You should save it to a python file (say called 'lec07_timestamper.py' and run it with the %run magic and include the argument (see the cell below)\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "ename": "OSError", "evalue": "[Errno 20] Not a directory: '/Users/joshgladden/Library/Jupyter/runtime/kernel-334b87b9-483a-4cd9-9b71-3856e61ab290.json'", "output_type": "error", "traceback": [ "\u001b[0;31m\u001b[0m", "\u001b[0;31mOSError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mfiletype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Too many arguments! Usage: 'timestamper.py .dat [ ./somedata/ ]\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mOSError\u001b[0m: [Errno 20] Not a directory: '/Users/joshgladden/Library/Jupyter/runtime/kernel-334b87b9-483a-4cd9-9b71-3856e61ab290.json'" ] } ], "source": [ "import os,sys,glob,time,subprocess\n", "\n", "origpath=os.getcwd()\n", "\n", "args=sys.argv\n", "if len(args) < 2:\n", " print \"A 3 character file extension is required (such as .dat).\\n Quitting..\"\n", " sys.exit()\n", "elif len(args) == 2:\n", " filetype = args[1]\n", "elif len(args) == 3:\n", " filetype = args[1]\n", " path = args[2]\n", " os.chdir(path)\n", "else:\n", " print \"Too many arguments! Usage: 'timestamper.py .dat [ ./somedata/ ]\"\n", " sys.exit()\n", "\t\t\n", "#Add preceeding dot for lazy users\n", "if filetype[0] != '.': filetype = '.'+filetype\n", "\n", "#Get local time down to day and make new directory\n", "ct = time.localtime()\n", "mon = ct.tm_mon\n", "day = ct.tm_mday #mday is day of the MONTH (could also use yday or wday for year or week)\n", "year = ct.tm_year\n", "dirname = 'FilesFor_%02i_%02i_%i'%(mon,day,year)\n", "\n", "#Note if directory already exists, mkdir() will return an error. \n", "#This checks if if directory exists and makes it if not\n", "if len(glob.glob(dirname))<1: \n", " print \"Making a new directory: \" + dirname\n", " os.mkdir(dirname)\n", "else: print \"Directory: \" + dirname + \" already exists.\"\n", "\n", "#Make a list of files and process them in a loop\n", "files = glob.glob('*'+filetype)\n", "\n", "for file in files:\n", " ct = time.localtime()\n", " hour = ct.tm_hour\n", " min = ct.tm_min\n", " insertion = '_%02i_%02i_%i_%02ihr_%02imin'%(mon,day,year,hour,min)\n", " rootname=file[:-4]\n", " newname=rootname+insertion+filetype\n", " subprocess.call(['cp',file,dirname+'/'+newname])\n", " print \"File %s has been renamed %s.\" % (file,newname)\n", "\t\n", "# Change back to original directory\n", "os.chdir(origpath)\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Making a new directory: FilesFor_02_11_2018\n", "File myplot.png has been renamed myplot_02_11_2018_15hr_19min.png.\n", "File banner_small.png has been renamed banner_small_02_11_2018_15hr_19min.png.\n", "File test.png has been renamed test_02_11_2018_15hr_19min.png.\n" ] } ], "source": [ "%run lec07_timestamper.py .png" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "** Excercise 2: Logistic Growth**\n", "This is a population growth model that links the current population to the population at the previous time step. Time step is indicated by the subscript i here. \n", "\n", "$ x_i = x_{i-1} + \\frac{\\rho}{100} x_{i-1} \\left( 1 - \\frac{x_{i-1}}{M} \\right)$\n", "\n", "I present two methods for coding this. The first uses standard python lists to build up the data and converts them to arrays for plotting. The second creates empty numpy arrays. Not suprsingly the arrays are faster!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Time for method 1: 1.10507 ms\n", "Time for method 2: 1.50108 ms\n" ] } ], "source": [ "%matplotlib wx\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import time\n", " \n", "def loggrowth(x,rho,M):\n", " newx=x + rho/(100.)*x*(1-x/float(M))\n", " return newx\n", "\n", "\t \n", "startT=time.time()\n", "rho=1.0\n", "M=1500.0\n", "tsteps=1000\n", "# Make sequences lists\n", "tlist=range(0,tsteps,1)\n", "pop=[100]\n", "for t in tlist[1:]:\n", " newpop=loggrowth(pop[-1],rho,M)\n", " pop.append(newpop)\n", "\n", "pop=np.array(pop)\n", "times=np.array(tlist)\n", "meth1T = time.time() - startT \n", "plt.plot(times,pop,'b-',label='Using Lists')\n", "\n", "#Another method: make sequences arrays\n", "startT=time.time()\n", "indexes=range(tsteps+1)\n", "pop=np.zeros(len(indexes))\n", "pop[0]=100\n", "for i in indexes[1:]:\n", " \tpop[i] = loggrowth(pop[i-1],rho,M)\n", "\n", "meth2T = time.time() - startT \n", "plt.plot(indexes,pop,'go',label='Using Arrays')\n", "plt.legend(loc=2)\n", "\n", "print('Time for method 1: %3.5f ms'% (meth1T*1000.))\n", "print('Time for method 2: %3.5f ms' % (meth2T*1000.))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "(1000,)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pop.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 0 }