Skip to content

2. Data input and output in Python

Kaihua Chen edited this page Nov 15, 2022 · 14 revisions

Procedures to read fMRI data in Python

Example data are at:

  1. Perspective-taking fMRI: /space_lin2/fhlin/perspective

  2. Human connectome project: /space_lin1/hcp

0. Python environment

The path of our current available Anaconda folder is: /space_lin2/kaihua/anaconda3

Activating our python environment is simple.

First, "source" the relevant profile, after which you can check if the "conda" command is available by outputting its version, as shown below:

source /space_lin2/kaihua/.bashrc
conda --version

Later, choose which virtual environment you want to utilise. Currently, there are mainly two environments: (base) and (py36), and you can activate one of them by entering "conda activate" command, as shown below:

conda activate base

conda activate py36

Different environments would contain different packages of different versions, which will meet the requirements of different tasks. Check the details of these packages by entering:

conda list

1. Read in-house fMRI data

Here are examples to read perspective-taking fMRI data in Python.

1.1 NII format

Take the NII file '/space_lin2/fhlin/perspective/subj_02/epi_data/unpack/bold/007/sfmcstc.nii' for example.

import nibabel as nb
nii_img = nb.load("/space_lin2/fhlin/perspective/subj_02/epi_data/unpack/bold/007/sfmcstc.nii")

nii_data = nii_img.get_fdata()  # shape: (72, 72, 42, 101)
affine = nii_img.affine  # affine matrix, shape: (4, 4)
hdr = nii_img.header  # header

1.2 STC format

import mne
stc = mne.read_source_estimate("/space_lin2/fhlin/perspective/subj_02/epi_data/unpack/f1/subj_02_2_fsaverage_sfmcstc-lh.stc") # <class 'mne.source_estimate.SourceEstimate'>
stc_data = stc.data # shape: (20484, 100), <class 'numpy.ndarray'>
stc_vertices = stc.vertices # shape: (2, 10242)
stc_tmin = stc.tmin # i.e., epoch_begin_latency=0.0
stc_tstep = stc.tstep # i.e., sample_period=2.0
stc_times = stc.times # shape: (87,)

2. Read Human Connectome Project data

The resulting STC files can be read by this python script file(not fully completed)

Clone this wiki locally