@@ -14,29 +14,25 @@ qdecr_prep_mgh <- function(input_path,
1414 if (measure2 == " w_g.pct" ) measure2 <- " w-g.pct"
1515 new_files <- file.path(input_path , files_list , " surf" , paste(hemi , measure , fwhmc , target , " mgh" , sep = " ." ))
1616 n <- length(new_files )
17-
1817 temp <- load.mgh(new_files [1 ])
1918
2019 m <- bigstatsr :: FBM(temp $ ndim1 , n , backingfile = backing )
2120 cl <- if (verbose ) parallel :: makeForkCluster(n_cores , outfile = " " ) else parallel :: makeForkCluster(n_cores )
2221 doParallel :: registerDoParallel(cl )
23-
2422 capture.output(pb <- txtProgressBar(0 , n , style = 3 ), file = " /dev/null" )
25-
2623 foreach(i = seq_len(n )) %dopar % {
2724 setTxtProgressBar(pb , i )
2825 m [,i ] <- load.mgh(new_files [i ])$ x
2926 NULL
3027 }
3128 parallel :: stopCluster(cl )
32-
3329 if (length(mask ) != nrow(m )) stop(" Length of mask does not equal number of vertices from data." )
3430 m
3531}
3632
37-
38-
3933# ' Load an MGH file into memory
34+ # '
35+ # ' Originally written by Heath Perdoe (09/12/2013)
4036# '
4137# ' @param input.file full path to the mgh file
4238# '
@@ -45,7 +41,6 @@ qdecr_prep_mgh <- function(input_path,
4541# ' @export
4642# '
4743load.mgh <- function (input.file ) {
48- # written by Heath Pardoe, heath.pardoe at nyumc.org, 09/12/2013
4944 to.read <- file(input.file , " rb" )
5045 v <- readBin(to.read , integer(), endian = " big" )
5146 ndim1 <- readBin(to.read , integer(), endian = " big" )
@@ -55,11 +50,8 @@ load.mgh <- function(input.file) {
5550 type <- readBin(to.read , integer(), endian = " big" )
5651 dof <- readBin(to.read , integer(), endian = " big" )
5752 close(to.read )
58-
5953 to.read <- file(input.file ," rb" )
6054 dump <- readBin(to.read , double(), size = 4 , n = 71 , endian = " big" )
61- # THIS SEEMED TO ONLY READ IN THE FIRST CHUNK OF DATA, and ignored if additional subjects were merged in
62- # added ndim1*nframes
6355 x <- readBin(to.read ,double(), size = 4 , n = ndim1 * nframes , endian = " big" )
6456 close(to.read )
6557 list (x = x , v = v , ndim1 = ndim1 , ndim2 = ndim2 , ndim3 = ndim3 , nframes =
@@ -75,7 +67,6 @@ load.mgh <- function(input.file) {
7567# ' @export
7668# '
7769load.annot <- function (input.file ) {
78- # rewrite of load.mgh
7970 to.read <- file(input.file , " rb" )
8071 on.exit (close(to.read ))
8172 a_vtxct <- readBin(to.read , size = 4 , integer(), endian = " big" )
@@ -86,7 +77,6 @@ load.annot <- function(input.file) {
8677 a_len <- readBin(to.read , size = 4 , integer(), endian = " big" )
8778 a_fname <- readChar(to.read , a_len )
8879 a_num_entries <- readBin(to.read , size = 4 , integer(), endian = " big" )
89-
9080 LUT_Label <- LUT_len <- LUT_labelname <- LUT_red <- LUT_green <- LUT_blue <- LUT_transp <- rep(NA , a_num_entries )
9181 for (i in seq_len(a_num_entries )) {
9282 LUT_Label [i ] <- readBin(to.read , size = 4 , integer(), endian = " big" )
@@ -99,11 +89,9 @@ load.annot <- function(input.file) {
9989 }
10090 close(to.read )
10191 on.exit()
102-
10392 a_vtxct2 <- seq(1 , a_vtxct * 2 , 2 )
10493 a_vd_vno <- a_vd [a_vtxct2 ]
10594 a_vd_label <- a_vd [a_vtxct2 + 1 ]
106-
10795 list (vtxct = a_vtxct ,
10896 vd_vno = a_vd_vno ,
10997 vd_label = a_vd_label ,
@@ -127,18 +115,14 @@ load.annot <- function(input.file) {
127115
128116# ' Save out an MGH file from a big matrix
129117# '
118+ # ' Modified version of save_mgh.m (by Heath Pardoe, 09/12/2013)
119+ # '
130120# ' @param vol MGH object (as from load.mgh)
131121# ' @param fname file name to be used to save out the data
132122# '
133123# ' @export
134124# '
135125bsfbm2mgh <- function (fbm , fname , filter = NULL ) {
136-
137- # R translation of save_mgh.m
138- # written by Heath Pardoe, heath.pardoe at nyumc.org, 09/12/2013
139- # modified by Ryan Muetzel to handle nframes/stacked data
140- # modified by Sander Lamballais to convert .bk files (from bigstatsr) to .mgh
141-
142126 MRI.UCHAR <- 0
143127 MRI.INT <- 1
144128 MRI.LONG <- 2
@@ -156,24 +140,20 @@ bsfbm2mgh <-function(fbm, fname, filter = NULL) {
156140 } else {
157141 filter <- seq_len(fbm $ nrow )
158142 }
159-
160143 width <- fbm $ ncol
161144 height <- 1
162145 depth <- 1
163146 nframes <- length(filter )
164-
165147 writeBin(as.integer(1 ), fid , size = 4 , endian = " big" )
166148 writeBin(as.integer(width ), fid , size = 4 , endian = " big" )
167149 writeBin(as.integer(height ), fid , size = 4 , endian = " big" )
168150 writeBin(as.integer(depth ), fid , size = 4 , endian = " big" )
169151 writeBin(as.integer(nframes ), fid , size = 4 , endian = " big" )
170152 writeBin(as.integer(MRI.FLOAT ), fid , size = 4 , endian = " big" )
171153 writeBin(as.integer(1 ), fid , size = 4 , endian = " big" )
172-
173154 UNUSED.SPACE.SIZE <- 256
174155 USED.SPACE.SIZE <- (3 * 4 + 4 * 3 * 4 )
175156 unused.space.size <- UNUSED.SPACE.SIZE - 2
176-
177157 writeBin(as.integer(0 ), fid , size = 2 , endian = " big" )
178158 writeBin(as.integer(rep.int(0 , unused.space.size )), fid , size = 1 )
179159 bpv <- 4
@@ -184,17 +164,15 @@ bsfbm2mgh <-function(fbm, fname, filter = NULL) {
184164}
185165
186166# ' Save out an MGH file from memory
167+ # '
168+ # ' #' R translation of save_mgh.m (by Heath Pardoe, 09/12/2013)
187169# '
188170# ' @param vol MGH object (as from load.mgh)
189171# ' @param fname file name to be used to save out the data
190172# '
191173# ' @export
192174# '
193175save.mgh <- function (vol ,fname ) {
194-
195- # R translation of save_mgh.m
196- # written by Heath Pardoe, heath.pardoe at nyumc.org, 09/12/2013
197-
198176 MRI.UCHAR <- 0
199177 MRI.INT <- 1
200178 MRI.LONG <- 2
@@ -203,90 +181,44 @@ save.mgh <-function(vol,fname) {
203181 MRI.BITMAP <- 5
204182 MRI.TENSOR <- 6
205183 slices <- c(1 : 256 )
206-
207184 fid <- file(fname , open = " wb" , blocking = TRUE )
208185 on.exit(close(fid ))
209-
210186 width <- vol $ ndim1
211187 height <- vol $ ndim2
212188 depth <- vol $ ndim3
213- # RLM added
214189 nframes <- vol $ nframes
215-
216190 writeBin(as.integer(1 ), fid , size = 4 , endian = " big" )
217191 writeBin(as.integer(width ), fid , size = 4 , endian = " big" )
218192 writeBin(as.integer(height ), fid , size = 4 , endian = " big" )
219193 writeBin(as.integer(depth ), fid , size = 4 , endian = " big" )
220- # here we replace the default of 1 frame
221194 writeBin(as.integer(nframes ), fid , size = 4 , endian = " big" )
222- # writeBin(as.integer(1),fid,size = 4, endian = "big")
223-
224- # HP note: I've ignored all the diffusion tensor stuff
225195 writeBin(as.integer(MRI.FLOAT ), fid , size = 4 , endian = " big" )
226-
227196 writeBin(as.integer(1 ), fid , size = 4 , endian = " big" )
228- # dof = fread(fid, 1, 'int');
229- # # HP note: ignored ^this line^ from save_mgh.m
230-
231197 UNUSED.SPACE.SIZE <- 256
232- USED.SPACE.SIZE <- (3 * 4 + 4 * 3 * 4 ) # space for ras transform
233-
198+ USED.SPACE.SIZE <- (3 * 4 + 4 * 3 * 4 )
234199 unused.space.size <- UNUSED.SPACE.SIZE - 2
235-
236- # ignored all the stuff about "M" - could probably do it if necessary so let me know
237- # if (nargin > 2)
238- # # fwrite(fid, 1, 'short') # ras.good.flag <- 0
239- # writeBin(1,fid,size = 2, endian = "big")
240- # unused.space.size <- unused.space.size - USED.SPACE.SIZE
241- # # fwrite(fid, sizes(1), 'float32') # xsize
242- # # fwrite(fid, sizes(2), 'float32') # ysize
243- # # fwrite(fid, sizes(3), 'float32') # zsize
244- #
245- # fwrite(fid, M(1,1), 'float32') # x.r
246- # fwrite(fid, M(2,1), 'float32') # x.a
247- # fwrite(fid, M(3,1), 'float32') # x.s
248-
249- # fwrite(fid, M(1,2), 'float32') # y.r
250- # fwrite(fid, M(2,2), 'float32') # y.a
251- # fwrite(fid, M(3,2), 'float32') # y.s
252-
253- # fwrite(fid, M(1,3), 'float32') # z.r
254- # fwrite(fid, M(2,3), 'float32') # z.a
255- # fwrite(fid, M(3,3), 'float32') # z.s
256-
257- # fwrite(fid, M(1,4), 'float32') # c.r
258- # fwrite(fid, M(2,4), 'float32') # c.a
259- # fwrite(fid, M(3,4), 'float32') # c.s
260- # else
261- # fwrite(fid, 0, 'short') # ras.good.flag <- 0
262200 writeBin(as.integer(0 ), fid , size = 2 , endian = " big" )
263-
264- # } #
265-
266201 writeBin(as.integer(rep.int(0 , unused.space.size )), fid , size = 1 )
267- bpv <- 4 # bytes/voxel
268- nelts <- width * height # bytes per slice
269- # writeBin(vol$x, fid, size = 4, endian = "big")
202+ bpv <- 4
203+ nelts <- width * height
270204 writeBin(vol $ x , fid , size = 4 , endian = " big" )
271205}
272206
273-
274207# ' Create an object that is structured like an mgh object
275208# '
276209# ' @param x the vertex-wise values
277- # ' @param v (to be added )
278- # ' @param ndim1 (to be added)
279- # ' @param ndim2 (to be added)
280- # ' @param ndim3 (to be added)
281- # ' @param nframes (to be added)
282- # ' @param type (to be added )
283- # ' @param dof (to be added)
210+ # ' @param v Version (default = 1 )
211+ # ' @param ndim1 Width / 1st dimension
212+ # ' @param ndim2 Height / 2nd dimension
213+ # ' @param ndim3 Depth / 3rd dimension
214+ # ' @param nframes Number of scalar components
215+ # ' @param type Data type, can be UCHAR (0), SHORT (4), INT (1) or FLOAT (3 )
216+ # ' @param dof Degrees of freedom
284217# '
285218# ' @export
286219# '
287220
288221as_mgh <- function (x , v = NULL , ndim1 = NULL , ndim2 = NULL , ndim3 = NULL , nframes , type , dof ) {
289-
290222 if (is.vector(x )) {
291223 v <- 1L
292224 ndim1 <- as.integer(length(x ))
@@ -298,7 +230,6 @@ as_mgh <- function(x, v = NULL, ndim1 = NULL, ndim2 = NULL, ndim3 = NULL, nframe
298230 } else {
299231 stop(" as_mgh only support objects of class `vector` right now." )
300232 }
301-
302233 out <- list (x = x ,
303234 v = v ,
304235 ndim1 = ndim1 ,
@@ -309,5 +240,4 @@ as_mgh <- function(x, v = NULL, ndim1 = NULL, ndim2 = NULL, ndim3 = NULL, nframe
309240 dof = dof )
310241
311242 return (out )
312-
313243}
0 commit comments