• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paulmthompson / WhiskerToolbox / 17270491352

27 Aug 2025 02:57PM UTC coverage: 65.333%. Remained the same
17270491352

push

github

paulmthompson
Merge branch 'main' of https://github.com/paulmthompson/WhiskerToolbox

352 of 628 new or added lines in 92 files covered. (56.05%)

357 existing lines in 24 files now uncovered.

26429 of 40453 relevant lines covered (65.33%)

1119.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.16
/src/DataManager/utils/TableView/computers/IntervalReductionComputer.cpp
1
#include "IntervalReductionComputer.h"
2

3
#include "utils/TableView/interfaces/IAnalogSource.h"
4

5
#include <algorithm>
6
#include <cmath>
7
#include <limits>
8
#include <numeric>
9
#include <stdexcept>
10

11
IntervalReductionComputer::IntervalReductionComputer(std::shared_ptr<IAnalogSource> source, 
42✔
12
                                                   ReductionType reduction)
42✔
13
    : IColumnComputer()
14
    , m_source(std::move(source))
42✔
15
    , m_reduction(reduction)
42✔
16
    , m_sourceName(m_source ? m_source->getName() : "null_source")
86✔
17
{
18
    if (!m_source) {
42✔
19
        throw std::invalid_argument("IAnalogSource cannot be null");
1✔
20
    }
21
}
44✔
22

23
IntervalReductionComputer::IntervalReductionComputer(std::shared_ptr<IAnalogSource> source, 
1✔
24
                                                   ReductionType reduction,
25
                                                   std::string sourceName)
1✔
26
    : IColumnComputer()
27
    , m_source(std::move(source))
1✔
28
    , m_reduction(reduction)
1✔
29
    , m_sourceName(std::move(sourceName))
2✔
30
{
31
    if (!m_source) {
1✔
32
        throw std::invalid_argument("IAnalogSource cannot be null");
×
33
    }
34
}
1✔
35

36
std::vector<double> IntervalReductionComputer::compute(const ExecutionPlan& plan) const {
40✔
37
    if (!plan.hasIntervals()) {
40✔
38
        throw std::invalid_argument("ExecutionPlan must contain intervals for IntervalReductionComputer");
1✔
39
    }
40

41
    // Get the list of intervals from the execution plan
42
    // This should also return the TimeFrame the intervals belong to
43
    // They way they can be converted.
44
    auto const & intervals = plan.getIntervals();
39✔
45
    auto destinationTimeFrame = plan.getTimeFrame();
39✔
46

47
    std::vector<double> results;
39✔
48
    results.reserve(intervals.size());
39✔
49

50
    for (auto const & interval: intervals) {
143✔
51
            
52
        auto sliceView = m_source->getDataInRange(
104✔
53
            interval.start, 
54
            interval.end, 
55
            destinationTimeFrame.get()
104✔
56
        );
104✔
57

58
         const float result = computeReduction(sliceView);
104✔
59

60
        results.emplace_back(result);
104✔
61
    }
104✔
62

63
    return results;
39✔
64

65
}
39✔
66

67
std::string IntervalReductionComputer::getSourceDependency() const {
88✔
68
    return m_sourceName;
88✔
69
}
70

71
float IntervalReductionComputer::computeReduction(std::span<const float> data) const {
104✔
72
    if (data.empty()) {
104✔
73
        return std::numeric_limits<float>::quiet_NaN();
×
74
    }
75

76
    switch (m_reduction) {
104✔
77
        case ReductionType::Mean:
48✔
78
            return computeMean(data);
48✔
79
        case ReductionType::Max:
17✔
80
            return computeMax(data);
17✔
81
        case ReductionType::Min:
10✔
82
            return computeMin(data);
10✔
83
        case ReductionType::StdDev:
11✔
84
            return computeStdDev(data);
11✔
85
        case ReductionType::Sum:
11✔
86
            return computeSum(data);
11✔
87
        case ReductionType::Count:
7✔
88
            return computeCount(data);
7✔
89
        default:
×
90
            throw std::invalid_argument("Unknown reduction type");
×
91
    }
92
}
93

94
float IntervalReductionComputer::computeMean(std::span<const float> data) const {
58✔
95
    if (data.empty()) {
58✔
96
        return std::numeric_limits<float>::quiet_NaN();
×
97
    }
98

99
    const float sum = std::accumulate(data.begin(), data.end(), 0.0f);
58✔
100
    return sum / static_cast<float>(data.size());
58✔
101
}
102

103
float IntervalReductionComputer::computeMax(std::span<const float> data) const {
17✔
104
    if (data.empty()) {
17✔
105
        return std::numeric_limits<float>::quiet_NaN();
×
106
    }
107

108
    return *std::max_element(data.begin(), data.end());
17✔
109
}
110

111
float IntervalReductionComputer::computeMin(std::span<const float> data) const {
10✔
112
    if (data.empty()) {
10✔
113
        return std::numeric_limits<float>::quiet_NaN();
×
114
    }
115

116
    return *std::min_element(data.begin(), data.end());
10✔
117
}
118

119
float IntervalReductionComputer::computeStdDev(std::span<const float> data) const {
11✔
120
    if (data.empty()) {
11✔
121
        return std::numeric_limits<float>::quiet_NaN();
×
122
    }
123

124
    if (data.size() == 1) {
11✔
125
        return 0.0;
1✔
126
    }
127

128
    const float mean = computeMean(data);
10✔
129
    float variance = 0.0;
10✔
130

131
    for (const float value : data) {
82✔
132
        const float diff = value - mean;
72✔
133
        variance += diff * diff;
72✔
134
    }
135

136
    variance /= static_cast<float>(data.size() - 1); // Sample standard deviation
10✔
137
    return std::sqrt(variance);
10✔
138
}
139

140
float IntervalReductionComputer::computeSum(std::span<const float> data) const {
11✔
141
    if (data.empty()) {
11✔
NEW
142
        return 0.0f;
×
143
    }
144

145
    return std::accumulate(data.begin(), data.end(), 0.0f);
11✔
146
}
147

148
float IntervalReductionComputer::computeCount(std::span<const float> data) const {
7✔
149
    return static_cast<float>(data.size());
7✔
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc