|
| 1 | +""" |
| 2 | +Collect Schaefer 2018 400-parcel cortical thickness (thickavg) values from |
| 3 | +per-subject FreeSurfer surface stats TSVs and write a wide-format summary TSV. |
| 4 | +
|
| 5 | +Input: |
| 6 | + /cbica/projects/grmpy/data/derivatives/freesurfer-post/sub-*/ |
| 7 | + sub-*_seg-Schaefer2018400Parcels7Networks_surfacestats.tsv |
| 8 | +
|
| 9 | +Output: |
| 10 | + /cbica/projects/grmpy/code/misc/Schaefer2018400Parcels7Networks_ct.tsv |
| 11 | +
|
| 12 | +Columns: participant_id | <one column per ROI (thickavg)> | mean_ct |
| 13 | +The Background+FreeSurfer_Defined_Medial_Wall ROI is excluded. |
| 14 | +""" |
| 15 | + |
| 16 | +import glob |
| 17 | +import os |
| 18 | + |
| 19 | +import pandas as pd |
| 20 | + |
| 21 | +DERIV_DIR = "/cbica/projects/grmpy/data/derivatives/freesurfer-post" |
| 22 | +TSV_GLOB = os.path.join( |
| 23 | + DERIV_DIR, "sub-*", "sub-*_seg-Schaefer2018400Parcels7Networks_surfacestats.tsv" |
| 24 | +) |
| 25 | +OUTPUT_PATH = "/cbica/projects/grmpy/code/misc/Schaefer2018400Parcels7Networks_ct.tsv" |
| 26 | +MEDIAL_WALL = "Background+FreeSurfer_Defined_Medial_Wall" |
| 27 | + |
| 28 | + |
| 29 | +def process_subject(tsv_path: str) -> pd.Series | None: |
| 30 | + df = pd.read_csv(tsv_path, sep="\t") |
| 31 | + |
| 32 | + df = df[df["structname"] != MEDIAL_WALL].copy() |
| 33 | + |
| 34 | + if df.empty: |
| 35 | + print(f" WARNING: no ROIs remaining after filtering medial wall in {tsv_path}") |
| 36 | + return None |
| 37 | + |
| 38 | + participant_id = df["participant_id"].iloc[0] |
| 39 | + |
| 40 | + roi_series = df.set_index("structname")["thickavg"] |
| 41 | + roi_series.name = None |
| 42 | + |
| 43 | + result = pd.Series({"participant_id": participant_id}) |
| 44 | + result = pd.concat([result, roi_series]) |
| 45 | + result["mean_ct"] = roi_series.mean() |
| 46 | + |
| 47 | + return result |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + tsv_files = sorted(glob.glob(TSV_GLOB)) |
| 52 | + |
| 53 | + if not tsv_files: |
| 54 | + raise FileNotFoundError( |
| 55 | + f"No TSV files found matching pattern:\n {TSV_GLOB}\n" |
| 56 | + "Check that the derivatives directory is mounted and the file naming is correct." |
| 57 | + ) |
| 58 | + |
| 59 | + print(f"Found {len(tsv_files)} subject TSV(s). Processing...") |
| 60 | + |
| 61 | + rows = [] |
| 62 | + for path in tsv_files: |
| 63 | + print(f" {os.path.basename(os.path.dirname(path))}") |
| 64 | + row = process_subject(path) |
| 65 | + if row is not None: |
| 66 | + rows.append(row) |
| 67 | + |
| 68 | + if not rows: |
| 69 | + raise RuntimeError("No valid data was collected. Output not written.") |
| 70 | + |
| 71 | + output_df = pd.DataFrame(rows).reset_index(drop=True) |
| 72 | + |
| 73 | + cols = ( |
| 74 | + ["participant_id"] |
| 75 | + + [c for c in output_df.columns if c not in ("participant_id", "mean_ct")] |
| 76 | + + ["mean_ct"] |
| 77 | + ) |
| 78 | + output_df = output_df[cols] |
| 79 | + |
| 80 | + os.makedirs(os.path.dirname(OUTPUT_PATH), exist_ok=True) |
| 81 | + output_df.to_csv(OUTPUT_PATH, sep="\t", index=False) |
| 82 | + |
| 83 | + print( |
| 84 | + f"\nDone. {len(output_df)} participant(s), {len(output_df.columns) - 2} ROI columns." |
| 85 | + ) |
| 86 | + print(f"Output written to:\n {OUTPUT_PATH}") |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments