-
Notifications
You must be signed in to change notification settings - Fork 0
/
nli2.py
121 lines (109 loc) · 4.71 KB
/
nli2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 20 15:23:25 2023
@author: mflores
"""
from nilearn import image
from nilearn import signal
from nilearn.masking import apply_mask
from nilearn.maskers import NiftiMasker
from scipy import stats
import numpy as np
import pandas as pd
import os
import argparse
from plotnine import *
######
# debbug
source_directory = "/bcbl/home/public/MarcoMotion/Habla_restingState/analysis_03-24_/"
directories=["func_preproc_hydra","func_preproc_nordic","func_preproc_vanilla"]
task="HABLA1200"
tsnr_class="echo2_fwhm_"
tsnr_extention="task-"+task+"_echo-2_part-mag_bold_"
#
ext=".nii.gz"
methods=["hydra_fwhmstats", "nordic_fwhmstats","vanilla_fwhmstats"]
#
labels=["Hydra", "Nordic","E2"]
voxel="2.4*2.4*2.4mm"
###### Arguments ########################################################3###########
#parser=argparse.ArgumentParser(description="""Generates histograms from fmri
# volumes""")
#parser.add_argument("--source_directory", default=None, type=str,
# help="Full path to the BIDS directory")
#
#parser.add_argument("--directories", default=None, type=list,
# help="""List of subdirectories inside bids directory
# that will be considered as different factors to plot histograms""")
#
#parser.add_argument("--oc_extention", default=None, type=str,
# help="""Extention of the target OC file to be look upon
# i.e., if my target file has the name:
# sub-001_task-HABLA1200_masked_epi_gm_ocDenoised.nii.gz
# then, OC_extention=task-HABLA1200_masked_epi_gm_ocDenoised.nii.gz""")
#
#parser.add_argument("--tsnr_extention", default=None, type=str,
# help="""Extention of the target tsnr file to be look upon
# i.e., if my target file has the name:
# sub-001_task-HABLA1200_masked_epi_gm_ocDenoised_tsnr.nii.gz
# then, OC_extention=task-HABLA1200_masked_epi_gm_ocDenoised_tsnr.nii.gz""")
#parser.add_argument("--tsnr_extention", default=None, type=str,
# help="""Extention of the target tsnr file to be look upon
# i.e., if my target file has the name:
# sub-001_task-HABLA1200_masked_epi_gm_ocDenoised_tsnr.nii.gz
# then, OC_extention=task-HABLA1200_masked_epi_gm_ocDenoised_tsnr.nii.gz""")
###### Find files ###################################################################
tsnr_list=[]
#source_oc_all=[]
source_tsnr_all=[]
preprocessing_method=[]
for i in range(len(methods)):
source_tsnr=sorted([file_
for file_ in os.listdir(source_directory)
if file_.endswith(tsnr_extention+methods[i]+ext)])
source_tsnr_all.extend(source_tsnr)
tmp_preprocessing_methods=[labels[i]]*len(source_tsnr)
preprocessing_method.extend(tmp_preprocessing_methods)
######
# Reading dataset
tsnr_masked=[]
for i in range (len(source_tsnr_all)):
number=source_tsnr_all[i].split("sub-00")
subj=number[1][0]
bold_tsnr_file=source_tsnr_all[i]
tsnr=image.load_img(source_directory+bold_tsnr_file)
# tsnr=tsnr.slicer[:,:,:,0]#we only need one slice, because they are all ==
######
# creating empty data frame per subject
######
# masking dataset
# data is masked, background = 0
masker_tsnr = NiftiMasker(mask_strategy="background", verbose=1)
masker_tsnr.fit(source_directory+bold_tsnr_file)
masked_tsnr = masker_tsnr.fit_transform(tsnr) # apply mask?
masked_tsnr=masked_tsnr.squeeze()#converting 1*n array into n,
tmp_tsnr_masked=np.column_stack((masked_tsnr,
np.repeat(subj,(len(masked_tsnr))))).tolist()
for sublist in tmp_tsnr_masked:
sublist.append(preprocessing_method[i])
tsnr_masked.extend(tmp_tsnr_masked)
################################################
# Example of how to plot basic histogramfrom data
# I am using variable as a 1-D vector
# Do not run
# df=pd.DataFrame(data=variable, columns=['var_name'])
# plotnine_obj=ggplot(df,aes(x='var_name'))+geom_histogram()
# plotnine_obj.draw() #plot inline
# plotnine_obj.save(filename='dir/dir/img.png',dpi=300)
df_tsnr=pd.DataFrame(data=tsnr_masked, columns=["value","ID","method"])
df_tsnr['value']=pd.to_numeric(df_tsnr['value'], errors='coerce')
df_tsnr_plot=df_tsnr.dropna()
df_tsnr_plot.to_csv(source_directory+task+tsnr_class+"tsnr_dataframe_group.csv",index=False)
## plot 2
plotnine_tsnr=(ggplot(df_tsnr_plot,aes(x="method",y="value",fill="ID"))+geom_boxplot()
+xlab(voxel)+ylab("TSNR value"))
plotnine_tsnr.draw()
plotnine_tsnr.save(source_directory+task+tsnr_class+"tsnr_masked_group.png", verbose=False)
######
# creating subject's dataframes