Feature: Saving and Exporting Networks
Relevant source files
The following files were used as context for generating this wiki page:The Network-Science project provides functionalities for both generating and analyzing networks. A key part of this workflow is the ability to save and export data. This includes saving generated network structures as edgelists and exporting the results of network analysis to structured files. These features are implemented across both the C# and Python components of the repository, enabling data persistence and interoperability.
The primary export formats are tab-separated text files (.txt) for edgelists and comma-separated value files (.csv) for analysis results. The C# application Barabasi.cs is responsible for generating and saving network edgelists, while the Network.py script handles the export of degree distribution analysis. The main C# GUI application (Form1.cs) consumes these edgelist files for further analysis and visualization.
C# Network Generation and Export
The C# console application in Barabasi.cs is designed to generate a network using the Barabási-Albert model (preferential attachment) and save it to a file as an edgelist.
Sources: Barabasi.cs, README.md:4
Core Components
The saving functionality is built around the Edgelist class and its Write method.
| Class | Description | Source File |
|---|---|---|
Edge | A simple class representing a link between two nodes, a and b. | Barabasi.cs:23-30 |
Edgelist | A collection of Edge objects. It includes methods to generate the network and write it to a file. | Barabasi.cs:31-113 |
The relationship between these classes is straightforward.
This class diagram shows that the Edgelist class contains a list of Edge objects.
Sources: Barabasi.cs:23-113
Export Workflow
The process of generating and saving a network is orchestrated in the Main method of Barabasi.cs.
- A
System.IO.StreamWriteris created, pointing to a destination file (e.g.,dissociative.edgelist.txt). - An
Edgelistobject is instantiated, which generates the network structure internally using preferential attachment. - The
edgelist.Write(file)method is called to persist the network. - The
StreamWriteris closed.
This flow is visualized in the diagram below.
This diagram illustrates the sequence of operations from file creation to writing the edgelist data.
Sources: Barabasi.cs:11-17, Barabasi.cs:110-113
Edgelist.Write() Method
This method iterates through each Edge in its internal list and writes the two connected nodes (a and b) to the provided StreamWriter, separated by a tab character.
// From: Barabasi.cs
public void Write(System.IO.StreamWriter file)
{
foreach (Edge edge in edgelist) file.WriteLine(edge.a + "\t" + edge.b);
}
Sources: Barabasi.cs:110-113
The output is a simple, two-column, tab-separated file representing the network's edges. This format is consumed by other parts of the project, such as the network analyzer GUI.
Sources: Form1.cs:202-204
Python Analysis and Export
The Network.py script provides a different type of export functionality. Instead of saving the raw network, it loads an edgelist, performs a degree distribution analysis, and exports the computed histogram data to a CSV file.
Sources: Network.py, README.md:3
Export Workflow
The script uses the pandas library to manage data and scipy for statistical regression.
- An edgelist is loaded from a text file into a pandas DataFrame.
- The in-degree or out-degree is calculated for each node.
- The degrees are grouped into logarithmic bins to create a histogram.
- A linear regression is performed on the log-transformed data to find the degree exponent (gamma).
- The resulting DataFrame, containing bin data and regression predictions, is saved to a CSV file named
[dataset].histo.csv.
The following diagram shows the data flow within the Python script.
This diagram outlines the process from loading data to exporting the analysis results.
Sources: Network.py:10-44
Exported CSV File Structure
The script exports the histo DataFrame, which contains the binned degree distribution and the results of the power-law fit. The to_csv function is used for the export.
# From: Network.py
histo.to_csv(filename, index=False)
Sources: Network.py:44
The resulting CSV file has the following columns:
| Column | Description | Source File |
|---|---|---|
Count | The number of nodes within a degree bin. | Network.py:32 |
Left | The left (lower) boundary of the degree bin. | Network.py:33 |
Right | The right (upper) boundary of the degree bin. | Network.py:34 |
Log_Right | The base-10 logarithm of the right bin boundary. | Network.py:36 |
Log_Count | The base-10 logarithm of the count. | Network.py:37 |
Pred_Log_Count | The predicted log-count value from the linear regression model. | Network.py:40 |
Pred_Count | The predicted count, transformed back from logarithmic scale (10^x). | Network.py:41 |
Sources: Network.py:32-44
Data Consumption in the GUI
The main C# GUI application, defined in Form1.cs, is the primary consumer of the edgelist files generated by Barabasi.cs. The "File -> Open" menu item triggers the loading process.
The following sequence diagram shows how a user interacts with the application to load and process a network file.
This diagram details the user-initiated file loading and processing sequence.
Sources: Form1.cs:195-214, Form1.Designer.cs:157-162, Program.cs:16-21
Summary
The project employs two distinct but complementary export strategies. The C# Barabasi.cs application provides a mechanism for generating and saving the fundamental structure of a network as a tab-separated edgelist. This raw data file serves as a portable representation of the network. The Python script Network.py consumes such an edgelist, performs a specific analysis (degree distribution), and exports the derived results into a structured CSV file. This separation allows for both the persistence of the raw network data and the sharing of specific analytical findings.