%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format='retina' 

# Add this to python2 code to make life easier
from __future__ import absolute_import, division, print_function

from itertools import combinations
import string

from IPython.display import IFrame, HTML, YouTubeVideo
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.pyplot import GridSpec
import seaborn as sns
import mpld3
import numpy as np
import pandas as pd
import os, sys
import warnings

sns.set();
plt.rcParams['figure.figsize'] = (12, 8)
sns.set_style("darkgrid")
sns.set_context("poster", font_scale=1.3)

"""
Explanation: Best practices
Let's start with pep8 (https://www.python.org/dev/peps/pep-0008/)

Imports should be grouped in the following order:

standard library imports
related third party imports
local application/library specific imports

You should put a blank line between each group of imports.
Put any relevant all specification after the imports.
End of explanation
"""


YouTubeVideo("ZbrRrXiWBKc", width=400, height=300)

!conda install pivottablejs -y

df = pd.read_csv("../data/mps.csv", encoding="ISO-8859-1")

df.head(10)

from pivottablejs import pivot_ui

"""
Explanation: Pivot Tables w/ pandas
http://nicolas.kruchten.com/content/2015/09/jupyter_pivottablejs/
End of explanation
"""


pivot_ui(df)
# Province, Party, Average, Age, Heatmap

"""
Explanation: Enhanced Pandas Dataframe Display
End of explanation
"""


# in select mode, shift j/k (to select multiple cells at once)
# split cell with ctrl shift -

first = 1

second = 2

third = 3

"""
Explanation: Keyboard shortcuts
For help, ESC + h
End of explanation
"""


%%bash
pwd 
for i in *.ipynb
do
    wc $i
done
echo 
echo "break"
echo
du -h *ipynb

def silly_absolute_value_function(xval):
    """Takes a value and returns the value."""
    xval_sq = xval ** 2.0
    xval_abs = np.sqrt(xval_sq)
    return xval_abs

silly_absolute_value_function?

silly_absolute_value_function??

# shift-tab
silly_absolute_value_function()

# shift-tab-tab
silly_absolute_value_function()

# shift-tab-tab-tab
silly_absolute_value_function()

import numpy as np

np.sin??

"""
Explanation: You can also get syntax highlighting if you tell it the language that you're including: 
```bash
mkdir toc
cd toc
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.js
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.css
cd ..
jupyter-nbextension install --user toc
jupyter-nbextension enable toc/toc
```
SQL
SELECT *
FROM tablename
End of explanation
"""


import numpy as np

# !conda install -c r rpy2 -y

import rpy2

%load_ext rpy2.ipython

X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])

%%R?

%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)

XYcoef

"""
Explanation: Stop here for now
R

pyRserve
rpy2
End of explanation
"""


