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

Implements Friends of Friends Sampling function #460

Merged
merged 9 commits into from
Dec 1, 2023
Merged

Conversation

atorchylo
Copy link
Contributor

Closes #16

Using fast3tree python package, I implemented the friends of friends sampling function. The algorithm works by precomputing groups for the given table, and then randomly sampling a group according to min_number and max_number requirements. The user has to provide link_distance to the algorithm, which determines the grouping of the galaxies. If the group exceeds the stamp size, the function raises an error. Here is the example code to quickly test:

import btk
import numpy as np
import matplotlib.pyplot as plt

catalog = btk.catalog.CatsimCatalog.from_file('data/input_catalog.fits')

sampling_function = btk.sampling_functions.FriendsOfFriendsSampling(
    min_number = 5, 
    max_number = 10, 
    link_distance=2.5,
    stamp_size=24
)
survey = btk.survey.get_surveys('LSST')
draw_generator = btk.draw_blends.CatsimGenerator(
    catalog,
    sampling_function,
    survey,
    batch_size=8,
    stamp_size=24,
    njobs=1,
    add_noise="background",
    seed=0, # use same seed here
)

blend_batch = next(draw_generator)
ii = np.random.randint(0, 8)

# plot the first blend in the batch, with the r-band
plt.imshow(blend_batch.blend_images[ii, 2, :, :], cmap="gray")

# plot centers
plt.scatter(blend_batch.catalog_list[ii]["x_peak"], 
            blend_batch.catalog_list[ii]["y_peak"], c="r", marker="x")

Sampled galaxies look like this:
Screen Shot 2023-09-19 at 12 59 24 PM
Screen Shot 2023-09-19 at 1 00 07 PM

@atorchylo atorchylo linked an issue Sep 19, 2023 that may be closed by this pull request
Copy link
Collaborator

@ismael-mendoza ismael-mendoza left a comment

Choose a reason for hiding this comment

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

Looks really good @atorchylo thanks for implementing this. Sorry it took me a while to get back to you. Please find some suggestions below

btk/sampling_functions.py Show resolved Hide resolved
btk/sampling_functions.py Outdated Show resolved Hide resolved
btk/sampling_functions.py Outdated Show resolved Hide resolved
btk/sampling_functions.py Outdated Show resolved Hide resolved
btk/sampling_functions.py Show resolved Hide resolved
@ismael-mendoza
Copy link
Collaborator

One more thing @atorchylo could you try reducing the link length even further and produce some examples to see if you only get very dramatic blends. Thanks!

btk/sampling_functions.py Outdated Show resolved Hide resolved
btk/sampling_functions.py Outdated Show resolved Hide resolved
@atorchylo
Copy link
Contributor Author

Hey @ismael-mendoza. I implemented your suggestions and also produced some examples of extreme blends. Code does seem to work as desired! Below are examples of galaxies blends with min number of 3 and link distance of 1.
Screen Shot 2023-11-26 at 10 42 52 PM
Screen Shot 2023-11-26 at 10 43 58 PM
Screen Shot 2023-11-26 at 10 45 19 PM

Copy link

codecov bot commented Dec 1, 2023

Codecov Report

Attention: 39 lines in your changes are missing coverage. Please review.

Comparison is base (cc89330) 62.53% compared to head (7b95cf9) 61.15%.

Files Patch % Lines
btk/sampling_functions.py 17.02% 39 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #460      +/-   ##
==========================================
- Coverage   62.53%   61.15%   -1.38%     
==========================================
  Files          19       19              
  Lines        1508     1555      +47     
==========================================
+ Hits          943      951       +8     
- Misses        565      604      +39     
Flag Coverage Δ
unittests 61.15% <17.02%> (-1.38%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ismael-mendoza ismael-mendoza merged commit cef8222 into dev Dec 1, 2023
5 of 6 checks passed
@ismael-mendoza ismael-mendoza deleted the fof-sampling branch December 1, 2023 18:24
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.

Implement friends-of-friends algorithm to generate blends
2 participants