Hi, I have two questions on using the ff package and wonder if anyone who used ff can share some thoughts. I need to save a matrix as a memory-mapped file and load it back later. To save the matrix, I use mat = matrix(1:20, 4, 5) matFF = ff(mat, dim=dim(mat), filename="~/a.mat", overwrite=TRUE, dimnames = dimnames(mat)) To load it back, I use matFF2 = ff(vmode = "double", dim= ???, filename="~/a.mat", overwrite=F) However, I don't always know the dimension when loading the matrix back. If I miss the dim attributes, ff will return it as vector. Is there a way to load the matrix without specifying the dimension? The second question is that the matrix may grow in terms of the number of rows. I would like to synchronize the change to the memory-mapped file. Is there an efficient way to do this? Thanks Jeff
Dear Jeff, This is not exactly what you are asking, but what I do is close the object, save it as RData, and then when I need to load the RData. The RData objects themselves are very small. Best, R. On Wed, Nov 25, 2009 at 2:28 AM, Hao Cen <hcen at andrew.cmu.edu> wrote:> Hi, > > I have two questions on using the ff package and wonder if anyone who used > ff can share some thoughts. > > I need to save a matrix as a memory-mapped file and load it back later. To > save the matrix, I use > > mat = matrix(1:20, 4, 5) > matFF = ff(mat, dim=dim(mat), filename="~/a.mat", overwrite=TRUE, dimnames > = dimnames(mat)) > > To load it back, I use > matFF2 = ff(vmode = "double", dim= ???, filename="~/a.mat", overwrite=F) > > However, I don't always know the dimension when loading the matrix back. > If I miss the dim attributes, ff will return it as vector. Is there a way > to load the matrix without specifying the dimension? > > The second question is that the matrix may grow in terms of the number of > rows. I would like to synchronize the change to the memory-mapped file. Is > there an efficient way to do this? > > Thanks > > Jeff > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ramon Diaz-Uriarte Structural Biology and Biocomputing Programme Spanish National Cancer Centre (CNIO) http://ligarto.org/rdiaz Phone: +34-91-732-8000 ext. 3019
Jeff,> I need to save a matrix as a memory-mapped file and load it back later. > To save the matrix, I use > mat = matrix(1:20, 4, 5) > matFF = ff(mat, dim=dim(mat), filename="~/a.mat" > , overwrite=TRUE, dimnames = dimnames(mat))# This stores the data in an ff file, # but not the metadata in R's ff object. # To do the latter you need to do save(matFF, file="~/matFF.RData") # Assuming that your ff file remains in the same location, # in a new R session you simply load(file="~/matFF.RData") # and the ff file is available automagically> However, I don't always know the dimension when loading the matrix back. > If I miss the dim attributes, ff will return it as vector. > Is there a way to load the matrix without specifying the dimension?# You can create an ff object using your existing ff file by matFF <- ff(filename="~/a.mat", vmode="double", dim=c(4,5)) # You can do the same at unknown file size with matFF <- ff(filename="~/a.mat", vmode="double") # which gives you the length of the ff object length(matFF) # if you know the number of columns you can calculate the number of rows and give your ff object the interpretation of a matrix dim(matFF) <- c(length(matFF)/5, 5)> the matrix may grow in terms of the number of rows. > Is there an efficient way to do this?# there are two ways to grow a matrix by rows # 1) you create the matrix in major row order matFF <- ff(1:20, dim=c(4,5), dimorder=c(2:1)) # then you require a higher number of rows nrow(matFF) <- 6 # as you can see there are new empty rows in the file matFF # 2) Instead of a matrix you create a ffdf data.frame # which you can also give more rows using nrow<- # An example of this is in read.table.ffdf # which reads a csv file in chunks and extends the # number of rows in the ffdf Jens Oehlschl?gel -- Preisknaller: GMX DSL Flatrate f?r nur 16,99 Euro/mtl.! http://portal.gmx.net/de/go/dsl02
> I wonder how efficiently it is to do the following command on a frequent basis. > nrow(matFF) <- nrow(matFF)+1Obviously there is overhead (closing file, enlarging file, openeing file). I recommend you measure yourself whether this is acceptable for you.> no large file copying is needed each time the nrow is changed?With a decent filesystem there is *no* copying from smaller to larger file.> would you think I can open 2000 large matrices and leave them open or Ineed to close each after it is opened and used? Not tested yet. I guess the number of open files can be configured when compiling your OS. Please test and let us know your experience. Regards Jens Oehlschl?gel