correlation_between_bahavior_and_DTI.py 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import pandas as pd
  3. from scipy.stats import spearmanr, shapiro, pearsonr
  4. import numpy as np
  5. from statsmodels.stats.multitest import multipletests
  6. # Get the directory where the code file is located
  7. code_dir = os.path.dirname(os.path.abspath(__file__))
  8. # Get the parent directory of the code directory
  9. parent_dir = os.path.dirname(code_dir)
  10. # Step 4: Save the resulting dataframe to a CSV file
  11. input_file_path = os.path.join(parent_dir, 'output', "Final_Quantitative_output", 'Quantitative_results_from_dwi_processing_merged_with_behavior_data.csv')
  12. df = pd.read_csv(input_file_path)
  13. df = df[(df["merged_timepoint"]<40)]
  14. tests = ['paw_dragZ-score', 'hindlimb_dropZ-score', 'foot_faultsZ-score','paw_drag', 'hindlimb_drop', 'foot_faults',"averageScore"]
  15. #%%
  16. # Empty list to store the results
  17. results = []
  18. for tt in tests:
  19. for dd in df["dialation_amount"].unique():
  20. # Iterate over unique values of 'Group', 'merged_timepoint', 'Qtype', and 'mask_name'
  21. for ss in df["Group"].unique():
  22. for time_point in df["merged_timepoint"].unique():
  23. for qq in df["Qtype"].unique():
  24. for mm in df["mask_name"].unique():
  25. # Filter the DataFrame for the current combination of 'Group', 'merged_timepoint', 'Qtype', and 'mask_name'
  26. df_f2 = df[(df["Group"] == ss) & (df["merged_timepoint"] == time_point) & (df["Qtype"] == qq) & (df["mask_name"] == mm) & (df["dialation_amount"] == dd)]
  27. # Remove rows with NaN values in 'Value' or 'averageScore' columns
  28. df_f2 = df_f2.dropna(subset=['Value', tt])
  29. if not df_f2.empty:
  30. shapiro_statValue, shapiro_pvalueQValue = shapiro(df_f2["Value"])
  31. shapiro_statScore, shapiro_pvalueBehavior = shapiro(df_f2[tt])
  32. if shapiro_pvalueQValue < 0.05 or shapiro_pvalueBehavior < 0.05:
  33. correlation_coefficient, p_value = pearsonr(df_f2["Value"], df_f2[tt])
  34. else:
  35. correlation_coefficient, p_value = pearsonr(df_f2["Value"], df_f2[tt])
  36. # Store the results in a dictionary
  37. result = {'Group': ss, 'merged_timepoint': time_point, 'Qtype': qq, 'mask_name': mm,
  38. 'Pval': p_value, 'R': correlation_coefficient,
  39. "Behavior_test":tt,"dialation_amount":dd}
  40. # Append the dictionary to the results list
  41. results.append(result)
  42. else:
  43. print(
  44. f"No valid data found for Group: {ss}, merged_timepoint: {time_point}, Qtype: {qq}, and mask_name: {mm}. Skipping.")
  45. # Create a DataFrame from the results list
  46. correlation_results_df = pd.DataFrame(results)
  47. unique_groups = df["Group"].unique()
  48. unique_masks = df["mask_name"].unique()
  49. unique_tp = df["merged_timepoint"].unique()
  50. unique_dd=df["dialation_amount"].unique()
  51. # =============================================================================
  52. # for dd in unique_dd:
  53. # for tt in unique_tp:
  54. # for mask in unique_masks:
  55. # for group in unique_groups:
  56. # time_mask = correlation_results_df['merged_timepoint'] == tt
  57. # mask_mask = correlation_results_df['mask_name'] == mask
  58. # group_mask = correlation_results_df['Group'] == group
  59. # dd_mask = correlation_results_df["dialation_amount"] == dd
  60. # combined_mask = time_mask & mask_mask & group_mask & dd_mask
  61. #
  62. # p_values = correlation_results_df[combined_mask]['Pval']
  63. # rejected, p_values_corrected, _, _ = multipletests(p_values, method='fdr_bh')
  64. #
  65. # # Assign the corrected p-values to the DataFrame
  66. # correlation_results_df.loc[combined_mask, 'Pval_corrected'] = p_values_corrected
  67. # =============================================================================
  68. # Define the output file path
  69. output_file_path = os.path.join(parent_dir, 'output', "Correlation_with_behavior", 'correlation_dti_with_behavior.csv')
  70. # Save the correlation results DataFrame to a CSV file
  71. correlation_results_df.to_csv(output_file_path, index=False)
  72. print("Correlation results with corrected p-values saved successfully to 'correlation_results.csv' in the output folder.")