#!/usr/bin/env python3 import os import re import csv from datetime import datetime import matplotlib.pyplot as plt import pandas as pd def find_measurement_folders(base_path): """ Searches for folders in the base path with the format: YYYY-MM-DD_HH:MM:SS_ptp4l_measurement Args: base_path (str): The directory where the script is located. Returns: list: A list of matching folder paths. """ folder_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2}_ptp4l_measurement$") matching_folders = [] for item in os.listdir(base_path): item_path = os.path.join(base_path, item) if os.path.isdir(item_path) and folder_pattern.match(item): matching_folders.append(item_path) return matching_folders def create_box_plots(csv_path, figures_path): """ Creates box plots for the offset values grouped by master and slave. Args: csv_path (str): Path to the CSV file. figures_path (str): Path to the folder where figures will be saved. """ 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 handle_csv_files_in_folder(folder_path): """ Handles 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}") for file in os.listdir(folder_path): if file.endswith('.csv'): csv_path = os.path.join(folder_path, file) print(f"Found CSV file: {csv_path}") create_box_plots(csv_path, figures_path) def main(): """Main function to execute the script.""" base_path = os.path.dirname(os.path.abspath(__file__)) measurement_folders = find_measurement_folders(base_path) if not measurement_folders: print("No measurement folders found.") return for folder in measurement_folders: handle_csv_files_in_folder(folder) if __name__ == "__main__": main()