Skip to content

Commit 31cdd7f

Browse files
author
Anton Eriksson
committed
feat: Add modular centrality score
1 parent 25d3057 commit 31cdd7f

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

interfaces/js/src/filetypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface TreeNode {
1313
path: number[];
1414
modules?: number[];
1515
flow?: number;
16+
modularCentrality?: number;
1617
name?: string;
1718
id: number;
1819
}

interfaces/swig/InfomapIterator.i

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ namespace std {
7575
"""
7676
return self._depth()
7777

78+
@property
79+
def modular_centrality(self):
80+
"""Get the modular centrality of the node.
81+
82+
Returns
83+
-------
84+
float
85+
The modular centrality
86+
"""
87+
return self.modularCentrality()
88+
7889
@property
7990
def child_index(self):
8091
"""Get the child index.
@@ -151,6 +162,17 @@ namespace std {
151162
"""
152163
return self._depth()
153164

165+
@property
166+
def modular_centrality(self):
167+
"""Get the modular centrality of the node.
168+
169+
Returns
170+
-------
171+
float
172+
The modular centrality
173+
"""
174+
return self.modularCentrality()
175+
154176
@property
155177
def child_index(self):
156178
"""Get the child index.
@@ -228,6 +250,17 @@ namespace std {
228250
"""
229251
return self._depth()
230252

253+
@property
254+
def modular_centrality(self):
255+
"""Get the modular centrality of the node.
256+
257+
Returns
258+
-------
259+
float
260+
The modular centrality
261+
"""
262+
return self.modularCentrality()
263+
231264
@property
232265
def child_index(self):
233266
"""Get the child index.
@@ -305,6 +338,17 @@ namespace std {
305338
"""
306339
return self._depth()
307340

341+
@property
342+
def modular_centrality(self):
343+
"""Get the modular centrality of the node.
344+
345+
Returns
346+
-------
347+
float
348+
The modular centrality
349+
"""
350+
return self.modularCentrality()
351+
308352
@property
309353
def child_index(self):
310354
"""Get the child index.
@@ -382,6 +426,17 @@ namespace std {
382426
"""
383427
return self._depth()
384428

429+
@property
430+
def modular_centrality(self):
431+
"""Get the modular centrality of the node.
432+
433+
Returns
434+
-------
435+
float
436+
The modular centrality
437+
"""
438+
return self.modularCentrality()
439+
385440
@property
386441
def child_index(self):
387442
"""Get the child index.

src/core/InfomapBase.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,7 @@ void InfomapBase::writeJsonTree(std::ostream& outStream, bool states, bool write
26292629
<< "\"modules\": [" << modules << "], "
26302630
<< "\"name\": \"" << nodeName(node) << "\", "
26312631
<< "\"flow\": " << node.data.flow << ", "
2632+
<< "\"modularCentrality\": " << it.modularCentrality() << ", "
26322633
<< "\"id\": " << node.physicalId << " }";
26332634
}
26342635
}
@@ -2653,7 +2654,8 @@ void InfomapBase::writeJsonTree(std::ostream& outStream, bool states, bool write
26532654
<< "\"path\": [" << path << "], "
26542655
<< "\"modules\": [" << modules << "], "
26552656
<< "\"name\": \"" << nodeName(node) << "\", "
2656-
<< "\"flow\": " << node.data.flow << ", ";
2657+
<< "\"flow\": " << node.data.flow << ", "
2658+
<< "\"modularCentrality\": " << it.modularCentrality() << ", ";
26572659

26582660
if (states) {
26592661
outStream << "\"stateId\": " << node.stateId << ", ";

src/core/InfomapIterator.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <deque>
1010
#include <map>
11+
#include <cmath>
1112
#include "InfoNode.h"
1213

1314
namespace infomap {
@@ -141,6 +142,24 @@ struct InfomapIterator {
141142
return m_depth;
142143
}
143144

145+
double modularCentrality() const
146+
{
147+
if (m_current->parent == nullptr) {
148+
// The root node has no modular centrality
149+
return 0.0;
150+
}
151+
152+
const auto p_m = m_current->parent->data.flow;
153+
const auto p_u = m_current->data.flow;
154+
const auto p_diff = p_m - p_u;
155+
156+
if (p_diff > 0.0) {
157+
return -p_diff * std::log2(p_diff / p_m);
158+
}
159+
160+
return 0.0;
161+
}
162+
144163
bool isEnd() const
145164
{
146165
return m_current == nullptr;
@@ -175,6 +194,7 @@ struct InfomapModuleIterator : public InfomapIterator {
175194
using InfomapIterator::childIndex;
176195
using InfomapIterator::current;
177196
using InfomapIterator::depth;
197+
using InfomapIterator::modularCentrality;
178198
using InfomapIterator::path;
179199
};
180200

@@ -217,6 +237,7 @@ struct InfomapLeafModuleIterator : public InfomapIterator {
217237
using InfomapIterator::childIndex;
218238
using InfomapIterator::current;
219239
using InfomapIterator::depth;
240+
using InfomapIterator::modularCentrality;
220241
using InfomapIterator::path;
221242
};
222243

@@ -259,6 +280,7 @@ struct InfomapLeafIterator : public InfomapIterator {
259280
using InfomapIterator::childIndex;
260281
using InfomapIterator::current;
261282
using InfomapIterator::depth;
283+
using InfomapIterator::modularCentrality;
262284
using InfomapIterator::path;
263285
};
264286

@@ -315,6 +337,7 @@ struct InfomapIteratorPhysical : public InfomapIterator {
315337
using InfomapIterator::childIndex;
316338
using InfomapIterator::current;
317339
using InfomapIterator::depth;
340+
using InfomapIterator::modularCentrality;
318341
using InfomapIterator::path;
319342
};
320343

@@ -363,6 +386,7 @@ struct InfomapLeafIteratorPhysical : public InfomapIteratorPhysical {
363386
using InfomapIteratorPhysical::childIndex;
364387
using InfomapIteratorPhysical::current;
365388
using InfomapIteratorPhysical::depth;
389+
using InfomapIteratorPhysical::modularCentrality;
366390
using InfomapIteratorPhysical::path;
367391
};
368392

0 commit comments

Comments
 (0)