Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview for Mira context #114

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/askem_beaker/contexts/mira/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ async def post_execute(self, message):
},
)

async def generate_preview(self):
"""
Preview renders of mira models.
"""
code_block = self.get_code(
"visualize_mira_models",
{
"model_vars": self.loaded_models,
},
)
result = await self.beaker_kernel.evaluate(code_block)
models = result.get("return", None)
return {
"Mira Models": models,
}

async def auto_context(self):
from .lib.utils import query_examples

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import base64

from mira.metamodel import TemplateModel
from mira.modeling.viz import GraphicalModel


def __visualize_model(name: str, model: TemplateModel) -> str:
graphical_model = GraphicalModel.from_template_model(model)
filename = f"_preview_{name}.png"
graphical_model.write(filename, format="png")
with open(filename, "rb") as f:
data = f.read()
enc = base64.b64encode(bytes(data))
return enc.decode("utf-8")
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will actually leave files in the "jupyter" user's home directory, since subkernels currently actually run as a normal kernel in the same environment. We probably don't want to create clutter.
We should probably delete these files after we read them, or write to a IOBuffer so the contents are all in memory.



__state = locals()
__models = {}

for model in {{model_vars}}:
model: str
__models[model] = {"text/plain": repr(__state[model]), "image/png": __visualize_model(model, __state[model])}

del __state
Comment on lines +17 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using jinja templating, I think we can clean this up to something like the following.
(Note: I haven't really tested this, so don't take this as a solved solution.)
As you can see, instead of generating and reading from a locals() dict, we are just generating code that access the variables directly.

Suggested change
__state = locals()
__models = {}
for model in {{model_vars}}:
model: str
__models[model] = {"text/plain": repr(__state[model]), "image/png": __visualize_model(model, __state[model])}
del __state
__models = {
{% for model_var in model_vars %}
"{{model_var}}": {"text/plain": repr({{model_var}}), "image/png": __visualize_model("{{model_var}}", {{model_var}})}
{% endfor %}
}


__models