remapping_voiting_qc.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import pandas as pd
  2. import os
  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. qc_csv = os.path.join(parent_dir, "output", "quality_control_aidaqc", "Votings.csv")
  8. df_qc = pd.read_csv(qc_csv)
  9. # Define a function to extract the timepoint information
  10. def extract_timepoint(path):
  11. parts = path.split(os.sep)
  12. for part in parts:
  13. if 'ses' in part:
  14. return part
  15. # Define the session mapping dictionary
  16. session_mapping = {
  17. "ses-Baseline": 0, "ses-Baseline1": 0, "ses-pre": 0,
  18. "ses-P1": 3, "ses-P2": 3, "ses-P3": 3, "ses-P4": 3, "ses-P5": 3,
  19. "ses-P6": 7, "ses-P7": 7, "ses-P8": 7, "ses-P9": 7, "ses-P10": 7,
  20. "ses-P11": 14, "ses-P12": 14, "ses-P13": 14, "ses-P14": 14,
  21. "ses-P15": 14, "ses-P16": 14, "ses-P17": 14, "ses-P18": 14,
  22. "ses-P19": 21, "ses-D21": 21, "ses-P20": 21, "ses-P21": 21,
  23. "ses-P22": 21, "ses-P23": 21, "ses-P24": 21, "ses-P25": 21,
  24. "ses-P26": 28, "ses-P27": 28, "ses-P28": 28, "ses-P29": 28,
  25. "ses-P30": 28, "ses-P42": 42, "ses-P43": 42,
  26. "ses-P56": 56, "ses-P57": 56, "ses-P58": 56,
  27. "ses-P151": 28
  28. }
  29. # Define a function to extract the subject ID
  30. def extract_subject_id(path):
  31. parts = path.split(os.sep)
  32. for part in parts:
  33. if 'sub-' in part and '.nii.' not in part:
  34. return part
  35. # Remove rows containing "brkraw" or "DN" in the "Pathes" column
  36. #df_qc = df_qc[~df_qc['Pathes'].str.contains('brkraw|DN')]
  37. # Create the "tp" column
  38. df_qc['tp'] = df_qc['Pathes'].apply(lambda x: extract_timepoint(x))
  39. # Map the timepoint using the updated session mapping directly
  40. df_qc['merged_timepoint'] = df_qc['tp'].map(session_mapping)
  41. # Add the "subjectID" column
  42. df_qc['subjectID'] = df_qc['Pathes'].apply(lambda x: extract_subject_id(x))
  43. # Save the DataFrame as a CSV file
  44. output_csv = os.path.join(parent_dir, "output", "quality_control_aidaqc", "voting_remapped.csv")
  45. df_qc.to_csv(output_csv, index=False)
  46. print("DataFrame saved as 'voting_remapped.csv'")