'update'
[epclust.git] / epclust / R / de_serialize.R
index 5a9dd1f..f28038b 100644 (file)
@@ -1,28 +1,28 @@
-#' @name de_serialize
-#' @rdname de_serialize
-#' @aliases binarize binarizeTransform getDataInFile
+#' (De)Serialization of a [big]matrix or data stream
 #'
-#' @title (De)Serialization of a [big]matrix or data stream
+#' \code{binarize()} serializes a matrix or CSV file with minimal overhead, into a
+#' binary file. \code{getDataInFile()} achieves the inverse task: she retrieves (ASCII)
+#' data rows from indices in the binary file. Finally, \code{binarizeTransform()}
+#' serialize transformations of all data chunks. To use it a data-retrieval function
+#' must be provided -- thus \code{binarize} will most likely be used first
+#' (and then a function defined to seek in generated binary file)
 #'
-#' @description \code{binarize()} serializes a matrix or CSV file with minimal overhead,
-#'   into a binary file. \code{getDataInFile()} achieves the inverse task: she retrieves
-#'   (ASCII) data rows from indices in the binary file. Finally,
-#'   \code{binarizeTransform()} serialize transformations of all data chunks; to use it,
-#'   a data-retrieval function must be provided, thus \code{binarize} will most likely be
-#'   used first (and then a function defined to seek in generated binary file)
-#'
-#' @param data_ascii Either a matrix or CSV file, with items in rows
-#' @param indices Indices of the lines to retrieve
-#' @param data_bin_file Name of binary file on output (\code{binarize})
-#'   or input (\code{getDataInFile})
+#' @param data_ascii Either a matrix (by columns) or CSV file or connection (by rows)
+#' @param data_bin_file Name of binary file on output of (\code{binarize})
+#'   or input of (\code{getDataInFile})
 #' @param nb_per_chunk Number of lines to process in one batch (big.matrix or connection)
-#' @inheritParams claws
 #' @param getData Function to retrieve data chunks
 #' @param transform Transformation function to apply on data chunks
+#' @param indices Indices of the lines to retrieve
+#' @inheritParams claws
 #'
 #' @return For \code{getDataInFile()}, the matrix with rows corresponding to the
 #'   requested indices. \code{binarizeTransform} returns the number of processed lines.
 #'   \code{binarize} is designed to serialize in several calls, thus returns nothing.
+#'
+#' @name de_serialize
+#' @rdname de_serialize
+#' @aliases binarize binarizeTransform getDataInFile
 NULL
 
 #' @rdname de_serialize
@@ -120,7 +120,7 @@ binarizeTransform = function(getData, transform, data_bin_file, nb_per_chunk,
                binarize(transformed_chunk, data_bin_file, nb_per_chunk, ",", nbytes, endian)
 
                index = index + nb_per_chunk
-               nb_items = nb_items + nrow(data_chunk)
+               nb_items = nb_items + ncol(data_chunk)
        }
        nb_items #number of transformed items
 }
@@ -139,7 +139,7 @@ getDataInFile = function(indices, data_bin_file, nbytes=4, endian=.Platform$endi
        # to compute the offset ( index i at 8 + i*data_length*nbytes )
        data_ascii = do.call( cbind, lapply( indices, function(i) {
                offset = 8+(i-1)*data_length*nbytes
-               if (offset > data_size)
+               if (offset >= data_size)
                        return (NULL)
                ignored = seek(data_bin, offset) #position cursor at computed offset
                readBin(data_bin, "double", n=data_length, size=nbytes, endian=endian)