Skip to content

Do not expect model variables to always have str_repr #6311

@ricardoV94

Description

@ricardoV94

When we create RVs via the the Distribution interface, they get some str_repr methods glued to them:

# add in pretty-printing support
rv_out.str_repr = types.MethodType(str_for_dist, rv_out)
rv_out._repr_latex_ = types.MethodType(
functools.partial(str_for_dist, formatting="latex"), rv_out
)

The Model.str_repr then expects all named variables to have this method, which fails for automatically generated RVs such as those in automatic imputation

import numpy as np
import pymc as pm

with pm.Model() as m:
   pm.Normal("x", observed=[np.nan, 0, 0])
m.str_repr()  # Called automatically in a Jupyter environment for pretty-printing
AttributeError: 'TensorVariable' object has no attribute 'str_repr'

It also which makes it annoying to replace / register variables manually:

x = pm.Normal.dist()  # No `str_repr` attached
with pm.Model() as m:
  m.register_rv(x, name="x")
m.str_repr()  # Same error

Suggestion

Do not rely on the methods being attached for model.str_repr

rv_reprs = [rv.str_repr(formatting=formatting, include_params=include_params) for rv in all_rv]

Instead just use a dispatch base approach to pretty-print any model variables based on their Op types as discussed elsewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions