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

graphia-app / graphia / 42502562509

08 Nov 2026 04:57PM UTC coverage: 71.457% (-0.05%) from 71.51%
42502562509

push

github

timangus
CLAUDE: Suppress cppcheck's duplInheritedMember check and drop NodePositions' second mutex

11725 of 17974 branches covered (65.23%)

Branch coverage included in aggregate %.

29934 of 40325 relevant lines covered (74.23%)

375397.35 hits per line

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

92.58
/source/plugins/correlation/loading/correlationpreprocessing.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
// The static parts of CorrelationFileParser, which transform the data before correlation
21

22
#include "correlationfileparser.h"
23

24
#include "plugins/correlation/featurescaling.h"
25
#include "plugins/correlation/quantilenormaliser.h"
26
#include "plugins/correlation/softmaxnormaliser.h"
27

28
#include "shared/utils/container.h"
29

30
#include <QtGlobal>
31

32
#include <algorithm>
33
#include <cmath>
34
#include <limits>
35
#include <span>
36
#include <vector>
37

38
double CorrelationFileParser::imputeValue(MissingDataType missingDataType,
39
    double replacementValue, const TabularData& tabularData,
40
    const QRect& dataRect, size_t columnIndex, size_t rowIndex)
41
{
24 ✔
42
    double imputedValue = 0.0;
24 ✔
43

44
    auto left = static_cast<size_t>(dataRect.x());
24 ✔
45
    auto right = static_cast<size_t>(dataRect.x()) + static_cast<size_t>(dataRect.width());
24 ✔
46

47
    switch(missingDataType)
24 ✔
48
    {
24 ✔
49
    case MissingDataType::Constant:
4 ✔
50
    {
4 ✔
51
        imputedValue = replacementValue;
4 ✔
52
        break;
4 ✔
53
    }
×
54
    case MissingDataType::ColumnAverage:
6 ✔
55
    {
6 ✔
56
        auto top = static_cast<size_t>(dataRect.y());
6 ✔
57
        auto bottom = static_cast<size_t>(dataRect.y()) + static_cast<size_t>(dataRect.height());
6 ✔
58

59
        // Calculate column averages
60
        double averageValue = 0.0;
6 ✔
61
        size_t rowCount = 0;
6 ✔
62
        for(size_t avgRowIndex = top; avgRowIndex < bottom; avgRowIndex++)
36 ✔
63
        {
30 ✔
64
            const auto& value = tabularData.valueAt(columnIndex, avgRowIndex);
30 ✔
65
            if(!value.isEmpty())
30 ✔
66
            {
20 ✔
67
                averageValue += value.toDouble();
20 ✔
68
                rowCount++;
20 ✔
69
            }
20 ✔
70
        }
30 ✔
71

72
        if(rowCount > 0)
6 !
73
            averageValue /= static_cast<double>(rowCount);
6 ✔
74

75
        imputedValue = averageValue;
6 ✔
76
        break;
6 ✔
77
    }
×
78
    case MissingDataType::RowInterpolation:
14 ✔
79
    {
14 ✔
80
        double rightValue = 0.0;
14 ✔
81
        double leftValue = 0.0;
14 ✔
82
        size_t leftDistance = 0;
14 ✔
83
        size_t rightDistance = 0;
14 ✔
84
        bool rightValueFound = false;
14 ✔
85
        bool leftValueFound = false;
14 ✔
86

87
        // Find right value
88
        for(size_t rightColumn = columnIndex; rightColumn < right; rightColumn++)
34 ✔
89
        {
30 ✔
90
            const auto& value = tabularData.valueAt(rightColumn, rowIndex);
30 ✔
91
            if(!value.isEmpty())
30 ✔
92
            {
10 ✔
93
                rightValue = value.toDouble();
10 ✔
94
                rightValueFound = true;
10 ✔
95
                rightDistance = (rightColumn > columnIndex) ? rightColumn - columnIndex : columnIndex - rightColumn;
10 !
96
                break;
10 ✔
97
            }
10 ✔
98
        }
30 ✔
99
        // Find left value
100
        for(size_t leftColumn = columnIndex; leftColumn-- != left;)
22 ✔
101
        {
18 ✔
102
            const auto& value = tabularData.valueAt(leftColumn, rowIndex);
18 ✔
103
            if(!value.isEmpty())
18 ✔
104
            {
10 ✔
105
                leftValue = value.toDouble();
10 ✔
106
                leftValueFound = true;
10 ✔
107
                leftDistance = (leftColumn > columnIndex) ? leftColumn - columnIndex : columnIndex - leftColumn;
10 !
108
                break;
10 ✔
109
            }
10 ✔
110
        }
18 ✔
111

112
        // Lerp the result if possible, otherwise just set to found value
113
        if(leftValueFound && rightValueFound)
14 ✔
114
        {
8 ✔
115
            const double tween = static_cast<double>(leftDistance) / static_cast<double>(leftDistance + rightDistance);
8 ✔
116
            // https://devblogs.nvidia.com/lerp-faster-cuda/
117
            const double lerpedValue = std::fma(tween, rightValue, std::fma(-tween, leftValue, leftValue));
8 ✔
118
            imputedValue = lerpedValue;
8 ✔
119
        }
8 ✔
120
        else if(leftValueFound && !rightValueFound)
6 !
121
            imputedValue = leftValue;
2 ✔
122
        else if(!leftValueFound && rightValueFound)
4 !
123
            imputedValue = rightValue;
2 ✔
124
        else // Nothing on the row, just zero it
2 ✔
125
            imputedValue = 0.0;
2 ✔
126
        break;
14 ✔
127
    }
×
128
    default:
×
129
        break;
×
130
    }
24 ✔
131

132
    return imputedValue;
24 ✔
133
}
24 ✔
134

135
void CorrelationFileParser::clipValues(ClippingType clippingType, double clippingValue,
136
    size_t width, std::vector<double>& data)
137
{
324 ✔
138
    Q_ASSERT(data.size() % width == 0);
324 !
139

140
    switch(clippingType)
324 ✔
141
    {
324 ✔
142
    case ClippingType::Constant:
4 ✔
143
        std::ranges::replace_if(data, [clippingValue](double value) { return value > clippingValue; }, clippingValue);
80 ✔
144
        break;
4 ✔
145

146
    case ClippingType::Winsorization:
6 ✔
147
    {
6 ✔
148
        auto it = data.begin();
6 ✔
149
        while(it != data.end())
24 ✔
150
        {
18 ✔
151
            using dt = std::vector<double>::difference_type;
18 ✔
152

153
            auto sortedIndices = u::sortedIndicesOf(std::span(it, width));
18 ✔
154
            auto metaIndex = static_cast<size_t>((clippingValue * static_cast<double>(width - 1)) / 100.0);
18 ✔
155
            auto clipIndex = sortedIndices.at(metaIndex);
18 ✔
156
            auto clipValue = *(it + static_cast<dt>(clipIndex));
18 ✔
157

158
            auto begin = sortedIndices.begin() + static_cast<dt>(metaIndex + 1);
18 ✔
159
            for(auto i = begin; i != sortedIndices.end(); i++)
32 ✔
160
                *(it + static_cast<dt>(*i)) = clipValue;
14 ✔
161

162
            it += static_cast<dt>(width);
18 ✔
163
        }
18 ✔
164

165
        break;
6 ✔
166
    }
×
167

168
    default:
314 ✔
169
        break;
314 ✔
170
    }
324 ✔
171
}
324 ✔
172

173
double CorrelationFileParser::scaleValue(ScalingType scalingType, double value, double epsilon)
174
{
18,610 ✔
175
    switch(scalingType)
18,610 ✔
176
    {
18,610 ✔
177
    case ScalingType::Log2:
66 ✔
178
        return std::log2(value + epsilon);
66 ✔
179
    case ScalingType::Log10:
62 ✔
180
        return std::log10(value + epsilon);
62 ✔
181
    case ScalingType::AntiLog2:
62 ✔
182
        return std::pow(2.0, value);
62 ✔
183
    case ScalingType::AntiLog10:
62 ✔
184
        return std::pow(10.0, value);
62 ✔
185
    case ScalingType::ArcSin:
2 ✔
186
        return std::asin(value);
2 ✔
187
    default:
18,356 ✔
188
        break;
18,356 ✔
189
    }
18,610 ✔
190
    return value;
18,356 ✔
191
}
18,610 ✔
192

193
bool CorrelationFileParser::normalise(NormaliseType normaliseType,
194
    ContinuousDataVectors& dataRows, IParser* parser)
195
{
46 ✔
196
    bool success = true;
46 ✔
197

198
    switch(normaliseType)
46 ✔
199
    {
46 ✔
200
    case NormaliseType::MinMax:
6 ✔
201
    {
6 ✔
202
        const MinMaxNormaliser normaliser;
6 ✔
203
        success = normaliser.process(dataRows, parser);
6 ✔
204
        break;
6 ✔
205
    }
×
206
    case NormaliseType::Mean:
6 ✔
207
    {
6 ✔
208
        const MeanNormaliser normaliser;
6 ✔
209
        success = normaliser.process(dataRows, parser);
6 ✔
210
        break;
6 ✔
211
    }
×
212
    case NormaliseType::Standardisation:
4 ✔
213
    {
4 ✔
214
        const StandardisationNormaliser normaliser;
4 ✔
215
        success = normaliser.process(dataRows, parser);
4 ✔
216
        break;
4 ✔
217
    }
×
218
    case NormaliseType::UnitScaling:
4 ✔
219
    {
4 ✔
220
        const UnitScalingNormaliser normaliser;
4 ✔
221
        success = normaliser.process(dataRows, parser);
4 ✔
222
        break;
4 ✔
223
    }
×
224
    case NormaliseType::Quantile:
8 ✔
225
    {
8 ✔
226
        const QuantileNormaliser normaliser;
8 ✔
227
        success = normaliser.process(dataRows, parser);
8 ✔
228
        break;
8 ✔
229
    }
×
230
    case NormaliseType::Softmax:
6 ✔
231
    {
6 ✔
232
        const SoftmaxNormaliser normaliser;
6 ✔
233
        success = normaliser.process(dataRows, parser);
6 ✔
234
        break;
6 ✔
235
    }
×
236
    default:
12 ✔
237
        break;
12 ✔
238
    }
46 ✔
239

240
    if(!success)
46 !
241
        return false;
×
242

243
    if(normaliseType != NormaliseType::None)
46 ✔
244
    {
34 ✔
245
        for(auto& dataRow : dataRows)
34 ✔
246
            dataRow.update();
96 ✔
247
    }
34 ✔
248

249
    return true;
46 ✔
250
}
46 ✔
251

252
double CorrelationFileParser::epsilonFor(const std::vector<double>& data)
253
{
388 ✔
254
    double minValue = std::numeric_limits<double>::max();
388 ✔
255
    bool anyPositive = false;
388 ✔
256
    for(const auto& value : data)
388 ✔
257
    {
20,394 ✔
258
        if(value > 0.0 && value < minValue)
20,394 ✔
259
        {
366 ✔
260
            minValue = value;
366 ✔
261
            anyPositive = true;
366 ✔
262
        }
366 ✔
263
    }
20,394 ✔
264

265
    if(!anyPositive)
388 ✔
266
        return std::nextafter(0.0, 1.0);
24 ✔
267

268
    // This is picked to be appropriately small for log scaling
269
    // data, but not so small that it obscures the values themselves
270
    return minValue * 0.5;
364 ✔
271
}
388 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc