LCOV - code coverage report
Current view: top level - variable - bins.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 29 96.6 %
Date: 2024-04-28 01:25:40 Functions: 18 24 75.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Simon Heybrock
       5             : #include "scipp/core/eigen.h"
       6             : #include "scipp/core/element/arg_list.h"
       7             : 
       8             : #include "scipp/variable/bins.h"
       9             : #include "scipp/variable/comparison.h"
      10             : #include "scipp/variable/reduction.h"
      11             : #include "scipp/variable/subspan_view.h"
      12             : #include "scipp/variable/transform.h"
      13             : #include "scipp/variable/util.h"
      14             : 
      15             : #include "operations_common.h"
      16             : 
      17             : namespace scipp::variable {
      18             : 
      19             : /// Used internally by BinArrayModel to implement copying.
      20             : ///
      21             : /// This is using transform, so only data (no coords, etc.) is copied, but we
      22             : /// only use this for buffers of type Variable.
      23       13626 : void copy_data(const Variable &src, Variable &dst) {
      24       13626 :   assert(src.dtype() == dtype<bucket<Variable>>);
      25       13626 :   assert(dst.dtype() == dtype<bucket<Variable>>);
      26             :   transform_in_place<double, float, int64_t, int32_t, bool, std::string,
      27             :                      core::time_point, scipp::index_pair, Eigen::Vector3d,
      28             :                      Eigen::Matrix3d, Eigen::Affine3d, core::Translation,
      29       13626 :                      core::Quaternion>(
      30    97793354 :       dst, src, [](auto &a, const auto &b) { a = b; }, "copy");
      31       13621 : }
      32             : 
      33             : /// Computes the size of each bin, i.e. the number of elements inside each bin.
      34        2235 : Variable bin_sizes(const Variable &var) {
      35        2235 :   if (is_bins(var)) {
      36        4470 :     const auto [begin, end] = unzip(var.bin_indices());
      37        2235 :     return end - begin;
      38        2235 :   }
      39           0 :   return makeVariable<scipp::index>(var.dims(), units::none);
      40             : }
      41             : 
      42       12490 : void copy_slices(const Variable &src, Variable dst, const Dim dim,
      43             :                  const Variable &srcIndices, const Variable &dstIndices) {
      44       24980 :   auto src_ = make_bins_no_validate(srcIndices, dim, src);
      45       24980 :   auto dst_ = make_bins_no_validate(dstIndices, dim, dst);
      46       12490 :   copy(src_, dst_);
      47       12494 : }
      48             : 
      49       31247 : Variable resize_default_init(const Variable &var, const Dim dim,
      50             :                              const scipp::index size) {
      51       31247 :   auto dims = var.dims();
      52       31247 :   if (dims.contains(dim))
      53       31247 :     dims.resize(dim, size);
      54             :   // Using variableFactory instead of variable::resize for creating
      55             :   // _uninitialized_ variable.
      56       31247 :   return variable::variableFactory().create(var.dtype(), dims, var.unit(),
      57       62494 :                                             var.has_variances());
      58       31247 : }
      59             : 
      60             : /// Construct a bin-variable over a variable.
      61             : ///
      62             : /// Each bin is represented by a VariableView. `indices` defines the array of
      63             : /// bins as slices of `buffer` along `dim`.
      64       32584 : Variable make_bins(Variable indices, const Dim dim, Variable buffer) {
      65       32584 :   expect_valid_bin_indices(indices, dim, buffer.dims());
      66       32578 :   return make_bins_no_validate(std::move(indices), dim, buffer);
      67             : }
      68             : 
      69             : /// Construct a bin-variable over a variable without index validation.
      70             : ///
      71             : /// Must be used only when it is guaranteed that indices are valid or overlap of
      72             : /// bins is acceptable.
      73      125079 : Variable make_bins_no_validate(Variable indices, const Dim dim,
      74             :                                Variable buffer) {
      75      125079 :   return variable::make_bins_impl(std::move(indices), dim, std::move(buffer));
      76             : }
      77             : 
      78             : } // namespace scipp::variable

Generated by: LCOV version 1.14