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

fixed fail 2 issue number 181 #220

Merged
merged 2 commits into from
May 3, 2023
Merged

Conversation

Karl-JT
Copy link
Contributor

@Karl-JT Karl-JT commented Apr 27, 2023

Hi, I'm trying to contribute by addressing issue #181 fail 2 by identifying sparse diagonal matrices.

@jakobsj
Copy link
Contributor

jakobsj commented Apr 28, 2023

Many thanks @Karl-JT for submitting this! We'll take a look and get back to you asap.

@jakobsj jakobsj requested a review from nabriis April 28, 2023 08:41
Copy link
Collaborator

@nabriis nabriis left a comment

Choose a reason for hiding this comment

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

Thank you @Karl-JT for the contribution! This seems to indeed have fixed the bug with sparse diagonal sqrtprec.

If you could be so kind as to also add a test in tests/test_distribution.py that checks against such future errors occurring. The code from the test would be something like this:

def test_Gaussian_from_sparse_sqrtprec():
    """ Test Gaussian distribution from sparse sqrtprec is equal to dense sqrtprec """
    N = 10; M = 5

    sqrtprec = sp.sparse.spdiags(np.random.randn(N), 0, N, N)

    y_from_sparse = cuqi.distribution.Gaussian(mean = np.zeros(N), sqrtprec = sqrtprec)
    y_from_dense = cuqi.distribution.Gaussian(mean = np.zeros(N), sqrtprec = sqrtprec.todense())

    assert y_from_dense.logpdf(np.ones(N)) == y_from_sparse.logpdf(np.ones(N))

For how to run tests please see https://cuqi-dtu.github.io/CUQIpy/dev/how_to.html

Option to fix entire issue

As to the issue in #181, I believe it would be good to check if the sqrtprec was not square and raise a ValueError in that case. If you are up for implementing that also it would be awesome :)

To follow "test driven development" you can add this test first and then work on raising the error such that the test passes.

def test_Gaussian_sqrtprec_must_be_square():
    """ Test Gaussian distribution raises ValueError if sqrtprec (sparse and dense) is not square """
    N = 10; M = 5

    # Create non square sqrtprec as sparse (later converted to dense)
    sqrtprec = sp.sparse.csr_matrix(np.random.randn(N, M))
    

    with pytest.raises(ValueError, match="sqrtprec must be square"):
        cuqi.distribution.Gaussian(mean = np.zeros(N), sqrtprec = sqrtprec)

    with pytest.raises(ValueError, match="sqrtprec must be square"):
        cuqi.distribution.Gaussian(mean = np.zeros(N), sqrtprec = sqrtprec.todense())

…t functions to check both fails in issue 181
@Karl-JT
Copy link
Contributor Author

Karl-JT commented Apr 29, 2023

Thank you very much @nabriis for the guide. I have updated it with an additional check on whether sqrtprec is square. The two suggested tests are included as well.

Copy link
Collaborator

@nabriis nabriis left a comment

Choose a reason for hiding this comment

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

@Karl-JT Thank you for the quick action. The changes look good to me. I will hand it to @jakobsj for the final review.

@nabriis nabriis requested a review from jakobsj May 1, 2023 06:58
Copy link
Contributor

@jakobsj jakobsj left a comment

Choose a reason for hiding this comment

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

Excellent, I have no further comments. Thank you very much for this contribution, we're very pleased to receive and merge this in.

Just in case you are interested to contribute further, feel free to take a look at our list of open issues
https://github.com/CUQI-DTU/CUQIpy/issues
or submit a new issue in case you discover a bug, have a suggestion for improvement of code or documentation or would like to propose a new feature.

Thanks again,
Jakob

@jakobsj jakobsj merged commit 14b926d into CUQI-DTU:main May 3, 2023
@Karl-JT Karl-JT deleted the juntao_bug181_fix branch May 3, 2023 08:12
@nabriis
Copy link
Collaborator

nabriis commented May 3, 2023

Thanks again. A short note. The changes will automatically be part of our next release, but can already be acquired via our pre releases installed with:

pip install cuqipy --pre

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

Successfully merging this pull request may close these issues.

3 participants