-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_global_intensities.R
153 lines (137 loc) · 5.79 KB
/
calculate_global_intensities.R
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# author: Dani Cosme
# email: [email protected]
# version: 0.1
# date: 2017-03-03
# This script loads functional volumes, calculates the mean global intensity value,
# and returns a csv file 'study_globalIntensities.csv'
#
# Inputs:
# * subjectDir = path to subject directory
# * functionalDir = path from subject's directory to to functional files
# * outputDir = path where study_globalIntensities.csv will be written
# * study = study name
# * subPattern = regular expression for subject IDs
# * prefix = SPM prefix appended to functional images; use "" to ignore
# * runPattern = regular expression for run names; use "" to specify all directories in $functionalDir
# * threshold = voxel intensity value used to truncate the distribution
# * final_output_csv = path and file name for 'study_globalIntensities.csv'
# * parallelize = use TRUE to parallelize, FALSE if not
# * leave_n_free_cores = number of cores to leave free
#
# Outputs:
# * study_globalIntensities.csv = CSV file with global intensity value for each image
#------------------------------------------------------
# load packages
#------------------------------------------------------
osuRepo = 'http://ftp.osuosl.org/pub/cran/'
if(!require(devtools)){
install.packages('devtools',repos=osuRepo)
}
if(!require(RNifti)){
devtools::install_github("jonclayden/RNifti")
}
require(RNifti)
if(!require(tidyverse)){
install.packages('tidyverse',repos=osuRepo)
}
require(tidyverse)
if(!require(parallel)){
install.packages('parallel',repos=osuRepo)
}
require(parallel)
#------------------------------------------------------
# define variables
# these variables are all you should need to change
# to run the script
#------------------------------------------------------
# paths
subjectDir = "/Users/ralph/Documents/tds/fMRI/subjects/" #"/Volumes/FP/research/dsnlab/Studies/FP/subjects/" #"/Volumes/psych-cog/dsnlab/TDS/archive/subjects_G80/"
functionalDir = "" #"/ppc/functionals/"
outputDir = "/Users/ralph/Documents/tds/fMRI/analysis/fx/motion/auto-motion-output/" #"/Volumes/psych-cog/dsnlab/auto-motion-output/"
# variables
study = "tds" #"FP"
subPattern = "^[0-9]{3}" #"^FP[0-9]{3}"
prefix = "ru" #"o"
runPattern = "(cyb|stop|vid)[1-8]" #"^run*"
threshold = 5000
final_output_csv = file.path(outputDir,paste0(study,'_globalIntensities.csv'))
parallelize = TRUE
leave_n_free_cores = 1
#------------------------------------------------------
# calculate mean intensity for each functional image
#------------------------------------------------------
# get subjects list from subject directory
subjects = list.files(subjectDir, pattern = subPattern)
globint_for_sub <- function(sub, subjectDir, functionalDir, runPattern, prefix, threshold){
runs = list.files(paste0(subjectDir,sub,functionalDir), pattern=runPattern)
for (run in runs){
# assign pattern based on prefix and run
filePattern = paste0('^',prefix,'.*',run,'_*([0-9]{4}).nii.*')
# generate file path
path = file.path(subjectDir,sub,'/',functionalDir,run)
file_list = list.files(path, pattern = filePattern)
for (file in file_list){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
img = RNifti::readNifti(paste0(path,"/",file), internal = FALSE) #using `::` allows us to not load the package when parallelized
dataset = tidyr::extract(data.frame(subjectID = sub,
file = file,
run = run,
volMean = mean(img[img > threshold], na.rm=TRUE),
volSD = sd(img[img > threshold], na.rm=TRUE)),
file, c("volume"), filePattern)
}
# if the merged dataset does exist, append to it
else {
img = RNifti::readNifti(paste0(path,"/",file), internal = FALSE)
temp_dataset = tidyr::extract(data.frame(subjectID = sub,
file = file,
run = run,
volMean = mean(img[img > threshold], na.rm=TRUE),
volSD = sd(img[img > threshold], na.rm=TRUE)),
file, c("volume"), filePattern)
dataset <- dplyr::bind_rows(dataset, temp_dataset)
rm(temp_dataset)
}
}
}
if (!exists("dataset")){
dataset = data.frame(subjectID = sub,
run = NA,
volMean = NA,
volSD = NA,
volume = NA)
}
return(dataset)
}
if(parallelize){
time_it_took <- system.time({
parallelCluster <- parallel::makeCluster(parallel::detectCores() - leave_n_free_cores)
print(parallelCluster)
datasets <- parallel::parLapply(parallelCluster,
subjects,
globint_for_sub, subjectDir, functionalDir, runPattern, prefix, threshold)
outdata <- bind_rows(datasets)
# Shutdown cluster neatly
cat("Shutting down cluster...")
if(!is.null(parallelCluster)) {
parallel::stopCluster(parallelCluster)
parallelCluster <- c()
}
})
} else {
time_it_took <- system.time({
datasets <- lapply(subjects,
globint_for_sub, subjectDir, functionalDir, runPattern, prefix, threshold)
outdata <- bind_rows(datasets)
})
}
cat(paste0("For ", length(subjects), " participant IDs, the system logged this much time: \n"))
print(time_it_took)
#------------------------------------------------------
# write csv
#------------------------------------------------------
if (!dir.exists(dirname(final_output_csv))){
dir.create(dirname(final_output_csv))
}
write.csv(outdata, final_output_csv, row.names = FALSE)