Skip to content

Commit

Permalink
Update parameters dependency to modelfit_parameters
Browse files Browse the repository at this point in the history
Also clang-format everything again
  • Loading branch information
taranu committed Apr 29, 2024
1 parent 201c195 commit e907eb4
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 65 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ package - but it can be used for any kind of image or domain.
*gauss2dfit* is provided in two parts; a C++ shared library (libgauss2dfit) and
optional Python bindings. Both depend on the base package
`gauss2d <https://github.com/lsst-dm/gauss2d/>`, as well as the
`parameters <https://github.com/lsst-dm/parameters/>` package for defining
parameters with limits and transformations.
`modelfit_parameters <https://github.com/lsst/modelfit_parameters/>` package
for defining parameters with limits and transformations.

*gauss2dfit* is built with `Meson <https://github.com/mesonbuild>`_ and uses
`pkg-config <https://github.com/pkgconf/pkgconf>`_ to store package
Expand Down
9 changes: 4 additions & 5 deletions include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class Model : public ParametricModel {
unsigned int n_obsired = map_extra_weak.expired() + map_grad_weak.expired()
+ factors_grad_weak.expired() + factors_extra_weak.expired();
const bool expired = n_obsired == 4;
if(print) {
if (print) {
std::cout << "expired=" << expired << "\n";
}
if (!((n_obsired == 0) || expired)) {
Expand Down Expand Up @@ -770,9 +770,9 @@ class Model : public ParametricModel {
std::vector<double> loglike_new_plus;
std::vector<double> loglike_new_minus;
try {
param.set_value_transformed(value + diff/2.);
param.set_value_transformed(value + diff / 2.);
loglike_new_plus = this->evaluate();
param.set_value_transformed(value - diff/2.);
param.set_value_transformed(value - diff / 2.);
loglike_new_minus = this->evaluate();
} catch (std::runtime_error& e) {
param.set_value_transformed(value + diff);
Expand Down Expand Up @@ -1192,8 +1192,7 @@ class Model : public ParametricModel {
std::vector<std::vector<std::shared_ptr<Image>>> outputs = {},
std::vector<std::shared_ptr<Image>> residuals = {},
std::vector<std::shared_ptr<Image>> outputs_prior = {},
std::shared_ptr<Image> residuals_prior = nullptr,
bool force = false,
std::shared_ptr<Image> residuals_prior = nullptr, bool force = false,
bool print = false) {
const size_t n_outputs = outputs.size();
const bool has_outputs = n_outputs > 0;
Expand Down
4 changes: 3 additions & 1 deletion include/param_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <vector>

#include "parameters/parameter.h"
#include "lsst/modelfit/parameters.h"

namespace parameters = lsst::modelfit::parameters;

namespace gauss2d::fit {
using ParamBase = parameters::ParameterBase<double>;
Expand Down
5 changes: 3 additions & 2 deletions include/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <cfloat>
#include <iostream>

#include "parameters/parameter.h"
#include "parameters/unit.h"
#include "lsst/modelfit/parameters.h"

namespace parameters = lsst::modelfit::parameters;

namespace gauss2d::fit {

Expand Down
7 changes: 4 additions & 3 deletions include/sersicmixcomponent.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef GAUSS2D_FIT_SERSICMIXCOMPONENT_H
#define GAUSS2D_FIT_SERSICMIXCOMPONENT_H

#include <parameters/parameter.h>
#include "lsst/modelfit/parameters.h"

#include "channel.h"
#include "ellipticalcomponent.h"
Expand Down Expand Up @@ -68,7 +68,7 @@ class SersicParametricEllipseHolder {
}
};

// TODO: Add ref to derivation of weights, when publicized
// TODO: Add ref to derivation of weights, when published
/**
* @brief A Gaussian mixture approximation to a Sersic profile Component.
*
Expand All @@ -81,7 +81,8 @@ class SersicParametricEllipseHolder {
* but deliberately excludes the very inner and outermost regions.
*
* @note See https://ned.ipac.caltech.edu/level5/March05/Graham/Graham2.html
* for a useful summary of various properties of the Sersic profile.
* (Graham & Driver 2005) for a useful summary of various properties
* of the Sersic profile.
*/
class SersicMixComponent : private SersicParametricEllipseHolder, public EllipticalComponent {
private:
Expand Down
12 changes: 2 additions & 10 deletions include/transforms.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
//
// Created by dtaranu on 4/8/21.
//

#ifndef GAUSS2D_FIT_TRANSFORMS_H
#define GAUSS2D_FIT_TRANSFORMS_H

#include <cmath>
#include <iostream>
#include <memory>

#ifndef PARAMETERS_LIMITS_H
#include "parameters/limits.h"
#endif
#include "lsst/modelfit/parameters.h"

#ifndef PARAMETERS_TRANSFORM_H
#include "parameters/transform.h"
#endif
namespace parameters = lsst::modelfit::parameters;

namespace gauss2d::fit {
#pragma GCC diagnostic push
Expand Down
6 changes: 3 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ if gsl_dep.found()
endif

if use_eups
gauss2d_eupsdir = run_command('eups', 'list', '-d', 'gauss2d', check: true).stdout().strip()
gauss2d_eupsdir = run_command('sh', '-c', 'echo $GAUSS2D_DIR', check: true, capture: true).stdout().strip()
cpp = meson.get_compiler('cpp')
gauss2d_dep = declare_dependency(
dependencies: [cpp.find_library('gauss2d', dirs: [gauss2d_eupsdir + '/build-release/lib64']),],
include_directories: gauss2d_eupsdir + '/build-release/include',
)

parameters_eupsdir = run_command('eups', 'list', '-d', 'parameters', check: true).stdout().strip()
parameters_eupsdir = run_command('sh', '-c', 'echo $MODELFIT_PARAMETERS_DIR', check: true, capture: true).stdout().strip()
parameters_dep = declare_dependency(
include_directories: parameters_eupsdir + '/build-release/include',
)
else
gauss2d_dep = dependency('gauss2d')
parameters_dep = dependency('parameters')
parameters_dep = dependency('lsst_modelfit_parameters')
endif

inc = include_directories('include')
Expand Down
4 changes: 2 additions & 2 deletions python/gauss2d/fit/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "gauss2d/fit/parameters.h"
#include "gauss2d/fit/transforms.h"
#include "gauss2d/fit/util.h"
#include "parameters/transform.h"
#include "lsst/modelfit/parameters.h"

#include "parameters.h"
#include "pybind11.h"
Expand Down Expand Up @@ -71,7 +71,7 @@ void bind_parameters(py::module &m) {
declare_sizeparameter<double, g2f::SigmaYParameter, g2f::SizeYParameter>(m, "SigmaY");
declare_parameter<double, g2f::StdDevParameter>(m, "StdDev");
declare_transform_base<double>(m);
declare_transform<double, parameters::UnitTransform<double>>(m, "Unit");
declare_transform<double, lsst::modelfit::parameters::UnitTransform<double>>(m, "Unit");
declare_transform<double, g2f::InverseTransform>(m, "Inverse");
declare_transform<double, g2f::JanskyToABMagTransform>(m, "JanskyToABMag");
declare_transform<double, g2f::NanojanskyToABMagTransform>(m, "NanojanskyToABMag");
Expand Down
4 changes: 2 additions & 2 deletions python/gauss2d/fit/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

#include "gauss2d/fit/parameters.h"
#include "gauss2d/fit/transforms.h"
#include "parameters/limits.h"
#include "parameters/transform.h"
#include "lsst/modelfit/parameters.h"

#include "pybind11.h"
#include "utils.h"
Expand All @@ -44,6 +43,7 @@ namespace py = pybind11;
using namespace pybind11::literals;

namespace g2f = gauss2d::fit;
namespace parameters = lsst::modelfit::parameters;

template <typename T>
void declare_limits(py::module &m) {
Expand Down
2 changes: 1 addition & 1 deletion python/gauss2d/fit/sersicmixcomponent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#include "gauss2d/fit/parameters.h"
#include "gauss2d/fit/sersicmixcomponent.h"
#include "gauss2d/fit/sersicparametricellipse.h"
#include "lsst/modelfit/parameters.h"

#include "parameters.h"
#include "parameters/parameter.h"
#include "pybind11.h"

namespace py = pybind11;
Expand Down
4 changes: 2 additions & 2 deletions python/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if gsl_dep.found()
endif

if use_eups
gauss2d_eupsdir = run_command('eups', 'list', '-d', 'gauss2d', check: true).stdout().strip()
gauss2d_eupsdir = run_command('sh', '-c', 'echo $GAUSS2D_DIR', check: true, capture: true).stdout().strip()
cpp = meson.get_compiler('cpp')
gauss2d_dep = declare_dependency(
dependencies: [cpp.find_library('gauss2d', dirs: [gauss2d_eupsdir + '/build-release/lib64']),],
Expand All @@ -32,7 +32,7 @@ if use_eups
include_directories: gauss2d_eupsdir + '/python/lib/build-release/include',
)
gauss2dfit_eupsdir = gauss2dfit_eups.stdout().strip()
parameters_eupsdir = run_command('eups', 'list', '-d', 'parameters', check: true).stdout().strip()
parameters_eupsdir = run_command('sh', '-c', 'echo $MODELFIT_PARAMETERS_DIR', check: true, capture: true).stdout().strip()
# TODO: Can this dependency be extracted from '../meson.build' somehow
gauss2dfit_dep = declare_dependency(
dependencies: [cpp.find_library('gauss2dfit', dirs: [gauss2dfit_eupsdir + '/build-release/lib64']),],
Expand Down
4 changes: 1 addition & 3 deletions src/gaussianprior.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ PriorEvaluation GaussianPrior::evaluate(bool calc_jacobians, bool normalize) con
std::map<ParamBaseCRef, std::vector<double>> jacobians = {};
// dresidual/dx = d((x - mean)/stddev)/dx = 1/stddev
if (calc_jacobians) {
jacobians[*_param] = {
1.0 / (_stddev*(_transformed ? 1.0 : _param->get_transform_derivative()))
};
jacobians[*_param] = {1.0 / (_stddev * (_transformed ? 1.0 : _param->get_transform_derivative()))};
}

return PriorEvaluation{prior, {residual}, jacobians};
Expand Down
8 changes: 2 additions & 6 deletions src/sersicmixcomponent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ ParamCRefs& SersicMixComponent::get_parameters_const(ParamCRefs& params, ParamFi
return params;
}

double SersicMixComponent::get_sersicindex() const {
return this->_sersicindex->get_value();
}
double SersicMixComponent::get_sersicindex() const { return this->_sersicindex->get_value(); }

SersicMixComponentIndexParameter& SersicMixComponent::get_sersicindex_param() const {
return *(this->_sersicindex);
Expand Down Expand Up @@ -451,9 +449,7 @@ void SersicMixComponent::set_grad_param_factors(const Channel& channel, GradPara
}
}

void SersicMixComponent::set_sersicindex(double value) {
this->_sersicindex->set_value(value);
}
void SersicMixComponent::set_sersicindex(double value) { this->_sersicindex->set_value(value); }

std::string SersicMixComponent::repr(bool name_keywords) const {
return "SersicMixComponent(" + EllipticalComponent::repr(name_keywords) + ", "
Expand Down
38 changes: 15 additions & 23 deletions tests/test_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ typedef std::vector<std::shared_ptr<const g2f::Channel>> Channels;
const auto CHANNEL_NONE = g2f::Channel::NONE_PTR();
Channels CHANNELS_NONE = {CHANNEL_NONE};

std::shared_ptr<Data> make_data(Channels channels, const size_t n_x, const size_t n_y,
const size_t dn_x = 0., const size_t dn_y = 0.,
const double sigma_inv_value = 1.) {
std::shared_ptr<Data> make_data(Channels channels, const size_t n_x, const size_t n_y, const size_t dn_x = 0.,
const size_t dn_y = 0., const double sigma_inv_value = 1.) {
std::vector<std::shared_ptr<const Observation>> observations;
double dx_min = 0., dy_min = 0.;
for (const auto& channel_ptr : channels) {
Expand Down Expand Up @@ -214,26 +213,23 @@ void verify_model(Model& model, const std::vector<std::shared_ptr<const g2f::Cha

// TODO: Determine less arbitrary thresholds
double is_close_threshold = 2e-3;
if(
(name_row == g2f::ReffXParameter::_name)
if ((name_row == g2f::ReffXParameter::_name)
|| (name_row == g2f::ReffYParameter::_name)
|| (name_col == g2f::ReffXParameter::_name)
|| (name_col == g2f::ReffYParameter::_name)
|| (name_row == g2f::SersicIndexParameter::_name)
|| (name_col == g2f::SersicIndexParameter::_name)
) {
|| (name_col == g2f::SersicIndexParameter::_name)) {
is_close_threshold *= 10.;
}

if (!(
(skip_rho && (name_row == g2f::RhoParameter::_name))
if (!((skip_rho && (name_row == g2f::RhoParameter::_name))
|| (skip_rho && (name_col == g2f::RhoParameter::_name)))) {
auto value2 = hessian2->get_value(row, col);
auto msg = g2f::isclose(
value, value2, is_close_threshold, is_close_threshold/10
).isclose;
CHECK_MESSAGE(msg, "hessian[", row,
",", col, "]=(", value, ")!= hessian2 value=(", value2,
auto msg
= g2f::isclose(value, value2, is_close_threshold, is_close_threshold / 10)
.isclose;
CHECK_MESSAGE(msg, "hessian[", row, ",", col, "]=(", value,
")!= hessian2 value=(", value2,
") for params_free[row], params_free[col]=", param_str_row.str(),
param_str_col.str());
}
Expand Down Expand Up @@ -474,10 +470,7 @@ TEST_CASE("Model") {
double frac_value = 0.3;
for (const auto& channel : channels) {
auto frac1 = std::make_shared<g2f::ProperFractionParameter>(
frac_value,
nullptr,
g2f::get_transform_default<g2f::Log10Transform>()
);
frac_value, nullptr, g2f::get_transform_default<g2f::Log10Transform>());
auto frac2 = std::make_shared<g2f::ProperFractionParameter>(1.0, nullptr, nullptr, nullptr, true);
data_frac1.emplace_back(*channel, frac1);
data_frac2.emplace_back(*channel, frac2);
Expand Down Expand Up @@ -538,9 +531,9 @@ TEST_CASE("Model PSF") {

comps.emplace_back(std::make_shared<g2f::GaussianComponent>(
std::make_shared<g2f::GaussianParametricEllipse>(
std::make_shared<g2f::SigmaXParameter>(size, nullptr, transform_log10),
std::make_shared<g2f::SigmaYParameter>(size, nullptr, transform_log10),
std::make_shared<g2f::RhoParameter>(0, nullptr, transform_rho)),
std::make_shared<g2f::SigmaXParameter>(size, nullptr, transform_log10),
std::make_shared<g2f::SigmaYParameter>(size, nullptr, transform_log10),
std::make_shared<g2f::RhoParameter>(0, nullptr, transform_rho)),
cens, last));
}
g2f::Components psfcomps = {g2f::GaussianComponent::make_uniq_default_gaussians({0.})};
Expand Down Expand Up @@ -602,8 +595,7 @@ TEST_CASE("Model with priors") {
std::make_shared<g2f::ReffXParameter>(1.475109), std::make_shared<g2f::ReffYParameter>(1.469544),
std::make_shared<g2f::RhoParameter>(-0.020842));

auto ell_g2
= g2::Ellipse(ellipse_src->get_size_x(), ellipse_src->get_size_y(), ellipse_src->get_rho());
auto ell_g2 = g2::Ellipse(ellipse_src->get_size_x(), ellipse_src->get_size_y(), ellipse_src->get_rho());
auto ell_maj = g2::EllipseMajor(ell_g2);
double axrat = ell_maj.get_axrat();
double size_ell = sqrt(std::pow(ell_maj.get_r_major(), 2) * axrat);
Expand Down
5 changes: 5 additions & 0 deletions ups/eupspkg.cfg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ build()
(build_cc && build_py)
}

clean()
{
./clean.sh
}

config_cc()
{
([ -d "$GAUSS2DFIT_DIR" ] && ./clean-cc.sh && meson setup --prefix="$GAUSS2DFIT_DIR/build-release" \
Expand Down

0 comments on commit e907eb4

Please sign in to comment.