-
Notifications
You must be signed in to change notification settings - Fork 14
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
Batch search inside stack search #530
Merged
jeremykubica
merged 10 commits into
dirac-institute:main
from
vlnistor:I-356/batch-search
Apr 8, 2024
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e75f93f
batch search inside stack search
vlnistor 666d2e5
moved the batch search inside a context manager to remove the need to…
vlnistor 88d3289
fixed to include 2nd argument in prepare_batch
vlnistor 12894f8
conclude merge
vlnistor 0b25c32
resolving search_batch merge conflicts with main
vlnistor cbcaa9c
added move assignment operators for both TrajectoryList and GPUArray …
vlnistor cc5d52d
cleaned up comments
vlnistor 2106801
search_batch functionality extracted from search_all and search_singl…
vlnistor 9b5226f
renamed helper to compute_max_results and minor style changes
vlnistor 6180283
kbmod_reference notebook only change search.search -> search.search_all
vlnistor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class BatchSearchManager: | ||
def __init__(self, stack_search, search_list, min_observations): | ||
""" | ||
Initialize the context manager with an instance of StackSearch, | ||
the list of trajectories to search, and the minimum number of observations. | ||
|
||
Parameters: | ||
vlnistor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- stack_search: Instance of the StackSearch class. | ||
- search_list: List of trajectories to search. | ||
- min_observations: Minimum number of observations for the search. | ||
""" | ||
self.stack_search = stack_search | ||
self.search_list = search_list | ||
self.min_observations = min_observations | ||
|
||
def __enter__(self): | ||
""" | ||
This method is called when entering the context managed by the `with` statement. | ||
It prepares the batch search by calling `prepare_batch_search` on the StackSearch instance. | ||
""" | ||
# Prepare memory for the batch search. | ||
self.stack_search.prepare_batch_search(self.search_list, self.min_observations) | ||
return self.stack_search | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
""" | ||
This method is called when exiting the context. | ||
It cleans up resources by calling `finish_search` on the StackSearch instance. | ||
|
||
Parameters: | ||
- exc_type: The exception type if an exception was raised in the `with` block. | ||
- exc_value: The exception value if an exception was raised. | ||
- traceback: The traceback object if an exception was raised. | ||
""" | ||
# Clean up | ||
self.stack_search.finish_search() | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import numpy as np | ||
|
||
from kbmod.batch_search import BatchSearchManager | ||
from kbmod.configuration import SearchConfiguration | ||
from kbmod.fake_data.fake_data_creator import add_fake_object, make_fake_layered_image, FakeDataSet | ||
from kbmod.run_search import SearchRunner | ||
|
@@ -932,6 +933,73 @@ def test_coadd_filter_gpu(self): | |
self.assertEqual(meanStamps[2].width, 1) | ||
self.assertEqual(meanStamps[2].height, 1) | ||
|
||
@staticmethod | ||
def result_hash(res): | ||
return hash((res.x, res.y, res.vx, res.vy, res.lh, res.obs_count)) | ||
|
||
def test_search_batch(self): | ||
width = 50 | ||
height = 50 | ||
results_per_pixel = 8 | ||
min_observations = 2 | ||
|
||
# Simple average PSF | ||
psf_data = np.zeros((5, 5), dtype=np.single) | ||
psf_data[1:4, 1:4] = 0.1111111 | ||
p = PSF(psf_data) | ||
|
||
# Create a stack with 10 20x20 images with random noise and times ranging from 0 to 1 | ||
count = 10 | ||
imlist = [make_fake_layered_image(width, height, 5.0, 25.0, n / count, p) for n in range(count)] | ||
stack = ImageStack(imlist) | ||
im_list = stack.get_images() | ||
# Create a new list of LayeredImages with the added object. | ||
new_im_list = [] | ||
vlnistor marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed now |
||
for im, time in zip(im_list, stack.build_zeroed_times()): | ||
add_fake_object(im, 5.0 + (time * 8.0), 35.0 + (time * 0.0), 25000.0) | ||
new_im_list.append(im) | ||
|
||
# Save these images in a new ImageStack and create a StackSearch object from them. | ||
stack = ImageStack(new_im_list) | ||
search = StackSearch(stack) | ||
|
||
# Sample generator | ||
gen = KBMODV1Search( | ||
10, 5, 15, 10, -0.1, 0.1 | ||
) # velocity_steps, min_vel, max_vel, angle_steps, min_ang, max_ang, | ||
candidates = [trj for trj in gen] | ||
|
||
# Peform complete in-memory search | ||
search.search(candidates, min_observations) | ||
total_results = width * height * results_per_pixel | ||
# Need to filter as the fields are undefined otherwise | ||
results = [ | ||
result | ||
for result in search.get_results(0, total_results) | ||
if result.lh > -1 and result.obs_count >= min_observations | ||
] | ||
|
||
with BatchSearchManager(StackSearch(stack), candidates, min_observations) as batch_search: | ||
|
||
batch_results = [] | ||
for i in range(0, width, 5): | ||
batch_search.set_start_bounds_x(i, i + 5) | ||
for j in range(0, height, 5): | ||
batch_search.set_start_bounds_y(j, j + 5) | ||
batch_results.extend(batch_search.search_batch()) | ||
|
||
# Need to filter as the fields are undefined otherwise | ||
batch_results = [ | ||
result for result in batch_results if result.lh > -1 and result.obs_count >= min_observations | ||
] | ||
|
||
# Check that the results are the same. | ||
results_hash_set = {test_search.result_hash(result) for result in results} | ||
batch_results_hash_set = {test_search.result_hash(result) for result in batch_results} | ||
|
||
for res_hash in results_hash_set: | ||
self.assertTrue(res_hash in batch_results_hash_set) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main idea here was that I didn't want the user to have to call
prepare_batch_search
andfinish_search
manuallyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR, but something to consider as a future extension: In theory this could be extended further by making this a generator:
where the
BatchSearchManager
tracks the block sizes to search, current block locations, etc.