From 62c9de24cdbf4b4f30953feba67bf046af26fcf7 Mon Sep 17 00:00:00 2001 From: jjYBdx4IL Date: Thu, 16 Jun 2016 23:50:24 +0200 Subject: [PATCH] modify function signatures to allow passing back errors --- src/fann_cascade.c | 2 +- src/fann_train_data.c | 43 ++++++++++++++-------------- src/include/config.h | 2 +- src/include/fann_internal.h | 2 +- src/include/fann_train.h | 11 +++---- src/include/fann_training_data_cpp.h | 8 +++--- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/fann_cascade.c b/src/fann_cascade.c index 51954907..ec837b69 100644 --- a/src/fann_cascade.c +++ b/src/fann_cascade.c @@ -130,7 +130,7 @@ FANN_EXTERNAL void FANN_API fann_cascadetrain_on_file(struct fann *ann, const ch unsigned int neurons_between_reports, float desired_error) { - struct fann_train_data *data = fann_read_train_from_file(filename); + struct fann_train_data *data = fann_read_train_from_file((struct fann_error *) ann, filename); if(data == NULL) { diff --git a/src/fann_train_data.c b/src/fann_train_data.c index fd706730..e4bb3c44 100644 --- a/src/fann_train_data.c +++ b/src/fann_train_data.c @@ -28,18 +28,18 @@ /* * Reads training data from a file. */ -FANN_EXTERNAL struct fann_train_data *FANN_API fann_read_train_from_file(const char *configuration_file) +FANN_EXTERNAL struct fann_train_data *FANN_API fann_read_train_from_file(struct fann_error *err, const char *configuration_file) { struct fann_train_data *data; FILE *file = fopen(configuration_file, "r"); if(!file) { - fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_R, configuration_file); + fann_error(err, FANN_E_CANT_OPEN_CONFIG_R, configuration_file); return NULL; } - data = fann_read_train_from_fd(file, configuration_file); + data = fann_read_train_from_fd(err, file, configuration_file); fclose(file); return data; } @@ -302,7 +302,7 @@ FANN_EXTERNAL void FANN_API fann_train_on_file(struct fann *ann, const char *fil unsigned int epochs_between_reports, float desired_error) { - struct fann_train_data *data = fann_read_train_from_file(filename); + struct fann_train_data *data = fann_read_train_from_file((struct fann_error *)ann, filename); if(data == NULL) { @@ -806,7 +806,7 @@ int fann_save_train_internal_fd(struct fann_train_data *data, FILE * file, const /* * Creates an empty set of training data */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int num_data, unsigned int num_input, unsigned int num_output) +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(struct fann_error *err, unsigned int num_data, unsigned int num_input, unsigned int num_output) { fann_type *data_input, *data_output; unsigned int i; @@ -815,7 +815,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n if(data == NULL) { - fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM); + fann_error(err, FANN_E_CANT_ALLOCATE_MEM); return NULL; } @@ -827,7 +827,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n data->input = (fann_type **) calloc(num_data, sizeof(fann_type *)); if(data->input == NULL) { - fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM); + fann_error(err, FANN_E_CANT_ALLOCATE_MEM); fann_destroy_train(data); return NULL; } @@ -835,7 +835,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n data->output = (fann_type **) calloc(num_data, sizeof(fann_type *)); if(data->output == NULL) { - fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM); + fann_error(err, FANN_E_CANT_ALLOCATE_MEM); fann_destroy_train(data); return NULL; } @@ -843,7 +843,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n data_input = (fann_type *) calloc(num_input * num_data, sizeof(fann_type)); if(data_input == NULL) { - fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM); + fann_error(err, FANN_E_CANT_ALLOCATE_MEM); fann_destroy_train(data); return NULL; } @@ -851,7 +851,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n data_output = (fann_type *) calloc(num_output * num_data, sizeof(fann_type)); if(data_output == NULL) { - fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM); + fann_error(err, FANN_E_CANT_ALLOCATE_MEM); fann_destroy_train(data); return NULL; } @@ -866,11 +866,11 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n return data; } -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array(unsigned int num_data, unsigned int num_input, fann_type **input, unsigned int num_output, fann_type **output) +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array(struct fann_error *err, unsigned int num_data, unsigned int num_input, fann_type **input, unsigned int num_output, fann_type **output) { unsigned int i; struct fann_train_data *data; - data = fann_create_train(num_data, num_input, num_output); + data = fann_create_train(err, num_data, num_input, num_output); if(data == NULL) return NULL; @@ -884,11 +884,11 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array( return data; } -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(unsigned int num_data, unsigned int num_input, fann_type *input, unsigned int num_output, fann_type *output) +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(struct fann_error *err, unsigned int num_data, unsigned int num_input, fann_type *input, unsigned int num_output, fann_type *output) { unsigned int i; struct fann_train_data *data; - data = fann_create_train(num_data, num_input, num_output); + data = fann_create_train(err, num_data, num_input, num_output); if(data == NULL) return NULL; @@ -906,7 +906,8 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(unsigned /* * Creates training data from a callback function. */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_callback(unsigned int num_data, +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_callback(struct fann_error *err, + unsigned int num_data, unsigned int num_input, unsigned int num_output, void (FANN_API *user_function)( unsigned int, @@ -916,7 +917,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_callback( fann_type * )) { unsigned int i; - struct fann_train_data *data = fann_create_train(num_data, num_input, num_output); + struct fann_train_data *data = fann_create_train(err, num_data, num_input, num_output); if(data == NULL) { return NULL; @@ -948,7 +949,7 @@ FANN_EXTERNAL fann_type * FANN_API fann_get_train_output(struct fann_train_data /* * INTERNAL FUNCTION Reads training data from a file descriptor. */ -struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filename) +struct fann_train_data *fann_read_train_from_fd(struct fann_error *err, FILE * file, const char *filename) { unsigned int num_input, num_output, num_data, i, j; unsigned int line = 1; @@ -956,12 +957,12 @@ struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filenam if(fscanf(file, "%u %u %u\n", &num_data, &num_input, &num_output) != 3) { - fann_error(NULL, FANN_E_CANT_READ_TD, filename, line); + fann_error(err, FANN_E_CANT_READ_TD, filename, line); return NULL; } line++; - data = fann_create_train(num_data, num_input, num_output); + data = fann_create_train(err, num_data, num_input, num_output); if(data == NULL) { return NULL; @@ -973,7 +974,7 @@ struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filenam { if(fscanf(file, FANNSCANF " ", &data->input[i][j]) != 1) { - fann_error(NULL, FANN_E_CANT_READ_TD, filename, line); + fann_error(err, FANN_E_CANT_READ_TD, filename, line); fann_destroy_train(data); return NULL; } @@ -984,7 +985,7 @@ struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filenam { if(fscanf(file, FANNSCANF " ", &data->output[i][j]) != 1) { - fann_error(NULL, FANN_E_CANT_READ_TD, filename, line); + fann_error(err, FANN_E_CANT_READ_TD, filename, line); fann_destroy_train(data); return NULL; } diff --git a/src/include/config.h b/src/include/config.h index f2fb1c8a..8e1c98e9 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -2,7 +2,7 @@ /* #undef PACKAGE */ /* Version number of package */ -#define VERSION "2.2.0" +/* #undef VERSION */ /* Define for the x86_64 CPU famyly */ /* #undef X86_64 */ diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h index 81787b3d..693d46cb 100644 --- a/src/include/fann_internal.h +++ b/src/include/fann_internal.h @@ -76,7 +76,7 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, . void fann_init_error_data(struct fann_error *errdat); struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file); -struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filename); +struct fann_train_data *fann_read_train_from_fd(struct fann_error * err, FILE * file, const char *filename); void fann_compute_MSE(struct fann *ann, fann_type * desired_output); void fann_update_output_weights(struct fann *ann); diff --git a/src/include/fann_train.h b/src/include/fann_train.h index ae377d96..2748d922 100644 --- a/src/include/fann_train.h +++ b/src/include/fann_train.h @@ -249,7 +249,7 @@ FANN_EXTERNAL float FANN_API fann_test_data(struct fann *ann, struct fann_train_ This function appears in FANN >= 1.0.0 */ -FANN_EXTERNAL struct fann_train_data *FANN_API fann_read_train_from_file(const char *filename); +FANN_EXTERNAL struct fann_train_data *FANN_API fann_read_train_from_file(struct fann_error * err, const char *filename); /* Function: fann_create_train @@ -261,7 +261,7 @@ FANN_EXTERNAL struct fann_train_data *FANN_API fann_read_train_from_file(const c This function appears in FANN >= 2.2.0 */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int num_data, unsigned int num_input, unsigned int num_output); +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(struct fann_error *err, unsigned int num_data, unsigned int num_input, unsigned int num_output); /* Function: fann_create_train_pointer_array Creates an training data struct and fills it with data from provided arrays of pointer. @@ -276,7 +276,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train(unsigned int n This function appears in FANN >= 2.3.0 */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array(unsigned int num_data, unsigned int num_input, fann_type **input, unsigned int num_output, fann_type **output); +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array(struct fann_error *err, unsigned int num_data, unsigned int num_input, fann_type **input, unsigned int num_output, fann_type **output); /* Function: fann_create_train_array Creates an training data struct and fills it with data from provided arrays, where the arrays must have the dimensions: @@ -293,7 +293,7 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_pointer_array( This function appears in FANN >= 2.3.0 */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(unsigned int num_data, unsigned int num_input, fann_type *input, unsigned int num_output, fann_type *output); +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(struct fann_error *err, unsigned int num_data, unsigned int num_input, fann_type *input, unsigned int num_output, fann_type *output); /* Function: fann_create_train_from_callback Creates the training data struct from a user supplied function. @@ -325,7 +325,8 @@ FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_array(unsigned This function appears in FANN >= 2.1.0 */ -FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_callback(unsigned int num_data, +FANN_EXTERNAL struct fann_train_data * FANN_API fann_create_train_from_callback(struct fann_error *err, + unsigned int num_data, unsigned int num_input, unsigned int num_output, void (FANN_API *user_function)( unsigned int, diff --git a/src/include/fann_training_data_cpp.h b/src/include/fann_training_data_cpp.h index b85d0551..aca02d2e 100644 --- a/src/include/fann_training_data_cpp.h +++ b/src/include/fann_training_data_cpp.h @@ -106,7 +106,7 @@ namespace FANN { */ bool read_train_from_file(const std::string &filename) { destroy_train(); - train_data = fann_read_train_from_file(filename.c_str()); + train_data = fann_read_train_from_file(NULL, filename.c_str()); return (train_data != NULL); } @@ -333,7 +333,7 @@ namespace FANN { void set_train_data(unsigned int num_data, unsigned int num_input, fann_type **input, unsigned int num_output, fann_type **output) { - set_train_data(fann_create_train_pointer_array(num_data, num_input, input, num_output, output)); + set_train_data(fann_create_train_pointer_array(NULL, num_data, num_input, input, num_output, output)); } /* Method: set_train_data @@ -357,7 +357,7 @@ namespace FANN { void set_train_data(unsigned int num_data, unsigned int num_input, fann_type *input, unsigned int num_output, fann_type *output) { - set_train_data(fann_create_train_array(num_data, num_input, input, num_output, output)); + set_train_data(fann_create_train_array(NULL, num_data, num_input, input, num_output, output)); } private: @@ -406,7 +406,7 @@ namespace FANN { fann_type *, fann_type *)) { destroy_train(); - train_data = fann_create_train_from_callback(num_data, num_input, num_output, user_function); + train_data = fann_create_train_from_callback(NULL, num_data, num_input, num_output, user_function); } #ifndef FIXEDFANN