add_motion_figures.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import pandas as pd
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. from scipy.stats import ttest_ind
  5. import os
  6. # Setting font and font size
  7. plt.rcParams['font.family'] = 'Times New Roman'
  8. plt.rcParams['font.size'] = 8
  9. # Function to read CSV files and store them in a dictionary
  10. script_dir = os.path.dirname(__file__)
  11. dirpath = os.path.dirname(script_dir)
  12. out_path = os.path.join(dirpath, 'figures')
  13. #os.mkdir(out_path)
  14. file_address = os.path.join(dirpath, "input", "noise_and_motion", "caculated_features_func.csv")
  15. # Load CSV file into a DataFrame
  16. df = pd.read_csv(file_address)
  17. # Filter data into two groups based on the presence of keywords in the FileAddress column
  18. group_original = df[~df['FileAddress'].str.contains('mc.nii|motion_added', na=False)]
  19. group_added_motion = df[df['FileAddress'].str.contains('motion_added', na=False)]
  20. # Extract Displacement factor values for each group
  21. dfactor_original = group_original['Displacement factor (std of Mutual information)']
  22. dfactor_added_motion = group_added_motion['Displacement factor (std of Mutual information)']
  23. # Perform t-test
  24. ttest_dfactor_am_vs_original = ttest_ind(dfactor_added_motion, dfactor_original,equal_var=False)
  25. # Print t-test result
  26. print("\nT-test results for Displacement factor (Added Motion vs. Original):")
  27. print("Statistic:", ttest_dfactor_am_vs_original.statistic)
  28. print("P-value:", ttest_dfactor_am_vs_original.pvalue)
  29. cm = 1/2.53
  30. # Create boxplot for Displacement factor
  31. plt.figure(figsize=(6*cm, 4*cm),dpi=300) # Adjust the figure size as needed
  32. # Combine the data into a list
  33. data = [dfactor_added_motion,dfactor_original ]
  34. # Create boxplot
  35. sns.boxplot(data=data, fliersize=3 ,flierprops={"marker": "o"},showfliers=False,palette="Set2",width=0.6, boxprops={'zorder': 3}, linewidth=1)
  36. # Add labels to the x-axis
  37. plt.xticks([0, 1], ['Added motion', 'Original'], fontsize=8, fontname='Times New Roman')
  38. # Add title and labels
  39. plt.title('(c)', fontsize=10, fontweight='bold', fontname='Times New Roman', loc='left')
  40. plt.xlabel('Group', fontsize=8, fontname='Times New Roman')
  41. plt.ylabel('Motion Severity (a.u)', fontsize=8, fontname='Times New Roman')
  42. plt.ylim([0,0.11])
  43. # Save the plot
  44. plt.savefig(os.path.join(out_path, "displacement_factor_comparison.svg"), format='svg' ,dpi=300, bbox_inches='tight')
  45. # Show the plot
  46. plt.show()