Skip to content

Commit

Permalink
Merge branch 'main' into rawimage_time
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykubica committed Sep 30, 2024
2 parents 80b7a1d + d9589d4 commit 49e4550
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/kbmod/image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ def fromTargets(cls, tgts, force=None, config=None, **kwargs):
of the registered standardizers can be provided. Optionally,
provide the `Standardizer` class itself in which case it will be
called for each target in the iterable.
config : `~StandardizerConfig`, `dict` or `None`, optional
Standardizer configuration or dictionary containing the config
parameters for standardization. When `None` default values for the
appropriate `Standardizer` will be used.
**kwargs : `dict`
Remaining keyword arguments are passed to the `Standardizer`.
Expand All @@ -296,10 +300,14 @@ def fromDir(cls, dirpath, recursive=False, force=None, config=None, **kwargs):
recursive : `bool`
If the location is a local filesystem directory, scan it
recursively including all sub-directories.
forceStandardizer : `Standardizer` or `None`
force : `Standardizer` or `None`
If `None`, when applicable, determine the correct `Standardizer` to
use automatically. Otherwise force the use of the given
`Standardizer`.
config : `~StandardizerConfig`, `dict` or `None`, optional
Standardizer configuration or dictionary containing the config
parameters for standardization. When `None` default values for the
appropriate `Standardizer` will be used.
**kwargs : `dict`
Remaining kwargs, not listed here, are passed onwards to
the underlying `Standardizer`.
Expand Down
20 changes: 12 additions & 8 deletions src/kbmod/search/stamp_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std::vector<RawImage> StampCreator::create_stamps(ImageStack& stack, const Traje
bool keep_no_data, const std::vector<bool>& use_index) {
if (use_index.size() > 0)
assert_sizes_equal(use_index.size(), stack.img_count(), "create_stamps() use_index");
bool use_all_stamps = use_index.size() == 0;
bool use_all_stamps = (use_index.size() == 0);

std::vector<RawImage> stamps;
unsigned int num_times = stack.img_count();
Expand Down Expand Up @@ -65,8 +65,8 @@ std::vector<RawImage> StampCreator::get_coadded_stamps(ImageStack& stack, std::v
std::vector<std::vector<bool>>& use_index_vect,
const StampParameters& params, bool use_gpu) {
logging::Logger* rs_logger = logging::getLogger("kbmod.search.stamp_creator");
rs_logger->info("Performing stamp filtering on " + std::to_string(t_array.size()) + " trajectories.");
DebugTimer timer = DebugTimer("stamp filtering", rs_logger);
rs_logger->info("Generating co_added stamps on " + std::to_string(t_array.size()) + " trajectories.");
DebugTimer timer = DebugTimer("coadd generating", rs_logger);

// We use the GPU if we have it for everything except STAMP_VAR_WEIGHTED which is CPU only.
if (use_gpu && (params.stamp_type != STAMP_VAR_WEIGHTED)) {
Expand Down Expand Up @@ -277,7 +277,7 @@ std::vector<RawImage> StampCreator::create_variance_stamps(ImageStack& stack, co
int radius, const std::vector<bool>& use_index) {
if (use_index.size() > 0)
assert_sizes_equal(use_index.size(), stack.img_count(), "create_stamps() use_index");
bool use_all_stamps = use_index.size() == 0;
bool use_all_stamps = (use_index.size() == 0);

std::vector<RawImage> stamps;
unsigned int num_times = stack.img_count();
Expand All @@ -297,22 +297,26 @@ std::vector<RawImage> StampCreator::create_variance_stamps(ImageStack& stack, co

RawImage StampCreator::get_variance_weighted_stamp(ImageStack& stack, const Trajectory& trj, int radius,
const std::vector<bool>& use_index) {
if (radius < 0) throw std::runtime_error("Invalid stamp radius. Must be >= 0.");
unsigned int num_images = stack.img_count();
if (num_images == 0) throw std::runtime_error("Unable to create mean image given 0 images.");
unsigned int stamp_width = 2 * radius + 1;

// Make the stamps for each time step.
std::vector<bool> empty_vect;
std::vector<RawImage> sci_stamps = create_stamps(stack, trj, radius, true /*=keep_no_data*/, use_index);
std::vector<RawImage> var_stamps = create_variance_stamps(stack, trj, radius, use_index);
if (sci_stamps.size() != var_stamps.size()) {
throw std::runtime_error("Mismatched number of stamps returned.");
}
num_images = sci_stamps.size();

// Do the weighted mean.
Image result = Image::Zero(stamp_width, stamp_width);
for (int y = 0; y < stamp_width; ++y) {
for (int x = 0; x < stamp_width; ++x) {
for (unsigned int y = 0; y < stamp_width; ++y) {
for (unsigned int x = 0; x < stamp_width; ++x) {
float sum = 0.0;
float scale = 0.0;
for (int i = 0; i < num_images; ++i) {
for (unsigned int i = 0; i < num_images; ++i) {
float sci_val = sci_stamps[i].get_pixel({y, x});
float var_val = var_stamps[i].get_pixel({y, x});
if (pixel_value_valid(sci_val) && pixel_value_valid(var_val) && (var_val != 0.0)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ def toLayeredImage(self):
# copy. TODO: fix when/if CPP stuff is fixed.
imgs = []
for sci, var, mask, psf, t in zip(sciences, variances, masks, psfs, mjds):
# Make sure the science and variance layers are float32.
sci = sci.astype(np.float32)
var = var.astype(np.float32)

# Converts nd array mask from bool to np.float32
mask = mask.astype(np.float32)
imgs.append(LayeredImage(RawImage(sci), RawImage(var), RawImage(mask), psf, obs_time=t))
Expand Down

0 comments on commit 49e4550

Please sign in to comment.