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    # get the path to the participants file
10    # we assume that it is in the same directory as this script
11    path_participants = Path(__file__).parent / "example1-participants.csv"
12
13    # load the participants file
14    # note that the participant column is read as a string because
15    # otherwise the leading zeros would be removed
16    df_participants = pd.read_csv(path_participants, dtype={"participant": str})
17
18    data_for_manifest = []
19    for _, row in df_participants.iterrows():
20        # no change for participant_id
21        participant_id = row["participant"]
22
23        # use the same visit_id and session_id for all participants
24        # since the study is cross-sectional
25        visit_id = 1
26        session_id = 1
27
28        # all participants have anat data
29        datatype = ["anat"]
30        if row["dwi"]:
31            datatype.append("dwi")
32        if row["func"]:
33            datatype.append("func")
34
35        # create the manifest entry
36        data_for_manifest.append(
37            {
38                "participant_id": participant_id,
39                "visit_id": visit_id,
40                "session_id": session_id,
41                "datatype": datatype,
42            }
43        )
44
45    df_manifest = pd.DataFrame(data_for_manifest)
46
47    # write the manifest in the same directory as this script
48    df_manifest.to_csv(
49        Path(__file__).parent / "example1-manifest.tsv", sep="\t", index=False
50    )

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’]