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

Mechanical application of clang-format #372

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/kbmod/search/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@
using pp = search::PixelPos;
using std::to_string;


PYBIND11_MODULE(search, m) {
m.attr("KB_NO_DATA") = pybind11::float_(search::NO_DATA);
m.attr("HAS_GPU") = pybind11::bool_(search::HAVE_GPU);
py::enum_<search::StampType>(m, "StampType")
.value("STAMP_SUM", search::StampType::STAMP_SUM)
.value("STAMP_MEAN", search::StampType::STAMP_MEAN)
.value("STAMP_MEDIAN", search::StampType::STAMP_MEDIAN)
.export_values();
search::psf_bindings(m);
search::raw_image_bindings(m);
search::layered_image_bindings(m);
search::image_stack_bindings(m);
search::stack_search_bindings(m);
search::trajectory_bindings(m);
search::pixel_pos_bindings(m);
search::image_moments_bindings(m);
search::stamp_parameters_bindings(m);
// Functions from raw_image.cpp
m.def("create_median_image", &search::create_median_image);
m.def("create_summed_image", &search::create_summed_image);
m.def("create_mean_image", &search::create_mean_image);
// Functions from filtering.cpp
m.def("sigmag_filtered_indices", &search::sigmaGFilteredIndices);
m.def("calculate_likelihood_psi_phi", &search::calculateLikelihoodFromPsiPhi);
m.attr("KB_NO_DATA") = pybind11::float_(search::NO_DATA);
m.attr("HAS_GPU") = pybind11::bool_(search::HAVE_GPU);
py::enum_<search::StampType>(m, "StampType")
.value("STAMP_SUM", search::StampType::STAMP_SUM)
.value("STAMP_MEAN", search::StampType::STAMP_MEAN)
.value("STAMP_MEDIAN", search::StampType::STAMP_MEDIAN)
.export_values();
search::psf_bindings(m);
search::raw_image_bindings(m);
search::layered_image_bindings(m);
search::image_stack_bindings(m);
search::stack_search_bindings(m);
search::trajectory_bindings(m);
search::pixel_pos_bindings(m);
search::image_moments_bindings(m);
search::stamp_parameters_bindings(m);
// Functions from raw_image.cpp
m.def("create_median_image", &search::create_median_image);
m.def("create_summed_image", &search::create_summed_image);
m.def("create_mean_image", &search::create_mean_image);
// Functions from filtering.cpp
m.def("sigmag_filtered_indices", &search::sigmaGFilteredIndices);
m.def("calculate_likelihood_psi_phi", &search::calculateLikelihoodFromPsiPhi);
}
217 changes: 99 additions & 118 deletions src/kbmod/search/common.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
#ifndef COMMON_H_
#define COMMON_H_


#include <string>
#include "pydocs/common_docs.h"


namespace search {
#ifdef HAVE_CUDA
constexpr bool HAVE_GPU = true;
constexpr bool HAVE_GPU = true;
#else
constexpr bool HAVE_GPU = false;
constexpr bool HAVE_GPU = false;
#endif

constexpr unsigned int MAX_KERNEL_RADIUS = 15;
constexpr unsigned short MAX_STAMP_EDGE = 64;
constexpr unsigned short CONV_THREAD_DIM = 32;
constexpr unsigned short THREAD_DIM_X = 128;
constexpr unsigned short THREAD_DIM_Y = 2;
constexpr unsigned short RESULTS_PER_PIXEL = 8;
constexpr float NO_DATA = -9999.0;

enum StampType { STAMP_SUM = 0, STAMP_MEAN, STAMP_MEDIAN };

/*
* Data structure to represent an objects trajectory
* through a stack of images
*/
struct Trajectory {
constexpr unsigned int MAX_KERNEL_RADIUS = 15;
constexpr unsigned short MAX_STAMP_EDGE = 64;
constexpr unsigned short CONV_THREAD_DIM = 32;
constexpr unsigned short THREAD_DIM_X = 128;
constexpr unsigned short THREAD_DIM_Y = 2;
constexpr unsigned short RESULTS_PER_PIXEL = 8;
constexpr float NO_DATA = -9999.0;

enum StampType { STAMP_SUM = 0, STAMP_MEAN, STAMP_MEDIAN };

/*
* Data structure to represent an objects trajectory
* through a stack of images
*/
struct Trajectory {
// Trajectory velocities
float vx;
float vy;
Expand All @@ -43,45 +41,34 @@ namespace search {

// I can't believe string::format is not a thing until C++ 20
const std::string to_string() const {
return "lh: " + std::to_string(lh) +
" flux: " + std::to_string(flux) +
" x: " + std::to_string(x) +
" y: " + std::to_string(y) +
" vx: " + std::to_string(vx) +
" vy: " + std::to_string(vy) +
" obs_count: " + std::to_string(obs_count);
return "lh: " + std::to_string(lh) + " flux: " + std::to_string(flux) + " x: " + std::to_string(x) +
Copy link
Collaborator

Choose a reason for hiding this comment

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

This was a little easier to read in the original formatting (one field per line). Dunno about disabling code formatting for this block, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed that it was a little easier to read before. But I will keep it this way for now for consistency.

" y: " + std::to_string(y) + " vx: " + std::to_string(vx) + " vy: " + std::to_string(vy) +
" obs_count: " + std::to_string(obs_count);
}

// returns a yaml-compliant string
const std::string to_yaml() const {
return "{lh: " + std::to_string(lh) +
", flux: " + std::to_string(flux) +
", x: " + std::to_string(x) +
", y: " + std::to_string(y) +
", vx: " + std::to_string(vx) +
", vy: " + std::to_string(vy) +
", obs_count: " + std::to_string(obs_count)
+"}";
return "{lh: " + std::to_string(lh) + ", flux: " + std::to_string(flux) +
", x: " + std::to_string(x) + ", y: " + std::to_string(y) + ", vx: " + std::to_string(vx) +
", vy: " + std::to_string(vy) + ", obs_count: " + std::to_string(obs_count) + "}";
}
};
};

// The position (in pixels) of a trajectory.
struct PixelPos {
// The position (in pixels) of a trajectory.
struct PixelPos {
float x;
float y;

const std::string to_string() const {
return "x: " + std::to_string(x) + " y: " + std::to_string(y);
}
const std::string to_string() const { return "x: " + std::to_string(x) + " y: " + std::to_string(y); }

const std::string to_yaml() const {
return "{x: " + std::to_string(x) + " y: " + std::to_string(y) + "}";
return "{x: " + std::to_string(x) + " y: " + std::to_string(y) + "}";
Copy link
Collaborator

Choose a reason for hiding this comment

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

the to_yaml above uses a comma between fields. Would be nice to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to keep as-is for now to keep this a purely mechanical application of clang-format, but will change this in the next PR.

}
};
};

/* The parameters to use for the on device search. */
/* The parameters to use for the on device search. */

struct SearchParameters {
struct SearchParameters {
// Basic filtering paramets.
int min_observations;
float min_lh;
Expand All @@ -105,24 +92,24 @@ namespace search {

// Provide debugging output.
bool debug;
};
};

struct scaleParameters {
struct scaleParameters {
float min_val;
float max_val;
float scale;
};
};

// Search data on a per-image basis.
struct PerImageData {
// Search data on a per-image basis.
struct PerImageData {
int num_images = 0;

float* image_times = nullptr;
scaleParameters* psi_params = nullptr;
scaleParameters* phi_params = nullptr;
};
float *image_times = nullptr;
scaleParameters *psi_params = nullptr;
scaleParameters *phi_params = nullptr;
};

struct StampParameters {
struct StampParameters {
int radius = 10;
StampType stamp_type = STAMP_SUM;
bool do_filtering = false;
Expand All @@ -138,89 +125,83 @@ namespace search {
float m11_limit;
float m02_limit;
float m20_limit;
};
};

// Basic image moments use for analysis.
struct ImageMoments {
// Basic image moments use for analysis.
struct ImageMoments {
float m00;
float m01;
float m10;
float m11;
float m02;
float m20;
};
};

#ifdef Py_PYTHON_H
namespace py = pybind11;
namespace py = pybind11;

static void trajectory_bindings(py::module &m) {
static void trajectory_bindings(py::module &m) {
using tj = Trajectory;

py::class_<tj>(m, "Trajectory", pydocs::DOC_Trajectory)
.def(py::init<>())
.def_readwrite("vx", &tj::vx)
.def_readwrite("vy", &tj::vy)
.def_readwrite("lh", &tj::lh)
.def_readwrite("flux", &tj::flux)
.def_readwrite("x", &tj::x)
.def_readwrite("y", &tj::y)
.def_readwrite("obs_count", &tj::obs_count)
.def("__repr__", [](const tj &t) { return "Trajectory(" + t.to_string() + ")"; })
.def("__str__", &tj::to_string)
.def(py::pickle(
[] (const tj &p) { // __getstate__
.def(py::init<>())
.def_readwrite("vx", &tj::vx)
.def_readwrite("vy", &tj::vy)
.def_readwrite("lh", &tj::lh)
.def_readwrite("flux", &tj::flux)
.def_readwrite("x", &tj::x)
.def_readwrite("y", &tj::y)
.def_readwrite("obs_count", &tj::obs_count)
.def("__repr__", [](const tj &t) { return "Trajectory(" + t.to_string() + ")"; })
.def("__str__", &tj::to_string)
.def(py::pickle(
[](const tj &p) { // __getstate__
return py::make_tuple(p.vx, p.vy, p.lh, p.flux, p.x, p.y, p.obs_count);
},
[] (py::tuple t) { // __setstate__
if (t.size() != 7)
throw std::runtime_error("Invalid state!");
tj trj = {
t[0].cast<float>(), t[1].cast<float>(), t[2].cast<float>(),
t[3].cast<float>(), t[4].cast<short>(), t[5].cast<short>(),
t[6].cast<short>()
};
},
[](py::tuple t) { // __setstate__
if (t.size() != 7) throw std::runtime_error("Invalid state!");
tj trj = {t[0].cast<float>(), t[1].cast<float>(), t[2].cast<float>(),
t[3].cast<float>(), t[4].cast<short>(), t[5].cast<short>(),
t[6].cast<short>()};
return trj;
})
);
}
}));
}

static void pixel_pos_bindings(py::module &m) {
static void pixel_pos_bindings(py::module &m) {
py::class_<PixelPos>(m, "PixelPos", pydocs::DOC_PixelPos)
.def(py::init<>())
.def_readwrite("x", &PixelPos::x)
.def_readwrite("y", &PixelPos::y)
.def("__repr__", [] (const PixelPos &p) {
return "PixelPos(" + p.to_string() + ")";
})
.def("__str__", &PixelPos::to_string);
}

static void image_moments_bindings(py::module &m) {
.def(py::init<>())
.def_readwrite("x", &PixelPos::x)
.def_readwrite("y", &PixelPos::y)
.def("__repr__", [](const PixelPos &p) { return "PixelPos(" + p.to_string() + ")"; })
.def("__str__", &PixelPos::to_string);
}

static void image_moments_bindings(py::module &m) {
py::class_<ImageMoments>(m, "ImageMoments", pydocs::DOC_ImageMoments)
.def(py::init<>())
.def_readwrite("m00", &ImageMoments::m00)
.def_readwrite("m01", &ImageMoments::m01)
.def_readwrite("m10", &ImageMoments::m10)
.def_readwrite("m11", &ImageMoments::m11)
.def_readwrite("m02", &ImageMoments::m02)
.def_readwrite("m20", &ImageMoments::m20);
}

static void stamp_parameters_bindings(py::module &m) {
.def(py::init<>())
.def_readwrite("m00", &ImageMoments::m00)
.def_readwrite("m01", &ImageMoments::m01)
.def_readwrite("m10", &ImageMoments::m10)
.def_readwrite("m11", &ImageMoments::m11)
.def_readwrite("m02", &ImageMoments::m02)
.def_readwrite("m20", &ImageMoments::m20);
}

static void stamp_parameters_bindings(py::module &m) {
py::class_<StampParameters>(m, "StampParameters", pydocs::DOC_StampParameters)
.def(py::init<>())
.def_readwrite("radius", &StampParameters::radius)
.def_readwrite("stamp_type", &StampParameters::stamp_type)
.def_readwrite("do_filtering", &StampParameters::do_filtering)
.def_readwrite("center_thresh", &StampParameters::center_thresh)
.def_readwrite("peak_offset_x", &StampParameters::peak_offset_x)
.def_readwrite("peak_offset_y", &StampParameters::peak_offset_y)
.def_readwrite("m01_limit", &StampParameters::m01_limit)
.def_readwrite("m10_limit", &StampParameters::m10_limit)
.def_readwrite("m11_limit", &StampParameters::m11_limit)
.def_readwrite("m02_limit", &StampParameters::m02_limit)
.def_readwrite("m20_limit", &StampParameters::m20_limit);
}
.def(py::init<>())
.def_readwrite("radius", &StampParameters::radius)
.def_readwrite("stamp_type", &StampParameters::stamp_type)
.def_readwrite("do_filtering", &StampParameters::do_filtering)
.def_readwrite("center_thresh", &StampParameters::center_thresh)
.def_readwrite("peak_offset_x", &StampParameters::peak_offset_x)
.def_readwrite("peak_offset_y", &StampParameters::peak_offset_y)
.def_readwrite("m01_limit", &StampParameters::m01_limit)
.def_readwrite("m10_limit", &StampParameters::m10_limit)
.def_readwrite("m11_limit", &StampParameters::m11_limit)
.def_readwrite("m02_limit", &StampParameters::m02_limit)
.def_readwrite("m20_limit", &StampParameters::m20_limit);
}

#endif /* Py_PYTHON_H */

Expand Down
Loading