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