1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495openPrintfmoduleL=Listtypegamma=floattypekernel=RBFofgamma|Lineartypefilename=string(* capture everything in case of error *)letcollect_script_and_log=Utls.collect_script_and_logtypenb_columns=inttypesparsity=Dense|Sparseofnb_columnsletread_matrix_strmaybe_sparsedata_fn=matchmaybe_sparsewith|Dense->sprintf"as.matrix(read.table('%s'))"data_fn|Sparsencol->sprintf"read.matrix.csr('%s', fac = FALSE, ncol = %d)"data_fnncol(* train model and return the filename it was saved to upon success *)lettrain?debug:(debug=false)(sparse:sparsity)~cost:costkernel(data_fn:filename)(labels_fn:filename):Result.t=letmodel_fn:filename=Filename.temp_file"orsvm_e1071_model_"".bin"in(* create R script and store it in a temp file *)letr_script_fn=Filename.temp_file"orsvm_e1071_train_"".r"inletkernel_str=matchkernelwith|RBFgamma->sprintf"kernel = 'radial', gamma = %f"gamma|Linear->"kernel = 'linear'"inletread_x_str=read_matrix_strsparsedata_fninUtls.with_out_filer_script_fn(funout->fprintfout"library('e1071')\n\
x = %s\n\
y = as.factor(as.vector(read.table('%s'), mode = 'numeric'))\n\
stopifnot(nrow(x) == length(y))\n\
model <- svm(x, y, type = 'C-classification', scale = FALSE, %s, \
cost = %f)\n\
save(model, file='%s')\n\
quit()\n"read_x_strlabels_fnkernel_strcostmodel_fn);letr_log_fn=Filename.temp_file"orsvm_e1071_train_"".log"in(* execute it *)letcmd=sprintf"R --vanilla --slave < %s 2>&1 > %s"r_script_fnr_log_fninifdebugthenLog.debug"%s"cmd;ifSys.commandcmd<>0thencollect_script_and_logdebugr_script_fnr_log_fnmodel_fnelseUtls.ignore_fst(ifnotdebugthenL.iterSys.remove[r_script_fn;r_log_fn])(Result.Okmodel_fn)(* use model in 'model_fn' to predict decision values for test data in 'data_fn'
and return the filename containing values upon success *)letpredict?debug:(debug=false)(sparse:sparsity)(maybe_model_fn:Result.t)(data_fn:filename):Result.t=matchmaybe_model_fnwith|Errorerr->Errorerr|Okmodel_fn->letpredictions_fn=Filename.temp_file"orsvm_e1071_predictions_"".txt"in(* create R script in temp file *)letr_script_fn=Filename.temp_file"orsvm_e1071_predict_"".r"inletread_x_str=read_matrix_strsparsedata_fninUtls.with_out_filer_script_fn(funout->fprintfout"library('e1071')\n\
newdata = %s\n\
load('%s')\n\
values = attributes(predict(model, newdata, decision.values = TRUE)\
)$decision.values\n\
stopifnot(nrow(newdata) == length(values))\n\
write.table(values, file = '%s', sep = '\\n', \
row.names = FALSE, col.names = FALSE)\n\
quit()\n"read_x_strmodel_fnpredictions_fn);(* execute it *)letr_log_fn=Filename.temp_file"orsvm_e1071_predict_"".log"inletcmd=sprintf"R --vanilla --slave < %s 2>&1 > %s"r_script_fnr_log_fninifdebugthenLog.debug"%s"cmd;ifSys.commandcmd<>0thencollect_script_and_logdebugr_script_fnr_log_fnpredictions_fnelseUtls.ignore_fst(ifnotdebugthenL.iterSys.remove[r_script_fn;r_log_fn])(Result.Okpredictions_fn)(* read predicted decision values *)letread_predictions?debug:(debug=false)=Utls.read_predictionsdebug