temp_code.py 1.1 KB

12345678910111213141516171819202122232425
  1. import os
  2. import pandas as pd
  3. # Get the directory where the code file is located
  4. code_dir = os.path.dirname(os.path.abspath(__file__))
  5. # Get the parent directory of the code directory
  6. parent_dir = os.path.dirname(code_dir)
  7. # Define the input file path
  8. input_file_path = os.path.join(parent_dir, 'output', "Correlation_with_behavior", 'correlation_dti_with_behavior.csv')
  9. # Read the CSV file
  10. df = pd.read_csv(input_file_path)
  11. # Filter the DataFrame to get the highest absolute correlation coefficient for each combination
  12. correlation_results_shortened_df = df.loc[df.groupby(['Group', 'merged_timepoint', 'Qtype', 'mask_name', 'Behavior_test'])['R'].apply(lambda x: x.abs().idxmax())]
  13. # Define the output file path for the shortened DataFrame
  14. output_file_path_shortened = os.path.join(parent_dir, 'output', "Correlation_with_behavior", 'correlation_dti_with_behavior_shortened.csv')
  15. # Save the shortened correlation results DataFrame to a CSV file
  16. correlation_results_shortened_df.to_csv(output_file_path_shortened, index=False)
  17. print("Shortened correlation results saved successfully to 'correlation_dti_with_behavior_shortened.csv' in the output folder.")