Creating a manifest from another tabular file for a cross-sectional study¶
In this example, we have a cross-sectional study where not all participants have the same MRI modalities (datatypes).
The CSV file example1-participants.csv
(shown below) indicates whether participants have diffusion or functional data. All participants have anatomical data.
participant |
age |
sex |
dwi |
func |
---|---|---|---|---|
001 |
25 |
F |
True |
True |
002 |
26 |
F |
False |
True |
003 |
27 |
F |
True |
False |
004 |
28 |
F |
False |
False |
005 |
29 |
M |
True |
True |
006 |
30 |
M |
False |
True |
007 |
31 |
M |
True |
True |
008 |
32 |
M |
True |
False |
Here is a script that creates a Nipoppy manifest from the above file:
Attention
The script below was written for Python 3.11 with pandas
2.2.3.
It may not work with older/different versions.
1#!/usr/bin/env python
2"""Manifest-generation script for Example 1."""
3
4from pathlib import Path
5
6import pandas as pd
7
8if __name__ == "__main__":
9
10 # get the path to the participants file
11 # we assume that it is in the same directory as this script
12 path_participants = Path(__file__).parent / "example1-participants.csv"
13
14 # load the participants file
15 # note that the participant column is read as a string because
16 # otherwise the leading zeros would be removed
17 df_participants = pd.read_csv(path_participants, dtype={"participant": str})
18
19 data_for_manifest = []
20 for _, row in df_participants.iterrows():
21
22 # no change for participant_id
23 participant_id = row["participant"]
24
25 # use the same visit_id and session_id for all participants
26 # since the study is cross-sectional
27 visit_id = 1
28 session_id = 1
29
30 # all participants have anat data
31 datatype = ["anat"]
32 if row["dwi"]:
33 datatype.append("dwi")
34 if row["func"]:
35 datatype.append("func")
36
37 # create the manifest entry
38 data_for_manifest.append(
39 {
40 "participant_id": participant_id,
41 "visit_id": visit_id,
42 "session_id": session_id,
43 "datatype": datatype,
44 }
45 )
46
47 df_manifest = pd.DataFrame(data_for_manifest)
48
49 # write the manifest in the same directory as this script
50 df_manifest.to_csv(
51 Path(__file__).parent / "example1-manifest.tsv", sep="\t", index=False
52 )
Running this script creates a manifest that looks like this:
participant_id |
visit_id |
session_id |
datatype |
---|---|---|---|
001 |
1 |
1 |
[‘anat’, ‘dwi’, ‘func’] |
002 |
1 |
1 |
[‘anat’, ‘func’] |
003 |
1 |
1 |
[‘anat’, ‘dwi’] |
004 |
1 |
1 |
[‘anat’] |
005 |
1 |
1 |
[‘anat’, ‘dwi’, ‘func’] |
006 |
1 |
1 |
[‘anat’, ‘func’] |
007 |
1 |
1 |
[‘anat’, ‘dwi’, ‘func’] |
008 |
1 |
1 |
[‘anat’, ‘dwi’] |