{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Today's Agenda\n", "* Basic bash commands\n", "* Paths\n", "* Jupyter notebooks\n", "* Python 2 vs Python 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Info\n", "\n", "* Make sure you have completed the form: [https://goo.gl/y2ammQ](https://goo.gl/y2ammQ) to be added to the __Slack__ group and __Github Group__.\n", "* Mike and I will be meeting to finalize the schedule and start the interactive part of the __course__.\n", "* Starting this week, the notebooks will be available the __Sunday__ before the class, so that you know the schedule ahead of time and decide if you want to join or not.\n", "* We added a new plug-in [__Anamak Bot__](http://anamakbot.com) to make it easier to ask questions if you want to remain anonymous.\n", "* Next week we'll start with the interactive session of _Python_. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Before we begin, let's ...\n", "\n", "First, you will need to __clone__ the existing repository for the course.\n", "\n", "```bash\n", "cd /Path/to/Repositories/folder\n", "```\n", "Then clone the repository [https://github.com/VandyAstroML/Vanderbilt_Computational_Bootcamp](https://github.com/VandyAstroML/Vanderbilt_Computational_Bootcamp)\n", "```bash\n", "git clone https://github.com/VandyAstroML/Vanderbilt_Computational_Bootcamp\n", "```\n", "\n", "And now _pull_ all the changes to the repository\n", "```bash\n", "git pull\n", "```\n", "\n", "Now, whenever you do \"`git pull`\" from inside that directory, you'll have the updated version of the repository and notebooks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to bash\n", "\n", "bash is a command-line interpreter or shell that instructs the operating system to carry out commands on a line-by-line basis, or when written together as scripts and is run in a terminal. bash is a Unix-based shell that we will be focusing on, but there are others out there (such as tcsh). If you're not sure what shell you're using, running the commands 'echo \\$0' or 'ps -p \\$\\$' will *usually* tell you. In bash, \"\\$\" denotes at the start of a string denotes a variable.\n", "\n", "\n", "### Basic bash commands\n", "
pwd
- Returns the pathway that you are currently in (i.e. how to get to your current directory)\n",
"cd [path]
- Change the path you are in\n",
"ls
- list all files in the current location\n",
"locate [file]
- find file with a particular name\n",
"cp [file1] [file2]
- Copy a file to another place (original file remains)\n",
"mv [file1] [file2]
- Move a file to another place (original file is gone)\n",
"mkdir [name]
- Create a new directory\n",
"rm [file1]
- Remove a file **WARNING: Using this incorrectly, you can delete EVERYTHING**\n",
"rmdir [directory]
- Remove a directory. This one will only delete __empty__ directories, so it's not that useful after all\n",
"man [command]
- Opens the manual page for a file for full usage instructions and all options available. You can also use '[command] --help' for most standard commands to see how to use them\n",
"echo [string/variable]
- prints the string or variable to the terminal\n",
"head [file]
- prints the first 10 lines of the file to screen\n",
"tail [file]
- prints the last 10 lines of the file to screen\n",
"cat [file]
- prints out the content of [file] to the screen.\n",
"PATH=$PATH:newdirectorypath
\n",
"After adding this, every time you start a terminal, this location will be part of the directories that will be checked for executibles.\n",
"\n",
"An example for this is _Ananconda_. It appends the Anaconda directory to your '~/.bashrc' file.\n",
"![Anaconda_Path](./images/Anaconda_PATH.png)\n",
"\n",
"### Aliases\n",
"\n",
"Aliases are ways to call a set of commands that you use frequently with a short nickname. In Linux systems, these are often stored in the .bashrc file (a file that contains the settings for when you run a bash terminal) or in .profile (a file that contains settings for running shell scripts). For Macs, these are stored in .bash_profile (again, contains settings for running shell scripts, but will not be seen by shells other than bash). We can talk about them later, but useful to know they're an available tool.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding aliases to your `~/.bashrc` file\n",
"\n",
"Normally, you add _aliases_ like e.g.\n",
"```bash\n",
"alias somename=' cd /useful/directory/for/research'\n",
"```\n",
"and then you can use \"somename\" to access to that directory.\n",
"\n",
"But that is works for the current shell session. But normally you want to be able to access these aliases _at any given time_. So you need to __add it to your ~/.bashrc file__."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. __First create an \"~/.aliases file__\n",
"```bash\n",
"touch ~/.aliases\n",
"```\n",
"2. Open the file using your preferred text editors. I use _vim_ for this example\n",
"```bash\n",
"vim ~/.aliases\n",
"```\n",
"3. Add useful aliases to the file.\n",
"![Aliases in my _.aliases_ file](./images/aliases_vc.png)\n",
"4. Add it to your __~/.bashrc__ file by _sourcing_ it.\n",
"```bash\n",
"vim ~/.bashrc\n",
"```\n",
"![Sourcing Aliases to bashrc file](./images/source_aliases.png)\n",
"\n",
"Everytime that you start a new shell session, you will be able to use the aliases in the _~/.aliases_ file.\n",
"\n",
"Some __useful__ aliases for are:\n",
"\n",
"```bash\n",
"alias lll='ls -lah' # Lists files and directories (including hidden ones) as a list\n",
"alias lla=lll # Same as above\n",
"alias llh='ls -lh' # Lists files and directories (only visible ones) in a human-readable format\n",
"alias llt='ls -lahtr' # Lists files based on _date of last modified_\n",
"alias LS='ls' # In case you misspell \"ls\" \n",
"alias sl='ls' # In case you misspell \"ls\" \n",
"alias SL=ls # In case you misspell \"ls\" \n",
"alias CLEAR='clear'\n",
"alias clc='clear'\n",
"alias CLC='clear'\n",
"alias en='emacs -nw' # Opens up a new \"Emacs\" window\n",
"alias pushd='cd -' # Returns to the previous directory\n",
"alias untar_file='tar -zxvf $1' # Untars a file to the current directory\n",
"\n",
"# You can also use functions as aliases\n",
"scppics() {\n",
" scp -rp username@host.address:$1 /path/to/destination/folder\n",
" }\n",
"\n",
"git_ref(){\n",
" git remote -v\n",
" git fetch upstream\n",
" git checkout master\n",
" git merge upstream/master\n",
" git merge upstream/master\n",
" }\n",
"\n",
"# You can add the ssh-keys for easy access to Github, or other computers\n",
"alias skeys='ssh-add ~/.ssh/id_rsa; ssh-add ~/.ssh/personalid; ssh-add ~/.ssh/vpac; ssh-add ~/.ssh/github_vandy'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quick bash tutorial\n",
"\n",
"To go over some of the commands, we're going to try doing some basic operations with some bash commands. To do this, first open up a terminal window. Run one of the commands from earlier to check that you're running bash by default. If you're not, you *should* be able to start a bash shell by just running 'bash'.\n",
"\n",
"Now we'll set up a quick directory to play in. I'll call it compshop\n",
"mkdir compshop
\n",
"And then go into this directory\n",
"cd compshop
\n",
"We can quickly check to see the full path of where we are now\n",
"pwd
\n",
"And we can confirm that right now the file is empty, too\n",
"ls
\n",
"Because we're working with computers, we have to print this to the screen at some point....\n",
"echo Hello, World!
\n",
"Since a lot of what bash is useful for is moving files around, we'll make a file to work with. We can also have echo write to a file by adding '> [filename]' at the end of the command.\n",
"echo Hello, World! > filegreeting
\n",
"A quick check again with ls and we'll see we now have one file in the directory. We can also see that that file contains exactly what we wanted it to\n",
"head filegreeting
\n",
"We can make a second copy of this file\n",
"cp filegreeting filegreeting2
\n",
"Checking ls again, we now have two files. If we wanted to rename one of them, we can just use the mv command for that\n",
"mv filegreeting2 filegreetings
\n",
"Check ls again, and we still have two files, but we changed the name of one of them. If we want to see more functionality about what ls can do, there's always google, but we can also check from the command line too. Try each of these:\n",
"man ls
\n",
"ls --help
\n",
"You'll see that they both start off with how we have to use ls, and then include a lot of information about all the different options we can run. For example, we can list all files, even those that would normally be hidden, by running this:\n",
"ls -a
\n",
"If we want to see the last modification dates and file sizes of all the files, we can run\n",
"ls -l
\n",
"We can even combine these two, so we see all files (even ones that might be hidden) and get their sizes, dates, etc.\n",
"ls -al
\n",
"Finally, as we've tried out most of the bash commands here, we'll get rid of these two files. We can just give a list of files to be removed.\n",
"rm filegreeting filegreetings
\n",
"\n",
"All of these operations can get a lot more complex or sophisticated (like echo did when we had it write to a file), but these are a lot of the most common commands to show up in bash."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jupyter notebooks\n",
"\n",
"A Jupyter notebook is a file (like this one) that contains cells that can contain either plain text or code. So far, we've only been using plain text, but as we start working with Python, we can explain stuff in the text, and then you'll be able to run the sections that contain code and see what happens, as well as change the code to see how those changes effect the results. The first example of this is going to be in the last section today.\n",
"\n",
"### Running Jupyter notebooks\n",
"\n",
"There's two ways of viewing Jupyter notebooks, both of which take place inside a browser. On the github we're using, the notebooks can be ran directly on github. In this sense, it works the same as simply visiting a website. However, this will only allow you to view it as a static page. It won't allow you to use any of the interactive features of a Jupyter notebook. For that, you'll need to use the second method, which takes place locally.\n",
"\n",
"The second way requires having Jupyter downloaded, although Anaconda should have included this. From here, you may have an icon to start Jupyter. You can also start it from the terminal by running 'jupyter notebook'. This starts a jupyter server, and will also open up a tab in your default browser. Using that default browser, go through your directories to locate the notebook file that you want to run (you can also run 'jupyter notebook' while you're in the directory that file is in to speed this up).\n",
"\n",
"\n",
"### Create Jupyter notebooks\n",
"\n",
"Creating a Jupyter notebook file is very similar to the second way of accessing a Jupyter notebook file. As before, you have to run 'jupyter notebook' to start up the Jupyter server. In this case, though, you go to the directory you want the file to be located, and then select 'new' in the upper-right corner and select a Python notebook.\n",
"\n",
"For now, we won't go into the details of formatting within a Jupyter notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to create a new iPython notebook, one starts one by"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```bash\n",
" $: cd /path/to/directory\n",
" $: jupyter notebook \n",
"```\n",
"\n",
"This will start a _kernel_ in the current directory.\n",
"![iPython Notebook](./images/iPython_notebook_Screenshot.png)\n",
"\n",
"You then click on \"_New > Python 2_\" (or Python 3 if you have that one installed) and it will start a new iPython notebook.\n",
"![iPython Notebook](./images/iPython_notebook_Screenshot_2.png)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/victor2/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n",
" warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n"
]
}
],
"source": [
"# You can easily run and debug your code\n",
"%matplotlib inline\n",
"\n",
"from __future__ import (absolute_import, division,\n",
" print_function, unicode_literals)\n",
"\n",
"import matplotlib\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"x = np.arange(50)\n",
"y = x.copy()\n",
"\n",
"print( len(x))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24\n",
" 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n"
]
}
],
"source": [
"# Printing arrays\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"