2014-05-16 21:33:40 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from IPython.core.display import HTML
|
|
|
|
import matplotlib.pylab as pylab
|
2014-12-01 00:34:06 +01:00
|
|
|
import matplotlib.pyplot as plt
|
2014-12-22 22:34:50 +01:00
|
|
|
import json
|
2015-01-11 09:12:21 +01:00
|
|
|
import numpy as np
|
2015-01-13 16:17:45 +01:00
|
|
|
import sys
|
2015-02-22 20:33:51 +01:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2014-12-14 05:05:06 +01:00
|
|
|
|
2015-01-28 00:14:06 +01:00
|
|
|
sys.path.insert(0,'./code') # allow us to import book_format
|
|
|
|
|
2014-12-14 05:05:06 +01:00
|
|
|
def test_filterpy_version():
|
|
|
|
import filterpy
|
2015-05-30 23:44:49 +02:00
|
|
|
min_version = [0,0,18]
|
2014-12-14 05:05:06 +01:00
|
|
|
v = filterpy.__version__
|
|
|
|
tokens = v.split('.')
|
|
|
|
for i,v in enumerate(tokens):
|
|
|
|
if int(v) > min_version[i]:
|
|
|
|
return
|
|
|
|
|
|
|
|
i = len(tokens) - 1
|
|
|
|
if min_version[i] > int(tokens[i]):
|
|
|
|
raise Exception("Minimum FilterPy version supported is {}.{}.{}.\n"
|
2015-05-30 23:44:49 +02:00
|
|
|
"Please install a more recent version.\n"
|
|
|
|
" ex: pip install filterpy --upgrade".format(
|
2014-12-14 05:05:06 +01:00
|
|
|
*min_version))
|
|
|
|
v = int(tokens[0]*1000)
|
|
|
|
|
|
|
|
|
|
|
|
# ensure that we have the correct filterpy loaded. This is
|
|
|
|
# called when this module is imported at the top of each book
|
|
|
|
# chapter so the reader can see that they need to update FilterPy.
|
|
|
|
test_filterpy_version()
|
|
|
|
|
2014-05-16 21:33:40 +02:00
|
|
|
|
2014-12-01 00:34:06 +01:00
|
|
|
def equal_axis():
|
|
|
|
pylab.rcParams['figure.figsize'] = 10,10
|
|
|
|
plt.axis('equal')
|
|
|
|
|
2015-04-05 21:26:21 +02:00
|
|
|
|
2014-12-01 00:34:06 +01:00
|
|
|
def reset_axis():
|
2015-04-05 21:26:21 +02:00
|
|
|
pylab.rcParams['figure.figsize'] = 11, 4
|
2014-12-01 00:34:06 +01:00
|
|
|
|
2015-04-05 21:26:21 +02:00
|
|
|
def set_figsize(x=11, y=4):
|
2015-02-18 01:10:24 +01:00
|
|
|
pylab.rcParams['figure.figsize'] = x, y
|
2015-02-22 20:33:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2015-04-05 21:26:21 +02:00
|
|
|
def figsize(x=11, y=4):
|
2015-02-22 20:33:51 +01:00
|
|
|
"""Temporarily set the figure size using 'with figsize(a,b):'"""
|
|
|
|
|
2015-04-05 21:26:21 +02:00
|
|
|
size = pylab.rcParams['figure.figsize']
|
|
|
|
set_figsize(x, y)
|
2015-02-22 20:33:51 +01:00
|
|
|
yield
|
2015-04-05 21:26:21 +02:00
|
|
|
pylab.rcParams['figure.figsize'] = size
|
2015-02-22 20:33:51 +01:00
|
|
|
|
2015-02-28 06:21:54 +01:00
|
|
|
@contextmanager
|
|
|
|
def numpy_precision(precision):
|
|
|
|
old = np.get_printoptions()['precision']
|
|
|
|
np.set_printoptions(precision=precision)
|
|
|
|
yield
|
|
|
|
np.set_printoptions(old)
|
|
|
|
|
2015-06-07 04:49:56 +02:00
|
|
|
@contextmanager
|
|
|
|
def printoptions(*args, **kwargs):
|
|
|
|
original = np.get_printoptions()
|
|
|
|
np.set_printoptions(*args, **kwargs)
|
|
|
|
yield
|
|
|
|
np.set_printoptions(**original)
|
2015-02-28 06:21:54 +01:00
|
|
|
|
2014-12-31 19:56:09 +01:00
|
|
|
def _decode_list(data):
|
|
|
|
rv = []
|
|
|
|
for item in data:
|
|
|
|
if isinstance(item, unicode):
|
|
|
|
item = item.encode('utf-8')
|
|
|
|
elif isinstance(item, list):
|
|
|
|
item = _decode_list(item)
|
|
|
|
elif isinstance(item, dict):
|
|
|
|
item = _decode_dict(item)
|
|
|
|
rv.append(item)
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def _decode_dict(data):
|
|
|
|
rv = {}
|
|
|
|
for key, value in data.iteritems():
|
|
|
|
if isinstance(key, unicode):
|
|
|
|
key = key.encode('utf-8')
|
|
|
|
if isinstance(value, unicode):
|
|
|
|
value = value.encode('utf-8')
|
|
|
|
elif isinstance(value, list):
|
|
|
|
value = _decode_list(value)
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
value = _decode_dict(value)
|
|
|
|
rv[key] = value
|
|
|
|
return rv
|
|
|
|
|
2015-01-13 16:17:45 +01:00
|
|
|
|
2015-01-27 18:51:52 +01:00
|
|
|
def load_style(directory = '.', name='/styles/custom2.css'):
|
2015-01-13 16:17:45 +01:00
|
|
|
if sys.version_info[0] >= 3:
|
2015-01-27 18:51:52 +01:00
|
|
|
s = json.load(open(directory + "/code/538.json"))
|
2015-01-13 16:17:45 +01:00
|
|
|
else:
|
2015-01-27 18:51:52 +01:00
|
|
|
s = json.load(open(directory + "/code/538.json"), object_hook=_decode_dict)
|
2015-01-13 16:17:45 +01:00
|
|
|
plt.rcParams.update(s)
|
|
|
|
reset_axis ()
|
|
|
|
np.set_printoptions(suppress=True)
|
|
|
|
|
2015-01-27 18:51:52 +01:00
|
|
|
styles = open(directory + name, 'r').read()
|
2015-01-27 18:24:52 +01:00
|
|
|
return HTML(styles)
|