Skip to content

Commit 29821a5

Browse files
Fix issue 3793 (#3843)
* Demonstrate issue 3793. * Give Data model-relative names. Use Model.name_for() to relativize pm.Data names. * Move test description into docstring * mention changes to pm.Data naming closes #3793 Co-authored-by: michaelosthege <[email protected]>
1 parent 74b7788 commit 29821a5

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

Diff for: RELEASE-NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Remove `sample_ppc` and `sample_ppc_w` that were deprecated in 3.6.
1515
- Tuning results no longer leak into sequentially sampled `Metropolis` chains (see #3733 and #3796).
1616
- Deprecated `sd` in version 3.7 has been replaced by `sigma` now raises `DepreciationWarning` on using `sd` in continuous, mixed and timeseries distributions. (see #3837 and #3688).
17+
- In named models, `pm.Data` objects now get model-relative names (see [#3843](https://github.com/pymc-devs/pymc3/pull/3843))
1718

1819
## PyMC3 3.8 (November 29 2019)
1920

Diff for: pymc3/data.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,6 @@ class Data:
479479
https://docs.pymc.io/notebooks/data_container.html
480480
"""
481481
def __new__(self, name, value):
482-
# `pm.model.pandas_to_array` takes care of parameter `value` and
483-
# transforms it to something digestible for pymc3
484-
shared_object = theano.shared(pm.model.pandas_to_array(value), name)
485-
486-
# To draw the node for this variable in the graphviz Digraph we need
487-
# its shape.
488-
shared_object.dshape = tuple(shared_object.shape.eval())
489482

490483
# Add data container to the named variables of the model.
491484
try:
@@ -494,6 +487,18 @@ def __new__(self, name, value):
494487
raise TypeError("No model on context stack, which is needed to "
495488
"instantiate a data container. Add variable "
496489
"inside a 'with model:' block.")
490+
491+
name = model.name_for(name)
492+
493+
# `pm.model.pandas_to_array` takes care of parameter `value` and
494+
# transforms it to something digestible for pymc3
495+
shared_object = theano.shared(pm.model.pandas_to_array(value), name)
496+
497+
# To draw the node for this variable in the graphviz Digraph we need
498+
# its shape.
499+
shared_object.dshape = tuple(shared_object.shape.eval())
500+
501+
497502
model.add_random_variable(shared_object)
498503

499504
return shared_object

Diff for: pymc3/tests/test_data_container.py

+12
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,15 @@ def test_model_to_graphviz_for_model_with_data_container(self):
143143
assert text in g.source
144144
text = 'obs [label="obs ~ Normal" style=filled]'
145145
assert text in g.source
146+
147+
148+
def test_data_naming():
149+
"""
150+
This is a test for issue #3793 -- `Data` objects in named models are
151+
not given model-relative names.
152+
"""
153+
with pm.Model("named_model") as model:
154+
x = pm.Data("x", [1.0, 2.0, 3.0])
155+
y = pm.Normal("y")
156+
assert y.name == "named_model_y"
157+
assert x.name == "named_model_x"

0 commit comments

Comments
 (0)