Winpython Default checker

(earlier versions before may miss some functionalities)


In [2]:
%matplotlib inline

In [3]:
# checking Numba JIT toolchain + Mingwpy compiler toolchain
import numpy as np
image = np.zeros((1024, 1536), dtype = np.uint8)

from pylab import imshow, show
from timeit import default_timer as timer
from numba import autojit

@autojit
def mandel(x, y, max_iters):
  c = complex(x, y)
  z = 0.0j
  for i in range(max_iters):
    z = z*z + c
    if (z.real*z.real + z.imag*z.imag) >= 4:
      return i
  return max_iters

def create_fractal(min_x, max_x, min_y, max_y, image, iters , mandelx):
  height = image.shape[0]
  width = image.shape[1]
  pixel_size_x = (max_x - min_x) / width
  pixel_size_y = (max_y - min_y) / height
    
  for x in range(width):
    real = min_x + x * pixel_size_x
    for y in range(height):
      imag = min_y + y * pixel_size_y
      color = mandelx(real, imag, iters)
      image[y, x] = color
start = timer()
create_fractal(-2.0, 1.0, -1.0, 1.0, image, 20 , mandel) 
dt = timer() - start

print ("Mandelbrot created by numba in %f s" % dt)
imshow(image)
show()


Mandelbrot created by numba in 1.061223 s

In [4]:
# Cython toolchain test
%load_ext Cython

In [5]:
%%cython 
# checking a Cython compilation
def mandel_cython(x, y, max_iters):
  cdef int   i 
  cdef double   cx, cy , zx, zy
  cx , cy = x, y 
  zx , zy =0 ,0 
  for i in range(max_iters):
    zx , zy = zx*zx - zy*zy + cx , zx*zy*2 + cy
    if (zx*zx + zy*zy) >= 4:
      return i
  return max_iters


---------------------------------------------------------------------------
DistutilsPlatformError                    Traceback (most recent call last)
<ipython-input-5-961e9ee61af3> in <module>()
----> 1 get_ipython().run_cell_magic('cython', '', '# checking a Cython compilation\ndef mandel_cython(x, y, max_iters):\n  cdef int   i \n  cdef double   cx, cy , zx, zy\n  cx , cy = x, y \n  zx , zy =0 ,0 \n  for i in range(max_iters):\n    zx , zy = zx*zx - zy*zy + cx , zx*zy*2 + cy\n    if (zx*zx + zy*zy) >= 4:\n      return i\n  return max_iters')

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2129             magic_arg_s = self.var_expand(line, stack_depth)
   2130             with self.builtin_trap:
-> 2131                 result = fn(magic_arg_s, cell)
   2132             return result
   2133 

<decorator-gen-158> in cython(self, line, cell)

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\site-packages\Cython\Build\IpythonMagic.py in cython(self, line, cell)
    327 
    328         self._build_extension(extension, lib_dir, pgo_step_name='use' if args.pgo else None,
--> 329                               quiet=args.quiet)
    330 
    331         module = imp.load_dynamic(module_name, module_path)

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\site-packages\Cython\Build\IpythonMagic.py in _build_extension(self, extension, lib_dir, temp_dir, pgo_step_name, quiet)
    437             if not quiet:
    438                 old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
--> 439             build_extension.run()
    440         finally:
    441             if not quiet and old_threshold is not None:

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\command\build_ext.py in run(self)
    337 
    338         # Now actually compile and link everything.
--> 339         self.build_extensions()
    340 
    341     def check_extensions_list(self, extensions):

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\command\build_ext.py in build_extensions(self)
    446             self._build_extensions_parallel()
    447         else:
--> 448             self._build_extensions_serial()
    449 
    450     def _build_extensions_parallel(self):

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\command\build_ext.py in _build_extensions_serial(self)
    471         for ext in self.extensions:
    472             with self._filter_build_errors(ext):
--> 473                 self.build_extension(ext)
    474 
    475     @contextlib.contextmanager

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\command\build_ext.py in build_extension(self, ext)
    531                                          debug=self.debug,
    532                                          extra_postargs=extra_args,
--> 533                                          depends=ext.depends)
    534 
    535         # XXX outdated variable, kept here in case third-part code

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\_msvccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    343 
    344         if not self.initialized:
--> 345             self.initialize()
    346         compile_info = self._setup_compile(output_dir, macros, include_dirs,
    347                                            sources, depends, extra_postargs)

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\_msvccompiler.py in initialize(self, plat_name)
    236         plat_spec = PLAT_TO_VCVARS[plat_name]
    237 
--> 238         vc_env = _get_vc_env(plat_spec)
    239         if not vc_env:
    240             raise DistutilsPlatformError("Unable to find a compatible "

C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\distutils\_msvccompiler.py in _get_vc_env(plat_spec)
    132     vcvarsall, vcruntime = _find_vcvarsall(plat_spec)
    133     if not vcvarsall:
--> 134         raise DistutilsPlatformError("Unable to find vcvarsall.bat")
    135 
    136     try:

DistutilsPlatformError: Unable to find vcvarsall.bat

In [ ]:
%%cython -a
# with %%cython -a , full C-speed lines are shown in white, slowest python-speed lines are shown in dark yellow lines 
# ==> put your cython rewrite effort on dark yellow lines
def mandel_cython(x, y, max_iters):
  cdef int   i 
  cdef double   cx, cy , zx, zy
  cx , cy = x, y 
  zx , zy =0 ,0 
  for i in range(max_iters):
    zx , zy = zx*zx - zy*zy + cx , zx*zy*2 + cy
    if (zx*zx + zy*zy) >= 4:
      return i
  return max_iters

In [ ]:
start = timer()
create_fractal(-2.0, 1.0, -1.0, 1.0, image, 20 , mandel_cython) 
dt = timer() - start

print ("Mandelbrot created by cython in %f s" % dt)
imshow(image)
show()

Holoviews and Bokeh are in Winpython after July 2015 07th


In [ ]:
from holoviews import *
%load_ext holoviews.ipython
fractal = Image(image)

((fractal * HLine(y=0.16)).hist() + fractal.sample(y=0.16))

In [ ]:
# checking statsmodels
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import statsmodels.api as sm
data = sm.datasets.anes96.load_pandas()
party_ID = np.arange(7)
labels = ["Strong Democrat", "Weak Democrat", "Independent-Democrat",
          "Independent-Independent", "Independent-Republican",
          "Weak Republican", "Strong Republican"]
plt.rcParams['figure.subplot.bottom'] = 0.23  # keep labels visible
plt.rcParams['figure.figsize'] = (6.0, 4.0)  # make plot larger in notebook
age = [data.exog['age'][data.endog == id] for id in party_ID]
fig = plt.figure()
ax = fig.add_subplot(111)
plot_opts={'cutoff_val':5, 'cutoff_type':'abs',
                                'label_fontsize':'small',
                                'label_rotation':30}
sm.graphics.beanplot(age, ax=ax, labels=labels,
                     plot_opts=plot_opts)
ax.set_xlabel("Party identification of respondent")
ax.set_ylabel("Age")

In [ ]:
# lmfit test (from http://nbviewer.ipython.org/github/lmfit/lmfit-py/blob/master/examples/lmfit-model.ipynb)
import numpy as np
import matplotlib.pyplot as plt
def decay(t, N, tau):
    return N*np.exp(-t/tau)
t = np.linspace(0, 5, num=1000)
data = decay(t, 7, 3) + np.random.randn(*t.shape)

from lmfit import Model

model = Model(decay, independent_vars=['t'])
result = model.fit(data, t=t, N=10, tau=1)
plt.plot(t, data)  # data
plt.plot(t, decay(t=t, **result.values), color='orange', linewidth=5)  # best-fit model

In [ ]:
# checking seaborn (it changes matplotlib defaults, so testing last)
import seaborn as sns
sns.set()
df = sns.load_dataset("iris")
sns.pairplot(df, hue="species", size=2.5)

In [ ]:
# For PyQtgraph (testing Installed Qt library)
# double-clik in "Basic Plotting" example (the second choice going down from top options)
import pyqtgraph.examples ; pyqtgraph.examples.run()

In [ ]:
import pandas as pd
from bokeh import mpl
from bokeh.plotting import output_notebook, show
import matplotlib as mplc
# Generate the pandas dataframe
data = np.random.multivariate_normal([0, 0], [[1, 2], [2, 20]], size=100)
data = pd.DataFrame(data, columns=["X", "Y"])
mplc.rc("figure", figsize=(6, 6))

# Just plot seaborn kde
sns.kdeplot(data, cmap="BuGn_d")

plt.title("Seaborn kdeplot in bokeh.")

output_notebook()

show(mpl.to_bokeh())

In [ ]:
# checking sympy 
import sympy
a=sympy.Symbol('a')
b=sympy.Symbol('b')
e=(a+b)**5
print (e.expand())

In [6]:
# checking Scipy and Scikit-learn
import numpy as np
X = np.random.random((1000, 3))

from scipy.spatial.distance import cdist
%timeit cdist(X, X)

from sklearn.metrics import euclidean_distances
%timeit euclidean_distances(X, X)


9.42 ms ± 333 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
15.8 ms ± 474 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [7]:
# checking Ipython Notebook interactivity.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from IPython.html.widgets import interactive
from IPython.display import Audio, display
def beat_freq(f1=220.0, f2=224.0):
    max_time = 3
    rate = 8000
    times = np.linspace(0,max_time,rate*max_time)
    signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)
    print(f1, f2, abs(f1-f2))
    display(Audio(data=signal, rate=rate))
    return signal
v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))
display(v)


C:\Program Files\WinPython-64bit-3.6.5.0\python-3.6.5.amd64\lib\site-packages\IPython\html.py:14: ShimWarning: The `IPython.html` package has been deprecated since IPython 4.0. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.
  "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)

SQL tools


In [8]:
# checking Ipython-sql, sqlparse, SQLalchemy
%load_ext sql

In [9]:
%%sql sqlite:///.baresql.db
DROP TABLE IF EXISTS writer;
CREATE TABLE writer (first_name, last_name, year_of_death);
INSERT INTO writer VALUES ('William', 'Shakespeare', 1616);
INSERT INTO writer VALUES ('Bertold', 'Brecht', 1956);
SELECT  * , sqlite_version() as sqlite_version from Writer order by Year_of_death


Done.
Done.
1 rows affected.
1 rows affected.
Done.
Out[9]:
first_name last_name year_of_death sqlite_version
William Shakespeare 1616 3.21.0
Bertold Brecht 1956 3.21.0

In [ ]:
# checking sqlite_bro: this should lanch a separate non-browser window with sqlite_bro's welcome
!cmd start cmd /C sqlite_bro

In [ ]:
# checking baresql
from __future__ import print_function, unicode_literals, division  # line needed only if Python2.7
from baresql import baresql
bsql = baresql.baresql(connection="sqlite:///.baresql.db")
bsqldf = lambda q: bsql.df(q, dict(globals(),**locals()))

users = ['Alexander', 'Billy', 'Charles', 'Danielle', 'Esmeralda', 'Franz', 'Greg']
#  We use the python 'users' list like a SQL table
sql = "select 'Welcome ' ||  c0 || ' !' as say_hello, length(c0) as name_length from users$$ where c0 like '%a%' "
bsqldf(sql)

In [ ]:
# checking db.py
from db import DB
db=DB(dbtype="sqlite", filename=".baresql.db")
db.query("select  sqlite_version() as sqlite_version  ;")

In [ ]:
db.tables

In [ ]:
# checking Web Scraping: beautifulsoup and requests 
import requests
from bs4 import BeautifulSoup

URL = 'http://en.wikipedia.org/wiki/Franklin,_Tennessee'

req = requests.get(URL, headers={'User-Agent' : "Mining the Social Web"})
soup = BeautifulSoup(req.text, "lxml")

geoTag = soup.find(True, 'geo')

if geoTag and len(geoTag) > 1:
    lat = geoTag.find(True, 'latitude').string
    lon = geoTag.find(True, 'longitude').string
    print ('Location is at', lat, lon)
elif geoTag and len(geoTag) == 1:
    (lat, lon) = geoTag.string.split(';')
    (lat, lon) = (lat.strip(), lon.strip())
    print ('Location is at', lat, lon)
else:
    print ('No location found')

In [ ]:
# checking nbconvert 
!ipython nbconvert "Beginner's FAQ.ipynb" --to html

In [ ]:
%%HTML
<iframe src="Beginner's FAQ.html" width=700 height350></iframe>

Operations Research


In [ ]:
# Pulp example : minimizing the weight to carry 99 pennies
#                (from Philip I Thomas)
# see https://www.youtube.com/watch?v=UmMn-N5w-lI#t=995
# Import PuLP modeler functions
from pulp import *
# The prob variable is created to contain the problem data        
prob = LpProblem("99 pennies Problem",LpMinimize)

# Variables represent how many of each coin we want to carry
pennies = LpVariable("Number of pennies",0,None,LpInteger)
nickels = LpVariable("Number of nickels",0,None,LpInteger)
dimes = LpVariable("Number of dimes",0,None,LpInteger)
quarters = LpVariable("Number of quarters",0,None,LpInteger)

# The objective function is added to 'prob' first

# we want to minimize (LpMinimize) this 
prob += 2.5 * pennies + 5 * nickels + 2.268 * dimes + 5.670 * quarters, "Total coins Weight"

# We want exactly 99 cents
prob += 1 * pennies + 5 * nickels + 10 * dimes + 25 * quarters == 99, ""

# The problem data is written to an .lp file
prob.writeLP("99cents.lp")
prob.solve()

# print ("status",LpStatus[prob.status] )
print ("Minimal Weight to carry exactly 99 pennies is %s grams" % value(prob.objective))
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print (v.name, "=", v.varValue)

Deep Learning


In [ ]:
# Checking Theano
import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
f([[1, 2], [3, 4]], [[10, 20], [30, 40]])

Wrap-up


In [ ]:
# optional scipy full test (takes up to 10 minutes)
#!cmd /C start cmd /k python.exe  -c "import scipy;scipy.test()"

In [ ]:
import IPython;IPython.__version__