Day 3

  • Computational Physics (PHYS 202)
  • Cal Poly, Spring 2015
  • Brian E. Granger

In class

Outline

Cookies and web authentication

  • When you log into a website, the remote web server sends back a small bit of data called a cookie.
  • Anytime you request a new page or resource from that website, your web browser sends the cookie back to the web server.
  • The web server uses the cookie to verify that you are logged in.
  • Having the cookie is equivalent with being you.
  • The web browser saves the cookie while you are logged in, and anyone can view it.
  • When you log out, the web browser deletes the cookie.
  • If you forget to logout from a website, the next person who comes along to that browser will be logged on as you and can steal your cookie.
  • Incognito/Private browsing mode starts with no cookies, ends with no cookies.
  • Always use Incognito/Private mode on shared computers.

Log onto JupyterHub on dirac1

Bash/Terminal basics

The UNIX/LINUX terminal or shell provides a command line interface to a UNIX/LINUX computer. Bash is a common, and often default, programming language for the UNIX shell.

There are a couple of ways you can run terminal/bash command:

  1. You can access a full blown terminal from the Jupyter Notebook by selecting Terminal from the New dropdown in the Files tab of the dashboard.
  2. Running cells in a notebook that is using the Bash kernel.
  3. Prefixing commands with ! in a Python kernel.

To see the different terminal commands that are available on your system, you can use the ls command in the following direcot

In UNIX systems, the different commands you can run are typically put into various bin directories:

  • /usr/bin
  • /usr/local/bin

Here are the commands in /usr/local/bin:


In [1]:
!ls /usr/local/bin


acs_destripe		 iptest			pyflakes
autopep8		 iptest2		pygmentize
bower			 iptest3		pyraf
calcos			 ipython		resetbits
configurable-http-proxy  ipython2		rst2html.py
convertwaiveredfits	 ipython3		rst2latex.py
cygdb			 isympy			rst2man.py
cython			 jsonpointer		rst2odt_prepstyles.py
cythonize		 jsonschema		rst2odt.py
easy_install		 jupyterhub		rst2pseudoxml.py
easy_install-2.7	 jupyterhub-singleuser	rst2s5.py
easy_install-3.4	 lessc			rst2xetex.py
f2py			 mdriz			rst2xml.py
f2py3			 nbgrader		rstpep2html.py
fits2bitmap		 nosetests		runastrodriz
fitscheck		 nosetests-2.7		samp_hub
fitsdiff		 nosetests-3.4		skivi
fitsheader		 pep8			sphinx-apidoc
grin			 pilconvert.py		sphinx-autogen
grind			 pildriver.py		sphinx-build
ipcluster		 pilfile.py		sphinx-quickstart
ipcluster2		 pilfont.py		stscidocs
ipcluster3		 pilprint.py		tdspysyn
ipcontroller		 pip			timefilter
ipcontroller2		 pip2			updatenpol
ipcontroller3		 pip2.7			volint
ipengine		 pip3			wcslint
ipengine2		 pip3.4
ipengine3		 pybabel

You can always find out where a command lives using which:


In [2]:
!which python2


/usr/bin/python2

Paths

In UNIX, files and directories are found according to their path. An example of a path is /usr/bin/python2 above. Some details about paths:

  • Path components are separated by / (unlike \ on Windows)
  • The root of the filesystem is always a single /
  • Absolute paths start with /
  • Relative paths don't begin with / and are relative to your current working directory
  • Paths are case sensitive (foo is not the same as Foo or FOO)
  • You can put spaces in your paths, but life is easier without them
  • The path component . refers to the cwd
  • The path component .. refers to the parent of the cwd
  • The path component ~ refers to your home directory
  • The path component * matches any path

Your current working directory can be found using pwd:


In [3]:
!pwd


/nfshome/bgranger/phys202/phys202-2015/Resources/Schedule

ls

To list the contents of your current working directory or a path, use the ls command.

List the current working directory:


In [4]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__  test

List your home directory:


In [5]:
!ls ~


Desktop  iraf  nbgrader  phys202

You can add the -l flag to show more details:


In [6]:
!ls -l ~


total 16
drwxr-xr-x 2 bgranger domain^users 4096 Apr 10  2014 Desktop
drwxr-xr-x 3 bgranger domain^users 4096 Apr  4  2014 iraf
drwxr-xr-x 3 bgranger domain^users 4096 Apr  6 08:02 nbgrader
drwxr-xr-x 5 bgranger domain^users 4096 Mar 31 22:48 phys202

Files beginning with a . in their filename are hidden by default. You can show them using the -a flag:


In [7]:
!ls -a ~


.	       .dbus	    .git-credential-cache  .matlab	  .ssh
..	       Desktop	    .ipynb_checkpoints	   .matplotlib	  .subversion
.aptitude      .distlib     .ipython		   .mozilla	  .virtualenvs
.bash_history  .felix	    iraf		   .nano_history  .w3m
.bash_logout   .fontconfig  .java		   nbgrader	  .Xauthority
.bashrc        .gconf	    .k5login		   phys202
.cache	       .gconfd	    .local		   .profile
.config        .gitconfig   .Mathematica	   .root_hist

Notice how your home directory has many hidden files. These are typically used to configure various programs on your system. You can edit these files in the notebook using a URL like the following:

/user/bgranger/edit/nfshome/bgranger/.bashrc

cd

The cd command is used the change directories.

touch and mkdir

The touch command is used to create a new empty file, or update the timestamp of an existing file:


In [8]:
!touch testfile.py

The mkdir command is used to create a new empty directory:


In [9]:
!mkdir testdir

In [10]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__  test  testdir  testfile.py

rm

The rm command is used to remove files or directories. Remove the test file we created above:


In [11]:
!rm testfile.py

To remove a directory, you have to add the -rf flags:


In [12]:
!rm -rf testdir

In [13]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__  test

cp and mv

The cp command is used to copy a file or files to a new location, while preserving the original.


In [26]:
!touch foo.py

In [27]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  foo.py  __pycache__  test

In [28]:
!cp foo.py foocopy.py

In [29]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  foocopy.py  foo.py  __pycache__	test

In [30]:
!rm foo.py foocopy.py

In [31]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__  test

The mv command is used to move a file to a new location.

tree

The tree command provides a nice visual representation of a path hierarchy:


In [20]:
!tree -L 2 ~


/nfshome/bgranger
├── Desktop
├── iraf
│   ├── login.cl
│   └── uparm
├── nbgrader
│   └── phys202-2015
└── phys202
    ├── C4P
    ├── phys202-2015
    └── phys202-2015-work

9 directories, 1 file

Editing files

Files can be edited in two ways:

  1. Using a command line editor, such as nano or vim. We will use nano for this course.
  2. Using the Jupyter Notebook by clicking on the file.

Environment variables

Bash uses a set of environment variables to configure how various commands operate. To see your current variables, use env:


In [21]:
!env


USER=bgranger
HOME=/nfshome/bgranger
PAGER=cat
TERM=xterm-color
COLUMNS=80
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
LANG=en_US.UTF-8
SHELL=/bin/bash
GIT_PAGER=cat
CLICOLOR=1
PWD=/nfshome/bgranger/phys202/phys202-2015/Resources/Schedule
JPY_PARENT_PID=1129
LINES=24

Notice how environment variables are typically UPPERCASE_WITH_UNDERSCORES. To set an env var, you need use the export command in one of the Bash configuration files:

  • .bashrc
  • .bash_profile

The syntax looks like this:

export FOO=bar

Before the variable will show in env you have to restart Bash.

Exercise

  • Set the EDITOR variable in .bashrc to nano and verify it is set using env
  • Create a phys202 directory in your home directory

Exercise

  • Create your own phys202-2015-work repo and clone locally
    • Pick a license (I suggest MIT)
    • Pick a language for .gitignore
  • Create days and assignments subdirectories
  • Copy PLEDGE.md to your phys202-2015-work and sign it.
  • Add .ipynb_checkpoints to your .gitignore file
  • Add, commit and push everything.

nbgrader

Project Jupyter's nbgrader command will be used in this course to manage course content and assignments. It enables:

  • Instructors to release content and assignments to student.
  • Students to list and fetch content and assignments.
  • Students to submit completed assignments.
  • Instructors to automate the grading of collected/submitted assignments.

In order to use nbgrader, you need to know the course_id for your course.

The course_id for this course is phys202-2015.

To list the assignments in the nbgrader exchange for your course_id:


In [34]:
!nbgrader list phys202-2015


[ListApp | INFO] Released assignments:
[ListApp | INFO] phys202-2015 test

Now fetch the test assignment into your current working directory:


In [35]:
!nbgrader fetch phys202-2015 test


[FetchApp | INFO] Source: /srv/nbgrader/exchange/phys202-2015/outbound/test
[FetchApp | INFO] Destination: /nfshome/bgranger/phys202/phys202-2015/Resources/Schedule/test
[FetchApp | INFO] Fetched as: phys202-2015 test

Note that this creates a directory of that name in the current working directory:


In [36]:
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__  test

To submit a completed assignment to your instructor, you will first need to make sure you are in the parent directory of the assignment you want to submit. Put another way, if you want to submit the test assignment, you should see the test directory when you run ls.

Go ahead and submit the test assignment:


In [37]:
!nbgrader submit test phys202-2015


[SubmitApp | INFO] Source: /nfshome/bgranger/phys202/phys202-2015/Resources/Schedule/test
[SubmitApp | INFO] Destination: /srv/nbgrader/exchange/phys202-2015/inbound/bgranger+test+2015-04-06 17:44:22 UTC
[SubmitApp | INFO] Submitted as: phys202-2015 test 2015-04-06 17:44:22 UTC

Each time you submit an assignment, it is timestamped with the current time. If you submit an assignment multiple times, your instructor will always see the most recent submission. These timestamps are also used to track late work.


In [38]:
!rm -r test
!ls


Day01.ipynb  Day02.ipynb  Day03.ipynb  __pycache__

Exercise

To practice using nbgrader fetch, use it to fetch the following bundles:

  • day01, day02, day03 into the days folder of your phys202-2015-work repo.
  • assignment01 into the assignments folder of your phys202-2015-work repo.

To verify that it has worked, run tree from the root directory of your phys202-2015-work repo.

Once you have the content in the right place, add and commit these changes to your git repo. Finally, push the changes to GitHub.

Out of class

  • Complete assignment01 by the begining of class on Wednesday.
  • To submit assignment01 for grading, run the following from your assignments directory:

      nbgrader submit assignment01 phys202-2015