Skip to content

Commit 472b791

Browse files
author
Anton Eriksson
committed
feat: Write node metadata to json
1 parent 1b44bda commit 472b791

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/core/StateNetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class StateNetwork {
176176
std::map<unsigned int, std::string>& names() { return m_names; }
177177
const std::map<unsigned int, std::string>& names() const { return m_names; }
178178

179+
virtual const std::map<unsigned int, std::vector<int>>& metaData() const = 0;
180+
179181
bool haveDirectedInput() const { return m_haveDirectedInput; }
180182
bool haveMemoryInput() const { return m_haveMemoryInput; }
181183
bool higherOrderInputMethodCalled() const { return m_higherOrderInputMethodCalled; }

src/io/Network.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Network : public StateNetwork {
9292
virtual void readMetaData(const std::string& filename);
9393

9494
unsigned int numMetaDataColumns() const { return m_numMetaDataColumns; }
95-
const std::map<unsigned int, std::vector<int>>& metaData() const { return m_metaData; }
95+
const std::map<unsigned int, std::vector<int>>& metaData() const override { return m_metaData; }
9696

9797
bool isMultilayerNetwork() const { return !m_layerNodeToStateId.empty(); }
9898
const std::map<unsigned int, std::map<unsigned int, unsigned int>>& layerNodeToStateId() const { return m_layerNodeToStateId; }

src/io/Output.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ void writeJsonTree(InfomapBase& im, const StateNetwork& network, std::ostream& o
382382

383383
const auto multilevelModules = im.getMultilevelModules(states);
384384

385+
auto metaData = network.metaData();
386+
auto writeMeta = [&metaData](auto& outStream, auto nodeId) {
387+
outStream << "\"metadata\":{";
388+
auto meta = metaData[nodeId];
389+
for (unsigned int i = 0; i < meta.size(); ++i) {
390+
outStream << '"' << i << "\":"
391+
<< '"' << meta[i] << '"'; // metadata class as string to highlight that this is a categorical variable
392+
if (i < meta.size() - 1)
393+
outStream << ',';
394+
}
395+
outStream << "},";
396+
};
397+
385398
// don't append a comma after the last entry
386399
auto first = true;
387400

@@ -435,6 +448,11 @@ void writeJsonTree(InfomapBase& im, const StateNetwork& network, std::ostream& o
435448
<< "\"flow\":" << node.data.flow << ","
436449
<< "\"mec\":" << it.modularCentrality() << ",";
437450

451+
// can't currently use both memory and meta map equation
452+
if (im.haveMetaData() && !states) {
453+
writeMeta(outStream, node.physicalId);
454+
}
455+
438456
if (states) {
439457
outStream << "\"stateId\":" << node.stateId << ",";
440458
if (im.isMultilayerNetwork())

0 commit comments

Comments
 (0)