2+4

print("hello")

a=2
print("Hello world!")

"""
Explanation: <img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/rc_logo.png' style="height:75px">
Data Analysis and Visualization with the IPython Notebook
<img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/data_overview.png' style="height:500px">
Materials from Monte Lunacek and Thomas Hauser tutorials
Objectives

Become familiar with the IPython Notebook.
Introduce the IPython landscape.
Getting started with visualization and data analysis in Python

Conducting reproducible data analysis, visualization and computing experiments


How do you currently:

wrangle data?
visualize results?
Analysis: machine learning, stats
Parallel computing
Big data



What is Python?
<blockquote>
<p>
Python is a general-purpose programming language that blends procedural, functional, and object-oriented paradigms
<p>
Mark Lutz, <a href="http://www.amazon.com/Learning-Python-Edition-Mark-Lutz/dp/1449355730">Learning Python</a>
</blockquote>


Simple, clean syntax
Easy to learn
Interpreted
Strong, dynamically typed
Runs everywhere: Linux, Mac, and Windows
Free and open
Expressive: do more with fewer lines of code
Lean: modules
Options: Procedural, object-oriented, and functional.

Abstractions

Python provides high-level abstraction
Performance can be on par with compiled code if right approach is used

<img src="https://s3.amazonaws.com/research_computing_tutorials/matrix_multiply_compare.png" style="margin:5px auto; height:400px; display:block;">
IPython and the Jupyter Notebook
IPython

Platform for interactive computing
Shell or browser-based notebook
Project Jupyter: https://jupyter.org
Language independent notebook
Can be used with R, Julia, bash ...

Jupyter IPython Notebook
http://blog.fperez.org/2012/01/ipython-notebook-historical.html
Interactive web-based computing, data analysis, and documentation.

One document for code and output
Run locally and remote
Document process
Share results

<img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/traditional_python.png'>
<img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/ipython-notebook.png'>
Integrate Code and Documentation

Data structure ouput
Inline plots
Conversation sytle programming (Literate programming)

Telling a data story


Great for iterative programming.

Data analysis
Quick scripts
Prototyping



2 type of cells:

Markdown for documentation
Markdown can contain LaTeX for equations


Code for execution programs



Markdown

Github flavored markdown: https://help.github.com/articles/github-flavored-markdown/
Markdown basics: https://help.github.com/articles/markdown-basics/

Here is a formula: 
$f(x,y) = x^2 + e^x$
Images
&lt;img src='https://s3.amazonaws.com/research_computing_tutorials/monty-python.png' width="300"&gt;
This is an image:
<img src='https://s3.amazonaws.com/research_computing_tutorials/monty-python.png' width="300">
Code
End of explanation
"""


%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(10000)
print(x)

"""
Explanation: Locally and Remote

Run locally
Connect to the cloud (e.g AWS)
Connect to supercomputer (e.g. XSEDE Resource)
Add compute power:
mpi4py
IPython Parallel
spark big distributed data
Numbapro GPU
...



Documentation and Sharing
<img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/ipython-notebook-sharing.png'>
Keyboard Shortcuts
<img src='https://raw.githubusercontent.com/scientific-visualization-2016/ClassMaterials/master/Images/ipython-notebook-keyboard.png'>
Embeded Plots
End of explanation
"""


plt.hist(x, bins=50)
plt.show()

"""
Explanation: Plot a Histogram of x
End of explanation
"""


%lsmagic

%timeit y = np.random.randn(100000)

%ls

"""
Explanation: Notebooks can be customized

Custom CSS
Custom javascript libraries
Create your own output format.
Tools and workflow

Magic Commands

Built-in useful functions
% line commands
%% cell commands
End of explanation
"""


%%bash
ls -l

files = !ls # But glob is a better way
print files[:5]


"""
Explanation: Other Languages: Bash
End of explanation
"""


%%writefile example.cpp
#include <iostream>

int main(){
    std::cout << "hello from c++" << std::endl;
}

%ls example.cpp

%%bash
g++ example.cpp -o example
./example

"""
Explanation: Keep it all together
End of explanation
"""


!ipython nbconvert --to 'PDF' 01_introduction-IPython-notebook.ipynb

!open 01_introduction-IPython-notebook.pdf

!ipython nbconvert --to 'html' 01_introduction-IPython-notebook.ipynb

"""
Explanation: NBconvert examples

HTML
PDF (print) - you have to have LaTex installed
Slides
Dynamic Slides
ReStructured Text (sphinx)
End of explanation
"""


