LCOV - code coverage report
Current view: top level - variable - cumulative.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 31 100.0 %
Date: 2024-11-17 01:47:58 Functions: 4 4 100.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/variable/cumulative.h"
       6             : #include "scipp/core/element/cumulative.h"
       7             : #include "scipp/variable/accumulate.h"
       8             : #include "scipp/variable/astype.h"
       9             : #include "scipp/variable/util.h"
      10             : 
      11             : using namespace scipp;
      12             : 
      13             : namespace scipp::variable {
      14             : 
      15             : namespace {
      16       31536 : auto as_precise(const Variable &var) {
      17       63072 :   return (var.dtype() == dtype<float>) ? astype(var, dtype<double>) : var;
      18             : }
      19             : } // namespace
      20             : 
      21        6593 : Variable cumsum(const Variable &var, const Dim dim, const CumSumMode mode) {
      22        6593 :   if (var.dims()[dim] == 0)
      23          10 :     return copy(var);
      24       13166 :   Variable cumulative = as_precise(copy(var.slice({dim, 0})));
      25        6583 :   fill_zeros(cumulative);
      26        6583 :   Variable out = copy(var);
      27        6583 :   if (mode == CumSumMode::Inclusive)
      28           3 :     accumulate_in_place(cumulative, out, core::element::inclusive_scan,
      29             :                         "cumsum");
      30             :   else
      31        6580 :     accumulate_in_place(cumulative, out, core::element::exclusive_scan,
      32             :                         "cumsum");
      33        6583 :   return out;
      34        6583 : }
      35             : 
      36       24953 : Variable cumsum(const Variable &var, const CumSumMode mode) {
      37       49906 :   Variable cumulative(as_precise(Variable(var, Dimensions{})));
      38       24953 :   Variable out = copy(var);
      39       24953 :   if (mode == CumSumMode::Inclusive)
      40       24951 :     accumulate_in_place(cumulative, out, core::element::inclusive_scan,
      41             :                         "cumsum");
      42             :   else
      43           2 :     accumulate_in_place(cumulative, out, core::element::exclusive_scan,
      44             :                         "cumsum");
      45       49906 :   return out;
      46       24953 : }
      47             : 
      48           3 : Variable cumsum_bins(const Variable &var, const CumSumMode mode) {
      49           3 :   Variable out = copy(var);
      50           3 :   const auto type = variable::variableFactory().elem_dtype(var);
      51           3 :   auto cumulative = Variable(type == dtype<float> ? dtype<double> : type,
      52           3 :                              var.dims(), var.unit());
      53           3 :   if (mode == CumSumMode::Inclusive)
      54           2 :     accumulate_in_place(cumulative, out, core::element::inclusive_scan,
      55             :                         "cumsum_bins");
      56             :   else
      57           1 :     accumulate_in_place(cumulative, out, core::element::exclusive_scan,
      58             :                         "cumsum_bins");
      59           6 :   return out;
      60           3 : }
      61             : 
      62             : } // namespace scipp::variable

Generated by: LCOV version 1.14