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-12-01 01:56:34 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       15119 : void copy_data(const Variable &src, Variable &dst) {
      24       15119 :   assert(src.dtype() == dtype<bucket<Variable>>);
      25       15119 :   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       15119 :                      core::Quaternion>(
      30    99982533 :       dst, src, [](auto &a, const auto &b) { a = b; }, "copy");
      31       15114 : }
      32             : 
      33             : /// Computes the size of each bin, i.e. the number of elements inside each bin.
      34        2587 : Variable bin_sizes(const Variable &var) {
      35        2587 :   if (is_bins(var)) {
      36        5174 :     const auto [begin, end] = unzip(var.bin_indices());
      37        2587 :     return end - begin;
      38        2587 :   }
      39           0 :   return makeVariable<scipp::index>(var.dims(), units::none);
      40             : }
      41             : 
      42       14019 : void copy_slices(const Variable &src, Variable dst, const Dim dim,
      43             :                  const Variable &srcIndices, const Variable &dstIndices) {
      44       28038 :   auto src_ = make_bins_no_validate(srcIndices, dim, src);
      45       28038 :   auto dst_ = make_bins_no_validate(dstIndices, dim, dst);
      46       14019 :   copy(src_, dst_);
      47       14023 : }
      48             : 
      49       31581 : Variable resize_default_init(const Variable &var, const Dim dim,
      50             :                              const scipp::index size) {
      51       31581 :   auto dims = var.dims();
      52       31581 :   if (dims.contains(dim))
      53       31581 :     dims.resize(dim, size);
      54             :   // Using variableFactory instead of variable::resize for creating
      55             :   // _uninitialized_ variable.
      56       31581 :   return variable::variableFactory().create(var.dtype(), dims, var.unit(),
      57       63162 :                                             var.has_variances());
      58       31581 : }
      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      128763 : Variable make_bins_no_validate(Variable indices, const Dim dim,
      74             :                                Variable buffer) {
      75      128763 :   return variable::make_bins_impl(std::move(indices), dim, std::move(buffer));
      76             : }
      77             : 
      78             : } // namespace scipp::variable

Generated by: LCOV version 1.14