Notebook created by Martín Villanueva - martin.villanueva@usm.cl
- DI UTFSM - April 2017.
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
Although Python is an open-source, cross-platform language, installing it with the usual scientific packages used to be overly complicated. Fortunately, there is now an all-in-one scientific Python distribution, Anaconda
(by Continuum Analytics), that is free, cross-platform, and easy to install.
Note: There are other distributions and installation options (like Canopy, WinPython, Python(x, y), and others).
Why to use Anaconda:
conda
manager which allows us to handle the packages and magage environments.NumPy
, SciPy
, Scikit-Learn
and others) are compiled with MKL support.Note: In this course we will use Python3
.
Download installation script here. Run in a terminal:
bash Anaconda3-4.3.1-Linux-x86_64.sh
Then modify the PATH
environment variable in your ~/.bashrc
appending the next line:
export PATH=~/anaconda3/bin:$PATH
Run source ~/.bashrc
and test your installation by calling the python interpreter!
conda install package_name
conda update package_name
conda update --all
conda search package_pattern
conda clean {--lock, --tarballs, --index-cache, --packages, --source-cache, --all}
Isolated distribution of packages.
conda create --name env_name python=version packages_to_install_in_env
conda create --name python2 python=2.7 anaconda
conda create --name python26 python=2.6 python
source activate env_name
conda info --envs
conda remove --name env_name --all
Important Note: If you install packages with pip
, they will be installed in the running environment.
For more info about conda see here
Git is a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for software development, but it can be used to keep track of changes in any files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.
Online providers supporting Git include GitHub (https://github.com), Bitbucket (https://bitbucket.org), Google code (https://code.google.com), Gitorious (https://gitorious.org), and SourceForge (https://sourceforge.net).
In order to get your git repository ready for use, follow these instructions:
mkdir project && cd project
git init
touch README
git add .
# To unstage a file, use 'git reset HEAD YOUR-FILE'.
git commit -m "First commit"
# To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
git remote add origin remote_repository_URL
# Sets the new remote
git remote -v
# Verifies the new remote URL
git push -u origin master
IPython
IPython
its just an improved version of the standard Python
shell, that provides tools for interactive computing in Python.
Here are some cool features of IPython
:
cd
, ls
, pwd
, rm
, mkdir
, etc). Additional commands can be exectuted with: !command
.who
command to see defined variables in the current session.?
.This is rich text with links, equations: $$ \hat{f}(\xi) = \int_{-\infty}^{+\infty} f(x) \, \mathrm{e}^{-i \xi x} dx, $$ code with syntax highlighting
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
images:
and plots:
In [2]:
xgrid = np.linspace(-3,3,50)
f1 = np.exp(-xgrid**2)
f2 = np.tanh(xgrid)
plt.figure(figsize=(8,6))
plt.plot(xgrid, f1, 'bo-')
plt.plot(xgrid, f2, 'ro-')
plt.title('Just a demo plot')
plt.grid()
plt.show()
IPython also comes with a sophisticated display system that lets us insert rich web elements in the notebook. Here you can see an example of how to add Youtube videos in a notebook
In [3]:
from IPython.display import YouTubeVideo
YouTubeVideo('HrxX9TBj2zY')
Out[3]:
The IPython Kernel is a separate IPython
process which is responsible for running user code, and things like computing possible completions. Frontends, like the notebook or the Qt console, communicate with the IPython Kernel
using JSON messages sent over ZeroMQ sockets.
The core execution machinery for the kernel is shared with terminal IPython:
The Notebook frontend does something extra. In addition to running your code, it stores code and output, together with markdown notes, in an editable document called a notebook. When you save it, this is sent from your browser to the notebook server, which saves it on disk as a JSON file with a .ipynb extension.
The notebook server, not the kernel, is responsible for saving and loading notebooks, so you can edit notebooks even if you don’t have the kernel for that language —you just won’t be able to run code. The kernel doesn’t know anything about the notebook document: it just gets sent cells of code to execute when the user runs them.
Kernels
There are two ways to develop a kernel for another language. Wrapper kernels reuse the communications machinery from IPython, and implement only the core execution part. Native kernels implement execution and communications in the target language:
Note: To see a list of all available kernels (and installation instructions) visit here.
It is also possible to convert the original JSON notebook to the following formats: html, latex, pdf, rst, markdown and script. For that you must run
jupyter-nbconvert --to FORMAT notebook.ipynb
with FORMAT as one of the above options. Lets convert this notebook to htlm!
In [4]:
# this will list all magic commands
%lsmagic
Out[4]:
In [5]:
# also work in ls, cd, mkdir, etc
%pwd
Out[5]:
In [6]:
%history
In [18]:
# this will execute and show the output of the program
%run ./hola_mundo.py
In [19]:
def naive_loop():
for i in range(100):
for j in range(100):
for k in range(100):
a = 1+1
return None
%timeit -n 10 naive_loop()
In [20]:
%time naive_loop()
In [10]:
%%bash
cd ..
ls
lets you capture the standard output and error output of some code into a Python variable. Here is an example (the outputs are captured in the output Python variable).
In [11]:
%%capture output
!ls
In [12]:
%%writefile myfile.txt
Holanda que talca!
In [13]:
!cat myfile.txt
!rm myfile.txt
In [14]:
from IPython.core.magic import register_cell_magic
To create a new cell magic, we create a function that takes a line (containing possible options) and a cell's contents as its arguments, and we decorate it with @register_cell_magic
.
In [15]:
@register_cell_magic
def cpp(line, cell):
"""Compile, execute C++ code, and return the
standard output."""
# We first retrieve the current IPython interpreter instance.
ip = get_ipython()
# We define the source and executable filenames.
source_filename = '_temp.cpp'
program_filename = '_temp'
# We write the code to the C++ file.
with open(source_filename, 'w') as f:
f.write(cell)
# We compile the C++ code into an executable.
compile = ip.getoutput("g++ {0:s} -o {1:s}".format(
source_filename, program_filename))
# We execute the executable and return the output.
output = ip.getoutput('./{0:s}'.format(program_filename))
print('\n'.join(output))
In [16]:
%%cpp
#include<iostream>
int main()
{
std::cout << "Hello world!";
}
This cell magic is currently only available in your interactive session. To distribute it, you need to create an IPython extension. This is a regular Python module or package that extends IPython.
To create an IPython extension, copy the definition of the cpp()
function (without the decorator) to a Python module, named cpp_ext.py
for example. Then, add the following at the end of the file:
def load_ipython_extension(ipython):
"""This function is called when the extension is loaded.
It accepts an IPython InteractiveShell instance.
We can register the magic with the `register_magic_function`
method of the shell instance."""
ipython.register_magic_function(cpp, 'cell')
Then, you can load the extension with %load_ext cpp_ext. The cpp_ext.py le needs to be in the PYTHONPATH, for example in the current directory.
In [17]:
%load_ext cpp_ext