Metric Calculation Logic
Relevant source files
The following files were used as context for generating this wiki page:
The Network-Science repository contains two primary implementations for calculating network metrics: a C# Windows Forms application for comprehensive analysis and a Python script focused on degree distribution. This document details the logic and architecture of these metric calculation systems, primarily focusing on the C# implementation which computes clustering, degree correlation, and degree distribution. The Python script provides an alternative method for analyzing degree distribution using logarithmic binning.
The main entry point for the C# analysis is the Form1 class, which is launched by Program.cs. The user initiates the analysis by opening a network data file (an edgelist) through the UI, which triggers a series of calculations. The results are then displayed in charts and text labels on the form.
Sources: README.md, Program.cs:17-19, Form1.cs:192-334
C# Metric Calculation Engine
The core of the analysis is within the Charts namespace, specifically in the Form1.cs file. It defines the data structures for representing the network and contains the methods for computing all metrics.
Sources: Form1.cs
Data Structures
The network is represented using three main classes: Edge, Node, and NodeList.
| Class | Description | Fields |
|---|---|---|
Edge | Represents a connection between two nodes, identified by integer IDs. | int a, int b |
Node | Represents a single node in the network. It stores its ID, degree, a list of its neighbors' IDs, its clustering coefficient, and the average degree of its neighbors. | node_ID, degree, neighbors, clustering, neighbors_degree |
NodeList | A collection class that holds all Node objects. It orchestrates the calculation of most network metrics by processing an initial List<Edge>. | nodelist, selfies, dupes, max_degree, min_degree |
Sources: Form1.cs:46-77, Barabasi.cs:23-30
The relationship between these classes is shown in the diagram below.
This diagram illustrates that Form1 uses NodeList, which in turn is built from a list of Edge objects and contains multiple Node objects.
Sources: Form1.cs:46-179
Data Processing Flow
The metric calculation process is initiated when the user selects a file via the "Open" menu item. The entire workflow happens within the openToolStripMenuItem_Click event handler.
The following flowchart outlines the sequence of operations:
This flowchart shows the step-by-step process from file loading to the final display of calculated metrics and charts.
Sources: Form1.cs:192-334
Detailed Metric Calculations
Basic Metrics
Upon loading the edgelist and constructing the NodeList, several basic metrics are immediately available.
- Nodes & Links: The number of unique nodes and total edges are counted.
- Average Degree: Calculated as
2 * links / nodes. - Self-links & Duplicate links: Counted during the initial pass over the edgelist.
- Min/Max Degree: Determined after calculating the degree for every node.
These values are then used to update the corresponding UI labels (lblNodes, lblLinks, lblMean, etc.).
Sources: Form1.cs:91-140, Form1.cs:220-227, Form1.Designer.cs:92-125
Average Clustering Coefficient
If the "Clustering" checkbox (chkClustering) is checked, the NodeList.Clustering() method is called.
The calculation logic is as follows:
- Iterate through each
nodein thenodelist. - For each
node, iterate through all pairs of itsneighbors(leftandright). - Check if an edge exists between
leftandright. If so, increment aclustercounter. - The node's individual clustering coefficient is calculated as
2 * cluster / (degree * (degree - 1)), fordegree > 1. - The total average clustering coefficient for the network is the average of all individual node clustering coefficients.
// Form1.cs:161-177
public double Clustering(List<Edge> e, Control msg)
{
double total_cluster = 0;
int nodes = nodelist.Count();
foreach (Node node in nodelist)
{
// ...
int cluster = 0;
foreach (int left in node.neighbors)
{
foreach (int right in node.neighbors)
{
if (e.Exists(x => x.a == left && x.b == right)) cluster++;
}
}
if (node.degree > 1) node.clustering = (double) 2 * cluster / (node.degree * (node.degree - 1));
total_cluster += node.clustering;
}
return total_cluster / nodes; //Average clustering
}
Sources: Form1.cs:161-179, Form1.cs:228
Degree Correlation
The application calculates the average degree of the neighbors for nodes of a given degree k, denoted as k_nn(k).
- A
Seriesnamedneighborsis created to store the data points(k, k_nn(k)). - The code iterates from
deg = 1tomax_degree. - For each
deg, it finds all nodes with that degree, sums theirneighbors_degreevalues, and calculates the average. - If nodes of that degree exist, a point
(deg, average_neighbors_degree)is added to theneighborsseries. - A power-law fit (
y = c * x^exp) is applied to these points usingMathNet.Numerics.Fit.Power. - The resulting exponent
expis displayed inlblCorrelation. - The data points and the regression line are plotted on
chart1.
Sources: Form1.cs:229-270, Form1.Designer.cs:70-81
Cumulative Degree Distribution (Gamma)
The application calculates the cumulative degree distribution P(k), which is the probability that a node has a degree greater than or equal to k.
- A
Seriesnameddistrois created. - It iterates from
deg = 1tomax_degree. - For each
deg, it counts the number of nodes with that degree (count). - The cumulative probability
P(k)is calculated iteratively. A point(deg, P(k))is added to thedistroseries.P(k)is stored in theprevvariable, which is updated asprev = prev - count / nodes. - A "shadow" series is created containing only the points from the distribution where the degree is greater than a user-defined
txtCutoffvalue. - A power-law fit is applied to this "shadow" series to find an exponent
exp2. - The degree exponent
gammais calculated as1 - exp2(since for a cumulative distribution,gamma = 1 - alphawherealphais the power-law exponent). - The result is displayed in
lblGamma, and the full distribution and regression line for the tail are plotted onchart2.
Sources: Form1.cs:294-334, Form1.Designer.cs:169-176, Form1.Designer.cs:193-199
Python Script Analysis (Network.py)
A separate Python script, Network.py, offers a different implementation for calculating the degree distribution and its exponent, gamma.
Key Libraries
pandasfor data manipulation.numpyfor numerical operations, especially logarithmic binning.scipy.stats.linregressfor linear regression.bokehfor plotting.
Sources: Network.py:1-7
Python Data Processing Flow
This diagram shows the Python script's workflow, which relies on logarithmic binning before performing a linear regression.
Sources: Network.py:9-52
Gamma Calculation
The script calculates the degree exponent (gamma) differently from the C# application:
- It uses logarithmic binning to group degrees, which is a standard practice for visualizing heavy-tailed distributions.
- It performs a linear regression on the
log10of the binned counts versus thelog10of the degree. - The degree exponent
gammais taken as the negative of the slope of this log-log linear fit.
This approach calculates the exponent for the probability density function p(k), whereas the C# application calculates it for the cumulative distribution function P(k).
Sources: Network.py:28-39, Network.py:47