-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomputeMode.R
More file actions
executable file
·88 lines (67 loc) · 2.17 KB
/
computeMode.R
File metadata and controls
executable file
·88 lines (67 loc) · 2.17 KB
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
#!/usr/bin/Rscript
args <- commandArgs(TRUE)
if (length(args) == 0)
cat ("computeMode.R usage: computeMode.R 4dFMRIFile brainMaskFile scaleMode\n")
suppressMessages(library(oro.nifti))
suppressMessages(library(stats))
suppressMessages(library(ftnonpar))
#for testing
#fmriFilename <- "nfswktmd_10128_5.nii"
#maskFilename <- "wktmd_10128_98_2_mask.nii.gz"
#scaleTo <- 1000
fmriFilename <- args[1]
#determine whether nifti or afni
isNifti <- function(fn) grepl(".*(\\.nii|\\.nii\\.gz)+$", fn, perl=TRUE)
isAfni <- function(fn) grepl("[^\\+]+\\+(tlrc|orig|acpc)+\\.(HEAD|BRIK|BRIK\\.gz)+$", fn, perl=TRUE)
fmriAfni <- isAfni(fmriFilename)
fmriNifti <- isNifti(fmriFilename)
if (!fmriAfni && !fmriNifti) stop("Cannot determine fmri file type from file name: ", fmriFilename)
mask <- FALSE
if (length(args) > 1) {
mask <- TRUE
maskFilename <- args[2]
maskAfni <- isAfni(maskFilename)
maskNifti <- isNifti(maskFilename)
}
#if a third parameter is given
if (length(args) > 2) {
scaleTo <- as.numeric(args[3])
} else {
scaleTo <- 1
}
if (file.exists(fmriFilename)) {
if (fmriNifti) {
nif <- readNIfTI(fmriFilename)@.Data
} else if (fmriAfni) {
nif <- readAFNI(fmriFilename)@.Data
}
#browser()
if (mask && file.exists(maskFilename)) {
if (maskAfni)
maskMat <- readAFNI(maskFilename)@.Data
else if (maskNifti)
maskMat <- readNIfTI(maskFilename)@.Data
#apply mask at each volume in 4d time series
nif <- apply(nif, 4, function(submat) {
submat[which(maskMat==0)] <- NA_real_
submat
})
nif <- as.vector(nif)
} else {
#if mask not provided, avoid problem of 0 being the mode because of many non-brain voxels
nif <- nif[which(nif != 0)]
}
#density estimate of mode
dens <- density(na.omit(nif), n=1024)
modalVal <- dens$x[dens$y==max(dens$y)]
#note that the above is giving low estimates for preprocessFunctional data, probably of low intensity voxels at the edge
#dens$x[dens$y >= quantile(dens$y, 0.95)]
#this is just one approach to mode estimation with continuous data
if (scaleTo == 1) {
cat(modalVal, "\n")
} else {
cat(scaleTo/modalVal, "\n")
}
} else {
cat("-1\n")
}