separate TODO and wrapper example
[epclust.git] / old_C_code / wrapper.R
1 #Exemple :
2 #
3 #dans old_C_code/build :
4 #cmake ../stage1/src
5 #make
6 #
7 #dans data/, lancer R puis :
8 #source("../old_C_code/wrapper.R")
9 #serialize("../old_C_code/build", "2009.csv","2009.bin",1)
10 #library(parallel)
11 #np = detectCores()
12 #nbSeriesPerChunk = 3000
13 #nbClusters = 20
14 #ppam_exe("../old_C_code/build",np,"2009.bin",nbSeriesPerChunk,nbClusters)
15 #C = getMedoids("../old_C_code/build", "ppamResult.xml", "ppamFinalSeries.bin")
16 #first100series = deserialize("../old_C_code/build", "2009.bin", "2009.csv.part", "1-100")
17 #distor = getDistor("../old_C_code/build", "ppamResult.xml", "2009.bin")
18
19 ppam_exe = function(path=".", np=parallel::detectCores(), data=NULL,
20 nbSeriesPerChunk, nbClusters, randomize=1, p_dissims=2)
21 {
22 args = paste(nbSeriesPerChunk," ",nbClusters," ",randomize," ",p_dissims,sep="")
23
24 command_line = paste("mpirun -np ",np," ",path,"/ppam.exe cluster",sep="")
25
26 #if data provided (as data.frame or matrix...): binarize it, and add it as first argument
27 if (!is.null(data))
28 {
29 if (!is.character(data))
30 {
31 #assuming matrix or data.frame, WITH row names
32 #( identifiers; could be line number... e.g. data <- cbind(1:nrow(data),data) )
33 write.table(data, "/tmp/data_csv", sep=",", row.names=FALSE, col.names=FALSE)
34 system(paste(path,"/ppam.exe serialize /tmp/data_csv /tmp/data_bin 0 0",sep=""))
35 } else
36 {
37 system(paste(path,"/ppam.exe serialize ",data," /tmp/data_bin 0 0",sep=""))
38 }
39 command_line = paste(command_line," /tmp/data_bin",sep="")
40 }
41
42 command_line = paste(command_line," ",args,sep="")
43 system(command_line)
44 }
45
46 #NOTE: identifiers in first column
47 getMedoids = function(path=".", xmlResult = "ppamResult.xml",
48 finalSeries = "ppamFinalSeries.bin")
49 {
50 system(paste(path,"/ppam.exe deserialize ",finalSeries," ppamFinalSeries.csv -1",sep=""))
51 curves = read.table("ppamFinalSeries.csv", sep=",")
52 library(XML)
53 ranks = as.integer( xmlToList( xmlParse(xmlResult) )$ranks )
54 return ( curves[ranks,] ) # == medoids
55 }
56
57 getDistor = function(path=".", xmlResult = "ppamResult.xml",
58 finalSeries = "ppamFinalSeries.bin")
59 {
60 system(paste(path,"/ppam.exe classif ",finalSeries," ",xmlResult,sep=""))
61 }
62
63 serialize = function(path=".", csvSeries, binSeries, byCols=0, nbSeries=0)
64 {
65 system(paste(path,"/ppam.exe serialize ",csvSeries," ",binSeries," ",byCols," ",nbSeries,
66 sep=""))
67 }
68
69 deserialize = function(path=".", binSeries, csvSeries, ranks="-1", return=TRUE)
70 {
71 system(paste(path,"/ppam.exe deserialize ",binSeries," ",csvSeries," ",ranks,sep=""))
72 if (return)
73 return ( read.table(csvSeries, sep=",") )
74 }