forked from abcdqc/ABCD-s3links2BIDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_csv.py
45 lines (40 loc) · 1.5 KB
/
create_csv.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
import os, csv
raw_data_dir = '/data/MBDU/ABCDraw/'
# Create the first row of the CSV file
first_row = sorted(os.listdir(raw_data_dir))
first_row.insert(0, 'SUBJECTKEY')
# Scan all directories and collect all subject IDs
subject_keys = []
for directory in first_row:
abspath = raw_data_dir + directory
if not os.path.isdir(abspath):
continue
temp_list = os.listdir(abspath)
temp_keys = [x.split('_')[0].strip('sub-') for x in temp_list]
subject_keys += temp_keys
# Make subject keys unique
subject_keys = set(subject_keys)
# Start constructing each line of the CSV file...
with open('summary_csv.csv', 'w') as wf:
writer = csv.writer(wf, delimiter=',')
# First line containing description
writer.writerow(first_row)
''' For each subject cd into each directory and try to find
a file with matching subject ID. If a file is found append
to the subject row and go to next directory (i.e. a single file per
subject per directory is expected)...'''
for subject in subject_keys:
row = [subject]
for directory in first_row:
abspath = raw_data_dir + directory
if not os.path.isdir(abspath):
continue
file_exists = False
for files in os.listdir(abspath):
if subject in files:
row.append(files)
file_exists = True
break
if not file_exists:
row.append('N/A')
writer.writerow(row)