Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rlabbe
GitHub Repository: rlabbe/Kalman-and-Bayesian-Filters-in-Python
Path: blob/master/pdf/merge_book.py
687 views
1
from __future__ import print_function
2
import io
3
import nbformat
4
import sys
5
from formatting import *
6
7
8
def inplace_change(filename, old_string, new_string):
9
# Safely read the input filename using 'with'
10
with open(filename, encoding='utf-8') as f:
11
s = f.read()
12
if old_string not in s:
13
return
14
15
# Safely write the changed content, if found in the file
16
with open(filename, 'w', encoding='utf-8') as f:
17
s = s.replace(old_string, new_string)
18
f.write(s)
19
20
21
def merge_notebooks(outfile, filenames):
22
merged = None
23
added_appendix = False
24
for fname in filenames:
25
with io.open(fname, 'r', encoding='utf-8') as f:
26
nb = nbformat.read(f, nbformat.NO_CONVERT)
27
#remove_formatting(nb)
28
if not added_appendix and fname[0:8] == 'Appendix':
29
remove_links_add_appendix(nb)
30
added_appendix = True
31
else:
32
remove_links(nb)
33
if merged is None:
34
merged = nb
35
else:
36
merged.cells.extend(nb.cells)
37
#merged.metadata.name += "_merged"
38
39
outfile.write(nbformat.writes(merged, nbformat.NO_CONVERT))
40
41
42
if __name__ == '__main__':
43
with open('book.ipynb', 'w', encoding='utf-8') as f:
44
45
merge_notebooks(f,
46
['./tmp/00-Preface.ipynb',
47
'./tmp/01-g-h-filter.ipynb',
48
'./tmp/02-Discrete-Bayes.ipynb',
49
'./tmp/03-Gaussians.ipynb',
50
'./tmp/04-One-Dimensional-Kalman-Filters.ipynb',
51
'./tmp/05-Multivariate-Gaussians.ipynb',
52
'./tmp/06-Multivariate-Kalman-Filters.ipynb',
53
'./tmp/07-Kalman-Filter-Math.ipynb',
54
'./tmp/08-Designing-Kalman-Filters.ipynb',
55
'./tmp/09-Nonlinear-Filtering.ipynb',
56
'./tmp/10-Unscented-Kalman-Filter.ipynb',
57
'./tmp/11-Extended-Kalman-Filters.ipynb',
58
'./tmp/12-Particle-Filters.ipynb',
59
'./tmp/13-Smoothing.ipynb',
60
'./tmp/14-Adaptive-Filtering.ipynb',
61
'./tmp/Appendix-A-Installation.ipynb',
62
'./tmp/Appendix-B-Symbols-and-Notations.ipynb',
63
'./tmp/Appendix-D-HInfinity-Filters.ipynb',
64
'./tmp/Appendix-E-Ensemble-Kalman-Filters.ipynb'])
65
66
67
#remove text printed for matplotlib charts
68
inplace_change('book.ipynb', '<IPython.core.display.Javascript object>', '')
69
inplace_change('book.ipynb', '<IPython.core.display.HTML object>', '')
70