Debian Codegraph @

demo/Benchmarking Jupytext.py

raw symbols SCIP
1 # In this notebook, we benchmark the Jupytext formats for Jupyter notebooks against the base format
2 # Open this script as a notebook in Jupyter to run it and see the plots
3
4 import timescip-python python python-stdlib 3.11 time/__init__:
5 import copyscip-python python python-stdlib 3.11 copy/__init__:
6 import pandas as pd
7 import plotly.graph_objects as go
8 from plotly.colorsscip-python python jupytext 1.16.4 `plotly.colors`/__init__: import DEFAULT_PLOTLY_COLORS
9
10 import nbformatscip-python python nbformat 5.10.4 nbformat/__init__:
11 import jupytext
12
13 # The notebook to be tested
14 notebookscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook. = jupytext.read('World population.ipynb')
15
16 # Same notebook, with no outputs, for a fair comparison
17 notebook_no_outputsscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook_no_outputs. = copyscip-python python python-stdlib 3.11 copy/__init__:.deepcopyscip-python python python-stdlib 3.11 copy/deepcopy().(notebookscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook.)
18 for cellscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/cell. in notebook_no_outputsscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook_no_outputs..cells:
19 cellscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/cell..outputs = []
20 cellscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/cell..execution_count = None
21
22 # +
23 JUPYTEXT_FORMATSscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/JUPYTEXT_FORMATS. = ['ipynb', 'md', 'py:light', 'py:percent', 'py:sphinx']
24
25 # Let's see if we have pandoc here
26 try:
27 jupytext.writes(notebookscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook., fmt='md:pandoc')
28 JUPYTEXT_FORMATSscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/JUPYTEXT_FORMATS..appendscip-python python python-stdlib 3.11 builtins/list#append().('md:pandoc')
29 except jupytext.formats.JupytextFormatError as errscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/err.:
30 printscip-python python python-stdlib 3.11 builtins/print().(strscip-python python python-stdlib 3.11 builtins/str#(errscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/err.))
31
32
33 # Let's see if we have myst-parser installed here
34 try:
35 jupytext.writes(notebookscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook., fmt='myst')
36 JUPYTEXT_FORMATSscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/JUPYTEXT_FORMATS..appendscip-python python python-stdlib 3.11 builtins/list#append().('myst')
37 except jupytext.formats.JupytextFormatError as errscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/err.:
38 printscip-python python python-stdlib 3.11 builtins/print().(strscip-python python python-stdlib 3.11 builtins/str#(errscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/err.))
39
40 # -
41
42
43 def sample_perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(nbscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(nb), nscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(n)=30):
44 samples = pd.DataFrame(
45 pd.np.NaN,
46 index=pd.MultiIndex.from_product(
47 (rangescip-python python python-stdlib 3.11 builtins/range#(nscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(n)), ['nbformat'] + JUPYTEXT_FORMATSscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/JUPYTEXT_FORMATS.), names=['sample', 'implementation']),
48 columns=pd.Index(['size', 'read', 'write'], name='measure'))
49
50 for i, fmt in samples.index:
51 t0 = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().()
52 if fmt == 'nbformat':
53 text = nbformatscip-python python nbformat 5.10.4 nbformat/__init__:.writes(nbscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(nb))
54 else:
55 text = jupytext.writes(nbscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(nb), fmt)
56 t1 = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().()
57 samples.loc[(i, fmt), 'write'] = t1 - t0
58 samples.loc[(i, fmt), 'size'] = len(text)
59 t0 = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().()
60 if fmt == 'nbformat':
61 nbformatscip-python python nbformat 5.10.4 nbformat/__init__:.reads(text, as_version=4)
62 else:
63 jupytext.reads(text, fmt)
64 t1 = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().()
65 samples.loc[(i, fmt), 'read'] = t1 - t0
66 return samples
67
68
69 def performance_plotscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perf), titlescip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(title)):
70 formats = ['nbformat'] + JUPYTEXT_FORMATSscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/JUPYTEXT_FORMATS.
71 mean = perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perf).groupby('implementation').mean().loc[formats]
72 std = perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perf).groupby('implementation').std().loc[formats]
73 data = [go.Bar(x=mean.index,
74 y=mean[col],
75 error_y=dictscip-python python python-stdlib 3.11 builtins/dict#(
76 type='data',
77 array=std[col],
78 color=color,
79 thickness=0.5
80 ) if col != 'size' else dictscip-python python python-stdlib 3.11 builtins/dict#(),
81 name=col,
82 yaxis={'read': 'y1', 'write': 'y2', 'size': 'y3'}[col])
83 for col, color in zipscip-python python python-stdlib 3.11 builtins/zip#(mean.columns, DEFAULT_PLOTLY_COLORS)]
84 layout = go.Layout(title=titlescip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(title),
85 xaxis=dictscip-python python python-stdlib 3.11 builtins/dict#(title='Implementation', anchor='y3'),
86 yaxis=dictscip-python python python-stdlib 3.11 builtins/dict#(domain=[0.7, 1], title='Read (secs)'),
87 yaxis2=dictscip-python python python-stdlib 3.11 builtins/dict#(domain=[0.35, .65], title='Write (secs)'),
88 yaxis3=dictscip-python python python-stdlib 3.11 builtins/dict#(domain=[0, .3], title='Size')
89 )
90 return go.Figure(data=data, layout=layout)
91
92
93 perf_no_outputsscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/perf_no_outputs. = sample_perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(notebook_no_outputsscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook_no_outputs., 30)
94
95 performance_plotscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perf_no_outputsscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/perf_no_outputs., 'Benchmarking Jupytext on the World Population notebook<br>(Outputs filtered)')
96
97 perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/perf. = sample_perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/sample_perf().(notebookscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/notebook., 30)
98
99 performance_plotscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/performance_plot().(perfscip-python python jupytext 1.16.4 `demo.Benchmarking Jupytext`/perf., 'Benchmarking Jupytext on the World Population notebook<br>(With outputs)')