public void NoConnectionsTest() { // creates network nodes const int num = 10; var network = new Network(); for (var i = 0u; i < num; i++) { network.AddVertex(i); } Console.WriteLine(network); // creates and updates community algorithm var commAlgorithm = new CommunityAlgorithm(network); commAlgorithm.Update(); commAlgorithm.DisplayNodesCommunities(); commAlgorithm.DisplayCommunities(); Assert.AreEqual(0, network.EdgeCount, "Network should not contain edges."); for (var i = 0u; i < num; i++) { Assert.AreEqual(i, commAlgorithm.NodesCommunities[i], $"Node {i} should be in community {i}."); } Assert.AreEqual(num, commAlgorithm.GetNumberCommunities(), $"Num. communities should be {num}."); Assert.AreEqual(0, commAlgorithm.GetModularity(), double.Epsilon, "Community modularity should be 0."); }
public void CommunityGraphTest() { // creates network nodes const int num = 10; var network = new Network(); for (var i = 0u; i < num; i++) { network.AddVertex(i); } Console.WriteLine(network); // creates and updates community algorithm var commAlgorithm = new CommunityAlgorithm(network); commAlgorithm.Update(); commAlgorithm.DisplayNodesCommunities(); commAlgorithm.DisplayCommunities(); // gets community graph var communityGraph = commAlgorithm.GetCommunityNetwork(); Console.WriteLine(communityGraph); Assert.AreEqual(0, communityGraph.EdgeCount, "Community graph should not contain edges."); Assert.AreEqual(num, communityGraph.VertexCount, $"Num. communities should be {num}."); Assert.AreEqual(0, communityGraph.TotalWeight, double.Epsilon, "Community graph total weight should be 0."); }
public void SaveGraphEvolutionFileTest() { var network = new Network(); for (var i = 0u; i < NUM_NODES; i++) { network.AddVertex(i); } var fullPath = Path.Combine(Path.GetFullPath("."), EVO_FILE_NAME); File.Delete(fullPath); Console.WriteLine(fullPath); var communityAlg = new CommunityAlgorithm(network, NUM_PASSES, MIN_MODULARITY); using (var tracker = new CommunityTracker(communityAlg, fullPath, 6, Formatting.Indented)) { var e01 = new Connection(1, 0); var e02 = new Connection(0, 2); var e12 = new Connection(2, 1); var e13 = new Connection(1, 3); var e34 = new Connection(4, 3); Update(communityAlg, tracker); Console.WriteLine("Inserting 0->1, 0->2 and 1->2"); network.AddEdge(e01); network.AddEdge(e02); network.AddEdge(e12); Update(communityAlg, tracker); Console.WriteLine("Inserting 1->3 and 3->4"); network.AddEdge(e13); network.AddEdge(e34); Update(communityAlg, tracker); Console.WriteLine("Removing 0->1, 0->2 and 1->2"); network.RemoveEdge(e01); network.RemoveEdge(e02); network.RemoveEdge(e12); Update(communityAlg, tracker); Console.WriteLine("Removing 1->3 and 3->4"); network.RemoveEdge(e13); network.RemoveEdge(e34); Update(communityAlg, tracker); Console.WriteLine("Inserting 0->1, 0->2 and 1->2"); network.AddEdge(e01); network.AddEdge(e02); network.AddEdge(e12); Update(communityAlg, tracker); } Assert.IsTrue(File.Exists(fullPath), $"D3 json file should exist in {fullPath}."); Assert.IsTrue(new FileInfo(fullPath).Length > 0, "Json file size should be > 0 bytes."); #if !DEBUG File.Delete(fullPath); #endif }
public void DisposeTest() { // creates network nodes const int num = 10; var network = new Network(); for (var i = 0u; i < num; i++) { network.AddVertex(i); } // creates edges for (var i = 0u; i < num; i++) { for (var j = i + 1; j < num; j++) { network.AddEdge(new Connection(i, j)); } } // creates and updates community algorithm var commAlgorithm = new CommunityAlgorithm(network); commAlgorithm.Update(); // disposes algorithm commAlgorithm.Dispose(); Assert.IsNull(commAlgorithm.Communities, "Community algorithm should not have any communities."); Assert.IsNull(commAlgorithm.NodesCommunities, "Community algorithm should not have any communities."); commAlgorithm.Dispose(); }
/// <summary> /// Creates a new <see cref="CommunityTracker" /> with the given algorithm and capacity. /// </summary> /// <param name="communityAlg"> /// The community algorithm containing the <see cref="Network" /> used to update the communities. /// </param> /// <param name="filePath">The path to the file in which to save the network graph.</param> /// <param name="maxUpdates">The maximum number of updates to the communities to be tracked.</param> /// <param name="formatting">The Json file formatting.</param> public CommunityTracker( CommunityAlgorithm communityAlg, string filePath, uint maxUpdates, Formatting formatting = Formatting.None) { this.CommunityAlg = communityAlg; this.FilePath = filePath; this._formatting = formatting; //writes header of json file this._jsonWriter = new JsonTextWriter(new StreamWriter(new FileStream(filePath, FileMode.Create), Encoding.UTF8)) { Formatting = formatting, CloseOutput = true }; this._jsonWriter.WriteStartObject(); // writes num time-steps this._jsonWriter.WritePropertyName(Constants.NUM_TIME_STEPS_PROP); this._jsonWriter.WriteValue(maxUpdates); // opens graphs this._jsonWriter.WritePropertyName(Constants.GRAPH_LIST_PROP); this._jsonWriter.WriteStartArray(); }
private void GenerateBtnClick(object sender, EventArgs e) { // creates network nodes var network = new Network(); for (var i = 0u; i < this.numNodesNumUD.Value; i++) { network.AddVertex(i); } // creates connections for (var i = 0u; i < this.numNodesNumUD.Value; i++) { for (var j = i + 1; j < this.numNodesNumUD.Value; j++) { if (this._rand.Next(CONNECTION_CHANCE) == 0) { network.AddEdge(new Connection(i, j, this._rand.NextDouble())); } } } // creates community algorithm var communityAlg = new CommunityAlgorithm( network, (int)this.numPassesNumUD.Value, (double)this.minModularityNumUD.Value); // creates tracker var filePath = Path.GetFullPath(Path.Combine(".", D3_FILE_NAME)); var maxUpdates = (uint)this.maxUpdatesNumUD.Value; var communityTracker = new CommunityTracker(communityAlg, filePath, maxUpdates, Formatting.Indented); // runs update algorithm this.EnableInterface(false); this.algorithmWorker.RunWorkerAsync(new object[] { communityTracker, maxUpdates }); }
private void OpenFileBtnClick(object sender, EventArgs e) { // opens network file if (this.openFileDialog.ShowDialog() != DialogResult.OK) { return; } // loads network var network = Network.LoadFromCsv(this.openFileDialog.FileName); if (network != null) { this._network = network; this._communityAlg = null; this.fileNameLabel.Text = Path.GetFileName(this.openFileDialog.FileName); this.infoLabel.Text = $"Successfully loaded network: {this._network}."; this.EnableInterface(true); this.exportBtn.Enabled = false; this.pictureBox.Image = null; } else { this.infoLabel.Text = $"Error: could not load network in: {this.openFileDialog.FileName}."; } }
private static UndirectedGraph <Node, Edge> GetGraph( CommunityAlgorithm communityAlg, bool showLabels, PaletteGenerator paletteGenerator) { // generates community color palette paletteGenerator = paletteGenerator ?? TolPalettes.CreateTolDivPalette; var colors = paletteGenerator(communityAlg.GetNumberCommunities()); // creates a graphviz from the network and communities var graph = new UndirectedGraph <Node, Edge>(); // adds nodes with community info var nodes = new Node[communityAlg.Size]; foreach (var nodeID in communityAlg.Network.Vertices) { var node = new Node(nodeID, communityAlg.NodesCommunities[nodeID]) { Color = colors[communityAlg.NodesCommunities[nodeID]], ShowLabel = showLabels }; nodes[nodeID] = node; graph.AddVertex(node); } // adds edges from connections foreach (var conn in communityAlg.Network.Edges) { graph.AddEdge(new Edge(nodes[conn.Source], nodes[conn.Target], conn.Weight) { ShowLabel = showLabels }); } return(graph); }
public void ZeroWeightsTest() { // creates network nodes const int num = 10; var network = new Network(); for (var i = 0u; i < num; i++) { network.AddVertex(i); } // creates edges (full with 0 weight) for (var i = 0u; i < num; i++) { for (var j = i + 1; j < num; j++) { network.AddEdge(new Connection(i, j, 0)); } } Console.WriteLine(network); // creates and updates community algorithm var commAlgorithm = new CommunityAlgorithm(network); commAlgorithm.Update(); commAlgorithm.DisplayNodesCommunities(); commAlgorithm.DisplayCommunities(); for (var i = 0u; i < num; i++) { Assert.AreEqual(i, commAlgorithm.NodesCommunities[i], $"Node {i} should be in community {i}."); } Assert.AreEqual(num, commAlgorithm.GetNumberCommunities(), $"Num. communities should be {num}."); Assert.AreEqual(0, commAlgorithm.GetModularity(), double.Epsilon, "Community modularity should be 0."); }
private static void Update(CommunityAlgorithm communityAlg, CommunityTracker tracker) { communityAlg.Update(); communityAlg.DisplayNodesCommunities(); Console.WriteLine("Modularity: {0}", communityAlg.GetModularity()); tracker.Update(); }
private static void SaveFileTest(PaletteGenerator paletteGenerator, string name) { // creates graph and adds nodes var network = new Network(); for (var i = 0u; i < NUM_NODES; i++) { network.AddVertex(i); } // adds connections network.AddEdge(new Connection(0, 1)); network.AddEdge(new Connection(0, 2)); network.AddEdge(new Connection(0, 9)); network.AddEdge(new Connection(2, 4)); network.AddEdge(new Connection(2, 9)); network.AddEdge(new Connection(4, 7)); network.AddEdge(new Connection(7, 9)); network.AddEdge(new Connection(8, 9)); // creates algorithm and updates communities var communityAlg = new CommunityAlgorithm(network, -1, 0.000001); communityAlg.Update(); communityAlg.DisplayCommunities(); var fullPath = Path.GetFullPath("."); foreach (var imageType in ImageTypes) { var fileName = $"{FILE_NAME}-{name}-{imageType}"; var dotPath = Path.Combine(fullPath, $"{fileName}.dot"); var imgPath = $"{dotPath}.{imageType.ToString().ToLower()}"; File.Delete(dotPath); File.Delete(imgPath); var filePath = communityAlg.ToGraphvizFile( fullPath, fileName, true, imageType, paletteGenerator, WAIT_TIMEOUT); Console.WriteLine(dotPath); Assert.IsTrue(File.Exists(dotPath), $"Dot file should exist in {dotPath}."); Assert.AreEqual(filePath, dotPath, $"Dot file should be exist in {imgPath}"); Console.WriteLine(imgPath); Assert.IsTrue(File.Exists(imgPath), $"Image file should exist in {imgPath}."); Assert.IsTrue(new FileInfo(imgPath).Length > 0, "Image size should be > 0 bytes."); #if !DEBUG File.Delete(dotPath); File.Delete(imgPath); #endif } }
private static void Main(string[] args) { var network = new Network(); for (var i = 0u; i < NUM_NODES; i++) { network.AddVertex(i); } var communityAlg = new CommunityAlgorithm(network, NUM_PASSES, MIN_MODULARITY); using (var tracker = new CommunityTracker(communityAlg, D3_JSON_FILE, MAX_UPDATES, Formatting.Indented)) { var e01 = new Connection(1, 0); var e02 = new Connection(0, 2); var e12 = new Connection(2, 1); var e13 = new Connection(1, 3); var e34 = new Connection(4, 3); Update(communityAlg, tracker); Console.WriteLine("Inserting 0->1, 0->2 and 1->2"); network.AddEdge(e01); network.AddEdge(e02); network.AddEdge(e12); Update(communityAlg, tracker); Console.WriteLine("Inserting 1->3 and 3->4"); network.AddEdge(e13); network.AddEdge(e34); Update(communityAlg, tracker); Console.WriteLine("Removing 0->1, 0->2 and 1->2"); network.RemoveEdge(e01); network.RemoveEdge(e02); network.RemoveEdge(e12); Update(communityAlg, tracker); Console.WriteLine("Removing 1->3 and 3->4"); network.RemoveEdge(e13); network.RemoveEdge(e34); Update(communityAlg, tracker); Console.WriteLine("Inserting 0->1, 0->2 and 1->2"); network.AddEdge(e01); network.AddEdge(e02); network.AddEdge(e12); Update(communityAlg, tracker); } Console.WriteLine("Done!"); Console.ReadKey(); }
private void GenerateBtnClick(object sender, EventArgs e) { if (this._network == null) { return; } // creates community algorithm this._communityAlg = new CommunityAlgorithm( this._network, (int)this.numPassesNumUD.Value, (double)this.minModularityNumUD.Value); // runs update algorithm this.EnableInterface(false); this.infoLabel.Text = "Working..."; this.algorithmWorker.RunWorkerAsync(this._paletteGenerator); }
/// <summary> /// Saves the network of the given <see cref="CommunityAlgorithm" /> to a d3.js graph file. /// </summary> /// <param name="communityAlg">The network to be saved to a graph file.</param> /// <param name="filePath">The path to the file in which to save the network graph.</param> /// <param name="formatting">The Json file formatting.</param> public static void ToD3GraphFile( this CommunityAlgorithm communityAlg, string filePath, Formatting formatting = Formatting.None) { // creates writers using (var fs = new FileStream(filePath, FileMode.Create)) using (var sw = new StreamWriter(fs, Encoding.UTF8)) using (var tw = new JsonTextWriter(sw) { Formatting = formatting }) { tw.WriteStartObject(); // writes num time-steps tw.WritePropertyName(Constants.NUM_TIME_STEPS_PROP); tw.WriteValue(1); // opens graphs tw.WritePropertyName(Constants.GRAPH_LIST_PROP); tw.WriteStartArray(); // creates a new graph structure with the network and communities info var graph = new Graph(); // adds links from network foreach (var edge in communityAlg.Network.Edges) { graph.Links.Add(new Link { Source = edge.Source, Target = edge.Target, Value = edge.Weight }); } // adds nodes with community info for (var i = 0u; i < communityAlg.Network.VertexCount; i++) { graph.Nodes.Add(new Node(i, communityAlg.NodesCommunities[i])); } // writes graph and closes json tw.WriteRawValue(JsonConvert.SerializeObject(graph, formatting)); tw.WriteEndArray(); tw.WriteEndObject(); } }
public void SaveFileTest() { // creates graph and adds nodes var network = new Network(); for (var i = 0u; i < NUM_NODES; i++) { network.AddVertex(i); } // adds connections network.AddEdge(new Connection(0, 1)); network.AddEdge(new Connection(0, 2)); network.AddEdge(new Connection(0, 9)); network.AddEdge(new Connection(2, 4)); network.AddEdge(new Connection(2, 9)); network.AddEdge(new Connection(4, 7)); network.AddEdge(new Connection(7, 9)); network.AddEdge(new Connection(8, 9)); // creates algorithm and updates communities var communityAlg = new CommunityAlgorithm(network, -1, 0.000001); communityAlg.Update(); communityAlg.DisplayCommunities(); var fullPath = Path.Combine(Path.GetFullPath("."), FILE_NAME); File.Delete(fullPath); communityAlg.ToD3GraphFile(FILE_NAME, Formatting.Indented); Console.WriteLine(fullPath); Assert.IsTrue(File.Exists(fullPath), $"D3 json file should exist in {fullPath}."); Assert.IsTrue(new FileInfo(fullPath).Length > 0, "Json file size should be > 0 bytes."); #if !DEBUG File.Delete(fullPath); #endif }
/// <summary> /// Saves the given <see cref="Network" /> to an image file. /// </summary> /// <param name="communityAlg">The root node of the tree to be saved.</param> /// <param name="basePath">The path in which to save the given tree</param> /// <param name="fileName">The name of the image file to be saved (without extension)</param> /// <param name="showLabels">Whether to show nodes' labels.</param> /// <param name="imageType">The type of image file in which to save the tree.</param> /// <param name="paletteGenerator">The color palette generator used to represent the different communities.</param> /// <param name="timeout">The maximum time to wait for Graphviz to create the image file.</param> /// <returns>The path to the file where the tree image file was saved.</returns> public static string ToGraphvizFile( this CommunityAlgorithm communityAlg, string basePath, string fileName, bool showLabels = true, GraphvizImageType imageType = GRAPHVIZ_IMAGE_TYPE, PaletteGenerator paletteGenerator = null, int timeout = GRAPHVIZ_TIMEOUT_MS) { var filePath = Path.Combine(basePath, $"{fileName}.dot"); if (File.Exists(filePath)) { File.Delete(filePath); } var graph = GetGraph(communityAlg, showLabels, paletteGenerator); var viz = new GraphvizAlgorithm <Node, Edge>(graph) { ImageType = imageType }; viz.FormatVertex += OnFormatVertex; viz.FormatEdge += OnFormatEdge; return(viz.Generate(new FileDotEngine(timeout), filePath)); }
public void NetworkPassesTest() { // creates network nodes const int numNodes = 16; var network = new Network(); for (var i = 0u; i < numNodes; i++) { network.AddVertex(i); } // creates edges (according to paper) network.AddEdge(new Connection(0, 2)); network.AddEdge(new Connection(0, 4)); network.AddEdge(new Connection(0, 3)); network.AddEdge(new Connection(0, 5)); network.AddEdge(new Connection(1, 2)); network.AddEdge(new Connection(1, 4)); network.AddEdge(new Connection(1, 7)); network.AddEdge(new Connection(2, 4)); network.AddEdge(new Connection(2, 5)); network.AddEdge(new Connection(2, 6)); network.AddEdge(new Connection(3, 7)); network.AddEdge(new Connection(4, 10)); network.AddEdge(new Connection(5, 7)); network.AddEdge(new Connection(5, 11)); network.AddEdge(new Connection(6, 7)); network.AddEdge(new Connection(6, 11)); network.AddEdge(new Connection(8, 9)); network.AddEdge(new Connection(8, 10)); network.AddEdge(new Connection(8, 11)); network.AddEdge(new Connection(8, 14)); network.AddEdge(new Connection(8, 15)); network.AddEdge(new Connection(9, 12)); network.AddEdge(new Connection(9, 14)); network.AddEdge(new Connection(10, 11)); network.AddEdge(new Connection(10, 12)); network.AddEdge(new Connection(10, 13)); network.AddEdge(new Connection(10, 14)); network.AddEdge(new Connection(11, 13)); // creates and updates community algorithm var communityAlg = new CommunityAlgorithm(network, -1, 0); communityAlg.Update(); communityAlg.DisplayCommunities(); //checks communities Assert.AreEqual(4, communityAlg.GetNumberCommunities(), "Network should have 4 communities."); var communities = new[] { new HashSet <uint> { 0, 1, 2, 4, 5 }, new HashSet <uint> { 3, 6, 7 }, new HashSet <uint> { 11, 13 }, new HashSet <uint> { 8, 9, 10, 12, 14, 15 } }; for (var i = 0; i < 4; i++) { var community = communityAlg.Communities[i]; Console.WriteLine(ArrayUtil.ToString(community.ToArray())); Assert.IsTrue(communities.Any(comm => comm.SetEquals(community)), $"Community {i} is unexpected."); } // gets and checks community graph (1st pass) communityAlg.DisplayCommunityGraph(); communityAlg.DisplayPartition(); network = communityAlg.GetCommunityNetwork(); Assert.AreEqual(4, network.VertexCount, "Community network should have 4 nodes."); // checks connections var exptConns = new HashSet <Connection> { new Connection(0, 0, 16), new Connection(0, 1, 1), new Connection(0, 2, 3), new Connection(1, 1, 14), new Connection(1, 2, 1), new Connection(1, 3, 4), new Connection(2, 2, 2), new Connection(2, 3, 1), new Connection(3, 3, 4) }; var netConns = new HashSet <Connection>(network.Edges); foreach (var exptConn in exptConns) { Console.WriteLine(exptConn); Assert.IsTrue(netConns.Contains(exptConn), $"Community network should contain connection: {exptConn}."); } //// creates and updates community algorithm //communityAlg = new CommunityAlgorithm(network, -1, 0); //communityAlg.Update(); //communityAlg.DisplayCommunities(); }
public void RenumberTest() { // creates network nodes const int num = 10; var network = new Network(); for (var i = 0u; i < num; i++) { network.AddVertex(i); } // creates edges (random) var random = new Random(); for (var i = 0u; i < num; i++) { for (var j = i + 1; j < num; j++) { if (random.NextDouble() > 0.6) { network.AddEdge(new Connection(i, j)); } } } Console.WriteLine(network); // creates and updates community algorithm var commAlgorithm = new CommunityAlgorithm(network); commAlgorithm.Update(false); // copies communities Console.WriteLine("Before renumbering:"); commAlgorithm.DisplayCommunities(); var beforeComms = new HashSet <uint> [num]; for (var i = 0; i < num; i++) { beforeComms[i] = new HashSet <uint>(commAlgorithm.Communities[i]); } var beforeNumComms = commAlgorithm.GetNumberCommunities(); // renumbers communities Console.WriteLine("After renumbering:"); commAlgorithm.RenumberCommunities(); commAlgorithm.DisplayCommunities(); Assert.AreEqual(beforeNumComms, commAlgorithm.GetNumberCommunities(), $"Number of communities after renumbering should be the same: {beforeNumComms}"); for (var i = 0u; i < num - 1; i++) { if (commAlgorithm.Communities[i].Count == 0) { Assert.IsTrue(commAlgorithm.Communities[i + 1].Count == 0, $"Community {i + 1} should have 0 nodes since community {i} has 0."); } if (i < beforeNumComms && beforeComms[i].Count > 0) { Assert.IsTrue(beforeComms[i].SetEquals(commAlgorithm.Communities[i]), $"Renumbering should have maintained community {i}."); } } }