Skip to content

Instantly share code, notes, and snippets.

@tritemio
Created November 10, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tritemio/816875a82e62758615b9 to your computer and use it in GitHub Desktop.
Save tritemio/816875a82e62758615b9 to your computer and use it in GitHub Desktop.
Run notebooks from another notebook, with input parameters
def run_notebook(notebook_name):
"""Runs the notebook `notebook_name` (file name with no extension).
This function executes notebook with name `notebook_name` (no extension)
and saves the fully executed notebook in a new file appending "-out"
to the original file name.
It also displays links to the original and executed notebooks.
"""
nb_name_full = notebook_name + '.ipynb'
display(FileLink(nb_name_full))
out_path = 'out_notebooks/'
out_nb_name = out_path + notebook_name + '-out.ipynb'
nb = nbformat.read(nb_name_full, as_version=4)
ep = ExecutePreprocessor(timeout = 3600)
try:
out = ep.preprocess(nb, {'metadata': {'path': './'}})
except Exception:
msg = 'Error executing the notebook "%s".\n\n' % notebook_name
msg += 'See notebook "%s" for the traceback.' % out_nb_name
print(msg)
raise
finally:
nbformat.write(nb, out_nb_name)
display(FileLink(out_nb_name))
def run_notebook_template(notebook_name, remove_out=True,
data_ids=['7d', '12d', '17d', '22d', '27d'],
ph_sel=None):
"""Run a template ALEX notebook for all the 5 samples.
Fit results are saved in the folder 'results'.
For each sample, the evaluated notebook containing both plots
and text output is saved in the 'out_notebooks' folder.
"""
## Compute TXT data results file name (removing a previous copy)
assert ph_sel in ['all-ph', 'Dex', 'DexDem', 'AexAem', 'AND-gate', None]
ph_sel_suffix = '' if ph_sel is None else '-%s' % ph_sel
data_fname = 'results/' + notebook_name + '%s.txt' % ph_sel_suffix
if remove_out and \
os.path.exists(data_fname):
os.remove(data_fname)
nb_name_full = notebook_name + '.ipynb'
display(FileLink(nb_name_full))
out_path = 'out_notebooks/'
ep = ExecutePreprocessor(timeout = 3600)
for data_id in data_ids:
nb = nbformat.read(nb_name_full, as_version=4)
nb['cells'].insert(1, nbformat.v4.new_code_cell('data_id = "%s"' % data_id))
nb['cells'].insert(1, nbformat.v4.new_code_cell('ph_sel_name = "%s"' % ph_sel))
out_nb_name = out_path + notebook_name + '-out%s-%s.ipynb' %\
(ph_sel_suffix, data_id)
try:
out = ep.preprocess(nb, {'metadata': {'path': './'}})
except:
msg = 'Error executing the notebook "%s".\n\n' % notebook_name
msg += 'See notebook "%s" for the traceback.' % out_nb_name
print(msg)
raise
finally:
nbformat.write(nb, out_nb_name)
display(FileLink(out_nb_name))
display(pd.read_csv(data_fname, sep="\s+").set_index('sample'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment