How to Add a New Network Metric
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the process for adding a new network analysis metric to the C# Network Analyzer application. The application is a Windows Forms project designed to load network data from an edgelist file, compute various statistical properties, and visualize the results. The core logic for data processing and metric calculation resides within the Form1.cs file.
Adding a new metric involves three main steps: implementing the calculation logic, adding a UI element to display the result, and integrating the calculation into the main data processing workflow. This guide uses the existing metrics, such as the clustering coefficient and degree correlation, as a reference for extending the application's functionality.
1. Core Architecture and Data Structures
Before adding a new metric, it's essential to understand the primary data structures used to represent the network. The application defines Edge, Node, and NodeList classes to model the network graph.
Sources: [Form1.cs:46-174](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L46-L174)
Key Classes
The following class diagram illustrates the relationship between the core data structures.
This diagram shows that Form1 orchestrates the process, holding the edgelist and the resulting nodelist. The NodeList class encapsulates the collection of Node objects and contains methods for calculating network-wide properties.
Sources: [Form1.cs:46-174](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L46-L174)
Data Structure Details
| Class | Property | Type | Description |
|---|---|---|---|
| Edge | a, b | int | The integer IDs of the two nodes connected by the edge. |
| Node | node_ID | int | A unique identifier for the node. |
degree | int | The total number of connections (degree) for the node. | |
neighbors | List<int> | A list of node_IDs for all directly connected nodes. | |
clustering | double | The node's local clustering coefficient. | |
neighbors_degree | double | The average degree of the node's neighbors. | |
| NodeList | nodelist | List<Node> | The private list of all Node objects in the network. |
max_degree | int | The maximum degree found in the network. | |
min_degree | int | The minimum degree found in the network. |
Sources: [Form1.cs:46-174](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L46-L174)
2. The Data Processing Workflow
The entire analysis process is triggered when the user selects a file via the "Open" menu item. The openToolStripMenuItem_Click event handler in Form1.cs controls the flow from file parsing to metric calculation and display.
Sources: [Form1.cs:181-331](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L181-L331)
The following flowchart illustrates the sequence of operations within openToolStripMenuItem_Click.
This flow shows that after the NodeList is constructed, various metrics are calculated sequentially before the UI is updated. A new metric calculation should be inserted into this sequence.
Sources: [Form1.cs:181-331](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L181-L331)
3. Step-by-Step Guide to Add a New Metric
Step 1: Add a UI Element for Display
First, add a new Label control to the form to display the result of your new metric.
- Open the form designer for
Form1(Form1.Designer.cs). - Add a new
System.Windows.Forms.Labelto theControlscollection. - Configure its properties (Name, Text, Location) similarly to existing labels like
lblClusterorlblGamma.
Example of adding a label in Form1.Designer.cs:
// In InitializeComponent()
this.lblNewMetric = new System.Windows.Forms.Label();
// ...
// lblNewMetric
//
this.lblNewMetric.AutoSize = true;
this.lblNewMetric.Location = new System.Drawing.Point(198, 105); // Adjust location
this.lblNewMetric.Name = "lblNewMetric";
this.lblNewMetric.Size = new System.Drawing.Size(60, 13);
this.lblNewMetric.TabIndex = 18; // Next available index
this.lblNewMetric.Text = "New Metric";
// ...
this.Controls.Add(this.lblNewMetric);
Sources: [Form1.Designer.cs:150-155, 255-266](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.Designer.cs#L150-L155)
Step 2: Implement the Calculation Logic
The logic for the new metric should be implemented. The best practice is to add a new public method to the NodeList class if the metric is a network-wide property. This keeps the calculation logic encapsulated with the network data.
For example, to add a metric that calculates the network density:
// Inside the NodeList class in Form1.cs
public double NetworkDensity(int num_edges)
{
int n = this.nodelist.Count();
if (n <= 1)
{
return 0;
}
// Formula for density: 2E / N(N-1)
return (double)(2 * num_edges) / (n * (n - 1));
}
This new method would be placed within the NodeList class definition. The existing Clustering method serves as a good template for iterating over nodes or edges.
Sources: [Form1.cs:81-174, 153-171](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L81-L174)
Step 3: Integrate into the Main Workflow
Finally, call the new calculation method from the openToolStripMenuItem_Click event handler and update the UI label you created.
- In
Form1.cs, locate theopenToolStripMenuItem_Clickmethod. - After the
NodeListis instantiated and basic properties are calculated, call your new metric method. - Update the
Textproperty of the new label with the formatted result.
// Inside openToolStripMenuItem_Click in Form1.cs, after nodelist is created
// ...
lblMin.Text = "Min Degree: " + min;
// ** START: New Metric Integration **
double newMetricValue = nodelist.NetworkDensity(links); // Assuming the method was added to NodeList
lblNewMetric.Text = "Network Density: " + String.Format("{0:n4}", newMetricValue);
// ** END: New Metric Integration **
if (chkClustering.Checked) lblCluster.Text = "Average clustering coefficient: " + String.Format("{0:n3}", nodelist.Clustering(edgelist, textBox1));
// ...
This code snippet should be placed logically among the other metric calculations.
Sources: [Form1.cs:214-222](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs#L214-L222)
4. Related Implementations
The project contains other scripts and programs that perform network analysis, which can serve as a conceptual reference.
- Python Analysis (
Network.py): This script calculates and plots the degree distribution usingpandasandbokeh. It computesgammavia a log-log linear regression, similar to the C# implementation. This demonstrates an alternative approach to the same problem.
Sources: [Network.py:1-57](https://github.com/mvirag2000/Network-Science/blob/HEAD/Network.py#L1-L57) - Network Generation (
Barabasi.cs): This console application generates networks using the Barabasi-Albert model. It contains its ownEdgeandEdgelistclasses, showing a consistent data modeling approach across the project.
Sources: [Barabasi.cs:23-40](https://github.com/mvirag2000/Network-Science/blob/HEAD/Barabasi.cs#L23-L40)
Summary
Adding a new network metric is a straightforward process of extending the existing architecture. By following the pattern of existing metrics, a developer can add a new calculation to the NodeList class, add a Label to the Form1 designer, and link them within the openToolStripMenuItem_Click event handler. This modular approach ensures that new features are well-integrated and maintain the separation between data processing and UI presentation.
Sources: [Form1.cs](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.cs), [Form1.Designer.cs](https://github.com/mvirag2000/Network-Science/blob/HEAD/Form1.Designer.cs), [Program.cs](https://github.com/mvirag2000/Network-Science/blob/HEAD/Program.cs)