Added my own FloatSlider, IntSlider

They create a slider with continuous_update=False. Just keeping
code more readable by avoiding long lines.
This commit is contained in:
Roger Labbe 2018-05-05 09:14:22 -07:00
parent 9ae79fa625
commit d605763855
7 changed files with 1858 additions and 740 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,129 +1,129 @@
# -*- coding: utf-8 -*-
"""Copyright 2015 Roger R Labbe Jr.
Code supporting the book
Kalman and Bayesian Filters in Python
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
This is licensed under an MIT license. See the LICENSE.txt file
for more information.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from contextlib import contextmanager
from IPython.core.display import HTML
import json
import matplotlib
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import numpy as np
import os.path
import sys
import warnings
from kf_book.book_plots import set_figsize, reset_axis
# version 1.4.3 of matplotlib has a bug that makes
# it issue a spurious warning on every plot that
# clutters the notebook output
if matplotlib.__version__ == '1.4.3':
warnings.simplefilter(action="ignore", category=FutureWarning)
try:
matplotlib.style.use('default')
except:
pass
def test_filterpy_version():
import filterpy
from distutils.version import LooseVersion
v = filterpy.__version__
min_version = "1.2.4"
if LooseVersion(v) < LooseVersion(min_version):
raise Exception("Minimum FilterPy version supported is {}.\n"
"Please install a more recent version.\n"
" ex: pip install filterpy --upgrade".format(
min_version))
# 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()
pylab.rcParams['figure.max_open_warning'] = 50
pylab.rcParams['figure.figsize'] = 8, 3
@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='kf_book/custom.css'):
version = [int(version_no) for version_no in matplotlib.__version__.split('.')]
try:
if sys.version_info[0] >= 3:
style = json.load(open(os.path.join(directory, "kf_book/538.json")))
else:
style = json.load(open(directory + "/kf_book/538.json"), object_hook=_decode_dict)
plt.rcParams.update(style)
except:
pass
set_figsize()
reset_axis ()
np.set_printoptions(suppress=True, precision=3,
threshold=10000., linewidth=70,
formatter={'float':lambda x:' {:.3}'.format(x)})
styles = open(os.path.join(directory, name), 'r').read()
set_figsize()
# I don't know why I have to do this, but I have to call
# with suppress a second time or the notebook doesn't suppress
# exponents
np.set_printoptions(suppress=True)
return HTML(styles)
# -*- coding: utf-8 -*-
"""Copyright 2015 Roger R Labbe Jr.
Code supporting the book
Kalman and Bayesian Filters in Python
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
This is licensed under an MIT license. See the LICENSE.txt file
for more information.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from contextlib import contextmanager
from IPython.core.display import HTML
import json
import matplotlib
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import numpy as np
import os.path
import sys
import warnings
from kf_book.book_plots import set_figsize, reset_axis
# version 1.4.3 of matplotlib has a bug that makes
# it issue a spurious warning on every plot that
# clutters the notebook output
if matplotlib.__version__ == '1.4.3':
warnings.simplefilter(action="ignore", category=FutureWarning)
try:
matplotlib.style.use('default')
except:
pass
def test_filterpy_version():
import filterpy
from distutils.version import LooseVersion
v = filterpy.__version__
min_version = "1.3.2"
if LooseVersion(v) < LooseVersion(min_version):
raise Exception("Minimum FilterPy version supported is {}.\n"
"Please install a more recent version.\n"
" ex: pip install filterpy --upgrade".format(
min_version))
# 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()
pylab.rcParams['figure.max_open_warning'] = 50
pylab.rcParams['figure.figsize'] = 8, 3
@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='kf_book/custom.css'):
version = [int(version_no) for version_no in matplotlib.__version__.split('.')]
try:
if sys.version_info[0] >= 3:
style = json.load(open(os.path.join(directory, "kf_book/538.json")))
else:
style = json.load(open(directory + "/kf_book/538.json"), object_hook=_decode_dict)
plt.rcParams.update(style)
except:
pass
set_figsize()
reset_axis ()
np.set_printoptions(suppress=True, precision=3,
threshold=10000., linewidth=70,
formatter={'float':lambda x:' {:.3}'.format(x)})
styles = open(os.path.join(directory, name), 'r').read()
set_figsize()
# I don't know why I have to do this, but I have to call
# with suppress a second time or the notebook doesn't suppress
# exponents
np.set_printoptions(suppress=True)
return HTML(styles)

View File

@ -19,13 +19,14 @@ from __future__ import (absolute_import, division, print_function,
from contextlib import contextmanager
import sys
import time
import ipywidgets
import matplotlib as mpl
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
import sys
import time
try:
import seabornee
@ -363,6 +364,22 @@ def plot_kf_output(xs, filter_xs, zs, title=None, aspect_equal=True):
plt.xlim((-1, len(xs)))
plt.show()
def FloatSlider(value, **kwargs):
"""
Creates an ipwidgets FloatSlider with continuous update
turned off
"""
return ipywidgets.FloatSlider(value, continuous_update=False, **kwargs)
def IntSlider(value, **kwargs):
"""
Creates an ipwidgets IntSlider with continuous update
turned off
"""
return ipywidgets.IntSlider(value, continuous_update=False, **kwargs)
def plot_measurements(xs, ys=None, dt=None, color='k', lw=1, label='Measurements',
lines=False, **kwargs):
@ -373,7 +390,7 @@ def plot_measurements(xs, ys=None, dt=None, color='k', lw=1, label='Measurements
ys = xs
xs = np.arange(0, len(ys)*dt, dt)
plt.autoscale(tight=True)
plt.autoscale(tight=False)
if lines:
if ys is not None:
return plt.plot(xs, ys, color=color, lw=lw, ls='--', label=label, **kwargs)