Note
Click here to download the full example code
Downloading KFTools Example Data¶
#
Importage¶
# KF Tools and related imports
from kftools.data import load_info,fetch_file
from mne.io import read_raw_snirf
# Some neuroimaging library things
import nibabel as nib
from nilearn.plotting import plot_stat_map
from nilearn.image import index_img
# Some generic things
from matplotlib import pyplot as plt
import os
import pandas as pd
Specify data download location¶
Give a specific location data_dir = ‘/external/rprshnas01/netdata_kcni/jglab/Data/kftools_data’ or maybe data_dir = ‘.’
If data_dir=None the default location is used, which is ‘~/.kftools’ data_dir=None
data_dir = '.'
List available files¶
info = load_info()
info[['fname', 'site','subid', 'task', 'sesid', 'datetime', 'filetype']]
fname | site | subid | task | sesid | datetime | filetype | |
---|---|---|---|---|---|---|---|
0 | snic_sub001_ft_ses01_0909-1523_kp-snf-mom.snirf | snic | sub001 | ft | ses01 | 0909-1523 | kp-snf-mom |
1 | snic_sub001_ft_ses02_0917-1313_kp-snf-mom.snirf | snic | sub001 | ft | ses02 | 0917-1313 | kp-snf-mom |
2 | snic_sub001_ft_ses01_0909-1523_kp-snf-hbm.snirf | snic | sub001 | ft | ses01 | 0909-1523 | kp-snf-hbm |
3 | snic_sub001_ft_ses02_0917-1313_kp-snf-hbm.snirf | snic | sub001 | ft | ses02 | 0917-1313 | kp-snf-hbm |
4 | snic_sub001_ft_ses01_0909-1523_kp-nii-hbo.nii.gz | snic | sub001 | ft | ses01 | 0909-1523 | kp-nii-hbo |
5 | snic_sub001_ft_ses02_0917-1313_kp-nii-hbo.nii.gz | snic | sub001 | ft | ses02 | 0917-1313 | kp-nii-hbo |
6 | snic_sub001_ft_ses01_0909-1523_kp-nii-hbr.nii.gz | snic | sub001 | ft | ses01 | 0909-1523 | kp-nii-hbr |
7 | snic_sub001_ft_ses02_0917-1313_kp-nii-hbr.nii.gz | snic | sub001 | ft | ses02 | 0917-1313 | kp-nii-hbr |
8 | snic_sub001_ft_ses01_0909-1523_kp-nii-evs.tsv | snic | sub001 | ft | ses01 | 0909-1523 | kp-nii-evs |
9 | snic_sub001_ft_ses02_0917-1313_kp-nii-evs.tsv | snic | sub001 | ft | ses02 | 0917-1313 | kp-nii-evs |
10 | snic_sub008_rec_ses02_0920-1711_kp-nii-hbo.nii.gz | snic | sub008 | rec | ses02 | 0920-1711 | kp-nii-hbo |
11 | pitch_sub010_ft_ses01_1017-1706_kp-snf-mom.snirf | pitch | sub010 | ft | ses01 | 1017-1706 | kp-snf-mom |
12 | pitch_sub010_ft_ses01_1017-1706_kp-snf-hbm.snirf | pitch | sub010 | ft | ses01 | 1017-1706 | kp-snf-hbm |
13 | pitch_sub010_ft_ses01_1017-1706_kp-nii-hbo.nii.gz | pitch | sub010 | ft | ses01 | 1017-1706 | kp-nii-hbo |
14 | pitch_sub010_ft_ses01_1017-1706_kp-nii-hbr.nii.gz | pitch | sub010 | ft | ses01 | 1017-1706 | kp-nii-hbr |
15 | pitch_sub010_ft_ses01_1017-1706_kp-nii-evs.tsv | pitch | sub010 | ft | ses01 | 1017-1706 | kp-nii-evs |
16 | snic_sub006_reo_ses01_1101-1810_kp-nii-hbo.nii.gz | snic | sub006 | reo | ses01 | 1101-1810 | kp-nii-hbo |
17 | snic_sub006_reo_ses01_1101-1810_kp-nii-hbr.nii.gz | snic | sub006 | reo | ses01 | 1101-1810 | kp-nii-hbr |
18 | snic_sub006_reo_ses02_1116-1655_kp-nii-hbo.nii.gz | snic | sub006 | reo | ses02 | 1116-1655 | kp-nii-hbo |
19 | snic_sub006_reo_ses02_1116-1655_kp-nii-hbr.nii.gz | snic | sub006 | reo | ses02 | 1116-1655 | kp-nii-hbr |
20 | snic_sub006_reo_ses03_1117-1740_kp-nii-hbo.nii.gz | snic | sub006 | reo | ses03 | 1117-1740 | kp-nii-hbo |
21 | snic_sub006_reo_ses03_1117-1740_kp-nii-hbr.nii.gz | snic | sub006 | reo | ses03 | 1117-1740 | kp-nii-hbr |
22 | snic_sub006_reo_ses01_1101-1810_kp-snf-hmb.snirf | snic | sub006 | reo | ses01 | 1101-1810 | kp-snf-hmb |
23 | snic_sub006_reo_ses01_1101-1810_kp-snf-mom.snirf | snic | sub006 | reo | ses01 | 1101-1810 | kp-snf-mom |
24 | snic_sub006_reo_ses02_1116-1655_kp-snf-hmb.snirf | snic | sub006 | reo | ses02 | 1116-1655 | kp-snf-hmb |
25 | snic_sub006_reo_ses02_1116-1655_kp-snf-mom.snirf | snic | sub006 | reo | ses02 | 1116-1655 | kp-snf-mom |
26 | snic_sub006_reo_ses03_1117-1740_kp-snf-hmb.snirf | snic | sub006 | reo | ses03 | 1117-1740 | kp-snf-hmb |
27 | snic_sub006_reo_ses03_1117-1740_kp-snf-mom.snirf | snic | sub006 | reo | ses03 | 1117-1740 | kp-snf-mom |
HB Moments file¶
The kernel portal gives two .snirf file options: 1. The ‘raw’ moments file 2. A ‘Hb moments’ file
The Hb moments file has some initial preprocessing applied to it, including optical density and modified beer lambert law calculations.
fetch_file(data_dir=data_dir, filetype='kp-snf-hbm',
site='snic', task='ft', subid='sub001', sesid='ses01')
raw = read_raw_snirf('snic_sub001_ft_ses01_0909-1523_kp-snf-hbm.snirf')
df_raw = raw.to_data_frame()
fig, ax = plt.subplots(figsize=(12,3))
df_raw[raw.ch_names[0:5]].loc[3000:].plot(ax=ax)
Out:
Downloading snic_sub001_ft_ses01_0909-1523_kp-snf-hbm.snirf
Downloading...
From: https://drive.google.com/uc?id=11Lcv-dczig_SLGHcyl8hstE3IS5R8oXO
To: /home/runner/work/kernel-flow-tools/kernel-flow-tools/examples/snic_sub001_ft_ses01_0909-1523_kp-snf-hbm.snirf
0%| | 0.00/259M [00:00<?, ?B/s]
0%| | 524k/259M [00:00<01:26, 2.97MB/s]
2%|2 | 5.24M/259M [00:00<00:11, 22.6MB/s]
4%|4 | 11.5M/259M [00:00<00:08, 30.4MB/s]
7%|6 | 17.3M/259M [00:00<00:06, 38.0MB/s]
8%|8 | 21.5M/259M [00:00<00:07, 30.8MB/s]
11%|# | 28.3M/259M [00:00<00:07, 32.8MB/s]
14%|#3 | 35.1M/259M [00:01<00:05, 38.6MB/s]
17%|#6 | 43.0M/259M [00:01<00:05, 36.4MB/s]
20%|#9 | 51.4M/259M [00:01<00:04, 41.9MB/s]
23%|##2 | 59.2M/259M [00:01<00:04, 45.8MB/s]
26%|##6 | 67.6M/259M [00:01<00:03, 48.2MB/s]
29%|##9 | 76.0M/259M [00:01<00:03, 51.7MB/s]
33%|###2 | 84.4M/259M [00:02<00:03, 54.6MB/s]
36%|###5 | 92.8M/259M [00:02<00:02, 56.5MB/s]
39%|###9 | 101M/259M [00:02<00:02, 56.3MB/s]
42%|####2 | 110M/259M [00:02<00:02, 55.8MB/s]
46%|####5 | 118M/259M [00:02<00:02, 56.1MB/s]
49%|####9 | 127M/259M [00:02<00:02, 64.5MB/s]
52%|#####2 | 135M/259M [00:02<00:02, 53.8MB/s]
55%|#####5 | 143M/259M [00:03<00:01, 58.1MB/s]
59%|#####8 | 152M/259M [00:03<00:01, 64.9MB/s]
62%|######1 | 160M/259M [00:03<00:01, 66.0MB/s]
65%|######5 | 168M/259M [00:03<00:01, 68.5MB/s]
68%|######8 | 177M/259M [00:03<00:01, 64.2MB/s]
72%|#######1 | 185M/259M [00:03<00:01, 66.1MB/s]
75%|#######4 | 193M/259M [00:03<00:01, 64.8MB/s]
78%|#######7 | 202M/259M [00:03<00:00, 64.8MB/s]
81%|########1 | 210M/259M [00:03<00:00, 66.2MB/s]
84%|########4 | 219M/259M [00:04<00:00, 62.1MB/s]
88%|########7 | 227M/259M [00:04<00:00, 62.8MB/s]
91%|######### | 235M/259M [00:04<00:00, 65.5MB/s]
94%|#########4| 244M/259M [00:04<00:00, 67.4MB/s]
99%|#########8| 256M/259M [00:04<00:00, 80.5MB/s]
100%|##########| 259M/259M [00:04<00:00, 55.8MB/s]
Loading /home/runner/work/kernel-flow-tools/kernel-flow-tools/examples/snic_sub001_ft_ses01_0909-1523_kp-snf-hbm.snirf
<AxesSubplot:>
Nifti file¶
In-the-brain, DOT-reconstructed HbO and HbR time series are provided in the form of 4D nifti images.
fetch_file(data_dir=data_dir, filetype='kp-nii-hbo',
site='snic', task='ft', subid='sub001', sesid='ses01')
f = 'snic_sub001_ft_ses01_0909-1523_kp-nii-hbo.nii.gz'
imgs = nib.load(f)
img = index_img(imgs,0)
disp = plot_stat_map(img)
Out:
Downloading snic_sub001_ft_ses01_0909-1523_kp-nii-hbo.nii.gz
Downloading...
From: https://drive.google.com/uc?id=16F2jq5QT-gzbOMyzUU15bD6Uqd3qbh43
To: /home/runner/work/kernel-flow-tools/kernel-flow-tools/examples/snic_sub001_ft_ses01_0909-1523_kp-nii-hbo.nii.gz
0%| | 0.00/35.6M [00:00<?, ?B/s]
4%|4 | 1.57M/35.6M [00:00<00:02, 15.4MB/s]
19%|#9 | 6.82M/35.6M [00:00<00:00, 36.8MB/s]
31%|### | 11.0M/35.6M [00:00<00:00, 32.9MB/s]
49%|####8 | 17.3M/35.6M [00:00<00:00, 24.0MB/s]
72%|#######2 | 25.7M/35.6M [00:00<00:00, 27.7MB/s]
96%|#########5| 34.1M/35.6M [00:01<00:00, 36.1MB/s]
100%|##########| 35.6M/35.6M [00:01<00:00, 33.4MB/s]
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/nilearn/plotting/img_plotting.py:300: FutureWarning: Default resolution of the MNI template will change from 2mm to 1mm in version 0.10.0
anat_img = load_mni152_template()
Note that for task-based statistical models, there is a separate and modified events file, that contains the same information as can be pulled directly from the .snirf files (see later examples), but with modified timings.
fetch_file(data_dir=data_dir, filetype='kp-nii-evs',
site='snic', task='ft', subid='sub001', sesid='ses01')
f = 'snic_sub001_ft_ses01_0909-1523_kp-nii-evs.tsv'
df = pd.read_csv(f, sep='\t')
df
Out:
Downloading snic_sub001_ft_ses01_0909-1523_kp-nii-evs.tsv
Downloading...
From: https://drive.google.com/uc?id=1DXUIQcbVJg3dc4WAiC7xStbweF9v_4V1
To: /home/runner/work/kernel-flow-tools/kernel-flow-tools/examples/snic_sub001_ft_ses01_0909-1523_kp-nii-evs.tsv
0%| | 0.00/12.0k [00:00<?, ?B/s]
100%|##########| 12.0k/12.0k [00:00<00:00, 33.3MB/s]
timestamp | event | duration | experiment_type | block | block_type | trial | trial_type | |
---|---|---|---|---|---|---|---|---|
0 | 0.000000 | start_experiment | 482.688649 | finger_tapping | NaN | NaN | NaN | NaN |
1 | 0.027455 | start_rest | 20.016757 | finger_tapping | NaN | NaN | NaN | NaN |
2 | 20.044290 | start_block | 72.990794 | finger_tapping | 1.0 | left | NaN | NaN |
3 | 20.063716 | start_ITI | 0.744280 | finger_tapping | 1.0 | left | NaN | NaN |
4 | 20.808075 | start_trial | 5.008220 | finger_tapping | 1.0 | left | 1.0 | index |
... | ... | ... | ... | ... | ... | ... | ... | ... |
145 | 461.571315 | start_ITI | 0.442044 | finger_tapping | 5.0 | left | NaN | NaN |
146 | 462.037850 | start_rest | 20.018416 | finger_tapping | NaN | NaN | NaN | NaN |
147 | 482.056380 | start_block | 0.632269 | finger_tapping | 6.0 | right | NaN | NaN |
148 | 482.075120 | start_ITI | 0.600871 | finger_tapping | 6.0 | right | NaN | NaN |
149 | 482.676100 | start_trial | 0.012548 | finger_tapping | 6.0 | right | 66.0 | index |
150 rows × 8 columns
Total running time of the script: ( 0 minutes 25.493 seconds)