correlation_between_bahavior_and_DTI.py 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #%%
  15. # Empty list to store the results
  16. results = []
  17. for dd in df["dialation_amount"].unique():
  18. # Iterate over unique values of 'Group', 'merged_timepoint', 'Qtype', and 'mask_name'
  19. for ss in df["Group"].unique():
  20. for time_point in df["merged_timepoint"].unique():
  21. for qq in df["Qtype"].unique():
  22. for mm in df["mask_name"].unique():
  23. # Filter the DataFrame for the current combination of 'Group', 'merged_timepoint', 'Qtype', and 'mask_name'
  24. df_f2 = df[(df["Group"] == ss) & (df["merged_timepoint"] == time_point) & (df["Qtype"] == qq) & (df["mask_name"] == mm) & (df["dialation_amount"] == dd)]
  25. # Remove rows with NaN values in 'Value' or 'hindlimb_dropZ-score' columns
  26. df_f2 = df_f2.dropna(subset=['Value', 'hindlimb_dropZ-score'])
  27. if not df_f2.empty:
  28. shapiro_statValue, shapiro_pvalueQValue = shapiro(df_f2["Value"])
  29. shapiro_statScore, shapiro_pvalueBehavior = shapiro(df_f2["hindlimb_dropZ-score"])
  30. if shapiro_pvalueQValue < 0.05 or shapiro_pvalueBehavior < 0.05:
  31. correlation_coefficient, p_value = pearsonr(df_f2["Value"], df_f2["hindlimb_dropZ-score"])
  32. else:
  33. correlation_coefficient, p_value = pearsonr(df_f2["Value"], df_f2["hindlimb_dropZ-score"])
  34. # Store the results in a dictionary
  35. result = {'Group': ss, 'merged_timepoint': time_point, 'Qtype': qq, 'mask_name': mm,
  36. 'Pval': p_value, 'R': correlation_coefficient,
  37. 'shapiro-wilk_pvalue_qtype': shapiro_pvalueQValue,'shapiro-wilk_pvalue_behavior': shapiro_pvalueBehavior,"dialation_amount":dd}
  38. # Append the dictionary to the results list
  39. results.append(result)
  40. else:
  41. print(
  42. f"No valid data found for Group: {ss}, merged_timepoint: {time_point}, Qtype: {qq}, and mask_name: {mm}. Skipping.")
  43. # Create a DataFrame from the results list
  44. correlation_results_df = pd.DataFrame(results)
  45. unique_groups = df["Group"].unique()
  46. unique_masks = df["mask_name"].unique()
  47. unique_tp = df["merged_timepoint"].unique()
  48. unique_dd=df["dialation_amount"].unique()
  49. for dd in unique_dd:
  50. for tt in unique_tp:
  51. for mask in unique_masks:
  52. for group in unique_groups:
  53. time_mask = correlation_results_df['merged_timepoint'] == tt
  54. mask_mask = correlation_results_df['mask_name'] == mask
  55. group_mask = correlation_results_df['Group'] == group
  56. dd_mask = correlation_results_df["dialation_amount"] == dd
  57. combined_mask = time_mask & mask_mask & group_mask & dd_mask
  58. p_values = correlation_results_df[combined_mask]['Pval']
  59. rejected, p_values_corrected, _, _ = multipletests(p_values, method='fdr_bh')
  60. # Assign the corrected p-values to the DataFrame
  61. correlation_results_df.loc[combined_mask, 'Pval_corrected'] = p_values_corrected
  62. # Define the output file path
  63. output_file_path = os.path.join(parent_dir, 'output', "Correlation_with_behavior", 'correlation_dti_with_behavior.csv')
  64. # Save the correlation results DataFrame to a CSV file
  65. correlation_results_df.to_csv(output_file_path, index=False)
  66. print("Correlation results with corrected p-values saved successfully to 'correlation_results.csv' in the output folder.")