Skip to content

Commit 2f7dc07

Browse files
committed
enh: add routine to collate mindboggle output across participants
1 parent 54c5f0c commit 2f7dc07

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

mindboggle/mio/tables.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,73 @@ def explode_mindboggle_tables(subject_path='', output_path='.',
11041104
format(output_dir))
11051105

11061106

1107+
def short_name(filepath):
1108+
"""
1109+
Generate a short name for a given branch of the mindboggle output
1110+
1111+
Parameters
1112+
----------
1113+
filepath: str
1114+
a path to a mindboggle output file
1115+
"""
1116+
return ''.join([v[0] for v in
1117+
filepath.split('/tables/')[-1].replace('/','_').split('_')])
1118+
1119+
1120+
def fname2df(fname):
1121+
"""
1122+
Read a single csv into a single dataframe row
1123+
1124+
Parameters
1125+
----------
1126+
fname: str
1127+
a path to a mindboggle output file
1128+
"""
1129+
import numpy as np
1130+
import pandas as pd
1131+
1132+
df = pd.read_csv(fname, na_values=[0.0]).dropna(axis=0)
1133+
sn = short_name(fname)
1134+
outerproduct = [[sn+'-'+x+'-'+y.lstrip() for x in df.name] for y in
1135+
df.keys()[2:]]
1136+
outerproduct = np.array(outerproduct).flatten().tolist()
1137+
df_row = pd.DataFrame(data=df.iloc[:, 2:].values.flatten()[None, :],
1138+
columns=outerproduct, index=[0])
1139+
return df_row
1140+
1141+
1142+
def collate_participant_tables(subject_ids, base_dir):
1143+
"""
1144+
Generate a pandas dataframe across all subjects
1145+
1146+
Parameters
1147+
----------
1148+
subject_ids: list
1149+
a list of subject identifiers in
1150+
base_dir: str
1151+
path to a mindboggle output base directory (mindboggled)
1152+
1153+
1154+
>>> from mindboggle.mio import collate_participant_tables
1155+
>>> dft = collate_participant_tables(['sub-1', 'sub-2'],
1156+
... '/path/to/mindboggled/') # doctest: +SKIP
1157+
"""
1158+
from glob import glob
1159+
import os
1160+
import pandas as pd
1161+
1162+
out = None
1163+
for id in subject_ids:
1164+
fl = glob(os.path.join(base_dir, id, 'tables', '*.csv')) + \
1165+
glob(os.path.join(base_dir, id, 'tables', '*', '*.csv'))
1166+
# skip vertices outputs
1167+
dft = pd.concat([fname2df(val) for val in sorted(fl)
1168+
if 'vertices' not in val], axis=1)
1169+
dft.index = [id]
1170+
out = dft if out is None else pd.concat((out, dft), axis=0)
1171+
return out
1172+
1173+
11071174
# ============================================================================
11081175
# Doctests
11091176
# ============================================================================

0 commit comments

Comments
 (0)