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

Fix the file path when rendering model #1857

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
9 changes: 7 additions & 2 deletions numpyro/infer/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,14 @@ def render_model(

if filename is not None:
filename = Path(filename)
# remove leading period from suffix
filename_without_suffix = filename.with_suffix("")
graph.render(
filename.stem, view=False, cleanup=True, format=filename.suffix[1:]
) # remove leading period from suffix
filename_without_suffix,
view=False,
cleanup=True,
format=filename.suffix[1:],
)

return graph

Expand Down
17 changes: 16 additions & 1 deletion test/test_model_rendering.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0

import os

import numpy as np
import pytest

import jax.numpy as jnp

import numpyro
import numpyro.distributions as dist
from numpyro.infer.inspect import generate_graph_specification, get_model_relations
from numpyro.infer.inspect import (
generate_graph_specification,
get_model_relations,
render_model,
)


def simple(data):
Expand Down Expand Up @@ -129,3 +135,12 @@ def test_model_transformation(test_model, model_kwargs, expected_graph_spec):
graph_spec = generate_graph_specification(relations)

assert graph_spec == expected_graph_spec


def test_render_model_filename():
def model():
numpyro.sample("x", dist.Normal(0, 1))

render_model(model, filename="graph.png")
assert os.path.exists("graph.png")
os.remove("graph.png")
Loading