-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_chunks.py
66 lines (52 loc) · 2.41 KB
/
aggregate_chunks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding: utf-8
#
# Takes CSV as input, and, for each subject (defined by the folder "sub-*/"), aggregates the lines that share
# the same vertebral level. The aggregation would be done using weighted average, the weighting being driven
# by the column "Size [vox]".
#
# Author: Julien Cohen-Adad
import argparse
import os
import pandas as pd
def aggregate_data(input_csv, output_csv):
# Load the CSV file
df = pd.read_csv(input_csv, sep=',')
# Extract subject information ("sub-XXX"), assuming the subject identifier is between "sub-" and "_"
df['Subject'] = df['Filename'].str.extract(r'(sub-[^_/]+)')
# Ensure VertLevel is treated as string
df['VertLevel'] = df['VertLevel'].astype(str)
# Split vertebral level ranges into separate rows
df = df.drop('VertLevel', axis=1).join(
df['VertLevel'].str.split(':', expand=True).stack().reset_index(level=1, drop=True).rename('VertLevel')
)
df['VertLevel'] = df['VertLevel'].astype(int)
# Group by subject and vertebral level, then calculate weighted averages
aggregated_df = df.groupby(['Subject', 'VertLevel']).apply(
lambda x: pd.Series({
'Size [vox]': x['Size [vox]'].sum(),
'MAP()': (x['Size [vox]'] * x['MAP()']).sum() / x['Size [vox]'].sum(),
'STD()': (x['Size [vox]'] * x['STD()']).sum() / x['Size [vox]'].sum(),
**x.iloc[0][['Timestamp', 'SCT Version', 'Filename', 'Label']].to_dict()
})
).reset_index()
# Reorder the columns as specified
ordered_columns = ['Subject', 'VertLevel', 'Label', 'Size [vox]', 'MAP()', 'STD()', 'Timestamp', 'Filename', 'SCT Version']
aggregated_df = aggregated_df[ordered_columns]
# Save the aggregated data to a new CSV file
aggregated_df.to_csv(output_csv, sep=',', index=False)
def main():
parser = argparse.ArgumentParser(description='Aggregate MRI data.')
parser.add_argument('input_csv', type=str, help='Path to the input CSV file.')
parser.add_argument('--output-csv', type=str, help='Path to the output CSV file.')
args = parser.parse_args()
input_csv = args.input_csv
if args.output_csv:
output_csv = args.output_csv
else:
base, ext = os.path.splitext(input_csv)
output_csv = f"{base}_aggregated{ext}"
aggregate_data(input_csv, output_csv)
print(f"Aggregated data saved to {output_csv}")
if __name__ == '__main__':
main()