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

graphia-app / graphia / 41450107117

02 Nov 2026 01:05PM UTC coverage: 71.33% (+0.08%) from 71.252%
41450107117

push

github

timangus
Use Qt 6.12.0 for CI

11708 of 17976 branches covered (65.13%)

Branch coverage included in aggregate %.

29876 of 40322 relevant lines covered (74.09%)

577037.85 hits per line

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

94.44
/source/app/transform/transforms/averageattributetransform.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
#include "averageattributetransform.h"
21

22
#include "app/transform/transformedgraph.h"
23

24
#include "app/graph/graphmodel.h"
25

26
#include <QtGlobal>
27

28
#include <type_traits>
29
#include <cstddef>
30
#include <map>
31

32
static Alert averageAttributeTransformConfigIsValid(const GraphModel& graphModel,
33
    const GraphTransformConfig& config)
34
{
9 ✔
35
    const auto& attributeNames = config.attributeNames();
9 ✔
36
    if(attributeNames.size() != 2)
9 ✔
37
        return {AlertType::Error, QObject::tr("Invalid parameters")};
2 ✔
38

39
    auto firstAttribute = graphModel.attributeExists(attributeNames.at(0)) ?
7 !
40
        graphModel.attributeValueByName(attributeNames.at(0)) : Attribute{};
7 ✔
41
    auto secondAttribute = graphModel.attributeExists(attributeNames.at(1)) ?
7 !
42
        graphModel.attributeValueByName(attributeNames.at(1)) : Attribute{};
7 ✔
43

44
    if(firstAttribute.elementType() != secondAttribute.elementType())
7 ✔
45
    {
2 ✔
46
        return {AlertType::Error,
2 ✔
47
            QObject::tr("Attributes must both be node or edge attributes, not a mixture")};
2 ✔
48
    }
2 ✔
49

50
    return {AlertType::None, {}};
5 ✔
51
}
7 ✔
52

53
QString AverageAttributeTransform::meanAttributeName(const QString& sourceAttributeName,
54
    const QString& sharedValuesAttributeName)
55
{
5 ✔
56
    auto compact = [](const QString& attributeName)
5 ✔
57
    {
10 ✔
58
        auto name = Attribute::prettify(attributeName);
10 ✔
59
        name.removeIf([](QChar c) { return c.isSpace() || c == u'›'; });
61 !
60
        return name;
10 ✔
61
    };
10 ✔
62

63
    return u"Mean%1Of%2"_s.arg(compact(sourceAttributeName), compact(sharedValuesAttributeName));
5 ✔
64
}
5 ✔
65

66
void AverageAttributeTransform::apply(TransformedGraph& target)
67
{
6 ✔
68
    setPhase(QObject::tr("Averaging Attribute"));
6 ✔
69

70
    auto alert = averageAttributeTransformConfigIsValid(*_graphModel, config());
6 ✔
71
    if(alert._type != AlertType::None)
6 ✔
72
    {
2 ✔
73
        addAlert(alert);
2 ✔
74
        return;
2 ✔
75
    }
2 ✔
76

77
    auto sharedValuesAttributeName = config().attributeNames().at(0);
4 ✔
78
    auto sourceAttributeName = config().attributeNames().at(1);
4 ✔
79

80
    auto sharedValuesAttribute = _graphModel->attributeValueByName(sharedValuesAttributeName);
4 ✔
81
    auto sourceAttribute = _graphModel->attributeValueByName(sourceAttributeName);
4 ✔
82

83
    auto sourceAttributeDisplayName = _graphModel->attributeDisplayName(sourceAttributeName);
4 ✔
84
    auto sharedValuesAttributeDisplayName = _graphModel->attributeDisplayName(sharedValuesAttributeName);
4 ✔
85

86
    auto& meanAttribute = _graphModel->createAttribute(meanAttributeName(sourceAttributeName, sharedValuesAttributeName))
4 ✔
87
        .setDisplayName(QObject::tr("Mean %1 of %2")
4 ✔
88
        .arg(sourceAttributeDisplayName, sharedValuesAttributeDisplayName))
4 ✔
89
        .setDescription(QObject::tr("The mean of %1 for each value of %2.")
4 ✔
90
        .arg(sourceAttributeDisplayName, sharedValuesAttributeDisplayName))
4 ✔
91
        .setFlag(AttributeFlag::AutoRange);
4 ✔
92

93
    auto setAverageFunction = [&](const auto& elementIds)
4 ✔
94
    {
4 ✔
95
        using E = typename std::remove_reference<decltype(elementIds)>::type::value_type;
4 ✔
96

97
        struct SharedValue
4 ✔
98
        {
4 ✔
99
            size_t _count = 0;
4 ✔
100
            double _total = 0.0;
4 ✔
101

102
            double mean() const { return _total / static_cast<double>(_count); }
15 ✔
103
        };
4 ✔
104

105
        std::map<QString, SharedValue> values;
4 ✔
106
        ElementIdArray<E, const SharedValue*> elementValues(target);
4 ✔
107

108
        for(auto elementId : elementIds)
4 ✔
109
        {
16 ✔
110
            auto value = sharedValuesAttribute.stringValueOf(elementId);
16 ✔
111

112
            if(!value.isEmpty())
16 !
113
            {
15 ✔
114
                auto* v = &values[value];
15 ✔
115
                v->_count++;
15 ✔
116
                v->_total += sourceAttribute.numericValueOf(elementId);
15 ✔
117
                elementValues[elementId] = v;
15 ✔
118
            }
15 ✔
119
        }
16 ✔
120

121
        ElementIdArray<E, double> averages(target);
4 ✔
122
        ElementIdArray<E, bool> missing(target, false);
4 ✔
123
        bool anyMissing = false;
4 ✔
124

125
        for(auto elementId : elementIds)
4 ✔
126
        {
16 ✔
127
            // Elements with no shared value have no mean
128
            if(elementValues[elementId] == nullptr)
16 !
129
            {
1 ✔
130
                missing.set(elementId, true);
1 ✔
131
                anyMissing = true;
1 ✔
132
                continue;
1 ✔
133
            }
1 ✔
134

135
            averages[elementId] = elementValues[elementId]->mean();
15 ✔
136
        }
15 ✔
137

138
        meanAttribute.setFloatValueFn([averages](E elementId)
4 ✔
139
        {
25 ✔
140
            return averages[elementId];
25 ✔
141
        });
25 ✔
142

143
        if(anyMissing)
4 !
144
        {
1 ✔
145
            meanAttribute.setValueMissingFn([missing](E elementId)
1 ✔
146
            {
1 ✔
147
                return missing.get(elementId);
1 ✔
148
            });
1 ✔
149
        }
1 ✔
150
    };
4 ✔
151

152
    switch(sourceAttribute.elementType())
4 ✔
153
    {
4 ✔
154
    case ElementType::Node: setAverageFunction(target.nodeIds()); break;
3 ✔
155
    case ElementType::Edge: setAverageFunction(target.edgeIds()); break;
1 ✔
156
    default: qFatal("Unhandled ElementType"); break;
×
157
    }
4 ✔
158
}
4 ✔
159

160
bool AverageAttributeTransformFactory::configIsValid(const GraphModel& graphModel, const GraphTransformConfig& graphTransformConfig) const
161
{
3 ✔
162
    return averageAttributeTransformConfigIsValid(graphModel, graphTransformConfig)._type == AlertType::None;
3 ✔
163
}
3 ✔
164

165
std::unique_ptr<GraphTransform> AverageAttributeTransformFactory::create(
166
    GraphModel& graphModel, const GraphTransformConfig&) const
167
{
6 ✔
168
    return std::make_unique<AverageAttributeTransform>(graphModel);
6 ✔
169
}
6 ✔
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