#!/usr/bin/env python3 import os import re import csv import sys from datetime import datetime import matplotlib.pyplot as plt import pandas as pd # Get the directory of the current script script_folder = os.path.dirname(os.path.abspath(__file__)) print(f"Script folder: {script_folder}") # Find all CSV files in the current directory. def find_csv_files(script_path): return [f for f in os.listdir(script_path) if os.path.isfile(os.path.join(script_path, f)) and f.endswith('.csv')] # Creates box plots for the offset values grouped by master and slave. def create_box_plots(csv_path, figures_path): data = pd.read_csv(csv_path, sep=';', header=None, names=['master', 'slave', 'mesh_combination', 'total_combinations', 'offset', 'state', 'frequency', 'path_delay']) masters = data['master'].unique() for master in masters: master_data = data[data['master'] == master] boxplot_data = [] slave_labels = [] for slave in master_data['slave'].unique(): slave_data = master_data[master_data['slave'] == slave]['offset'] boxplot_data.append(slave_data) slave_labels.append(f"Slave {slave}") plt.figure(figsize=(10, 6)) plt.boxplot(boxplot_data, labels=slave_labels, patch_artist=True) plt.title(f"Offset Box Plot for Master {master}") plt.xlabel("Slaves") plt.ylabel("Offset") plt.grid(True) plot_path = os.path.join(figures_path, f"offset_boxplot_master_{master}.png") plt.savefig(plot_path) plt.close() print(f"Saved box plot: {plot_path}") def sort_data_by_distance(all_data): """ Sorts all data into different lists based on node distance across all CSV files. Args: all_data (list of DataFrame): List of data frames from all CSV files. Returns: dict: A dictionary with distances as keys and corresponding data as values. """ 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 handle_csv_files_in_folder(folder_path): """ Handles all CSV files within a folder and ensures a "figures" subfolder exists. Args: folder_path (str): Path to the folder containing CSV files. """ print(f"Handling CSV files in folder: {folder_path}") figures_path = os.path.join(folder_path, "figures") if not os.path.exists(figures_path): os.makedirs(figures_path) print(f"Created folder: {figures_path}") else: print("Folder 'figures' already exists") 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) print(f"Reading CSV file: {csv_path}") 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 = [] sorted_data = sort_data_by_distance(all_data) print(sorted_data) # Converts a node number to its grid coordinates (5 by 5 APU mesh grid). def get_coordinates(node): return node // 5, node % 5 # Calculate the Chebyshev distance between two points. def chebyshev_distance(x1, y1, x2, y2): return max(abs(x1 - x2), abs(y1 - y2)) # Calculate the Chebyshev distance between two nodes in a grid. def node_distance(base_node, opposite_node): 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 main(): """Main function to execute the script with a specified folder path or default to the script's directory.""" 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.") print(f"Using path: {base_path}") handle_csv_files_in_folder(base_path) if __name__ == "__main__": main()