1 context("de_serialize")
3 test_that("serialization + getDataInFile retrieve original data / from matrix",
5 data_bin_file = ".epclust_test_m.bin"
8 # Dataset 200 cols / 30 rows
9 data_ascii = matrix(runif(200*30,-10,10),nrow=30)
10 nbytes = 4 #lead to a precision of 1e-7 / 1e-8
13 # Simulate serialization in one single call
14 binarize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
15 expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
16 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
18 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
19 expect_equal(data_lines, data_ascii[,indices], tolerance=1e-6)
23 # Serialization in several calls (last call complete, next call NULL)
25 binarize(data_ascii[,((i-1)*10+1):(i*10)], data_bin_file, 20, ",", nbytes, endian)
26 expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
27 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
29 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
30 expect_equal(data_lines, data_ascii[,indices], tolerance=1e-6)
35 test_that("serialization + transform + getDataInFile retrieve original transforms",
37 data_bin_file = ".epclust_test_t.bin"
40 # Dataset 200 cols / 30 rows
41 data_ascii = matrix(runif(200*30,-10,10),nrow=30)
45 binarize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
46 # Serialize transformation (just compute range) into a new binary file
47 trans_bin_file = ".epclust_test_t_trans.bin"
48 unlink(trans_bin_file)
49 getSeries = function(inds) getDataInFile(inds, data_bin_file, nbytes, endian)
50 binarizeTransform(getSeries, function(series) apply(series, 2, range),
51 trans_bin_file, 250, nbytes, endian)
53 expect_equal(file.info(trans_bin_file)$size, 2*ncol(data_ascii)*nbytes+8)
54 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
56 trans_cols = getDataInFile(indices, trans_bin_file, nbytes, endian)
57 expect_equal(trans_cols, apply(data_ascii[,indices],2,range), tolerance=1e-6)
59 unlink(trans_bin_file)
62 test_that("serialization + getDataInFile retrieve original data / from connection",
64 data_bin_file = ".epclust_test_c.bin"
67 # Dataset 300 cols / 50 rows
68 data_csv = system.file("testdata","de_serialize.csv",package="epclust")
71 data_ascii = t( as.matrix(read.table(data_csv, sep=";", header=FALSE)) ) #for ref
73 # Simulate serialization in one single call
74 binarize(data_csv, data_bin_file, 350, ";", nbytes, endian)
75 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
76 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
78 data_cols = getDataInFile(indices,data_bin_file,nbytes,endian)
79 #HACK: rows naming required to match (ref) data structure
80 rownames(data_cols) <- paste("V",seq_len(nrow(data_ascii)), sep="")
81 expect_equal(data_cols, data_ascii[,indices])
85 # Serialization in several calls / chunks of 29 --> 29*10 + 10, incomplete last
86 data_con = file(data_csv, "r")
87 binarize(data_con, data_bin_file, 29, ";", nbytes, endian)
88 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
89 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
91 data_cols = getDataInFile(indices,data_bin_file,nbytes,endian)
92 rownames(data_cols) <- paste("V",seq_len(nrow(data_ascii)), sep="")
93 expect_equal(data_cols, data_ascii[,indices])
96 #close(data_con) --> done in binarize()