Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91869# -*- coding: utf-8 -*-1from IPython.core.display import HTML2import matplotlib.pylab as pylab3import matplotlib.pyplot as plt4import json5import numpy as np6import sys7from contextlib import contextmanager8910sys.path.insert(0,'./code') # allow us to import book_format1112def test_filterpy_version():13import filterpy14min_version = [0,0,22]15v = filterpy.__version__16tokens = v.split('.')17for i,v in enumerate(tokens):18if int(v) > min_version[i]:19return2021i = len(tokens) - 122if min_version[i] > int(tokens[i]):23raise Exception("Minimum FilterPy version supported is {}.{}.{}.\n"24"Please install a more recent version.\n"25" ex: pip install filterpy --upgrade".format(26*min_version))27v = int(tokens[0]*1000)282930# ensure that we have the correct filterpy loaded. This is31# called when this module is imported at the top of each book32# chapter so the reader can see that they need to update FilterPy.33test_filterpy_version()343536def equal_axis():37pylab.rcParams['figure.figsize'] = 10,1038plt.axis('equal')394041def reset_axis():42pylab.rcParams['figure.figsize'] = 11, 44344def set_figsize(x=11, y=4):45pylab.rcParams['figure.figsize'] = x, y464748@contextmanager49def figsize(x=11, y=4):50"""Temporarily set the figure size using 'with figsize(a,b):'"""5152size = pylab.rcParams['figure.figsize']53set_figsize(x, y)54yield55pylab.rcParams['figure.figsize'] = size5657@contextmanager58def numpy_precision(precision):59old = np.get_printoptions()['precision']60np.set_printoptions(precision=precision)61yield62np.set_printoptions(old)6364@contextmanager65def printoptions(*args, **kwargs):66original = np.get_printoptions()67np.set_printoptions(*args, **kwargs)68yield69np.set_printoptions(**original)7071def _decode_list(data):72rv = []73for item in data:74if isinstance(item, unicode):75item = item.encode('utf-8')76elif isinstance(item, list):77item = _decode_list(item)78elif isinstance(item, dict):79item = _decode_dict(item)80rv.append(item)81return rv8283def _decode_dict(data):84rv = {}85for key, value in data.iteritems():86if isinstance(key, unicode):87key = key.encode('utf-8')88if isinstance(value, unicode):89value = value.encode('utf-8')90elif isinstance(value, list):91value = _decode_list(value)92elif isinstance(value, dict):93value = _decode_dict(value)94rv[key] = value95return rv969798def load_style(directory = '.', name='/styles/custom2.css'):99if sys.version_info[0] >= 3:100s = json.load(open(directory + "/code/538.json"))101else:102s = json.load(open(directory + "/code/538.json"), object_hook=_decode_dict)103plt.rcParams.update(s)104reset_axis ()105np.set_printoptions(suppress=True)106107styles = open(directory + name, 'r').read()108return HTML(styles)109110111