LCOV - code coverage report
Current view: top level - python - variable_creation.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 36 100.0 %
Date: 2024-04-28 01:25:40 Functions: 13 13 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/eigen.h"
       6             : #include "scipp/core/tag_util.h"
       7             : #include "scipp/variable/creation.h"
       8             : 
       9             : #include "dim.h"
      10             : #include "dtype.h"
      11             : #include "pybind11.h"
      12             : #include "unit.h"
      13             : 
      14             : using namespace scipp;
      15             : 
      16             : namespace py = pybind11;
      17             : 
      18             : template <class T> struct MakeZeros {
      19          90 :   static variable::Variable apply(const std::vector<std::string> &dims,
      20             :                                   const std::vector<scipp::index> &shape,
      21             :                                   const units::Unit &unit,
      22             :                                   const bool with_variances) {
      23             :     return with_variances
      24           3 :                ? makeVariable<T>(make_dims(dims, shape), unit, Values{},
      25           3 :                                  Variances{})
      26          93 :                : makeVariable<T>(make_dims(dims, shape), unit, Values{});
      27             :   }
      28             : };
      29             : 
      30           3 : void init_creation(py::module &m) {
      31           3 :   m.def(
      32             :       "empty",
      33         348 :       [](const std::vector<std::string> &dims,
      34             :          const std::vector<scipp::index> &shape, const ProtoUnit &unit,
      35             :          const py::object &dtype, const bool with_variances,
      36             :          const bool aligned) {
      37         348 :         const auto dtype_ = scipp_dtype(dtype);
      38         348 :         py::gil_scoped_release release;
      39         348 :         const auto unit_ = unit_or_default(unit, dtype_);
      40         696 :         return variable::empty(make_dims(dims, shape), unit_, dtype_,
      41        1044 :                                with_variances, aligned);
      42         348 :       },
      43           6 :       py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
      44           6 :       py::arg("dtype") = py::none(), py::arg("with_variances") = false,
      45           6 :       py::arg("aligned") = true);
      46           3 :   m.def(
      47             :       "zeros",
      48          90 :       [](const std::vector<std::string> &dims,
      49             :          const std::vector<scipp::index> &shape, const ProtoUnit &unit,
      50             :          const py::object &dtype, const bool with_variances) {
      51          90 :         const auto dtype_ = scipp_dtype(dtype);
      52          90 :         py::gil_scoped_release release;
      53          90 :         const auto unit_ = unit_or_default(unit, dtype_);
      54             :         return core::CallDType<
      55             :             double, float, int64_t, int32_t, bool, scipp::core::time_point,
      56             :             std::string, Eigen::Vector3d,
      57             :             Eigen::Matrix3d>::apply<MakeZeros>(dtype_, dims, shape, unit_,
      58         180 :                                                with_variances);
      59          90 :       },
      60           6 :       py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
      61           6 :       py::arg("dtype") = py::none(), py::arg("with_variances") = std::nullopt);
      62           3 :   m.def(
      63             :       "ones",
      64        1801 :       [](const std::vector<std::string> &dims,
      65             :          const std::vector<scipp::index> &shape, const ProtoUnit &unit,
      66             :          const py::object &dtype, const bool with_variances) {
      67        1801 :         const auto dtype_ = scipp_dtype(dtype);
      68        1801 :         py::gil_scoped_release release;
      69        1801 :         const auto unit_ = unit_or_default(unit, dtype_);
      70        3603 :         return variable::ones(make_dims(dims, shape), unit_, dtype_,
      71        5402 :                               with_variances);
      72        1801 :       },
      73           6 :       py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
      74           6 :       py::arg("dtype") = py::none(), py::arg("with_variances") = std::nullopt);
      75           3 : }

Generated by: LCOV version 1.14