-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Do not consider dims without coords volatile if length has not changed #7381
Merged
ricardoV94
merged 8 commits into
pymc-devs:main
from
JasonTam:length-only-coord-volatility-7376
Jul 7, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eeb4e12
compare coord length if no values
JasonTam b859845
pull out function `get_constant_coords` fn and add test
JasonTam e7264e2
include value coord in test
JasonTam 694660c
type check instead of attr check
JasonTam d40fffe
add integration test
JasonTam 49bbf8c
also test if trace has same len but dim set to diff len
JasonTam 13d3aef
fix merge conflict
JasonTam b76087d
comment wording
JasonTam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
from pymc.pytensorf import compile_pymc | ||
from pymc.sampling.forward import ( | ||
compile_forward_sampling_function, | ||
get_constant_coords, | ||
get_vars_in_point_list, | ||
observed_dependent_deterministics, | ||
) | ||
|
@@ -427,6 +428,36 @@ def test_mutable_coords_volatile(self): | |
"offsets", | ||
} | ||
|
||
def test_length_coords_volatile(self): | ||
with pm.Model() as model: | ||
model.add_coord("trial", length=3) | ||
x = pm.Normal("x", dims="trial") | ||
y = pm.Deterministic("y", x.mean()) | ||
|
||
# Same coord length -- `x` is not volatile | ||
trace_same_len = az_from_dict( | ||
posterior={"x": [[[np.pi] * 3]]}, | ||
coords={"trial": range(3)}, | ||
dims={"x": ["trial"]}, | ||
) | ||
with model: | ||
pp_same_len = pm.sample_posterior_predictive( | ||
trace_same_len, var_names=["y"] | ||
).posterior_predictive | ||
assert pp_same_len["y"] == np.pi | ||
|
||
# Coord length changed -- `x` is volatile | ||
trace_diff_len = az_from_dict( | ||
posterior={"x": [[[np.pi] * 2]]}, | ||
coords={"trial": range(2)}, | ||
dims={"x": ["trial"]}, | ||
) | ||
with model: | ||
pp_diff_len = pm.sample_posterior_predictive( | ||
trace_diff_len, var_names=["y"] | ||
).posterior_predictive | ||
assert pp_diff_len["y"] != np.pi | ||
|
||
|
||
class TestSamplePPC: | ||
def test_normal_scalar(self): | ||
|
@@ -1669,6 +1700,20 @@ def test_Triangular( | |
assert prior["target"].shape == (prior_samples, *shape) | ||
|
||
|
||
def test_get_constant_coords(): | ||
with pm.Model() as model: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a more integration-like test like? Spit-balling, perhaps something like: with pm.Model() as m:
m.add_coord("trial", length=3)
x = pm.Normal("x", shape="trial")
y = pm.Deterministic("y", x.mean())
# pass dims as well
idata = az.from_dict("x": [[np.pi, np.pi, np.pi]])
with m:
pp1 = pm.sample_posterior_predictive(idata, var_names=["y"]).posterior_predictive
assert pp1["y"] == np.pi
with m:
# change coord length
pp2 = pm.sample_posterior_predictive(idata, var_names=["y"]).posterior_predictive
assert pp2["y"] != np.pi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this look ok? d40fffe |
||
model.add_coord("length_coord", length=1) | ||
model.add_coord("value_coord", values=(3,)) | ||
|
||
trace_coords_same = {"length_coord": np.array([0]), "value_coord": np.array([3])} | ||
constant_coords_same = get_constant_coords(trace_coords_same, model) | ||
assert constant_coords_same == {"length_coord", "value_coord"} | ||
|
||
trace_coords_diff = {"length_coord": np.array([0, 1]), "value_coord": np.array([4])} | ||
constant_coords_diff = get_constant_coords(trace_coords_diff, model) | ||
assert constant_coords_diff == set() | ||
|
||
|
||
def test_get_vars_in_point_list(): | ||
with pm.Model() as modelA: | ||
pm.Normal("a", 0, 1) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to change the dim length in the pymc model and pass exactly the same idata to sample_posterior_predictive as was done in the first call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way to update an existing dim is with
Model.set_dim()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, just got to this now. Implemented in 49bbf8c -- hope I understood you correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup you got it!