Skip to content

Commit

Permalink
fix (run_in_notebook): Capture correct cell output as main metric
Browse files Browse the repository at this point in the history
Searching the first output of the last cell for the main metric was a legacy
of some early experiments that did not include figures and other text.

This change creates a final dedicated result cell
  • Loading branch information
Jacob-Stevens-Haas committed Apr 27, 2024
1 parent 8e145f3 commit 5ab65dc
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mitosis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ def _run_in_notebook(
)
step_runner_cells.append(nbformat.v4.new_code_cell(source=code))

nb["cells"] = [setup_cell] + step_loader_cells + step_runner_cells
result_cell = nbformat.v4.new_code_cell(source=("print(curr_result['main'])"))
nb["cells"] = [setup_cell] + step_loader_cells + step_runner_cells + [result_cell]
with open(trials_folder / "source.py", "w") as fh:
fh.write("".join(cell["source"] for cell in nb.cells))
ep = ExecutePreprocessor(timeout=-1)
Expand All @@ -411,7 +412,7 @@ def _run_in_notebook(
allowed = (CellExecutionError,)
try:
ep.preprocess(nb, {"metadata": {"path": trials_folder}})
metrics = nb["cells"][-1]["outputs"][0]["text"][:-1]
metrics = nb["cells"][-1]["outputs"][0]["text"][:-1] # last char is newline
except allowed as exc:
exception = exc
return nb, metrics, exception
Expand Down

0 comments on commit 5ab65dc

Please sign in to comment.