LCOV - code coverage report
Current view: top level - variable - creation.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 40 41 97.6 %
Date: 2024-04-28 01:25:40 Functions: 8 8 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/core/element/creation.h"
       6             : #include "scipp/core/time_point.h"
       7             : #include "scipp/variable/creation.h"
       8             : #include "scipp/variable/shape.h"
       9             : #include "scipp/variable/transform.h"
      10             : #include "scipp/variable/variable_factory.h"
      11             : 
      12             : namespace scipp::variable {
      13             : 
      14       43437 : Variable empty(const Dimensions &dims, const units::Unit &unit,
      15             :                const DType type, const bool with_variances,
      16             :                const bool aligned) {
      17       43437 :   auto var = variableFactory().create(type, dims, unit, with_variances);
      18       43437 :   var.set_aligned(aligned);
      19       43437 :   return var;
      20             : }
      21             : 
      22       12572 : Variable ones(const Dimensions &dims, const units::Unit &unit, const DType type,
      23             :               const bool with_variances) {
      24       12571 :   const auto make_prototype = [&](auto &&one) {
      25       12571 :     return with_variances
      26       12597 :                ? Variable{type, Dimensions{}, unit, Values{one}, Variances{one}}
      27       37739 :                : Variable{type, Dimensions{}, unit, Values{one}};
      28       12572 :   };
      29       12572 :   if (type == dtype<core::time_point>) {
      30           2 :     return copy(broadcast(make_prototype(core::time_point{1}), dims));
      31       12571 :   } else if (type == dtype<std::string>) {
      32             :     // This would result in a Variable containing (char)1.
      33           1 :     throw std::invalid_argument("Cannot construct 'ones' of strings.");
      34             :   } else {
      35       25140 :     return copy(broadcast(make_prototype(1), dims));
      36             :   }
      37             : }
      38             : 
      39             : /// Create empty (uninitialized) variable with same parameters as prototype.
      40             : ///
      41             : /// If specified, `shape` defines the shape of the output. If `prototype`
      42             : /// contains binned data `shape` may not be specified, instead `sizes` defines
      43             : /// the sizes of the desired bins.
      44      285974 : Variable empty_like(const Variable &prototype,
      45             :                     const std::optional<Dimensions> &shape,
      46             :                     const Variable &sizes) {
      47      285974 :   return variableFactory().empty_like(prototype, shape, sizes);
      48             : }
      49             : 
      50             : /// Create a variable with the same parameters as `prototype` with
      51             : /// values filled according to `fill`.
      52       43290 : Variable special_like(const Variable &prototype, const FillValue &fill) {
      53       43290 :   const char *name = "special_like";
      54       43290 :   if (fill == FillValue::Default)
      55        1555 :     return Variable(prototype, prototype.dims());
      56       41735 :   if (fill == FillValue::ZeroNotBool)
      57       20291 :     return transform(prototype, core::element::zeros_not_bool_like, name);
      58       21444 :   if (fill == FillValue::True)
      59         477 :     return transform(prototype, core::element::values_like<bool, true>, name);
      60       20967 :   if (fill == FillValue::False)
      61         105 :     return transform(prototype, core::element::values_like<bool, false>, name);
      62       20862 :   if (fill == FillValue::Max)
      63       10419 :     return transform(prototype, core::element::numeric_limits_max_like, name);
      64       10443 :   if (fill == FillValue::Lowest)
      65             :     return transform(prototype, core::element::numeric_limits_lowest_like,
      66       10443 :                      name);
      67           0 :   throw std::runtime_error("Unsupported fill value.");
      68             : }
      69             : 
      70             : /// Create a variable with the same parameters as `prototype` with the given
      71             : /// dimensions and values filled according to `fill`.
      72             : /// If `prototype` is binned, `accum` is dense
      73             : /// with the elem dtype of `prototype`.
      74       43080 : Variable dense_special_like(const Variable &prototype,
      75             :                             const Dimensions &target_dims,
      76             :                             const FillValue &fill) {
      77       43080 :   const auto type = variableFactory().elem_dtype(prototype);
      78       43080 :   const auto unit = variableFactory().elem_unit(prototype);
      79       43080 :   const auto has_variances = variableFactory().has_variances(prototype);
      80       43080 :   const auto scalar_prototype = empty(Dimensions{}, unit, type, has_variances);
      81      129240 :   return special_like(scalar_prototype.broadcast(target_dims), fill);
      82       43080 : }
      83             : 
      84             : /// Create scalar variable containing 0 with same parameters as prototype.
      85         233 : Variable zero_like(const Variable &prototype) {
      86         466 :   return {prototype, Dimensions{}};
      87             : }
      88             : 
      89             : } // namespace scipp::variable

Generated by: LCOV version 1.14