#!/usr/bin/env python3 import os import pandas as pd import matplotlib.pyplot as plt def sort_data_by_distance(all_data): """ Sorts all data into different lists based on node distance across all CSV files. """ sorted_data = {} for data in all_data: for _, row in data.iterrows(): distance = node_distance(row['master'], row['slave']) if distance not in sorted_data: sorted_data[distance] = [] sorted_data[distance].append(row) for key in sorted_data: sorted_data[key] = pd.DataFrame(sorted_data[key]) return sorted_data def get_coordinates(node): """Convert a node number to grid coordinates in a 5x5 grid.""" return node // 5, node % 5 def chebyshev_distance(x1, y1, x2, y2): """Calculate the Chebyshev distance between two points.""" return max(abs(x1 - x2), abs(y1 - y2)) def node_distance(base_node, opposite_node): """Calculate the Chebyshev distance between two nodes in a grid.""" base_node_x, base_node_y = get_coordinates(base_node) opposite_node_x, opposite_node_y = get_coordinates(opposite_node) return chebyshev_distance(base_node_x, base_node_y, opposite_node_x, opposite_node_y) def filter_one_hop_data(sorted_data): """Extract data for one-hop (distance = 1) connections and filter out extreme offset values.""" one_hop_data = sorted_data.get(1, None) if one_hop_data is not None: one_hop_data = one_hop_data[one_hop_data['offset'].abs() <= 5000] return one_hop_data def log_data(data, log_file): """Log the used data into a log file.""" with open(log_file, "w") as f: f.write(data.to_string()) print(f"Logged data to: {log_file}") def plot_one_hop_offsets(one_hop_data, figures_path): """Generate a box plot for one-hop connections based on offset values.""" if one_hop_data is None or one_hop_data.empty: print("No data available for one-hop connections.") return log_data(one_hop_data, os.path.join(figures_path, "one_hop_data.log")) plt.figure(figsize=(12, 6)) one_hop_data.boxplot(column='offset', by='master', grid=True) plt.title("Offset Box Plot for One-Hop Connections") plt.suptitle("") plt.xlabel("Master Node") plt.ylabel("Offset") plt.xticks(rotation=45) plot_path = os.path.join(figures_path, "offset_boxplot_one_hop.png") plt.savefig(plot_path) plt.close() print(f"Saved one-hop plot: {plot_path}") def handle_csv_files_in_folder(folder_path): """Handles all CSV files and generates a box plot for one-hop connections.""" figures_path = os.path.join(folder_path, "figures") os.makedirs(figures_path, exist_ok=True) all_data = [] csv_files = [file for file in os.listdir(folder_path) if file.endswith('.csv')] if not csv_files: print("No CSV files found in folder.") return for file in csv_files: csv_path = os.path.join(folder_path, file) data = pd.read_csv(csv_path, sep=';', header=None, names=['master', 'slave', 'mesh_combination', 'total_combinations', 'offset', 'state', 'frequency', 'path_delay']) data['filename'] = file all_data.append(data) sorted_data = sort_data_by_distance(all_data) one_hop_data = filter_one_hop_data(sorted_data) plot_one_hop_offsets(one_hop_data, figures_path) def main(): import sys if len(sys.argv) == 2: base_path = os.path.abspath(sys.argv[1]) else: base_path = os.path.dirname(os.path.abspath(__file__)) print("No path provided, using script directory.") handle_csv_files_in_folder(base_path) if __name__ == "__main__": main()