import os import sys import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import re def main(input_directory, pattern): # Initialize an empty DataFrame to store the data df_all_deltas = pd.DataFrame() # Include the '*_DELTA_*.xml3.csv' part in the pattern full_pattern = f'{pattern}_DELTA_.*\.xmlv3\.csv' # Load all *_DELTA_*.xml3.csv files into a single DataFrame for filename in os.listdir(input_directory): if re.match(full_pattern, filename): filepath = os.path.join(input_directory, filename) df = pd.read_csv(filepath) df['Task Set'] = filename # Add a column indicating the task set df_all_deltas = pd.concat([df_all_deltas, df]) # Reset the index of the combined DataFrame df_all_deltas = df_all_deltas.reset_index(drop=True) # Extract numeric task numbers and sort them task_numbers = df_all_deltas['task'].str.extract(r'(\d+)').astype(int) df_all_deltas['num'] = task_numbers df_all_deltas = df_all_deltas.sort_values(by='num') # Dropping irrelevant columns df_all_deltas = df_all_deltas.drop(columns=['num', 'missed', 'Task Set']) # Melting the df_all_deltas into a format suitable for a boxplot df_all_deltas_melted = pd.melt(df_all_deltas, id_vars=['task'], var_name='Measure', value_name='Difference') # Converting 'Difference' column to numeric (to handle any non-numeric entries) df_all_deltas_melted['Difference'] = pd.to_numeric(df_all_deltas_melted['Difference'], errors='coerce') # Filtering out the zero values from the 'Difference' column before calculating medians df_all_deltas_melted = df_all_deltas_melted[df_all_deltas_melted['Difference'] != 0] # Creating the boxplot of differences in response times plt.figure(figsize=(12, 8)) boxplot = df_all_deltas_melted.boxplot(column='Difference', by='Measure', grid=False, medianprops={'color': 'red', 'linewidth': 2}) # Adding the median values as text for i, measure in enumerate(df_all_deltas_melted['Measure'].unique()): median_value = df_all_deltas_melted[df_all_deltas_melted['Measure'] == measure]['Difference'].median() y_position = median_value + (df_all_deltas_melted['Difference'].max() - df_all_deltas_melted['Difference'].min()) * 0.03 # Adjust position plt.text(i + 1, y_position, f'{median_value:.2f}', horizontalalignment='center', color='blue', fontsize=10, weight='semibold', backgroundcolor='white') # Increase font size for title and axes labels title_fontsize = 16 # Adjust this size as needed axes_label_fontsize = 14 # Adjust this size as needed plt.title('Response Time Differences (PCP vs PPCP)', fontsize=title_fontsize) plt.suptitle('') # Removes the automatic 'Measure' subtitle plt.xlabel('Response Time Measure', fontsize=axes_label_fontsize) plt.ylabel('Response Time Difference', fontsize=axes_label_fontsize) # Adjust the tick parameters plt.xticks(rotation=0, fontsize=12) # Adjust fontsize as needed plt.yticks(fontsize=12) # Adjust fontsize as needed # Adjusting grid and removing margins plt.grid(axis='y', linestyle='--', alpha=0.7) plt.margins(x=0, y=0) # This reduces the margins around the plot # We want the plot to be tightly fitted plt.tight_layout() # Remove special characters and use the cleaned pattern in the output filename clean_pattern = re.sub(r'[^a-zA-Z0-9_]', '', pattern) output_filename = f'response_time_diffs_by_kind_{clean_pattern}.png' # Save the plot as a PNG figure in the same directory output_filepath = os.path.join(input_directory, output_filename) plt.savefig(output_filepath) # Debug df_all_deltas_melted.to_csv(os.path.join(input_directory, f'response_time_diffs_by_kind_{clean_pattern}.csv'), index=False) # Show the plot #plt.show() if __name__ == "__main__": if len (sys.argv) < 1 or len(sys.argv) > 3: print("Usage: python script.py [ input_directory [pattern] ]") sys.exit(1) input_directory = sys.argv[1] if len(sys.argv) > 1 else '.' pattern = sys.argv[2] if len(sys.argv) > 2 else '.*' print (input_directory, pattern) main (input_directory, pattern)