StructuralAllDataMerge.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import glob
  3. import pandas as pd
  4. # Step 1: Define the starting path and file pattern
  5. # Step 1: Define the starting path and file pattern
  6. start_path = r"C:\Users\aswen\Desktop\Code\Validation3"
  7. file_pattern = '*diff*.csv'
  8. # Step 2: Find all matching CSV files in the specified directory and its subdirectories
  9. csv_files = glob.glob(os.path.join(start_path, '**', file_pattern), recursive=True)
  10. # Step 3: Initialize an empty DataFrame to store the extracted data
  11. combined_df = pd.DataFrame()
  12. # Step 4: Loop through the CSV files and extract the specified columns
  13. for csv_file in csv_files:
  14. try:
  15. df = pd.read_csv(csv_file)
  16. selected_columns = ["FileAddress", "SNR Chang", "SNR Normal", "Displacement factor (std of Mutual information)", "Goasting"]
  17. df = df[selected_columns]
  18. df["dataset"] = csv_file.split(os.sep)[-3]
  19. # Concatenate the current DataFrame with the combined DataFrame
  20. combined_df = pd.concat([combined_df, df], ignore_index=True)
  21. except Exception as e:
  22. print(f"Error reading {csv_file}: {e}")
  23. # Step 5: Calculate the mean and standard deviation for SNR Chang, SNR Normal, and Displacement factor
  24. mean_snr_chang = combined_df["SNR Chang"].mean()
  25. std_snr_chang = combined_df["SNR Chang"].std()
  26. mean_snr_normal = combined_df["SNR Normal"].mean()
  27. std_snr_normal = combined_df["SNR Normal"].std()
  28. mean_displacement_factor = combined_df["Displacement factor (std of Mutual information)"].mean()
  29. std_displacement_factor = combined_df["Displacement factor (std of Mutual information)"].std()
  30. # Print the results
  31. print(f"Mean SNR Chang: {mean_snr_chang}")
  32. print(f"Standard Deviation SNR Chang: {std_snr_chang}")
  33. print(f"Mean SNR Normal: {mean_snr_normal}")
  34. print(f"Standard Deviation SNR Normal: {std_snr_normal}")
  35. print(f"Mean Displacement Factor: {mean_displacement_factor}")
  36. print(f"Standard Deviation Displacement Factor: {std_displacement_factor}")
  37. # Optionally, you can save the combined DataFrame to a CSV file
  38. p = r"C:\Users\aswen\Desktop\Code\AIDAqc_Figures\input"
  39. combined_df.to_csv(os.path.join(p,'combined_data_diff.csv'), index=False)