from ipywidgets import interact, fixed

from sympy import init_printing, Symbol, Eq, factor
init_printing(use_latex=True)

x = Symbol('x')

def factorit(n):
    return Eq(x**n-1, factor(x**n-1))

interact(factorit, n=(2,40))

# Import matplotlib (plotting), skimage (image processing) and interact (user interfaces)
# This enables their use in the Notebook.
%matplotlib inline
from matplotlib import pyplot as plt

from skimage import data
from skimage.feature import blob_doh
from skimage.color import rgb2gray

# Extract the first 500px square of the Hubble Deep Field.
image = data.hubble_deep_field()[0:500, 0:500]
image_gray = rgb2gray(image)

def plot_blobs(max_sigma=30, threshold=0.1, gray=False):
    """
    Plot the image and the blobs that have been found.
    """
    blobs = blob_doh(image_gray, max_sigma=max_sigma, threshold=threshold)
    
    fig, ax = plt.subplots(figsize=(8,8))
    ax.set_title('Galaxies in the Hubble Deep Field')
    
    if gray:
        ax.imshow(image_gray, interpolation='nearest', cmap='gray_r')
        circle_color = 'red'
    else:
        ax.imshow(image, interpolation='nearest')
        circle_color = 'yellow'
    for blob in blobs:
        y, x, r = blob
        c = plt.Circle((x, y), r, color=circle_color, linewidth=2, fill=False)
        ax.add_patch(c)

interact(plot_blobs, max_sigma=(10, 40, 2), threshold=(0.005, 0.02, 0.001))

"""
Explanation: Python for fun and profit

Juan Luis Cano Rodríguez
Madrid, 2016-05-13 @ ETS Asset Management Factory
Outline

Introduction
Python for Data Science
Python for IT
General advice
Conclusions

Outline

Introduction

Python for Data Science


Interactive computation with Jupyter

Numerical analysis with NumPy, SciPy
Visualization with matplotlib and others
Data manipulation with pandas

Machine Learning with scikit-learn


Python for IT


Data gathering with Requests and Scrapy

Information extraction with lxml, BeautifulSoup and others
User interfaces with PyQt, xlwings and others

Other: memcached, SOA


General advice


Python packaging


The future of Python


Conclusions


&gt;&gt;&gt; print(self)
<img src="static/pyconse.jpg" width="350px" style="float: right" />

Almost Aerospace Engineer
Quant Developer for BBVA at Indizen
Writer and furious tweeter at Pybonacci
Chair ~~and BDFL~~ of Python España non-profit
Co-creator and charismatic leader of AeroPython (*not the Lorena Barba course)
When time permits (rare) writes some open source Python code

Python for Data Science
<img src="static/scipy_eco.png" width="350px" style="float: right" />

Python is a dynamic, interpreted* language that is easy to learn
Very popular in science, research
Rich ecosystem of packages that interoperate
Multiple languages are used (FORTRAN, C/C++) and wrapped from Python for a convenient interface

Jupyter


Interactive computation environment in a browser
Traces its roots to IPython, created in 2001
Nowadays it's language-agnostic (40 languages)

Jupyter

Notebook
Exporting
Interactive Widgets
Slides
Extensions https://github.com/ipython-contrib/IPython-notebook-extensions

It's a notebook!

Code is computed in cells
These can contain text, code, images, videos...
All resulting plots can be integrated in the interface
We can export it to different formats using nbconvert or from the UI

It's interactive!
End of explanation
"""


import numpy as np

my_list  = list(range(0,100000))
res1 = %timeit -o sum(my_list)

array = np.arange(0, 100000)
res2 = %timeit -o np.sum(array)

res1.best / res2.best

"""
Explanation: It's highly extensible!


Some extensions https://github.com/ipython-contrib/IPython-notebook-extensions
A thorough guide http://mindtrove.info/4-ways-to-extend-jupyter-notebook/

NumPy
<img src="static/numpy.png" width="350px" style="float: right" />

N-dimensional data structure.
Homogeneously typed.
Efficient!

A universal function (or ufunc for short) is a function that operates on ndarrays. It is a “vectorized function".
End of explanation
"""


# This line integrates matplotlib with the notebook
%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np
x = np.linspace(-2, 10)
plt.plot(x, np.sin(x) / x)

def g(x, y):
    return np.cos(x) + np.sin(y) ** 2

x = np.linspace(-2, 3, 1000)
y = np.linspace(-2, 3, 1000)

xx, yy = np.meshgrid(x, y)
zz = g(xx, yy)

fig = plt.figure(figsize=(6, 6))
cs = plt.contourf(xx, yy, zz, np.linspace(-1, 2, 13), cmap=plt.cm.viridis)
plt.colorbar()

cs = plt.contour(xx, yy, zz, np.linspace(-1, 2, 13), colors='k')

plt.clabel(cs)

plt.xlabel("x")
plt.ylabel("y")
plt.title(r"Function $g(x, y) = \cos{x} + \sin^2{y}$")
plt.close()

fig

"""
Explanation: NumPy is much more:
<img src="static/broadcast_visual.png" width="350px" style="float: right" />

Advanced manipulation tricks: broadcasting, fancy indexing
Functions: generalized linear algebra, Fast Fourier transforms
Use case:
In-memory, fits-in-my-computer, homogeneous data
Easily vectorized operations

SciPy
<img src="static/scipy2016.png" width="350px" style="float: right" />
General purpose scientific computing library

scipy.linalg: ATLAS LAPACK and BLAS libraries
scipy.stats: distributions, statistical functions...
scipy.integrate: integration of functions and ODEs
scipy.optimization: local and global optimization, fitting, root finding...
scipy.interpolate: interpolation, splines...
scipy.fftpack: Fourier trasnforms
scipy.signal: Signal processing
scipy.special: Special functions
scipy.io: Reading/Writing scientific formats


matplotlib
<img src="static/matplotlib.png" width="350px" style="float: right" />

The father of all Python visualization packages
Modeled after MATLAB API
Powerful and versatile, but often complex and not so well documented
Undergoing a deep default style change
End of explanation
"""


import numpy as np
import pandas as pd

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
df

"""
Explanation: There are many alternatives to matplotlib, each one with its use cases, design decisions, and tradeoffs. Here are some of them:
<img src="static/encrucijada.jpg" width="350px" style="float: right" />

seaborn: High level layer on top of matplotlib, easier API and beautiful defaults for common visualizations
ggplot: For those who prefer R-like plotting (API and appearance)
plotly: 2D and 3D interactive plots in the browser as a web service
Bokeh: targets modern web browsers and big data
pyqtgraph: Qt embedding, realtime plots

Others: pygal, mpld3, bqplot...
Use the best tool for the job! And in case of doubt, just get matplotlib :)
pandas
<img src="static/pydata_cover.jpg" width="350px" style="float: right" />

High-performance, easy-to-use data structures and data analysis
Inspired by R DataFrames
Not just NumPy on steroids
Input/Output functions for a variety of formats
SQL and query-like operations
End of explanation
"""


