From 993c503ce1b440be6947bc91fbf1fa6098569b51 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Mon, 9 May 2016 20:45:06 +0200 Subject: use gensvm namespace for all crossval/timer/util --- Makefile | 16 ++--- include/crossval.h | 25 -------- include/gensvm_crossval.h | 25 ++++++++ include/gensvm_timer.h | 21 +++++++ include/gensvm_util.h | 27 +++++++++ include/timer.h | 21 ------- include/util.h | 27 --------- src/GenSVMgrid.c | 6 +- src/GenSVMpred.c | 2 +- src/GenSVMtrain.c | 4 +- src/GenSVMtraintest.c | 2 +- src/crossval.c | 143 --------------------------------------------- src/gensvm_crossval.c | 143 +++++++++++++++++++++++++++++++++++++++++++++ src/gensvm_io.c | 2 +- src/gensvm_kernel.c | 2 +- src/gensvm_matrix.c | 2 +- src/gensvm_timer.c | 73 +++++++++++++++++++++++ src/gensvm_train.c | 2 +- src/gensvm_train_dataset.c | 6 +- src/gensvm_util.c | 128 ++++++++++++++++++++++++++++++++++++++++ src/timer.c | 73 ----------------------- src/util.c | 128 ---------------------------------------- 22 files changed, 439 insertions(+), 439 deletions(-) delete mode 100644 include/crossval.h create mode 100644 include/gensvm_crossval.h create mode 100644 include/gensvm_timer.h create mode 100644 include/gensvm_util.h delete mode 100644 include/timer.h delete mode 100644 include/util.h delete mode 100644 src/crossval.c create mode 100644 src/gensvm_crossval.c create mode 100644 src/gensvm_timer.c create mode 100644 src/gensvm_util.c delete mode 100644 src/timer.c delete mode 100644 src/util.c diff --git a/Makefile b/Makefile index e796cb6..f8bc6ff 100644 --- a/Makefile +++ b/Makefile @@ -13,35 +13,35 @@ all: lib/libgensvm.a $(EXECS) override LDFLAGS+=-lcblas -llapack -lm lib/libgensvm.a: \ - src/crossval.o \ src/libGenSVM.o \ + src/gensvm_crossval.o \ src/gensvm_init.o \ src/gensvm_io.o \ src/gensvm_kernel.o \ src/gensvm_lapack.o \ src/gensvm_matrix.o \ src/gensvm_pred.o \ + src/gensvm_strutil.o \ src/gensvm_sv.o \ src/gensvm_train.o \ src/gensvm_train_dataset.o \ - src/strutil.o \ - src/timer.o \ - src/util.o + src/gensvm_timer.o \ + src/gensvm_util.o @ar rcs lib/libgensvm.a \ - src/crossval.o \ src/libGenSVM.o \ + src/gensvm_crossval.o \ src/gensvm_init.o \ src/gensvm_io.o \ src/gensvm_matrix.o \ src/gensvm_kernel.o \ src/gensvm_lapack.o \ src/gensvm_pred.o \ + src/gensvm_strutil.o \ src/gensvm_sv.o \ src/gensvm_train.o \ src/gensvm_train_dataset.o \ - src/strutil.o \ - src/timer.o \ - src/util.o + src/gensvm_timer.o \ + src/gensvm_util.o @echo libgensvm.a... gensvm: src/GenSVMtraintest.c lib/libgensvm.a diff --git a/include/crossval.h b/include/crossval.h deleted file mode 100644 index fa3cca7..0000000 --- a/include/crossval.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @file crossval.h - * @author Gertjan van den Burg - * @date January, 2014 - * @brief Header file for crossval.c - * - * @details - * Contains function declarations for functions needed for performing cross - * validation on GenData structures. - * - */ - -#ifndef GENSVM_CROSSVAL_H -#define GENSVM_CROSSVAL_H - -#include "globals.h" - -// forward delaration -struct GenData; - -void gensvm_make_cv_split(long N, long folds, long *cv_idx); -void gensvm_get_tt_split(struct GenData *full_data, struct GenData *train_data, - struct GenData *test_data, long *cv_idx, long fold_idx); - -#endif diff --git a/include/gensvm_crossval.h b/include/gensvm_crossval.h new file mode 100644 index 0000000..fa3cca7 --- /dev/null +++ b/include/gensvm_crossval.h @@ -0,0 +1,25 @@ +/** + * @file crossval.h + * @author Gertjan van den Burg + * @date January, 2014 + * @brief Header file for crossval.c + * + * @details + * Contains function declarations for functions needed for performing cross + * validation on GenData structures. + * + */ + +#ifndef GENSVM_CROSSVAL_H +#define GENSVM_CROSSVAL_H + +#include "globals.h" + +// forward delaration +struct GenData; + +void gensvm_make_cv_split(long N, long folds, long *cv_idx); +void gensvm_get_tt_split(struct GenData *full_data, struct GenData *train_data, + struct GenData *test_data, long *cv_idx, long fold_idx); + +#endif diff --git a/include/gensvm_timer.h b/include/gensvm_timer.h new file mode 100644 index 0000000..a1b60a7 --- /dev/null +++ b/include/gensvm_timer.h @@ -0,0 +1,21 @@ +/** + * @file timer.h + * @author Gertjan van den Burg + * @date August, 2013 + * @brief Header file for timer.c + * + * @details + * Function declaration for timer function used to measure computation time. + * + */ + +#ifndef GENSVM_TIMER_H +#define GENSVM_TIMER_H + +#include "globals.h" + +double elapsed_time(clock_t s_time, clock_t e_time); + +void get_time_string(char *buffer); + +#endif diff --git a/include/gensvm_util.h b/include/gensvm_util.h new file mode 100644 index 0000000..fe8d2a3 --- /dev/null +++ b/include/gensvm_util.h @@ -0,0 +1,27 @@ +/** + * @file util.h + * @author Gertjan van den Burg + * @date August, 2013 + * @brief Header file for util.c + * + * @details + * Function declarations for utility functions of the program. + * + */ + +#ifndef GENSVM_UTIL_H +#define GENSVM_UTIL_H + +#include "globals.h" + +// forward declarations +struct GenData; +struct GenModel; + +// function declarations +int gensvm_check_argv(int argc, char **argv, char *str); +int gensvm_check_argv_eq(int argc, char **argv, char *str); + +void note(const char *fmt,...); + +#endif diff --git a/include/timer.h b/include/timer.h deleted file mode 100644 index a1b60a7..0000000 --- a/include/timer.h +++ /dev/null @@ -1,21 +0,0 @@ -/** - * @file timer.h - * @author Gertjan van den Burg - * @date August, 2013 - * @brief Header file for timer.c - * - * @details - * Function declaration for timer function used to measure computation time. - * - */ - -#ifndef GENSVM_TIMER_H -#define GENSVM_TIMER_H - -#include "globals.h" - -double elapsed_time(clock_t s_time, clock_t e_time); - -void get_time_string(char *buffer); - -#endif diff --git a/include/util.h b/include/util.h deleted file mode 100644 index fe8d2a3..0000000 --- a/include/util.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @file util.h - * @author Gertjan van den Burg - * @date August, 2013 - * @brief Header file for util.c - * - * @details - * Function declarations for utility functions of the program. - * - */ - -#ifndef GENSVM_UTIL_H -#define GENSVM_UTIL_H - -#include "globals.h" - -// forward declarations -struct GenData; -struct GenModel; - -// function declarations -int gensvm_check_argv(int argc, char **argv, char *str); -int gensvm_check_argv_eq(int argc, char **argv, char *str); - -void note(const char *fmt,...); - -#endif diff --git a/src/GenSVMgrid.c b/src/GenSVMgrid.c index c02f848..0b6d33e 100644 --- a/src/GenSVMgrid.c +++ b/src/GenSVMgrid.c @@ -21,15 +21,15 @@ #include -#include "crossval.h" #include "gensvm.h" +#include "gensvm_crossval.h" #include "gensvm_io.h" #include "gensvm_init.h" #include "gensvm_pred.h" +#include "gensvm_strutil.h" #include "gensvm_train.h" #include "gensvm_train_dataset.h" -#include "gensvm_strutil.h" -#include "util.h" +#include "gensvm_util.h" #define MINARGS 2 diff --git a/src/GenSVMpred.c b/src/GenSVMpred.c index c874aaf..ef2c97c 100644 --- a/src/GenSVMpred.c +++ b/src/GenSVMpred.c @@ -28,7 +28,7 @@ #include "gensvm_init.h" #include "gensvm_io.h" #include "gensvm_pred.h" -#include "util.h" +#include "gensvm_util.h" #define MINARGS 3 diff --git a/src/GenSVMtrain.c b/src/GenSVMtrain.c index f0b931f..63e2512 100644 --- a/src/GenSVMtrain.c +++ b/src/GenSVMtrain.c @@ -14,13 +14,13 @@ #include #include -#include "gensvm_kernel.h" #include "libGenSVM.h" #include "gensvm.h" #include "gensvm_io.h" #include "gensvm_init.h" +#include "gensvm_kernel.h" #include "gensvm_train.h" -#include "util.h" +#include "gensvm_util.h" #define MINARGS 2 diff --git a/src/GenSVMtraintest.c b/src/GenSVMtraintest.c index e15b340..c5f09f8 100644 --- a/src/GenSVMtraintest.c +++ b/src/GenSVMtraintest.c @@ -19,7 +19,7 @@ #include "gensvm_train.h" #include "gensvm_pred.h" #include "libGenSVM.h" -#include "util.h" +#include "gensvm_util.h" #define MINARGS 2 diff --git a/src/crossval.c b/src/crossval.c deleted file mode 100644 index 85f9341..0000000 --- a/src/crossval.c +++ /dev/null @@ -1,143 +0,0 @@ -/** - * @file crossval.c - * @author Gertjan van den Burg - * @date January 7, 2014 - * @brief Functions for cross validation - * - * @details - * This file contains functions for performing cross validation. The funtion - * gensvm_make_cv_split() creates a cross validation vector for non-stratified - * cross validation. The function gensvm_get_tt_split() creates a train and - * test dataset from a given dataset and a pre-determined CV partition vector. - * See individual function documentation for details. - * - */ - -#include "crossval.h" -#include "gensvm.h" -#include "gensvm_matrix.h" - -/** - * @brief Create a cross validation split vector - * - * @details - * A pre-allocated vector of length N is created which can be used to define - * cross validation splits. The folds are contain between - * @f$ \lfloor N / folds \rfloor @f$ and @f$ \lceil N / folds \rceil @f$ - * instances. An instance is mapped to a partition randomly until all folds - * contain @f$ N \% folds @f$ instances. The zero fold then contains - * @f$ N / folds + N \% folds @f$ instances. These remaining @f$ N \% folds @f$ - * instances are then distributed over the first @f$ N \% folds @f$ folds. - * - * @param[in] N number of instances - * @param[in] folds number of folds - * @param[in,out] cv_idx array of size N which contains the fold index - * for each observation on exit - * - */ -void gensvm_make_cv_split(long N, long folds, long *cv_idx) -{ - long i, j, idx; - - for (i=0; in; - long m = full_data->m; - long K = full_data->K; - - double value; - - test_n = 0; - for (i=0; in = test_n; - train_data->n = train_n; - - train_data->K = K; - test_data->K = K; - - train_data->m = m; - test_data->m = m; - - train_data->y = Calloc(long, train_n); - test_data->y = Calloc(long, test_n); - - train_data->RAW = Calloc(double, train_n*(m+1)); - test_data->RAW = Calloc(double, test_n*(m+1)); - - k = 0; - l = 0; - for (i=0; iy[k] = full_data->y[i]; - for (j=0; jRAW, m+1, i, j); - matrix_set(test_data->RAW, m+1, k, j, value); - } - k++; - } else { - train_data->y[l] = full_data->y[i]; - for (j=0; jRAW, m+1, i, j); - matrix_set(train_data->RAW, m+1, l, j, value); - } - l++; - } - } - - train_data->Z = train_data->RAW; - test_data->Z = test_data->RAW; -} diff --git a/src/gensvm_crossval.c b/src/gensvm_crossval.c new file mode 100644 index 0000000..864e692 --- /dev/null +++ b/src/gensvm_crossval.c @@ -0,0 +1,143 @@ +/** + * @file crossval.c + * @author Gertjan van den Burg + * @date January 7, 2014 + * @brief Functions for cross validation + * + * @details + * This file contains functions for performing cross validation. The funtion + * gensvm_make_cv_split() creates a cross validation vector for non-stratified + * cross validation. The function gensvm_get_tt_split() creates a train and + * test dataset from a given dataset and a pre-determined CV partition vector. + * See individual function documentation for details. + * + */ + +#include "gensvm.h" +#include "gensvm_crossval.h" +#include "gensvm_matrix.h" + +/** + * @brief Create a cross validation split vector + * + * @details + * A pre-allocated vector of length N is created which can be used to define + * cross validation splits. The folds are contain between + * @f$ \lfloor N / folds \rfloor @f$ and @f$ \lceil N / folds \rceil @f$ + * instances. An instance is mapped to a partition randomly until all folds + * contain @f$ N \% folds @f$ instances. The zero fold then contains + * @f$ N / folds + N \% folds @f$ instances. These remaining @f$ N \% folds @f$ + * instances are then distributed over the first @f$ N \% folds @f$ folds. + * + * @param[in] N number of instances + * @param[in] folds number of folds + * @param[in,out] cv_idx array of size N which contains the fold index + * for each observation on exit + * + */ +void gensvm_make_cv_split(long N, long folds, long *cv_idx) +{ + long i, j, idx; + + for (i=0; in; + long m = full_data->m; + long K = full_data->K; + + double value; + + test_n = 0; + for (i=0; in = test_n; + train_data->n = train_n; + + train_data->K = K; + test_data->K = K; + + train_data->m = m; + test_data->m = m; + + train_data->y = Calloc(long, train_n); + test_data->y = Calloc(long, test_n); + + train_data->RAW = Calloc(double, train_n*(m+1)); + test_data->RAW = Calloc(double, test_n*(m+1)); + + k = 0; + l = 0; + for (i=0; iy[k] = full_data->y[i]; + for (j=0; jRAW, m+1, i, j); + matrix_set(test_data->RAW, m+1, k, j, value); + } + k++; + } else { + train_data->y[l] = full_data->y[i]; + for (j=0; jRAW, m+1, i, j); + matrix_set(train_data->RAW, m+1, l, j, value); + } + l++; + } + } + + train_data->Z = train_data->RAW; + test_data->Z = test_data->RAW; +} diff --git a/src/gensvm_io.c b/src/gensvm_io.c index ce92805..01f1db5 100644 --- a/src/gensvm_io.c +++ b/src/gensvm_io.c @@ -14,7 +14,7 @@ #include "gensvm_io.h" #include "gensvm_matrix.h" #include "gensvm_strutil.h" -#include "timer.h" +#include "gensvm_timer.h" /** * @brief Read data from file diff --git a/src/gensvm_kernel.c b/src/gensvm_kernel.c index 5548f34..a6bc9fc 100644 --- a/src/gensvm_kernel.c +++ b/src/gensvm_kernel.c @@ -18,7 +18,7 @@ #include "gensvm_kernel.h" #include "gensvm_lapack.h" #include "gensvm_matrix.h" -#include "util.h" +#include "gensvm_util.h" /** * @brief Do the preprocessing steps needed to perform kernel GenSVM diff --git a/src/gensvm_matrix.c b/src/gensvm_matrix.c index 43f284f..66e3947 100644 --- a/src/gensvm_matrix.c +++ b/src/gensvm_matrix.c @@ -13,7 +13,7 @@ */ #include "gensvm_matrix.h" -#include "util.h" +#include "gensvm_util.h" /** * @brief print a matrix diff --git a/src/gensvm_timer.c b/src/gensvm_timer.c new file mode 100644 index 0000000..a871887 --- /dev/null +++ b/src/gensvm_timer.c @@ -0,0 +1,73 @@ +/** + * @file timer.c + * @author Gertjan van den Burg + * @date January, 2014 + * @brief Utility functions relating to time + * + * @details + * This file contains a simple function for calculating the time in seconds + * elapsed between two clock() calls. It also contains a function for + * generating a string of the current time, used in writing output files. + */ + +#include + +#include "gensvm_timer.h" + +/** + * @brief Calculate the time between two clocks + * + * @param[in] s_time starting time + * @param[in] e_time end time + * @returns time elapsed in seconds + */ +double elapsed_time(clock_t s_time, clock_t e_time) +{ + return ((double) (e_time - s_time))/((double) CLOCKS_PER_SEC); +} + +/** + * @brief Get time string with UTC offset + * + * @details + * Create a string for the current system time. Include an offset of UTC for + * consistency. The format of the generated string is "DDD MMM D HH:MM:SS + * YYYY (UTC +HH:MM)", e.g. "Fri Aug 9, 12:34:56 2013 (UTC +02:00)". + * + * @param[in,out] buffer allocated string buffer, on exit contains + * formatted string + * + */ +void get_time_string(char *buffer) +{ + int diff, hours, minutes; + char timestr[MAX_LINE_LENGTH]; + time_t current_time, lt, gt; + struct tm *lclt; + + // get current time (in epoch) + current_time = time(NULL); + if (current_time == ((time_t)-1)) { + fprintf(stderr, "Failed to compute the current time.\n"); + return; + } + + // convert time to local time and create a string + lclt = localtime(¤t_time); + strftime(timestr, MAX_LINE_LENGTH, "%c", lclt); + if (timestr == NULL) { + fprintf(stderr, "Failed to convert time to string.\n"); + return; + } + + // calculate the UTC offset including DST + lt = mktime(localtime(¤t_time)); + gt = mktime(gmtime(¤t_time)); + diff = -difftime(gt, lt); + hours = (diff/3600); + minutes = (diff%3600)/60; + if (lclt->tm_isdst == 1) + hours++; + + sprintf(buffer, "%s (UTC %+03i:%02i)", timestr, hours, minutes); +} diff --git a/src/gensvm_train.c b/src/gensvm_train.c index 776e546..680d0dd 100644 --- a/src/gensvm_train.c +++ b/src/gensvm_train.c @@ -19,7 +19,7 @@ #include "gensvm_matrix.h" #include "gensvm_sv.h" #include "gensvm_train.h" -#include "util.h" +#include "gensvm_util.h" /** * Maximum number of iterations of the algorithm. diff --git a/src/gensvm_train_dataset.c b/src/gensvm_train_dataset.c index 5925b5c..4f42040 100644 --- a/src/gensvm_train_dataset.c +++ b/src/gensvm_train_dataset.c @@ -12,17 +12,17 @@ #include #include -#include "crossval.h" #include "libGenSVM.h" #include "gensvm.h" +#include "gensvm_crossval.h" #include "gensvm_init.h" #include "gensvm_kernel.h" #include "gensvm_matrix.h" #include "gensvm_train.h" #include "gensvm_train_dataset.h" #include "gensvm_pred.h" -#include "util.h" -#include "timer.h" +#include "gensvm_util.h" +#include "gensvm_timer.h" extern FILE *GENSVM_OUTPUT_FILE; diff --git a/src/gensvm_util.c b/src/gensvm_util.c new file mode 100644 index 0000000..aa4e5d9 --- /dev/null +++ b/src/gensvm_util.c @@ -0,0 +1,128 @@ +/** + * @file util.c + * @author Gertjan van den Burg + * @date January, 2014 + * @brief Utility functions + * + * @details + * This file contains several utility functions for coordinating input and + * output of data and model files. It also contains string functions. + * + */ +#include + +#include "gensvm_util.h" + +FILE *GENSVM_OUTPUT_FILE; ///< The #GENSVM_OUTPUT_FILE specifies the + ///< output stream to which all output is + ///< written. This is done through the + ///< internal (!) + ///< function gensvm_print_string(). The + ///< advantage of using a global output + ///< stream variable is that the output can + ///< temporarily be suppressed by importing + ///< this variable through @c extern and + ///< (temporarily) setting it to NULL. + +/** + * @brief Check if any command line arguments contain string + * + * @details + * Check if any of a given array of command line arguments contains a given + * string. If the string is found, the index of the string in argv is + * returned. If the string is not found, 0 is returned. + * + * This function is copied from MSVMpack/libMSVM.c. + * + * @param[in] argc number of command line arguments + * @param[in] argv command line arguments + * @param[in] str string to find in the arguments + * @returns index of the string in the arguments if found, 0 + * otherwise + */ +int gensvm_check_argv(int argc, char **argv, char *str) +{ + int i; + int arg_str = 0; + for (i=1; i - -#include "timer.h" - -/** - * @brief Calculate the time between two clocks - * - * @param[in] s_time starting time - * @param[in] e_time end time - * @returns time elapsed in seconds - */ -double elapsed_time(clock_t s_time, clock_t e_time) -{ - return ((double) (e_time - s_time))/((double) CLOCKS_PER_SEC); -} - -/** - * @brief Get time string with UTC offset - * - * @details - * Create a string for the current system time. Include an offset of UTC for - * consistency. The format of the generated string is "DDD MMM D HH:MM:SS - * YYYY (UTC +HH:MM)", e.g. "Fri Aug 9, 12:34:56 2013 (UTC +02:00)". - * - * @param[in,out] buffer allocated string buffer, on exit contains - * formatted string - * - */ -void get_time_string(char *buffer) -{ - int diff, hours, minutes; - char timestr[MAX_LINE_LENGTH]; - time_t current_time, lt, gt; - struct tm *lclt; - - // get current time (in epoch) - current_time = time(NULL); - if (current_time == ((time_t)-1)) { - fprintf(stderr, "Failed to compute the current time.\n"); - return; - } - - // convert time to local time and create a string - lclt = localtime(¤t_time); - strftime(timestr, MAX_LINE_LENGTH, "%c", lclt); - if (timestr == NULL) { - fprintf(stderr, "Failed to convert time to string.\n"); - return; - } - - // calculate the UTC offset including DST - lt = mktime(localtime(¤t_time)); - gt = mktime(gmtime(¤t_time)); - diff = -difftime(gt, lt); - hours = (diff/3600); - minutes = (diff%3600)/60; - if (lclt->tm_isdst == 1) - hours++; - - sprintf(buffer, "%s (UTC %+03i:%02i)", timestr, hours, minutes); -} diff --git a/src/util.c b/src/util.c deleted file mode 100644 index 23ee4e5..0000000 --- a/src/util.c +++ /dev/null @@ -1,128 +0,0 @@ -/** - * @file util.c - * @author Gertjan van den Burg - * @date January, 2014 - * @brief Utility functions - * - * @details - * This file contains several utility functions for coordinating input and - * output of data and model files. It also contains string functions. - * - */ -#include - -#include "util.h" - -FILE *GENSVM_OUTPUT_FILE; ///< The #GENSVM_OUTPUT_FILE specifies the - ///< output stream to which all output is - ///< written. This is done through the - ///< internal (!) - ///< function gensvm_print_string(). The - ///< advantage of using a global output - ///< stream variable is that the output can - ///< temporarily be suppressed by importing - ///< this variable through @c extern and - ///< (temporarily) setting it to NULL. - -/** - * @brief Check if any command line arguments contain string - * - * @details - * Check if any of a given array of command line arguments contains a given - * string. If the string is found, the index of the string in argv is - * returned. If the string is not found, 0 is returned. - * - * This function is copied from MSVMpack/libMSVM.c. - * - * @param[in] argc number of command line arguments - * @param[in] argv command line arguments - * @param[in] str string to find in the arguments - * @returns index of the string in the arguments if found, 0 - * otherwise - */ -int gensvm_check_argv(int argc, char **argv, char *str) -{ - int i; - int arg_str = 0; - for (i=1; i