Moved all code to \code subdirectory.
All these changes are to make the book easier to run from cloud.sagemath.org. You can share individual notebooks there, but not entire projects.By putting everything under code at the user only needs to grab that one directory. More on sagemath later, if I decide to pursue that as a delivery mechanism...
This commit is contained in:
109
code/book_format.py
Normal file
109
code/book_format.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from IPython.core.display import HTML
|
||||
import matplotlib.pylab as pylab
|
||||
import matplotlib.pyplot as plt
|
||||
import json
|
||||
import numpy as np
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
sys.path.insert(0,'./code') # allow us to import book_format
|
||||
|
||||
def test_filterpy_version():
|
||||
import filterpy
|
||||
min_version = [0,0,22]
|
||||
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"
|
||||
"Please install a more recent version.\n"
|
||||
" ex: pip install filterpy --upgrade".format(
|
||||
*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()
|
||||
|
||||
|
||||
def equal_axis():
|
||||
pylab.rcParams['figure.figsize'] = 10,10
|
||||
plt.axis('equal')
|
||||
|
||||
|
||||
def reset_axis():
|
||||
pylab.rcParams['figure.figsize'] = 11, 4
|
||||
|
||||
def set_figsize(x=11, y=4):
|
||||
pylab.rcParams['figure.figsize'] = x, y
|
||||
|
||||
|
||||
@contextmanager
|
||||
def figsize(x=11, y=4):
|
||||
"""Temporarily set the figure size using 'with figsize(a,b):'"""
|
||||
|
||||
size = pylab.rcParams['figure.figsize']
|
||||
set_figsize(x, y)
|
||||
yield
|
||||
pylab.rcParams['figure.figsize'] = size
|
||||
|
||||
@contextmanager
|
||||
def numpy_precision(precision):
|
||||
old = np.get_printoptions()['precision']
|
||||
np.set_printoptions(precision=precision)
|
||||
yield
|
||||
np.set_printoptions(old)
|
||||
|
||||
@contextmanager
|
||||
def printoptions(*args, **kwargs):
|
||||
original = np.get_printoptions()
|
||||
np.set_printoptions(*args, **kwargs)
|
||||
yield
|
||||
np.set_printoptions(**original)
|
||||
|
||||
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
|
||||
|
||||
|
||||
def load_style(directory = '.', name='/code/custom.css'):
|
||||
if sys.version_info[0] >= 3:
|
||||
s = json.load(open(directory + "/code/538.json"))
|
||||
else:
|
||||
s = json.load(open(directory + "/code/538.json"), object_hook=_decode_dict)
|
||||
plt.rcParams.update(s)
|
||||
reset_axis ()
|
||||
np.set_printoptions(suppress=True)
|
||||
|
||||
styles = open(directory + name, 'r').read()
|
||||
return HTML(styles)
|
||||
225
code/custom.css
Normal file
225
code/custom.css
Normal file
@@ -0,0 +1,225 @@
|
||||
<style>
|
||||
@import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');
|
||||
@import url('http://fonts.googleapis.com/css?family=Vollkorn');
|
||||
@import url('http://fonts.googleapis.com/css?family=Arimo');
|
||||
|
||||
div.cell{
|
||||
width: 900px;
|
||||
margin-left: 0% !important;
|
||||
margin-right: auto;
|
||||
}
|
||||
div.text_cell code {
|
||||
background: transparent;
|
||||
color: #000000;
|
||||
font-weight: 600;
|
||||
font-size: 11pt;
|
||||
font-style: bold;
|
||||
font-family: 'Source Code Pro', Consolas, monocco, monospace;
|
||||
}
|
||||
h1 {
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
}
|
||||
|
||||
div.input_area {
|
||||
background: #F6F6F9;
|
||||
border: 1px solid #586e75;
|
||||
}
|
||||
|
||||
.text_cell_render h1 {
|
||||
font-weight: 200;
|
||||
font-size: 30pt;
|
||||
line-height: 100%;
|
||||
color:#c76c0c;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 1em;
|
||||
display: block;
|
||||
white-space: wrap;
|
||||
text-align: left;
|
||||
}
|
||||
h2 {
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
text-align: left;
|
||||
}
|
||||
.text_cell_render h2 {
|
||||
font-weight: 200;
|
||||
font-size: 16pt;
|
||||
font-style: italic;
|
||||
line-height: 100%;
|
||||
color:#c76c0c;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 1.5em;
|
||||
display: block;
|
||||
white-space: wrap;
|
||||
text-align: left;
|
||||
}
|
||||
h3 {
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
}
|
||||
.text_cell_render h3 {
|
||||
font-weight: 200;
|
||||
font-size: 14pt;
|
||||
line-height: 100%;
|
||||
color:#d77c0c;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 2em;
|
||||
display: block;
|
||||
white-space: wrap;
|
||||
text-align: left;
|
||||
}
|
||||
h4 {
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
}
|
||||
.text_cell_render h4 {
|
||||
font-weight: 100;
|
||||
font-size: 14pt;
|
||||
color:#d77c0c;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 0.5em;
|
||||
display: block;
|
||||
white-space: nowrap;
|
||||
}
|
||||
h5 {
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
}
|
||||
.text_cell_render h5 {
|
||||
font-weight: 200;
|
||||
font-style: normal;
|
||||
color: #1d3b84;
|
||||
font-size: 16pt;
|
||||
margin-bottom: 0em;
|
||||
margin-top: 0.5em;
|
||||
display: block;
|
||||
white-space: nowrap;
|
||||
}
|
||||
div.text_cell_render{
|
||||
font-family: 'Arimo',verdana,arial,sans-serif;
|
||||
line-height: 125%;
|
||||
font-size: 120%;
|
||||
text-align:justify;
|
||||
text-justify:inter-word;
|
||||
}
|
||||
div.output_subarea.output_text.output_pyout {
|
||||
overflow-x: auto;
|
||||
overflow-y: scroll;
|
||||
max-height: 50000px;
|
||||
}
|
||||
div.output_subarea.output_stream.output_stdout.output_text {
|
||||
overflow-x: auto;
|
||||
overflow-y: scroll;
|
||||
max-height: 50000px;
|
||||
}
|
||||
div.output_wrapper{
|
||||
margin-top:0.2em;
|
||||
margin-bottom:0.2em;
|
||||
}
|
||||
|
||||
code{
|
||||
font-size: 70%;
|
||||
}
|
||||
.rendered_html code{
|
||||
background-color: transparent;
|
||||
}
|
||||
ul{
|
||||
margin: 2em;
|
||||
}
|
||||
ul li{
|
||||
padding-left: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 0.5em;
|
||||
}
|
||||
ul li li{
|
||||
padding-left: 0.2em;
|
||||
margin-bottom: 0.2em;
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
ol{
|
||||
margin: 2em;
|
||||
}
|
||||
ol li{
|
||||
padding-left: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 0.5em;
|
||||
}
|
||||
ul li{
|
||||
padding-left: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
a:link{
|
||||
font-weight: bold;
|
||||
color:#447adb;
|
||||
}
|
||||
a:visited{
|
||||
font-weight: bold;
|
||||
color: #1d3b84;
|
||||
}
|
||||
a:hover{
|
||||
font-weight: bold;
|
||||
color: #1d3b84;
|
||||
}
|
||||
a:focus{
|
||||
font-weight: bold;
|
||||
color:#447adb;
|
||||
}
|
||||
a:active{
|
||||
font-weight: bold;
|
||||
color:#447adb;
|
||||
}
|
||||
.rendered_html :link {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.rendered_html :hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
.rendered_html :visited {
|
||||
text-decoration: none;
|
||||
}
|
||||
.rendered_html :focus {
|
||||
text-decoration: none;
|
||||
}
|
||||
.rendered_html :active {
|
||||
text-decoration: none;
|
||||
}
|
||||
.warning{
|
||||
color: rgb( 240, 20, 20 )
|
||||
}
|
||||
hr {
|
||||
color: #f3f3f3;
|
||||
background-color: #f3f3f3;
|
||||
height: 1px;
|
||||
}
|
||||
blockquote{
|
||||
display:block;
|
||||
background: #fcfcfc;
|
||||
border-left: 5px solid #c76c0c;
|
||||
font-family: 'Open sans',verdana,arial,sans-serif;
|
||||
width:680px;
|
||||
padding: 10px 10px 10px 10px;
|
||||
text-align:justify;
|
||||
text-justify:inter-word;
|
||||
}
|
||||
blockquote p {
|
||||
margin-bottom: 0;
|
||||
line-height: 125%;
|
||||
font-size: 100%;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
extensions: ["AMSmath.js"]
|
||||
},
|
||||
tex2jax: {
|
||||
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
|
||||
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
|
||||
},
|
||||
displayAlign: 'center', // Change this to 'center' to center equations.
|
||||
"HTML-CSS": {
|
||||
scale:85,
|
||||
availableFonts: ["Neo-Euler"],
|
||||
preferredFont: "Neo-Euler",
|
||||
webFont: "Neo-Euler",
|
||||
styles: {'.MathJax_Display': {"margin": 4}}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -12,7 +12,7 @@ import numpy as np
|
||||
|
||||
|
||||
def plot1():
|
||||
|
||||
P = np.array([[6, 2.5], [2.5, .6]])
|
||||
stats.plot_covariance_ellipse((10, 2), P, facecolor='g', alpha=0.2)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user