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

BUG fix galsim random seeding again #28

Merged
merged 5 commits into from
Mar 27, 2024
Merged

BUG fix galsim random seeding again #28

merged 5 commits into from
Mar 27, 2024

Conversation

beckermr
Copy link
Collaborator

This PR is repeat of the fix from Y3 for galsim seeding. That fix was put into the Y3 scripts to launch the sims and so did not make it into the new code base.

xref: https://github.com/des-science/y3-wl_image_sims/commit/04cbd46cbba9031c31f5d72388c1ec99e21101c0

@beckermr
Copy link
Collaborator Author

@rmjarvis @sidneymau @esheldon please review!


# launder through the RNG a few times to randomize
for _ in range(10):
first = galsim.BaseDeviate(first).raw()

Choose a reason for hiding this comment

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

"a few times" is silly. There's absolutely no reason to do it more than once.

Copy link
Contributor

Choose a reason for hiding this comment

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

How does this fix the problem of the seeds?

montara/des_tile.py Outdated Show resolved Hide resolved
montara/des_tile.py Outdated Show resolved Hide resolved
@@ -626,6 +626,10 @@ def setup(self, config, base, file_num, logger):
if not isinstance(rs, list):
first = galsim.config.ParseValue(
base['image'], 'random_seed', base, int)[0]

# launder through the RNG to randomize
first = galsim.BaseDeviate(first).raw()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain how this fixes the seeding bug?

Copy link

@rmjarvis rmjarvis Mar 27, 2024

Choose a reason for hiding this comment

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

Sure.
GalSim uses a different random number generator for each object being rendered. This is so the stamp rendering can be parallelized over multiple processes deterministically. The random seed for each object is sequential based on the initial random seed (first here).
The problem that Sid was running into was that he was seeding different exposures with not very different random seeds. They were chosen from a set of integers that were fairly small. <32,000 maybe? I'm not sure exactly. But not nearly enough to make the sequences of seed for each exposure be disjoint from the sequences for any other exposure.
This fix (and the similar one in GalSim here) updates the given random seed to map onto some other value in [1,2^64). This essentially guarantees that the sequence of seeds that are used for each object in the exposure will be unique relative to any other exposure. Or at least the probability that they aren't is down at 1/2^64, rather than 1/2^15.

Copy link
Contributor

@esheldon esheldon Mar 27, 2024

Choose a reason for hiding this comment

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

This sounds reasonable, but I think there might be something else going on in addition to whatever this is fixing.

The problem was way more severe than 1/2^15. Essentially the exact series of rotation values were used for objects in all simulated tiles. There seem to be a small fraction that mismatched between the lists, which is odd in itself.

For example there were 31281722 entries in the catalog I had from a large set of simulated tiles, but only 93233 unique rotation values. And when matt looked at the catalogs, most of them were in the same sequence, in order, for different images.

This is almost like the same initial seed is being used for each run of the code (although the claim is that this is not true). If something like that is going on, I think the above would not fix it, because this first = galsim.BaseDeviate(first).raw() would be going from the same initial state.

Choose a reason for hiding this comment

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

I'm not definitively claiming that this is the only error. But it is a bug that we fixed in GalSim two years ago, which wasn't ported over to montara. It's possible there is something else wrong as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I think this is likely the bug. The truth files record every time an object is drawn across all exposures and bands. The fact that the rotation values are only unique for a subset is the correct result.

Instead the issue is that the unique values end up repeated across different tiles. This happens because of the small seeds as Mike points out.

Copy link
Contributor

Choose a reason for hiding this comment

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

My feeling is there is another issue as well, but we should go forward with this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep. My plan is to run some code at BNL to see what this fixes.

Copy link
Contributor

@esheldon esheldon Mar 27, 2024

Choose a reason for hiding this comment

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

One of the things that didn't make sense to me has been cleared up: I didn't realize the same object was in the truth catalog Nepoch times, which resulted in more duplicate rotation values than I would have predicted

Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering about that, but hadn't thought about nepochs (only bands). Glad that is worked out!

@beckermr beckermr merged commit 2636313 into main Mar 27, 2024
1 check passed
@beckermr beckermr deleted the beckermr-patch-1 branch March 27, 2024 18:04
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.

4 participants