[libfann] 42/242: Improved error handling.
Christian Kastner
chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:18 UTC 2014
This is an automated email from the git hooks/post-receive script.
chrisk-guest pushed a commit to tag Version2_0_0
in repository libfann.
commit 333b03f999329823debb74972da8dde815f26889
Author: Evan Nemerson <evan at coeus-group.com>
Date: Sun Jan 11 11:45:26 2004 +0000
Improved error handling.
---
src/fann.c | 75 ++++++++++++++++++++++++++++++++++++++-------
src/fann_internal.c | 64 ++++++++++++++++++++++++++++++++++++--
src/include/Makefile.am | 2 +-
src/include/fann.h | 21 +++++++++++++
src/include/fann_data.h | 6 ++++
src/include/fann_errno.h | 69 +++++++++++++++++++++++++++++++++++++++++
src/include/fann_internal.h | 2 ++
7 files changed, 224 insertions(+), 15 deletions(-)
diff --git a/src/fann.c b/src/fann.c
index 385a137..5e60b95 100644
--- a/src/fann.c
+++ b/src/fann.c
@@ -26,6 +26,8 @@
#include "fann.h"
+#include "fann_errno.h"
+
/* create a neural network.
*/
struct fann * fann_create_array(float connection_rate, float learning_rate, unsigned int num_layers, unsigned int * layers)
@@ -58,6 +60,10 @@ struct fann * fann_create_array(float connection_rate, float learning_rate, unsi
multiplier = ann->multiplier;
#endif
fann_initialise_result_array(ann);
+
+ /* Reset the errno/errstr information */
+ fann_reset_errstr(ann);
+ fann_reset_errno(ann);
/* determine how many neurons there should be in each layer */
i = 0;
@@ -276,7 +282,7 @@ struct fann * fann_create_from_file(const char *configuration_file)
FILE *conf = fopen(configuration_file, "r");
if(!conf){
- printf("Unable to open configuration file \"%s\" for reading.\n", configuration_file);
+ fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_R, configuration_file);
return NULL;
}
@@ -285,25 +291,29 @@ struct fann * fann_create_from_file(const char *configuration_file)
/* compares the version information */
if(strncmp(read_version, FANN_CONF_VERSION"\n", strlen(FANN_CONF_VERSION"\n")) != 0){
- printf("Wrong version of configuration file, aborting read of configuration file \"%s\".\n", configuration_file);
+ fann_error(NULL, FANN_E_WRONG_CONFIG_VERSION, configuration_file);
return NULL;
}
#ifdef FIXEDFANN
if(fscanf(conf, "%u\n", &decimal_point) != 1){
- printf("Error reading info from configuration file \"%s\".\n", configuration_file);
+ fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
return NULL;
}
multiplier = 1 << decimal_point;
#endif
if(fscanf(conf, "%u %f %f %u %u "FANNSCANF" "FANNSCANF"\n", &num_layers, &learning_rate, &connection_rate, &activation_function_hidden, &activation_function_output, &activation_hidden_steepness, &activation_output_steepness) != 7){
- printf("Error reading info from configuration file \"%s\".\n", configuration_file);
+ fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
return NULL;
}
ann = fann_allocate_structure(learning_rate, num_layers);
ann->connection_rate = connection_rate;
+
+ /* Reset the errno/errstr information */
+ fann_reset_errstr(ann);
+ fann_reset_errno(ann);
#ifdef FIXEDFANN
ann->decimal_point = decimal_point;
@@ -324,7 +334,7 @@ struct fann * fann_create_from_file(const char *configuration_file)
/* determine how many neurons there should be in each layer */
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++){
if(fscanf(conf, "%u ", &layer_size) != 1){
- printf("Error reading neuron info from configuration file \"%s\".\n", configuration_file);
+ fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
return ann;
}
/* we do not allocate room here, but we make sure that
@@ -347,7 +357,7 @@ struct fann * fann_create_from_file(const char *configuration_file)
for(neuron_it = ann->first_layer->first_neuron;
neuron_it != last_neuron; neuron_it++){
if(fscanf(conf, "%u ", &neuron_it->num_connections) != 1){
- printf("Error reading neuron info from configuration file \"%s\".\n", configuration_file);
+ fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
return ann;
}
ann->total_connections += neuron_it->num_connections;
@@ -361,7 +371,7 @@ struct fann * fann_create_from_file(const char *configuration_file)
for(i = 0; i < ann->total_connections; i++){
if(fscanf(conf, "(%u "FANNSCANF") ", &input_neuron, &weights[i]) != 2){
- printf("Error reading connections from configuration file \"%s\".\n", configuration_file);
+ fann_error(ann, FANN_E_CANT_READ_CONNECTIONS, configuration_file);
return ann;
}
connected_neurons[i] = first_neuron+input_neuron;
@@ -651,12 +661,12 @@ struct fann_train_data* fann_read_train_from_file(char *filename)
data = (struct fann_train_data *)malloc(sizeof(struct fann_train_data));
if(!file){
- printf("Unable to open train data file \"%s\" for reading.\n", filename);
+ fann_error(NULL, FANN_E_CANT_OPEN_TD_R, filename);
return NULL;
}
if(fscanf(file, "%u %u %u\n", &num_data, &num_input, &num_output) != 3){
- printf("Error reading info from train data file \"%s\", line: %d.\n", filename, line);
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
return NULL;
}
line++;
@@ -671,7 +681,7 @@ struct fann_train_data* fann_read_train_from_file(char *filename)
data->input[i] = (fann_type *)calloc(num_input, sizeof(fann_type));
for(j = 0; j != num_input; j++){
if(fscanf(file, FANNSCANF" ", &data->input[i][j]) != 1){
- printf("Error reading info from train data file \"%s\", line: %d.\n", filename, line);
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
return NULL;
}
}
@@ -680,7 +690,7 @@ struct fann_train_data* fann_read_train_from_file(char *filename)
data->output[i] = (fann_type *)calloc(num_output, sizeof(fann_type));
for(j = 0; j != num_output; j++){
if(fscanf(file, FANNSCANF" ", &data->output[i][j]) != 1){
- printf("Error reading info from train data file \"%s\", line: %d.\n", filename, line);
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
return NULL;
}
}
@@ -1006,3 +1016,46 @@ fann_type* fann_run(struct fann *ann, fann_type *input)
}
return ann->output;
}
+
+/* resets the last error number
+ */
+void fann_reset_errno(struct fann *ann)
+{
+ ann->errno = 0;
+}
+
+/* resets the last errstr
+ */
+void fann_reset_errstr(struct fann *ann)
+{
+ if ( ann->errstr != NULL )
+ free(ann->errstr);
+ ann->errstr = NULL;
+}
+
+/* returns the last error number
+ */
+unsigned int fann_get_errno(struct fann *ann)
+{
+ return ann->errno;
+}
+
+/* returns the last errstr
+ */
+char * fann_get_errstr(struct fann *ann)
+{
+ char *errstr = ann->errstr;
+
+ fann_reset_errno(ann);
+ fann_reset_errstr(ann);
+
+ return errstr;
+}
+
+/* prints the last error to stderr
+ */
+void fann_print_error(struct fann *ann) {
+ if ( ann->errno != FANN_E_NO_ERROR ){
+ fputs(ann->errstr, stderr);
+ }
+}
diff --git a/src/fann_internal.c b/src/fann_internal.c
index 5a652e5..7134f33 100644
--- a/src/fann_internal.c
+++ b/src/fann_internal.c
@@ -22,10 +22,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <string.h>
#include <sys/time.h>
#include <time.h>
+#include <stdarg.h>
#include "compat_time.h"
#include "fann.h"
#include "fann_internal.h"
+#include "fann_errno.h"
/* Allocates the main structure and sets some default values.
*/
@@ -128,7 +130,7 @@ void fann_allocate_connections(struct fann *ann)
}
if(connections_so_far != ann->total_connections){
- printf("ERROR connections_so_far=%d, total_connections=%d\n", connections_so_far, ann->total_connections);
+ fann_error(ann, FANN_E_WRONG_NUM_CONNECTIONS, connections_so_far, ann->total_connections);
exit(0);
}
}
@@ -154,7 +156,7 @@ int fann_save_internal(struct fann *ann, const char *configuration_file, unsigne
FILE *conf = fopen(configuration_file, "w+");
if(!conf){
- printf("Unable to open configuration file \"%s\" for writing.\n", configuration_file);
+ fann_error(ann, FANN_E_CANT_OPEN_CONFIG_W, configuration_file);
return -1;
}
@@ -298,7 +300,7 @@ void fann_save_train_internal(struct fann_train_data* data, char *filename, unsi
FILE *file = fopen(filename, "w");
if(!file){
- printf("Unable to open train data file \"%s\" for writing.\n", filename);
+ fann_error(NULL, FANN_E_CANT_OPEN_TD_W, filename);
return;
}
@@ -442,3 +444,59 @@ void fann_seed_rand()
}
srand(foo);
}
+
+/* Populate the error information
+ */
+void fann_error(struct fann *ann, const unsigned int errno, ...)
+{
+ va_list ap;
+ unsigned int flag = 0;
+ char * errstr;
+
+ errstr = (char *)malloc(FANN_ERRSTR_MAX);
+
+ va_start(ap, errno);
+ switch ( errno ) {
+ case FANN_E_NO_ERROR:
+ break;
+ case FANN_E_CANT_OPEN_CONFIG_R:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Unable to open configuration file \"%s\" for reading.\n", ap);
+ break;
+ case FANN_E_CANT_OPEN_CONFIG_W:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Unable to open configuration file \"%s\" for writing.\n", ap);
+ break;
+ case FANN_E_WRONG_CONFIG_VERSION:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Wrong version of configuration file, aborting read of configuration file \"%s\".\n", ap);
+ break;
+ case FANN_E_CANT_READ_CONFIG:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Error reading info from configuration file \"%s\".\n", ap);
+ break;
+ case FANN_E_CANT_READ_NEURON:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Error reading neuron info from configuration file \"%s\".\n", ap);
+ break;
+ case FANN_E_CANT_READ_CONNECTIONS:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Error reading connections from configuration file \"%s\".\n", ap);
+ break;
+ case FANN_E_WRONG_NUM_CONNECTIONS:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "ERROR connections_so_far=%d, total_connections=%d\n", ap);
+ break;
+ case FANN_E_CANT_OPEN_TD_W:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Unable to open train data file \"%s\" for writing.\n", ap);
+ break;
+ case FANN_E_CANT_OPEN_TD_R:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Unable to open train data file \"%s\" for writing.\n", ap);
+ break;
+ case FANN_E_CANT_READ_TD:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Error reading info from train data file \"%s\", line: %d.\n", ap);
+ break;
+ default:
+ vsnprintf(errstr, FANN_ERRSTR_MAX, "Unknown error.", ap);
+ break;
+ }
+ va_end(ap);
+
+ if ( ann == NULL )
+ fprintf(stderr, "FANN Error %d: %s", errstr);
+ else
+ ann->errstr = errstr;
+}
diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index d76c625..776033d 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -1 +1 @@
-include_HEADERS = fann.h doublefann.h fann_internal.h floatfann.h doublefann.h fann_data.h fixedfann.h compat_time.h
+include_HEADERS = fann.h doublefann.h fann_internal.h floatfann.h doublefann.h fann_data.h fixedfann.h compat_time.h fann_errno.h
diff --git a/src/include/fann.h b/src/include/fann.h
index 5990925..754e122 100644
--- a/src/include/fann.h
+++ b/src/include/fann.h
@@ -267,6 +267,27 @@ float fann_get_error(struct fann *ann);
*/
void fann_reset_error(struct fann *ann);
+/* resets the last error number
+ */
+void fann_reset_errno(struct fann *ann);
+
+/* resets the last error string
+ */
+void fann_reset_errstr(struct fann *ann);
+
+/* returns the last error number
+ */
+unsigned int fann_get_errno(struct fann *ann);
+
+/* returns the last errstr.
+ * This function calls fann_reset_errno and fann_reset_errstr
+ */
+char * fann_get_errstr(struct fann *ann);
+
+/* prints the last error to stderr
+ */
+void fann_print_error(struct fann *ann) ;
+
/* ----- Running�----- */
/* Runs a input through the network, and returns the output.
diff --git a/src/include/fann_data.h b/src/include/fann_data.h
index 2fd3e8c..02e727b 100644
--- a/src/include/fann_data.h
+++ b/src/include/fann_data.h
@@ -143,6 +143,12 @@ struct fann
the real mean square error is error_value/num_errors
*/
float error_value;
+
+ /* The type of error that last occured. */
+ unsigned int errno;
+
+ /* A string representation of the last error. */
+ char * errstr;
};
/* Structure used to store data, for use with training. */
diff --git a/src/include/fann_errno.h b/src/include/fann_errno.h
new file mode 100644
index 0000000..0510a4a
--- /dev/null
+++ b/src/include/fann_errno.h
@@ -0,0 +1,69 @@
+/*
+Fast Artificial Neural Network Library (fann)
+Copyright (C) 2003 Steffen Nissen (lukesky at diku.dk)
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef __fann_errno_h__
+#define __fann_errno_h__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* Maximum length (in bytes) of an error message */
+#define FANN_ERRSTR_MAX 128
+
+enum {
+ /* No error */
+ FANN_E_NO_ERROR = 0,
+
+ /* Unable to open configuration file for reading */
+ FANN_E_CANT_OPEN_CONFIG_R,
+
+ /* Unable to open configuration file for writing */
+ FANN_E_CANT_OPEN_CONFIG_W,
+
+ /* Wrong version of configuration file */
+ FANN_E_WRONG_CONFIG_VERSION,
+
+ /* Error reading info from configuration file */
+ FANN_E_CANT_READ_CONFIG,
+
+ /* Error reading neuron info from configuration file */
+ FANN_E_CANT_READ_NEURON,
+
+ /* Error reading connections from configuration file */
+ FANN_E_CANT_READ_CONNECTIONS,
+
+ /* Number of connections not equal to the number expected. */
+ FANN_E_WRONG_NUM_CONNECTIONS,
+
+ /* Unable to open train data file for writing */
+ FANN_E_CANT_OPEN_TD_W,
+
+ /* Unable to open train data file for reading. */
+ FANN_E_CANT_OPEN_TD_R,
+
+ /* Error reading training data from file. */
+ FANN_E_CANT_READ_TD
+};
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __fann_errno_h__ */
diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h
index 50e5322..f27640f 100644
--- a/src/include/fann_internal.h
+++ b/src/include/fann_internal.h
@@ -49,6 +49,8 @@ void fann_initialise_result_array(struct fann *ann);
void fann_update_stepwise_hidden(struct fann *ann);
void fann_update_stepwise_output(struct fann *ann);
+void fann_error(struct fann *ann, unsigned int errno, ...);
+
/* called fann_max, in order to not interferre with predefined versions of max */
#define fann_max(x, y) (((x) > (y)) ? (x) : (y))
#define fann_min(x, y) (((x) < (y)) ? (x) : (y))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/libfann.git
More information about the debian-science-commits
mailing list