Setting up the Python Environment
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the necessary Python environment setup for running the analysis and simulation scripts within the Network-Science repository. The project includes Python scripts for analyzing network degree distributions and simulating population changes under selection pressure. Proper setup requires installing several third-party libraries that provide functionality for data manipulation, numerical computation, and plotting.
The primary Python scripts are Network.py, which analyzes an edge list to calculate and plot degree distribution, and Selection.py, which simulates population genetics. These scripts rely on a common set of scientific computing libraries. The data format, a tab-separated edge list, is consistent with the C# components of the project, which can be used to generate these files.
Sources: README.md, Network.py, Selection.py, Barabasi.cs
Python Dependencies
To execute the Python scripts (Network.py, Selection.py), a Python environment with specific scientific computing libraries is required. There is no requirements.txt file, so dependencies must be identified from the import statements in the source files.
Required Libraries
The following table summarizes the libraries imported by the Python scripts and their roles in the project.
| Library | Used In | Purpose in Project |
|---|---|---|
pandas | Network.py, Selection.py | Used for reading input data (e.g., edgelist.txt) into DataFrames and for data manipulation. Sources: Network.py:1, Selection.py:4 |
numpy | Network.py, Selection.py | Performs numerical operations, such as creating logarithmically spaced bins for histograms and generating normally distributed random numbers for simulations. Sources: Network.py:3, Selection.py:3 |
scipy | Network.py | Used for statistical analysis, specifically for performing linear regression on the log-binned degree distribution to find the scaling exponent (gamma). Sources: Network.py:11 |
bokeh | Network.py | Creates interactive, browser-based visualizations. It is used to plot the degree distribution and the regression line, outputting an HTML file. Sources: Network.py:4 |
matplotlib | Selection.py | Used for creating and displaying static plots of the population trait distribution across generations. Sources: Selection.py:5 |
seaborn | Selection.py | Built on top of matplotlib, it is used to create more aesthetic statistical plots, specifically the histogram of surviving vs. non-surviving individuals. Sources: Selection.py:6 |
math | Network.py | Provides basic mathematical functions. Sources: Network.py:2 |
Installation
These libraries can be installed using pip:
pip install pandas numpy scipy bokeh matplotlib seaborn
Dependency Relationship Diagram
This diagram illustrates which script depends on which library.
Sources: Network.py:1-11, Selection.py:3-6
Data Input and Format
The Python scripts, particularly Network.py, are designed to process network data from an edge list file.
Edge List File
Network.py expects a tab-separated text file containing pairs of node identifiers that represent the edges of a network. The script is configured to read a file named www.edgelist.txt by default.
Sources: Network.py:12, 14
# Network.py
dataset = "www.edgelist.txt"
...
edgelist = pd.read_csv('F:\\Math\\Network Science\\' + dataset, sep='\t', lineterminator='\n', names=['A', 'B'])
Sources: Network.py:12, 14-15
This tab-separated format is a common convention within the repository, as the C# network generation (Barabasi.cs) and analysis (Form1.cs) components also produce and consume files in this format.
Sources: Barabasi.cs:100, Form1.cs:214
Script Execution and Output
The Python scripts are standalone and can be run directly from the command line.
Network.py Execution Flow
This script performs a full analysis pipeline: reading data, calculating degree distribution, performing regression, and generating output files.
The data processing and analysis flow in Network.py is as follows:
Sources: Network.py:14-77
Outputs:
- CSV File: A file named
<dataset>.histo.csvcontaining the binned degree distribution data, including the regression predictions.
Sources: Network.py:16, 51 - HTML File: An interactive chart named
chart.htmgenerated by Bokeh, showing the log-log degree distribution plot.
Sources: Network.py:73, 77
Selection.py Execution Flow
This script runs a simulation and visualizes the results in real-time.
- Initialization: Sets initial population parameters like size, trait mean, and standard deviation.
Sources: Selection.py:10-14 - Generational Loop: Iterates for a specified number of generations.
Sources: Selection.py:20 - Population Generation: In each generation, a new population with traits following a normal distribution is created as a pandas DataFrame.
Sources: Selection.py:22-23 - Selection: The individuals with the lowest trait values are culled based on a
PREDATIONrate.
Sources: Selection.py:26-27 - Visualization: A histogram of the population's traits is plotted using
seabornandmatplotlib, distinguishing between surviving and culled individuals. The plot is displayed immediately.
Sources: Selection.py:28-32 - Reproduction: The surviving population grows, and the new trait mean is calculated for the next generation.
Sources: Selection.py:33-36
Output:
- Plots: A series of plots are displayed on-screen, one for each generation, showing the shift in the population's trait distribution.
Sources: Selection.py:32