%matplotlib inline
%config InlineBackend.figure_format='retina' 

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

import numpy as np
# don't do:
# from numpy import *

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import ipywidgets
import os
import sys
import warnings

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

warnings.filterwarnings('ignore')

"""
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
"""


df = pd.read_csv("../data/coal_prod_cleaned.csv")

df.head()

df.shape

# import qgrid # Put imports at the top
# qgrid.nbinstall(overwrite=True)

# qgrid.show_grid(df[['MSHA_ID',
#                     'Year',
#                     'Mine_Name',
#                     'Mine_State',
#                     'Mine_County']], remote_js=True)
# Check out http://nbviewer.ipython.org/github/quantopian/qgrid/blob/master/qgrid_demo.ipynb for more (including demo)

"""
Explanation: Look at Pandas Dataframes
this is italicized
End of explanation
"""


!conda install pivottablejs -y

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

df.head(10)

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


# Province, Party, Average, Age, Heatmap

from pivottablejs import pivot_ui

pivot_ui(df)

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


import numpy as np

np.random.

"""
Explanation: Tab
End of explanation
"""


np.linspace(start=, )

"""
Explanation: shift-tab
End of explanation
"""


np.linspace(50, 150, num=100,)

"""
Explanation: shift-tab-tab
(equivalent in in Lab to shift-tab)
End of explanation
"""


np.linspace(start=, )

"""
Explanation: shift-tab-tab-tab-tab
(doesn't work in lab)
End of explanation
"""


np.linspace?

"""
Explanation: ?
End of explanation
"""


np.linspace??

"""
Explanation: ??
(Lab can scroll if you click)
End of explanation
"""


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

silly_absolute_value_function(2)

silly_absolute_value_function?

silly_absolute_value_function??

"""
Explanation: Inspect everything
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

first = 1

second = 2

third = 3

# a new cell above
# b new cell below

"""
Explanation: Keyboard shortcuts
For help, ESC + h
h doesn't work in Lab
l / shift L for line numbers
End of explanation
"""


%%latex

If you want to get crazier...

\begin{equation}
\oint_S {E_n dA = \frac{1}{{\varepsilon _0 }}} Q_\textrm{inside}
\end{equation}

"""
Explanation: Headings and LaTeX
With text and $\LaTeX$ support.
$$\begin{align}
        B'&=-\nabla \times E,\
        E'&=\nabla \times B - 4\pi j
\end{align}$$
End of explanation
"""


# Indent
# Cmd + [ 
# Cmd + ]

# Comment
# Cmd + /

"""
Explanation: More markdown
End of explanation
"""


# note difference w/ lab

"""
Explanation: You can also get monospaced fonts by indenting 4 spaces:
mkdir toc
cd toc

Wrap with triple-backticks and language:
bash
mkdir toc
cd toc
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
SQL
SELECT *
FROM tablename
End of explanation
"""


%%bash
pwd 
for i in *.ipynb
do
    echo ${i} | awk -F . '{print $1}'
done

echo
echo "break"
echo

for i in *.ipynb
do
    echo $i | awk -F - '{print $2}'
done

"""
Explanation: ```sql
SELECT first_name,
       last_name,
       year_of_birth
FROM presidents
WHERE year_of_birth > 1800;
```
End of explanation
"""


%%writefile ../scripts/temp.py
from __future__ import absolute_import, division, print_function

I promise that I'm not cheating!

!cat ../scripts/temp.py

"""
Explanation: Other cell-magics
End of explanation
"""


%load_ext autoreload
%autoreload 2

example_dict = {}

# Indent/dedent/comment
for _ in range(5):
    example_dict["one"] = 1
    example_dict["two"] = 2
    example_dict["three"] = 3
    example_dict["four"] = 4

"""
Explanation: Autoreload is cool -- don't have time to give it the attention that it deserves.
https://gist.github.com/jbwhit/38c1035c48cdb1714fc8d47fa163bfae
End of explanation
"""


example_dict["one_better_name"] = 1
example_dict["two_better_name"] = 2
example_dict["three_better_name"] = 3
example_dict["four_better_name"] = 4

"""
Explanation: Multicursor magic
Hold down option, click and drag.
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)

type(XYcoef)

XYcoef**2

thing()

"""
Explanation: Find and replace -- regex notebook (or cell) wide. 
R

pyRserve
rpy2
End of explanation
"""


