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

BUG: Deterministic variables with dims containing None break model_to_graphviz #6335

Closed
michaelosthege opened this issue Nov 24, 2022 · 2 comments

Comments

@michaelosthege
Copy link
Member

michaelosthege commented Nov 24, 2022

Describe the issue:

When a Deterministic variable is created, one may pass a dims=(None, "time") which is a valid dims tuple.

However, when attempting a pm.model_to_graphviz later, a KeyError: None is encountered when accessing Model.dim_lengths.

Reproduceable code example:

import numpy as np
import pymc as pm

with pm.Model(coords=dict(time=np.arange(5))) as pmodel:
    data = at.as_tensor(np.ones((3, 5)))
    # This works:
    # pm.Normal("n", data, dims=(None, "time"))

    # This does not:
    pm.Deterministic("n", data, dims=(None, "time"))
    
pm.model_to_graphviz(pmodel)

Error message:

File pymc\model_graph.py:198, in <genexpr>(.0)
    195 v = self.model[var_name]
    196 if var_name in self.model.RV_dims:
    197     plate_label = " x ".join(
--> 198         f"{d} ({self._eval(self.model.dim_lengths[d])})"
    199         for d in self.model.RV_dims[var_name]
    200     )
    201 else:
    202     plate_label = " x ".join(map(str, self._eval(v.shape)))

KeyError: None

PyMC version information:

4.1.7

Context for the issue:

No response

@michaelosthege
Copy link
Member Author

Using pm.ConstantData instead of pm.Deterministic breaks in a different way:

File pymc\data.py:716, in Data(name, value, dims, coords, export_index_as_coords, mutable, **kwargs)
    706         if not dname in model.dim_lengths:
    707             model.add_coord(
    708                 name=dname,
    709                 # Note: Coordinate values can't be taken from
   (...)
    713                 length=xshape[d],
    714             )
--> 716 model.add_random_variable(x, dims=dims)
    718 return x

File pymc\model.py:1552, in Model.add_random_variable(self, var, dims)
   1550             raise ValueError(f"Dimension {dim} is not specified in `coords`.")
   1551     if any(var.name == dim for dim in dims):
-> 1552         raise ValueError(f"Variable `{var.name}` has the same name as its dimension label.")
   1553     self._RV_dims[var.name] = dims
   1555 self.named_vars[var.name] = var

ValueError: Variable `None` has the same name as its dimension label.

@michaelosthege
Copy link
Member Author

I think this might be a rather simple fix: Checking is None and then printing more useful information if available.

michaelosthege added a commit to michaelosthege/pymc that referenced this issue Nov 27, 2022
michaelosthege added a commit to michaelosthege/pymc that referenced this issue Nov 27, 2022
ricardoV94 pushed a commit to michaelosthege/pymc that referenced this issue Nov 29, 2022
* Extract `ModelGraph._eval` to a function
* More robustness against unlabeled `dims` entries

Closes pymc-devs#6335
ricardoV94 pushed a commit to ricardoV94/pymc that referenced this issue Nov 29, 2022
* Extract `ModelGraph._eval` to a function
* More robustness against unlabeled `dims` entries

Closes pymc-devs#6335
wrongu pushed a commit to wrongu/pymc that referenced this issue Dec 1, 2022
* Extract `ModelGraph._eval` to a function
* More robustness against unlabeled `dims` entries

Closes pymc-devs#6335
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant