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

Removing unnecessary comp_shape from class NormalMixture #7098

Merged
merged 1 commit into from
Mar 25, 2024

Conversation

mohammed052
Copy link
Contributor

@mohammed052 mohammed052 commented Jan 11, 2024

This modification assumes that the Mixture class automatically handles the reshaping of component batch dimensions, and there is no longer a need to explicitly specify the comp_shape

Description

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pymc--7098.org.readthedocs.build/en/7098/

@ricardoV94 ricardoV94 changed the title Removing comp_shape from class NormalMixture Removing unnecessary comp_shape from class NormalMixture Jan 15, 2024
@ricardoV94
Copy link
Member

Thanks @mohammed052, running that tests and we can merge if everything passes

Copy link

codecov bot commented Jan 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.29%. Comparing base (a033261) to head (aa602ec).

❗ Current head aa602ec differs from pull request most recent head b6eeec8. Consider uploading reports for the commit b6eeec8 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7098   +/-   ##
=======================================
  Coverage   92.29%   92.29%           
=======================================
  Files         100      100           
  Lines       16875    16875           
=======================================
  Hits        15575    15575           
  Misses       1300     1300           
Files Coverage Δ
pymc/distributions/mixture.py 95.08% <100.00%> (ø)

@ricardoV94
Copy link
Member

@mohammed052 you can see here some tests failed because they were still using the removed keyword: https://github.com/pymc-devs/pymc/actions/runs/7492576052/job/20484268751?pr=7098

We need to tweak the tests to not use it, but keep the same behavior otherwise.

@mohammed052
Copy link
Contributor Author

@ricardoV94 can you please elaborate on what needs to be done next to tweak the tests

@ricardoV94
Copy link
Member

@mohammed052 you'll need to investigate yourself, and for that it may be useful to run the tests locally to see what they are doing

Copy link
Member

@larryshamalama larryshamalama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mohammed052, as @ricardoV94 mentioned, you will have to adjust tests in test_mixture.py as comp_shape is still passed as an argument to NormalMixture, which is why the tests are failing. Notice the lines where we have extra_args={"comp_shape": 2}. See snippets below. Once all tests pass, this PR would be ready to merge :)

Let us know if you have any other questions!

def test_random(self, seeded_test):
def ref_rand(size, w, mu, sigma):
component = np.random.choice(w.size, size=size, p=w)
return np.random.normal(mu[component], sigma[component], size=size)
continuous_random_tester(
NormalMixture,
{
"w": Simplex(2),
"mu": Domain([[0.05, 2.5], [-5.0, 1.0]], edges=(None, None)),
"sigma": Domain([[1, 1], [1.5, 2.0]], edges=(None, None)),
},
extra_args={"comp_shape": 2},
size=1000,
ref_rand=ref_rand,
)
continuous_random_tester(
NormalMixture,
{
"w": Simplex(3),
"mu": Domain([[-5.0, 1.0, 2.5]], edges=(None, None)),
"sigma": Domain([[1.5, 2.0, 3.0]], edges=(None, None)),
},
extra_args={"comp_shape": 3},
size=1000,
ref_rand=ref_rand,
)

def test_scalar_components(self):
nd = 3
npop = 4
# [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
mus = pt.constant(np.full((nd, npop), np.arange(npop)))
with Model() as model:
m = NormalMixture(
"m",
w=np.ones(npop) / npop,
mu=mus,
sigma=1e-5,
comp_shape=(nd, npop),
shape=nd,
)

@mohammed052
Copy link
Contributor Author

I was facing difficulty earlier in identifying the problem in tests.Thank You @larryshamalama for your input.
I have made required changes in test_mixture.py. @ricardoV94 kindly review them.

@larryshamalama
Copy link
Member

larryshamalama commented Feb 7, 2024

Thanks for the changes

I was facing difficulty earlier in identifying the problem in tests.Thank You @larryshamalama for your input. I have made required changes in test_mixture.py. @ricardoV94 kindly review them.

There seems to be a lot more tests in test_mixture.py that has comp_shape as an argument, e.g. test_normal_mixture_nd. To be more time efficient, I can't identify every instance where these tests need modifications; there seem to be more. However, akin to my previous comment, the edits should be very similar.

@mohammed052
Copy link
Contributor Author

mohammed052 commented Feb 12, 2024

I made a few more changes last week, @ricardoV94 @larryshamalama can you now review the pull request once.

@twiecki
Copy link
Member

twiecki commented Mar 25, 2024

@mohammed052 you need to run pre-commit

diff --git a/tests/distributions/test_mixture.py b/tests/distributions/test_mixture.py
index ec1baa7..7ce6084 100644
--- a/tests/distributions/test_mixture.py
+++ b/tests/distributions/test_mixture.py
@@ -821,9 +821,7 @@ class TestNormalMixture:
             taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
             ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp,))
             mixture0 = NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
-            obs0 = NormalMixture(
-                "obs", w=ws, mu=mus, tau=taus, observed=observed
-            )
+            obs0 = NormalMixture("obs", w=ws, mu=mus, tau=taus, observed=observed)
 
         with Model() as model1:

@ricardoV94
Copy link
Member

I was the one rebasing this PR

@ricardoV94 ricardoV94 merged commit 3841c7b into pymc-devs:main Mar 25, 2024
21 checks passed
Copy link

welcome bot commented Mar 25, 2024

Congratulations Banner]
Congrats on merging your first pull request! 🎉 We here at PyMC are proud of you! 💖 Thank you so much for your contribution 🎁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove NormalMixture comp_shape kwarg
4 participants