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

gh-269: add tests for glass.fields #374

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

Saransh-cpp
Copy link
Member

@Saransh-cpp Saransh-cpp commented Oct 16, 2024

Add tests and bump coverage to ~100 for glass.fields.

Closes: #269

@Saransh-cpp Saransh-cpp added the maintenance Maintenance: refactoring, typos, etc. label Oct 16, 2024
@Saransh-cpp Saransh-cpp self-assigned this Oct 16, 2024
@Saransh-cpp Saransh-cpp changed the base branch from main to paddy/issue-358 November 5, 2024 16:26
Base automatically changed from paddy/issue-358 to main November 7, 2024 10:32
@paddyroddy
Copy link
Member

Add tests and bump coverage to 100 for glass.points.

Closes: #269

I assume this is meant to say glass.fields? Is it ready?

@Saransh-cpp Saransh-cpp marked this pull request as ready for review November 13, 2024 16:09
@Saransh-cpp
Copy link
Member Author

Should be ready now! I'll create a new issue for reviewing scientific tests for glass.fields.

@Saransh-cpp
Copy link
Member Author

Almost at 80% with this PR

@paddyroddy
Copy link
Member

Almost at 80% with this PR

Wow, that went up a lot!

@paddyroddy paddyroddy self-requested a review November 13, 2024 16:38
assert gaussian_fields[0].shape == (hp.nside2npix(nside),)

# requires resetting the RNG for reproducibility
rng = np.random.default_rng(seed=42)
Copy link
Member

Choose a reason for hiding this comment

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

Why have it twice within this function?

Copy link
Member

Choose a reason for hiding this comment

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

Should we set it once at the top of the file maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Need it twice to ensure that rng.any_distribution() is same both the times, and it needs to be redefined (instead of using rng from the arguments) so that it starts sampling from scratch.

Setting it at the top of the file gives me -

        # requires resetting the RNG for reproducibility
>       gaussian_fields = list(generate_gaussian(gls, nside, rng=rng))
E       UnboundLocalError: cannot access local variable 'rng' where it is not associated with a value

Copy link
Member

Choose a reason for hiding this comment

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

Okay! Might a little comment saying why it's being redefined?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the right approach here is to use our central rng fixture and clone it:

from copy import deepcopy

def test_xyz(rng):
    rng1 = deepcopy(rng)
    rng2 = deepcopy(rng)
    ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

This probably needs a fairly recent numpy

Copy link
Collaborator

@ntessore ntessore Nov 14, 2024

Choose a reason for hiding this comment

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

But what is this actually testing? If it's that the generation is reproducible, it's on the RNG, not on us. In which case I would rather test that the rng was actually used. If you don't want to mock the entire RNG interface (let's not), we can check that the state changed:

initial_rng_state = rng.bit_generator.state

...

assert rng.bit_generator.state != initial_rng_state

Copy link
Member Author

Choose a reason for hiding this comment

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

My initial thought behind adding this test was to check if generate_gaussian with and without ncorr=1 outputs the same result (because that is a pattern that I observed while running the function manually). Is there a better test that can be added to check if passing the ncorr argument works, or should I leave it for #414?

rng = np.random.default_rng(seed=42)
lognormal_fields = list(generate_lognormal(gls, nside, shift=1.0, rng=rng))

rng = np.random.default_rng(seed=42)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@paddyroddy paddyroddy added the testing Work is related to testing label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Maintenance: refactoring, typos, etc. testing Work is related to testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add/review tests for glass.fields
3 participants