Commit 55fdf9c8 authored by Alp Mestanogullari's avatar Alp Mestanogullari

initial commit

parent c693cc39
# Revision history for hsinfomap
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.
packages: .
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef INFOMAP_H_
#define INFOMAP_H_
#include "core/InfomapBase.h"
#include "io/Config.h"
#include <string>
#include <utility>
#include <map>
namespace infomap {
// Wrapper class for the Python API
struct InfomapWrapper : public InfomapBase {
public:
InfomapWrapper() : InfomapBase() { }
InfomapWrapper(const std::string& flags) : InfomapBase(flags) { }
InfomapWrapper(const Config& conf) : InfomapBase(conf) { }
virtual ~InfomapWrapper() = default;
// ===================================================
// Wrapper methods
// ===================================================
void readInputData(std::string filename = "", bool accumulate = true) { m_network.readInputData(std::move(filename), accumulate); }
void addNode(unsigned int id) { m_network.addNode(id); }
void addNode(unsigned int id, std::string name) { m_network.addNode(id, std::move(name)); }
void addNode(unsigned int id, double weight) { m_network.addNode(id, weight); }
void addNode(unsigned int id, std::string name, double weight) { m_network.addNode(id, std::move(name), weight); }
void addName(unsigned int id, const std::string& name) { m_network.addName(id, name); }
std::string getName(unsigned int id) const
{
auto& names = m_network.names();
auto it = names.find(id);
return it != names.end() ? it->second : "";
}
const std::map<unsigned int, std::string>& getNames() const { return m_network.names(); }
void addPhysicalNode(unsigned int id, const std::string& name = "") { m_network.addPhysicalNode(id, name); }
void addStateNode(unsigned int id, unsigned int physId) { m_network.addStateNode(id, physId); }
void addLink(unsigned int sourceId, unsigned int targetId, double weight = 1.0) { m_network.addLink(sourceId, targetId, weight); }
void addLink(unsigned int sourceId, unsigned int targetId, unsigned long weight) { m_network.addLink(sourceId, targetId, weight); }
void addMultilayerLink(unsigned int layer1, unsigned int n1, unsigned int layer2, unsigned int n2, double weight = 1.0) { m_network.addMultilayerLink(layer1, n1, layer2, n2, weight); }
void addMultilayerIntraLink(unsigned int layer, unsigned int n1, unsigned int n2, double weight) { m_network.addMultilayerIntraLink(layer, n1, n2, weight); }
void addMultilayerInterLink(unsigned int layer1, unsigned int n, unsigned int layer2, double interWeight) { m_network.addMultilayerInterLink(layer1, n, layer2, interWeight); }
void setBipartiteStartId(unsigned int startId) { m_network.setBipartiteStartId(startId); }
std::map<std::pair<unsigned int, unsigned int>, double> getLinks(bool flow) const
{
std::map<std::pair<unsigned int, unsigned int>, double> links;
for (const auto& node : m_network.nodeLinkMap()) {
const auto sourceId = node.first.id;
for (const auto& link : node.second) {
const auto targetId = link.first.id;
links[{ sourceId, targetId }] = flow ? link.second.flow : link.second.weight;
}
}
return links;
}
std::map<unsigned int, unsigned int> getModules(int level = 1, bool states = false)
{
if (haveMemory() && !states) {
throw std::runtime_error("Cannot get modules on higher-order network without states.");
}
std::map<unsigned int, unsigned int> modules;
for (auto it = iterTree(level); !it.isEnd(); ++it) {
auto& node = *it;
if (node.isLeaf()) {
modules[states ? node.stateId : node.physicalId] = it.moduleId();
}
}
return modules;
}
using InfomapBase::codelength;
using InfomapBase::getMultilevelModules;
using InfomapBase::iterLeafNodes;
using InfomapBase::iterTree;
using InfomapBase::run;
};
} // namespace infomap
#endif // INFOMAP_H_
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#include "BiasedMapEquation.h"
#include "FlowData.h"
#include "InfoNode.h"
#include <vector>
#include <utility>
#include <cstdlib>
#include "StateNetwork.h"
namespace infomap {
double BiasedMapEquation::s_totalDegree = 1;
unsigned int BiasedMapEquation::s_numNodes = 0;
void BiasedMapEquation::setNetworkProperties(const StateNetwork& network)
{
s_totalDegree = network.sumWeightedDegree();
// Negative entropy bias is based on discrete counts, if average weight is below 1, use unweighted total degree
if (s_totalDegree < network.sumDegree()) {
s_totalDegree = network.sumDegree();
}
s_numNodes = network.numNodes();
}
double BiasedMapEquation::getIndexCodelength() const
{
return indexCodelength + indexEntropyBiasCorrection;
}
double BiasedMapEquation::getModuleCodelength() const
{
return moduleCodelength + biasedCost + moduleEntropyBiasCorrection;
}
double BiasedMapEquation::getCodelength() const
{
return codelength + biasedCost + getEntropyBiasCorrection();
}
double BiasedMapEquation::getEntropyBiasCorrection() const
{
return indexEntropyBiasCorrection + moduleEntropyBiasCorrection;
}
// ===================================================
// IO
// ===================================================
std::ostream& BiasedMapEquation::print(std::ostream& out) const
{
out << indexCodelength << " + " << moduleCodelength;
if (preferredNumModules != 0) {
out << " + " << biasedCost;
}
if (useEntropyBiasCorrection) {
out << " + " << getEntropyBiasCorrection();
}
out << " = " << io::toPrecision(getCodelength());
return out;
}
std::ostream& operator<<(std::ostream& out, const BiasedMapEquation& mapEq)
{
return mapEq.print(out);
}
// ===================================================
// Init
// ===================================================
void BiasedMapEquation::init(const Config& config)
{
Log(3) << "BiasedMapEquation::init()...\n";
preferredNumModules = config.preferredNumberOfModules;
useEntropyBiasCorrection = config.entropyBiasCorrection;
entropyBiasCorrectionMultiplier = config.entropyBiasCorrectionMultiplier;
}
void BiasedMapEquation::initNetwork(InfoNode& root)
{
Log(3) << "BiasedMapEquation::initNetwork()...\n";
Base::initNetwork(root);
}
void BiasedMapEquation::initPartition(std::vector<InfoNode*>& nodes)
{
calculateCodelength(nodes);
}
// ===================================================
// Codelength
// ===================================================
double BiasedMapEquation::calcNumModuleCost(unsigned int numModules) const
{
if (preferredNumModules == 0) return 0;
int deltaNumModules = numModules - preferredNumModules;
return 1 * std::abs(deltaNumModules);
}
double BiasedMapEquation::calcIndexEntropyBiasCorrection(unsigned int numModules) const
{
return useEntropyBiasCorrection ? correctionCoefficient() * numModules / s_totalDegree : 0;
}
double BiasedMapEquation::calcModuleEntropyBiasCorrection(unsigned int numModules) const
{
return useEntropyBiasCorrection ? correctionCoefficient() * (numModules + s_numNodes) / s_totalDegree : 0;
}
double BiasedMapEquation::calcEntropyBiasCorrection(unsigned int numModules) const
{
return useEntropyBiasCorrection ? correctionCoefficient() * (2 * numModules + s_numNodes) / s_totalDegree : 0;
}
void BiasedMapEquation::calculateCodelength(std::vector<InfoNode*>& nodes)
{
calculateCodelengthTerms(nodes);
calculateCodelengthFromCodelengthTerms();
currentNumModules = nodes.size();
biasedCost = calcNumModuleCost(currentNumModules);
indexEntropyBiasCorrection = calcIndexEntropyBiasCorrection(currentNumModules);
moduleEntropyBiasCorrection = calcModuleEntropyBiasCorrection(currentNumModules);
}
double BiasedMapEquation::calcCodelength(const InfoNode& parent) const
{
return parent.isLeafModule()
? calcCodelengthOnModuleOfLeafNodes(parent)
: calcCodelengthOnModuleOfModules(parent);
}
double BiasedMapEquation::calcCodelengthOnModuleOfModules(const InfoNode& parent) const
{
double L = Base::calcCodelengthOnModuleOfModules(parent);
if (!useEntropyBiasCorrection)
return L;
return L + correctionCoefficient() * (1 + parent.childDegree()) / s_totalDegree;
}
double BiasedMapEquation::calcCodelengthOnModuleOfLeafNodes(const InfoNode& parent) const
{
double L = Base::calcCodelength(parent);
if (!useEntropyBiasCorrection)
return L;
return L + correctionCoefficient() * (1 + parent.childDegree()) / s_totalDegree;
}
int BiasedMapEquation::getDeltaNumModulesIfMoving(unsigned int oldModule,
unsigned int newModule,
std::vector<unsigned int>& moduleMembers)
{
bool removeOld = moduleMembers[oldModule] == 1;
bool createNew = moduleMembers[newModule] == 0;
int deltaNumModules = removeOld && !createNew ? -1 : (!removeOld && createNew ? 1 : 0);
return deltaNumModules;
}
double BiasedMapEquation::getDeltaCodelengthOnMovingNode(InfoNode& current,
DeltaFlow& oldModuleDelta,
DeltaFlow& newModuleDelta,
std::vector<FlowData>& moduleFlowData,
std::vector<unsigned int>& moduleMembers)
{
double deltaL = Base::getDeltaCodelengthOnMovingNode(current, oldModuleDelta, newModuleDelta, moduleFlowData, moduleMembers);
if (preferredNumModules == 0)
return deltaL;
int deltaNumModules = getDeltaNumModulesIfMoving(oldModuleDelta.module, newModuleDelta.module, moduleMembers);
double deltaBiasedCost = calcNumModuleCost(currentNumModules + deltaNumModules) - biasedCost;
double deltaEntropyBiasCorrection = calcEntropyBiasCorrection(currentNumModules + deltaNumModules) - getEntropyBiasCorrection();
return deltaL + deltaBiasedCost + deltaEntropyBiasCorrection;
}
// ===================================================
// Consolidation
// ===================================================
void BiasedMapEquation::updateCodelengthOnMovingNode(InfoNode& current,
DeltaFlow& oldModuleDelta,
DeltaFlow& newModuleDelta,
std::vector<FlowData>& moduleFlowData,
std::vector<unsigned int>& moduleMembers)
{
Base::updateCodelengthOnMovingNode(current, oldModuleDelta, newModuleDelta, moduleFlowData, moduleMembers);
if (preferredNumModules == 0)
return;
int deltaNumModules = getDeltaNumModulesIfMoving(oldModuleDelta.module, newModuleDelta.module, moduleMembers);
currentNumModules += deltaNumModules;
biasedCost = calcNumModuleCost(currentNumModules);
indexEntropyBiasCorrection = calcIndexEntropyBiasCorrection(currentNumModules);
moduleEntropyBiasCorrection = calcModuleEntropyBiasCorrection(currentNumModules);
}
void BiasedMapEquation::consolidateModules(std::vector<InfoNode*>& modules)
{
unsigned int numModules = 0;
for (auto& module : modules) {
if (module == nullptr)
continue;
++numModules;
}
currentNumModules = numModules;
}
// ===================================================
// Debug
// ===================================================
void BiasedMapEquation::printDebug() const
{
std::cout << "BiasedMapEquation\n";
Base::printDebug();
}
} // namespace infomap
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef BIASED_MAPEQUATION_H_
#define BIASED_MAPEQUATION_H_
#include "MapEquation.h"
#include "FlowData.h"
#include "../utils/Log.h"
#include <vector>
#include <set>
#include <map>
#include <utility>
namespace infomap {
class InfoNode;
class StateNetwork;
class BiasedMapEquation : private MapEquation<> {
using Base = MapEquation<>;
public:
using FlowDataType = FlowData;
using DeltaFlowDataType = DeltaFlow;
// ===================================================
// Getters
// ===================================================
double getIndexCodelength() const override;
double getModuleCodelength() const override;
double getCodelength() const override;
double getEntropyBiasCorrection() const;
// ===================================================
// IO
// ===================================================
std::ostream& print(std::ostream& out) const override;
friend std::ostream& operator<<(std::ostream&, const BiasedMapEquation&);
// ===================================================
// Init
// ===================================================
void init(const Config& config) override;
void initTree(InfoNode& /*root*/) override { }
void initNetwork(InfoNode& root) override;
using Base::initSuperNetwork;
using Base::initSubNetwork;
void initPartition(std::vector<InfoNode*>& nodes) override;
// ===================================================
// Codelength
// ===================================================
double calcCodelength(const InfoNode& parent) const override;
using Base::addMemoryContributions;
double getDeltaCodelengthOnMovingNode(InfoNode& current,
DeltaFlow& oldModuleDelta,
DeltaFlow& newModuleDelta,
std::vector<FlowData>& moduleFlowData,
std::vector<unsigned int>& moduleMembers) override;
// ===================================================
// Consolidation
// ===================================================
void updateCodelengthOnMovingNode(InfoNode& current,
DeltaFlow& oldModuleDelta,
DeltaFlow& newModuleDelta,
std::vector<FlowData>& moduleFlowData,
std::vector<unsigned int>& moduleMembers) override;
void consolidateModules(std::vector<InfoNode*>& modules) override;
// ===================================================
// Debug
// ===================================================
void printDebug() const override;
private:
// ===================================================
// Private member functions
// ===================================================
double calcCodelengthOnModuleOfLeafNodes(const InfoNode& parent) const override;
double calcCodelengthOnModuleOfModules(const InfoNode& parent) const override;
double correctionCoefficient() const { return gamma * entropyBiasCorrectionMultiplier; }
static int getDeltaNumModulesIfMoving(unsigned int oldModule, unsigned int newModule, std::vector<unsigned int>& moduleMembers);
// ===================================================
// Init
// ===================================================
// ===================================================
// Codelength
// ===================================================
void calculateCodelength(std::vector<InfoNode*>& nodes) override;
using Base::calculateCodelengthTerms;
using Base::calculateCodelengthFromCodelengthTerms;
double calcNumModuleCost(unsigned int numModules) const;
double calcIndexEntropyBiasCorrection(unsigned int numModules) const;
double calcModuleEntropyBiasCorrection(unsigned int numModules) const;
double calcEntropyBiasCorrection(unsigned int numModules) const;
// ===================================================
// Consolidation
// ===================================================
public:
// ===================================================
// Public member variables
// ===================================================
using Base::codelength;
using Base::indexCodelength;
using Base::moduleCodelength;
private:
// ===================================================
// Private member variables
// ===================================================
using Base::enter_log_enter;
using Base::enterFlow;
using Base::enterFlow_log_enterFlow;
using Base::exit_log_exit;
using Base::flow_log_flow; // node.(flow + exitFlow)
using Base::nodeFlow_log_nodeFlow; // constant while the leaf network is the same
// For hierarchical
using Base::exitNetworkFlow;
using Base::exitNetworkFlow_log_exitNetworkFlow;
// For biased
unsigned int preferredNumModules = 0;
unsigned int currentNumModules = 0;
double biasedCost = 0.0;
// For entropy bias correction
bool useEntropyBiasCorrection = false;
double entropyBiasCorrectionMultiplier = 1;
double indexEntropyBiasCorrection = 0;
double moduleEntropyBiasCorrection = 0;
double gamma = 0.7;
static double s_totalDegree;
static unsigned int s_numNodes;
public:
static void setNetworkProperties(const StateNetwork& network);
};
} // namespace infomap
#endif // BIASED_MAPEQUATION_H_
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef FLOWDATA_H_
#define FLOWDATA_H_
#include <ostream>
#include <utility>
namespace infomap {
struct FlowData {
double flow = 0.0;
double enterFlow = 0.0;
double exitFlow = 0.0;
double teleportFlow = 0.0;
double teleportSourceFlow = 0.0;
double teleportWeight = 0.0;
double danglingFlow = 0.0;
FlowData() = default;
FlowData(double flow) : flow(flow) { }
FlowData& operator+=(const FlowData& other)
{
flow += other.flow;
enterFlow += other.enterFlow;
exitFlow += other.exitFlow;
teleportFlow += other.teleportFlow;
teleportSourceFlow += other.teleportSourceFlow;
teleportWeight += other.teleportWeight;
danglingFlow += other.danglingFlow;
return *this;
}
FlowData& operator-=(const FlowData& other)
{
flow -= other.flow;
enterFlow -= other.enterFlow;
exitFlow -= other.exitFlow;
teleportFlow -= other.teleportFlow;
teleportSourceFlow -= other.teleportSourceFlow;
teleportWeight -= other.teleportWeight;
danglingFlow -= other.danglingFlow;
return *this;
}
friend std::ostream& operator<<(std::ostream& out, const FlowData& data)
{
return out << "flow: " << data.flow << ", enter: " << data.enterFlow << ", exit: " << data.exitFlow
<< ", teleWeight: " << data.teleportWeight << ", danglingFlow: " << data.danglingFlow
<< ", teleFlow: " << data.teleportFlow;
}
};
struct DeltaFlow {
unsigned int module = 0;
double deltaExit = 0.0;
double deltaEnter = 0.0;
unsigned int count = 0;
explicit DeltaFlow(unsigned int module, double deltaExit, double deltaEnter)
: module(module),
deltaExit(deltaExit),
deltaEnter(deltaEnter) { }
DeltaFlow() = default;
DeltaFlow(const DeltaFlow&) = default;
DeltaFlow(DeltaFlow&&) = default;
DeltaFlow& operator=(const DeltaFlow&) = default;
DeltaFlow& operator=(DeltaFlow&&) = default;
virtual ~DeltaFlow() = default;
DeltaFlow& operator+=(const DeltaFlow& other)
{
module = other.module;
deltaExit += other.deltaExit;
deltaEnter += other.deltaEnter;
++count;
return *this;
}
virtual void reset()
{
module = 0;
deltaExit = 0.0;
deltaEnter = 0.0;
count = 0;
}
friend void swap(DeltaFlow& first, DeltaFlow& second) noexcept
{
std::swap(first.module, second.module);
std::swap(first.deltaExit, second.deltaExit);
std::swap(first.deltaEnter, second.deltaEnter);
std::swap(first.count, second.count);
}
friend std::ostream& operator<<(std::ostream& out, const DeltaFlow& data)
{
return out << "module: " << data.module << ", deltaEnter: " << data.deltaEnter << ", deltaExit: " << data.deltaExit << ", count: " << data.count;
}
};
struct MemDeltaFlow : DeltaFlow {
double sumDeltaPlogpPhysFlow = 0.0;
double sumPlogpPhysFlow = 0.0;
MemDeltaFlow() = default;
explicit MemDeltaFlow(unsigned int module, double deltaExit, double deltaEnter, double sumDeltaPlogpPhysFlow = 0.0, double sumPlogpPhysFlow = 0.0)
: DeltaFlow(module, deltaExit, deltaEnter),
sumDeltaPlogpPhysFlow(sumDeltaPlogpPhysFlow),
sumPlogpPhysFlow(sumPlogpPhysFlow) { }
MemDeltaFlow& operator+=(const MemDeltaFlow& other)
{
DeltaFlow::operator+=(other);
sumDeltaPlogpPhysFlow += other.sumDeltaPlogpPhysFlow;
sumPlogpPhysFlow += other.sumPlogpPhysFlow;
return *this;
}
void reset() override
{
DeltaFlow::reset();
sumDeltaPlogpPhysFlow = 0.0;
sumPlogpPhysFlow = 0.0;
}
friend void swap(MemDeltaFlow& first, MemDeltaFlow& second) noexcept
{
swap(static_cast<DeltaFlow&>(first), static_cast<DeltaFlow&>(second));
std::swap(first.sumDeltaPlogpPhysFlow, second.sumDeltaPlogpPhysFlow);
std::swap(first.sumPlogpPhysFlow, second.sumPlogpPhysFlow);
}
friend std::ostream& operator<<(std::ostream& out, const MemDeltaFlow& data)
{
return out << "module: " << data.module << ", deltaEnter: " << data.deltaEnter << ", deltaExit: " << data.deltaExit << ", count: " << data.count << ", sumDeltaPlogpPhysFlow: " << data.sumDeltaPlogpPhysFlow << ", sumPlogpPhysFlow: " << data.sumPlogpPhysFlow;
}
};
struct PhysData {
unsigned int physNodeIndex;
double sumFlowFromM2Node; // The amount of flow from the memory node in this physical node
explicit PhysData(unsigned int physNodeIndex, double sumFlowFromM2Node = 0.0)
: physNodeIndex(physNodeIndex), sumFlowFromM2Node(sumFlowFromM2Node) { }
friend std::ostream& operator<<(std::ostream& out, const PhysData& data)
{
return out << "physNodeIndex: " << data.physNodeIndex << ", sumFlowFromM2Node: " << data.sumFlowFromM2Node;
}
};
} // namespace infomap
#endif // FLOWDATA_H_
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#include "InfoEdge.h"
#include "InfoNode.h"
namespace infomap {
InfoNode& infomap::InfoEdge::other(InfoNode& node) const
{
return (node == *source) ? *target : *source;
}
std::ostream& operator<<(std::ostream& out, const InfoEdge& edge)
{
return out << "(" << *edge.source << ") -> (" << *edge.target << "), flow: "
<< edge.data.flow;
}
} // namespace infomap
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef INFOEDGE_H_
#define INFOEDGE_H_
#include <iostream>
namespace infomap {
struct EdgeData {
public:
EdgeData() = default;
EdgeData(double weight, double flow) : weight(weight), flow(flow) { }
double weight;
double flow;
};
class InfoNode;
class InfoEdge {
public:
InfoEdge(InfoNode& source, InfoNode& target, double weight, double flow)
: data(weight, flow),
source(&source),
target(&target) { }
InfoNode& other(InfoNode& node) const;
friend std::ostream& operator<<(std::ostream& out, const InfoEdge& edge);
EdgeData data;
InfoNode* source;
InfoNode* target;
};
} // namespace infomap
#endif // INFOEDGE_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef INFOMAP_CONFIG_H_
#define INFOMAP_CONFIG_H_
#include "../io/Config.h"
#include "../utils/Random.h"
#include "../utils/Log.h"
#include <string>
namespace infomap {
template <typename Infomap>
class InfomapConfig : public Config {
public:
InfomapConfig() = default;
InfomapConfig(const std::string& flags, bool isCli = false) : InfomapConfig(Config(flags, isCli)) { }
InfomapConfig(const Config& conf) : Config(conf), m_rand(conf.seedToRandomNumberGenerator)
{
Log::precision(conf.verboseNumberPrecision);
}
virtual ~InfomapConfig() = default;
InfomapConfig(const InfomapConfig&) = default;
InfomapConfig& operator=(const InfomapConfig&) = default;
InfomapConfig(InfomapConfig&&) noexcept = default;
InfomapConfig& operator=(InfomapConfig&&) noexcept = default;
private:
Infomap& get()
{
return static_cast<Infomap&>(*this);
}
protected:
Random m_rand;
public:
Config& getConfig()
{
return *this;
}
const Config& getConfig() const
{
return *this;
}
Infomap& setConfig(const Config& conf)
{
*this = conf;
m_rand.seed(conf.seedToRandomNumberGenerator);
Log::precision(conf.verboseNumberPrecision);
return get();
}
Infomap& setNonMainConfig(const Config& conf)
{
cloneAsNonMain(conf);
return get();
}
Infomap& setNumTrials(unsigned int N)
{
numTrials = N;
return get();
}
Infomap& setVerbosity(unsigned int level)
{
verbosity = level;
return get();
}
Infomap& setTwoLevel(bool value)
{
twoLevel = value;
return get();
}
Infomap& setTuneIterationLimit(unsigned int value)
{
tuneIterationLimit = value;
return get();
}
Infomap& setFastHierarchicalSolution(unsigned int level)
{
fastHierarchicalSolution = level;
return get();
}
Infomap& setOnlySuperModules(bool value)
{
onlySuperModules = value;
return get();
}
Infomap& setNoCoarseTune(bool value)
{
noCoarseTune = value;
return get();
}
Infomap& setNoInfomap(bool value = true)
{
noInfomap = value;
return get();
}
Infomap& setMarkovTime(double codeRate)
{
markovTime = codeRate;
return get();
}
Infomap& setDirected(bool value)
{
directed = value;
flowModel = directed ? FlowModel::directed : FlowModel::undirected;
return get();
}
Infomap& reseed(unsigned int seed)
{
m_rand.seed(seed);
return get();
}
};
} // namespace infomap
#endif // INFOMAP_CONFIG_H_
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef INFOMAP_OPTIMIZER_BASE_H_
#define INFOMAP_OPTIMIZER_BASE_H_
#include "InfomapBase.h"
#include "InfoNode.h"
#include "FlowData.h"
#include <vector>
namespace infomap {
class InfomapOptimizerBase {
friend class InfomapBase;
using FlowDataType = FlowData;
public:
InfomapOptimizerBase() = default;
virtual ~InfomapOptimizerBase() = default;
virtual void init(InfomapBase* infomap) = 0;
// ===================================================
// IO
// ===================================================
virtual std::ostream& toString(std::ostream& out) const = 0;
// ===================================================
// Getters
// ===================================================
virtual double getCodelength() const = 0;
virtual double getIndexCodelength() const = 0;
virtual double getModuleCodelength() const = 0;
virtual double getMetaCodelength(bool /*unweighted*/ = false) const { return 0.0; }
protected:
virtual unsigned int numActiveModules() const = 0;
// ===================================================
// Run: Init: *
// ===================================================
// Init terms that is constant for the whole network
virtual void initTree() = 0;
virtual void initNetwork() = 0;
virtual void initSuperNetwork() = 0;
virtual double calcCodelength(const InfoNode& parent) const = 0;
// ===================================================
// Run: Partition: *
// ===================================================
virtual void initPartition() = 0;
virtual void moveActiveNodesToPredefinedModules(std::vector<unsigned int>& modules) = 0;
virtual unsigned int optimizeActiveNetwork() = 0;
virtual unsigned int tryMoveEachNodeIntoBestModule() = 0;
// virtual unsigned int tryMoveEachNodeIntoBestModuleLocal() = 0;
virtual unsigned int tryMoveEachNodeIntoBestModuleInParallel() = 0;
virtual void consolidateModules(bool replaceExistingModules = true) = 0;
virtual bool restoreConsolidatedOptimizationPointIfNoImprovement(bool forceRestore = false) = 0;
// ===================================================
// Debug: *
// ===================================================
virtual void printDebug() = 0;
};
} /* namespace infomap */
#endif // INFOMAP_OPTIMIZER_BASE_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef ITER_WRAPPER_H_
#define ITER_WRAPPER_H_
namespace infomap {
template <typename Iter>
class IterWrapper {
Iter m_begin, m_end;
public:
IterWrapper(Iter begin, Iter end) : m_begin(begin), m_end(end) { }
template <typename Container>
IterWrapper(Container& container) : m_begin(container.begin()), m_end(container.end()) { }
Iter begin() noexcept { return m_begin; };
Iter end() noexcept { return m_end; };
};
} // namespace infomap
#endif // ITER_WRAPPER_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef CLUSTER_MAP_H_
#define CLUSTER_MAP_H_
#include <string>
#include <map>
#include <vector>
#include <deque>
namespace infomap {
using Path = std::deque<unsigned int>; // 1-based indexing
using NodePath = std::pair<unsigned int, Path>;
using NodePaths = std::vector<NodePath>;
class ClusterMap {
public:
void readClusterData(const std::string& filename, bool includeFlow = false, const std::map<unsigned int, std::map<unsigned int, unsigned int>>* layerNodeToStateId = nullptr);
const std::map<unsigned int, unsigned int>& clusterIds() const noexcept
{
return m_clusterIds;
}
const NodePaths& nodePaths() const noexcept { return m_nodePaths; }
const std::string& extension() const noexcept { return m_extension; }
private:
void readTree(const std::string& filename, bool includeFlow, const std::map<unsigned int, std::map<unsigned int, unsigned int>>* layerNodeToStateId = nullptr);
void readClu(const std::string& filename, bool includeFlow, const std::map<unsigned int, std::map<unsigned int, unsigned int>>* layerNodeToStateId = nullptr);
std::map<unsigned int, unsigned int> m_clusterIds;
std::map<unsigned int, double> m_flowData;
NodePaths m_nodePaths;
std::string m_extension;
bool m_isHigherOrder = false;
};
} // namespace infomap
#endif // CLUSTER_MAP_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef OUTPUT_H_
#define OUTPUT_H_
#include <map>
#include <string>
#include <utility>
namespace infomap {
class InfomapBase;
class StateNetwork;
std::string writeTree(InfomapBase&, const StateNetwork&, const std::string&, bool states);
std::string writeFlowTree(InfomapBase&, const StateNetwork&, const std::string&, bool states);
std::string writeNewickTree(InfomapBase&, const std::string&, bool states);
std::string writeJsonTree(InfomapBase&, const StateNetwork&, const std::string&, bool states, bool writeLinks);
std::string writeCsvTree(InfomapBase&, const StateNetwork&, const std::string&, bool states);
std::string writeClu(InfomapBase&, const StateNetwork&, const std::string&, bool states, int moduleIndexLevel);
} // namespace infomap
#endif // OUTPUT_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#include "Log.h"
namespace infomap {
unsigned int Log::s_verboseLevel = 0;
bool Log::s_silent = false;
} // namespace infomap
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*******************************************************************************
Infomap software package for multi-level network clustering
Copyright (c) 2013, 2014 Daniel Edler, Anton Eriksson, Martin Rosvall
This file is part of the Infomap software package.
See file LICENSE_AGPLv3.txt for full license details.
For more information, see <http://www.mapequation.org>
******************************************************************************/
#ifndef VERSION_H_
#define VERSION_H_
namespace infomap {
const char* const INFOMAP_VERSION = "2.5.0";
}
#endif // VERSION_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment