128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
import os
|
|
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
import scipy.stats as stats
|
|
from scipy.stats import shapiro
|
|
|
|
|
|
def read_dimacs(file):
|
|
idx = 0
|
|
with open(f"{folder}/{file}", "r") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line.startswith("c"):
|
|
continue
|
|
elif line.startswith("p"):
|
|
_, _, n, e = line.split()
|
|
nodes = int(n)
|
|
edges = int(e)
|
|
data = [0] * edges
|
|
elif line.startswith("a"):
|
|
_, u, v, w = line.split()
|
|
data[idx] = int(w)
|
|
idx += 1
|
|
return ((nodes, edges), data)
|
|
|
|
|
|
def visualize_weights(file):
|
|
(nodes, edges), data = read_dimacs(file)
|
|
data = np.array(data)
|
|
log_data = np.log(data)
|
|
|
|
skew = stats.skew(data)
|
|
skew_log = stats.skew(log_data)
|
|
kurt = stats.kurtosis(data) # excess kurtosis (정규=0)
|
|
kurt_log = stats.kurtosis(log_data)
|
|
print(f'\n--- Skewness / Kurtosis ---')
|
|
print(f'Skewness (original): {skew:.4f} (정규 기준: 0)')
|
|
print(f'Skewness (log): {skew_log:.4f} (정규 기준: 0)')
|
|
print(f'Kurtosis (original): {kurt:.4f} (정규 기준: 0, excess)')
|
|
print(f'Kurtosis (log): {kurt_log:.4f} (정규 기준: 0, excess)')
|
|
|
|
save_folder = f"{res_folder}/{file}"
|
|
os.makedirs(save_folder, exist_ok=True)
|
|
|
|
plt.figure(figsize=(8, 5))
|
|
plt.hist(data, bins=100)
|
|
plt.title("Edge Weight Distribution (Original Scale)")
|
|
plt.xlabel("Weight")
|
|
plt.ylabel("Frequency")
|
|
plt.tight_layout()
|
|
plt.savefig(f"{save_folder}/hist_original.png")
|
|
plt.close()
|
|
|
|
plt.figure(figsize=(8, 5))
|
|
stats.probplot(data, dist="norm", plot=plt)
|
|
plt.title("Q-Q Plot (Original Scale)")
|
|
plt.xlabel("Theoretical Quantiles (Normal)")
|
|
plt.ylabel("Sample Quantiles (Weight)")
|
|
plt.savefig(f"{save_folder}/Q-Q_original.png")
|
|
plt.close()
|
|
|
|
plt.figure(figsize=(8, 5))
|
|
plt.hist(log_data, bins=100)
|
|
plt.title("Log(Weight) Distribution")
|
|
plt.xlabel("log(Weight)")
|
|
plt.ylabel("Frequency")
|
|
plt.tight_layout()
|
|
plt.savefig(f"{save_folder}/hist_log.png")
|
|
plt.close()
|
|
|
|
plt.figure(figsize=(8, 5))
|
|
stats.probplot(log_data, dist="norm", plot=plt)
|
|
plt.title("Q-Q Plot (Log Weight)")
|
|
plt.xlabel("Theoretical Quantiles (Normal)")
|
|
plt.ylabel("Sample Quantiles (Weight)")
|
|
plt.savefig(f"{save_folder}/Q-Q_log.png")
|
|
plt.close()
|
|
|
|
# plt.figure(figsize=(8, 5))
|
|
# plt.hist(log_data, bins=100, density=True)
|
|
# plt.title("Log(Weight) Density")
|
|
# plt.xlabel("log(Weight)")
|
|
# plt.ylabel("Density")
|
|
# plt.tight_layout()
|
|
# plt.savefig(f"{save_folder}/hist_log_density.png")
|
|
# plt.close()
|
|
|
|
print(f"finish {file}")
|
|
return {
|
|
"file": file,
|
|
"nodes": nodes,
|
|
"edges": edges,
|
|
"density": edges / (nodes * (nodes - 1)),
|
|
"mean": np.mean(data),
|
|
"std": np.std(data),
|
|
"min": np.min(data),
|
|
"max": np.max(data),
|
|
"skew": stats.skew(data),
|
|
"skew_log": stats.skew(log_data),
|
|
}
|
|
|
|
folder = "experiments/real_data/data/dimacs_data"
|
|
files = [
|
|
# "USA-road-d.BAY.gr",
|
|
# "USA-road-d.CAL.gr",
|
|
# "USA-road-d.COL.gr",
|
|
# "USA-road-d.CTR.gr",
|
|
# "USA-road-d.E.gr",
|
|
# "USA-road-d.FLA.gr",
|
|
# "USA-road-d.LKS.gr",
|
|
# "USA-road-d.NE.gr",
|
|
# "USA-road-d.NW.gr",
|
|
# "USA-road-d.NY.gr",
|
|
"USA-road-d.USA.gr",
|
|
# "USA-road-d.W.gr"
|
|
]
|
|
|
|
res_folder = "results/real_data/derived/dimacs_graph_distribution"
|
|
os.makedirs(res_folder, exist_ok=True)
|
|
|
|
rows = []
|
|
for file in files:
|
|
row = visualize_weights(file)
|
|
rows.append(row)
|
|
df = pd.DataFrame(rows)
|
|
df.to_csv(f"{res_folder}/distribution.csv", index=False)
|