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-11-17 01:47:58 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       44676 : Variable empty(const Dimensions &dims, const units::Unit &unit,
      15             :                const DType type, const bool with_variances,
      16             :                const bool aligned) {
      17       44676 :   auto var = variableFactory().create(type, dims, unit, with_variances);
      18       44676 :   var.set_aligned(aligned);
      19       44676 :   return var;
      20             : }
      21             : 
      22       12700 : Variable ones(const Dimensions &dims, const units::Unit &unit, const DType type,
      23             :               const bool with_variances) {
      24       12699 :   const auto make_prototype = [&](auto &&one) {
      25       12699 :     return with_variances
      26       12725 :                ? Variable{type, Dimensions{}, unit, Values{one}, Variances{one}}
      27       38123 :                : Variable{type, Dimensions{}, unit, Values{one}};
      28       12700 :   };
      29       12700 :   if (type == dtype<core::time_point>) {
      30           2 :     return copy(broadcast(make_prototype(core::time_point{1}), dims));
      31       12699 :   } 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       25396 :     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      290215 : Variable empty_like(const Variable &prototype,
      45             :                     const std::optional<Dimensions> &shape,
      46             :                     const Variable &sizes) {
      47      290215 :   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       44493 : Variable special_like(const Variable &prototype, const FillValue &fill) {
      53       44493 :   const char *name = "special_like";
      54       44493 :   if (fill == FillValue::Default)
      55        1695 :     return Variable(prototype, prototype.dims());
      56       42798 :   if (fill == FillValue::ZeroNotBool)
      57       20942 :     return transform(prototype, core::element::zeros_not_bool_like, name);
      58       21856 :   if (fill == FillValue::True)
      59         495 :     return transform(prototype, core::element::values_like<bool, true>, name);
      60       21361 :   if (fill == FillValue::False)
      61         106 :     return transform(prototype, core::element::values_like<bool, false>, name);
      62       21255 :   if (fill == FillValue::Max)
      63       10614 :     return transform(prototype, core::element::numeric_limits_max_like, name);
      64       10641 :   if (fill == FillValue::Lowest)
      65             :     return transform(prototype, core::element::numeric_limits_lowest_like,
      66       10641 :                      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       44283 : Variable dense_special_like(const Variable &prototype,
      75             :                             const Dimensions &target_dims,
      76             :                             const FillValue &fill) {
      77       44283 :   const auto type = variableFactory().elem_dtype(prototype);
      78       44283 :   const auto unit = variableFactory().elem_unit(prototype);
      79       44283 :   const auto has_variances = variableFactory().has_variances(prototype);
      80       44283 :   const auto scalar_prototype = empty(Dimensions{}, unit, type, has_variances);
      81      132849 :   return special_like(scalar_prototype.broadcast(target_dims), fill);
      82       44283 : }
      83             : 
      84             : /// Create scalar variable containing 0 with same parameters as prototype.
      85         243 : Variable zero_like(const Variable &prototype) {
      86         486 :   return {prototype, Dimensions{}};
      87             : }
      88             : 
      89             : } // namespace scipp::variable

Generated by: LCOV version 1.14