CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91869
1
# -*- coding: utf-8 -*-
2
from IPython.core.display import HTML
3
import matplotlib.pylab as pylab
4
import matplotlib.pyplot as plt
5
import json
6
import numpy as np
7
import sys
8
from contextlib import contextmanager
9
10
11
sys.path.insert(0,'./code') # allow us to import book_format
12
13
def test_filterpy_version():
14
import filterpy
15
min_version = [0,0,22]
16
v = filterpy.__version__
17
tokens = v.split('.')
18
for i,v in enumerate(tokens):
19
if int(v) > min_version[i]:
20
return
21
22
i = len(tokens) - 1
23
if min_version[i] > int(tokens[i]):
24
raise Exception("Minimum FilterPy version supported is {}.{}.{}.\n"
25
"Please install a more recent version.\n"
26
" ex: pip install filterpy --upgrade".format(
27
*min_version))
28
v = int(tokens[0]*1000)
29
30
31
# ensure that we have the correct filterpy loaded. This is
32
# called when this module is imported at the top of each book
33
# chapter so the reader can see that they need to update FilterPy.
34
test_filterpy_version()
35
36
37
def equal_axis():
38
pylab.rcParams['figure.figsize'] = 10,10
39
plt.axis('equal')
40
41
42
def reset_axis():
43
pylab.rcParams['figure.figsize'] = 11, 4
44
45
def set_figsize(x=11, y=4):
46
pylab.rcParams['figure.figsize'] = x, y
47
48
49
@contextmanager
50
def figsize(x=11, y=4):
51
"""Temporarily set the figure size using 'with figsize(a,b):'"""
52
53
size = pylab.rcParams['figure.figsize']
54
set_figsize(x, y)
55
yield
56
pylab.rcParams['figure.figsize'] = size
57
58
@contextmanager
59
def numpy_precision(precision):
60
old = np.get_printoptions()['precision']
61
np.set_printoptions(precision=precision)
62
yield
63
np.set_printoptions(old)
64
65
@contextmanager
66
def printoptions(*args, **kwargs):
67
original = np.get_printoptions()
68
np.set_printoptions(*args, **kwargs)
69
yield
70
np.set_printoptions(**original)
71
72
def _decode_list(data):
73
rv = []
74
for item in data:
75
if isinstance(item, unicode):
76
item = item.encode('utf-8')
77
elif isinstance(item, list):
78
item = _decode_list(item)
79
elif isinstance(item, dict):
80
item = _decode_dict(item)
81
rv.append(item)
82
return rv
83
84
def _decode_dict(data):
85
rv = {}
86
for key, value in data.iteritems():
87
if isinstance(key, unicode):
88
key = key.encode('utf-8')
89
if isinstance(value, unicode):
90
value = value.encode('utf-8')
91
elif isinstance(value, list):
92
value = _decode_list(value)
93
elif isinstance(value, dict):
94
value = _decode_dict(value)
95
rv[key] = value
96
return rv
97
98
99
def load_style(directory = '.', name='/styles/custom2.css'):
100
if sys.version_info[0] >= 3:
101
s = json.load(open(directory + "/code/538.json"))
102
else:
103
s = json.load(open(directory + "/code/538.json"), object_hook=_decode_dict)
104
plt.rcParams.update(s)
105
reset_axis ()
106
np.set_printoptions(suppress=True)
107
108
styles = open(directory + name, 'r').read()
109
return HTML(styles)
110
111