Skip to content

Commit

Permalink
use xtensor concepts to fix clang builds
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-urban committed Sep 25, 2024
1 parent ef2592e commit 2275503
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/pymodule/amplitudecorrection/functions/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void init_m_functions(pybind11::module& m)

// range correction
submodule.def("get_sample_numbers_plus_half",
get_sample_numbers_plus_half<xt::pytensor, float, int64_t>,
get_sample_numbers_plus_half<xt::pytensor<float,1>, int64_t>,
DOC_functions(get_sample_numbers_plus_half),
py::arg("first_sample_nr"),
py::arg("last_sample_nr"),
Expand All @@ -63,7 +63,7 @@ void init_m_functions(pybind11::module& m)
py::arg("sound_velocity_m_s"));

submodule.def("approximate_ranges",
approximate_ranges<xt::pytensor, float, int64_t>,
approximate_ranges<xt::pytensor<float,1>, int64_t>,
DOC_functions(approximate_ranges),
py::arg("sample_interval_s"),
py::arg("sound_velocity_m_s"),
Expand All @@ -72,14 +72,14 @@ void init_m_functions(pybind11::module& m)
py::arg("step") = 1);

submodule.def("compute_cw_range_correction",
compute_cw_range_correction<xt::pytensor, float>,
compute_cw_range_correction<xt::pytensor<float,1>>,
DOC_functions(compute_cw_range_correction),
py::arg("ranges_m"),
py::arg("absorption_db_m"),
py::arg("tvg_factor"));

submodule.def("apply_wci_correction",
apply_wci_correction<xt::pytensor, float>,
apply_wci_correction<xt::pytensor<float, 2>, xt::pytensor<float, 1>>,
DOC_functions(apply_wci_correction),
py::arg("wci"),
py::arg("per_beam_offset"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: fd3765d68f5b2a0a54c3a4057bfde38c84b4a141f4eaacb1862ef398989d077a
//sourcehash: 449e2ac24adaedde10a18e765cfb777059a926172151583846f99e67ba681c04

/*
This file contains docstrings for use in the Python bindings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 1ae8666ee5111ec85d545eb0598565f91fc501c9641654802e5948d424bae907
//sourcehash: 9f9b40bf69cc0bc6ec707420219e7f552a786bcfa4cc47a6f78c86291bd384ae

/*
This file contains docstrings for use in the Python bindings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,61 @@
#include <Eigen/Dense>
#include <xtensor/xtensor.hpp>

#include <themachinethatgoesping/tools/helper/xtensor.hpp>

namespace themachinethatgoesping {
namespace algorithms {
namespace amplitudecorrection {
namespace functions {

template<template<typename, size_t> typename t_xtensor, typename t_float, typename t_int>
inline t_xtensor<t_float, 1> get_sample_numbers_plus_half(t_int first_sample_nr,
t_int last_sample_nr,
t_int step = 1)
// template<tools::helper::c_xtensor t_xtensor_2d, tools::helper::c_xtensor t_xtensor_1d>

template<tools::helper::c_xtensor t_xtensor_1d, typename t_int>
inline t_xtensor_1d get_sample_numbers_plus_half(t_int first_sample_nr,
t_int last_sample_nr,
t_int step = 1)
{
static_assert(tools::helper::c_xtensor_1d<t_xtensor_1d>,
"Template parameter must be a 1D tensor");

using t_float = tools::helper::xtensor_datatype<t_xtensor_1d>::type;

// calculate the sample numbers
return xt::arange<t_float>(first_sample_nr + 0.5, last_sample_nr + 0.5 + 1, step);
return xt::arange<t_float>(first_sample_nr + t_float(0.5), last_sample_nr + t_float(1.5), step);
}

template<typename t_float>
inline t_float approximate_range_factor(t_float sample_interval_s, t_float sound_velocity_m_s)
{
// calculate the ranges
return sample_interval_s * sound_velocity_m_s * 0.5;
return sample_interval_s * sound_velocity_m_s * t_float(0.5);
}

template<template<typename, size_t> typename t_xtensor, typename t_float, typename t_int>
inline t_xtensor<t_float, 1> approximate_ranges(t_float sample_interval_s,
t_float sound_velocity_m_s,
t_int first_sample_nr,
t_int last_sample_nr,
t_int step = 1)
template<tools::helper::c_xtensor t_xtensor_1d, typename t_int>
inline t_xtensor_1d approximate_ranges(
typename tools::helper::xtensor_datatype<t_xtensor_1d>::type sample_interval_s,
typename tools::helper::xtensor_datatype<t_xtensor_1d>::type sound_velocity_m_s,
t_int first_sample_nr,
t_int last_sample_nr,
t_int step = 1)
{
return get_sample_numbers_plus_half<t_xtensor, t_float, t_int>(
static_assert(tools::helper::c_xtensor_1d<t_xtensor_1d>,
"Template parameter must be a 1D tensor");

return get_sample_numbers_plus_half<t_xtensor_1d, t_int>(
first_sample_nr, last_sample_nr, step) *
approximate_range_factor(sample_interval_s, sound_velocity_m_s);
}

template<template<typename, size_t> typename t_xtensor, typename t_float>
inline t_xtensor<t_float, 1> compute_cw_range_correction(const t_xtensor<t_float, 1>& ranges_m,
t_float absorption_db_m,
t_float tvg_factor)
template<tools::helper::c_xtensor t_xtensor_1d>
inline t_xtensor_1d compute_cw_range_correction(
const t_xtensor_1d& ranges_m,
typename tools::helper::xtensor_datatype<t_xtensor_1d>::type absorption_db_m,
typename tools::helper::xtensor_datatype<t_xtensor_1d>::type tvg_factor)
{
static_assert(tools::helper::c_xtensor_1d<t_xtensor_1d>,
"Template parameter must be a 1D tensor");

// range correction = absorption*R + tvg_factor*log10(R)
return absorption_db_m * ranges_m + tvg_factor * xt::log10(ranges_m);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@
#include <fmt/core.h>
#include <xtensor/xadapt.hpp>
#include <xtensor/xarray.hpp>
#include <xtensor/xexpression.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xview.hpp>

#include <themachinethatgoesping/tools/helper/xtensor.hpp>

#include "rangecorrection.hpp"

namespace themachinethatgoesping {
namespace algorithms {
namespace amplitudecorrection {
namespace functions {

template<template<typename, size_t> typename t_xtensor, typename t_float>
inline t_xtensor<t_float, 2> apply_wci_correction(t_xtensor<t_float, 2> wci,
const t_xtensor<t_float, 1>& per_beam_offset,
const t_xtensor<t_float, 1>& per_sample_offset,
int mp_cores = 1)
template<tools::helper::c_xtensor t_xtensor_2d, tools::helper::c_xtensor t_xtensor_1d>
inline t_xtensor_2d apply_wci_correction(t_xtensor_2d wci,
const t_xtensor_1d& per_beam_offset,
const t_xtensor_1d& per_sample_offset,
int mp_cores = 1)
{
static_assert(tools::helper::c_xtensor_2d<t_xtensor_2d>,
"Template parameter must be a 2D tensor");
static_assert(tools::helper::c_xtensor_1d<t_xtensor_1d>,
"Template parameter must be a 1D tensor");

// assert that beam_offset has the same shape as wci.shape[0]
if (wci.shape(0) != per_beam_offset.size())
throw std::invalid_argument(fmt::format("wci.shape(0) [{}] != per_beam_offset.size() [{}]",
Expand Down

0 comments on commit 2275503

Please sign in to comment.