00001 /*************************************************************************** 00002 * @file gnn_dataset_view.c 00003 * @brief Dataset view implementation. 00004 * 00005 * @date : 30-05-04 22:57 00006 * @author : Pedro Ortega C. <peortega@dcc.uchile.cl> 00007 * Copyright 2003 Pedro Ortega C. 00008 ****************************************************************************/ 00009 /* 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00023 */ 00024 00025 00026 00027 /** 00028 * @defgroup gnn_dataset_view_doc gnn_dataset_view : A view for datasets. 00029 * @ingroup gnn_dataset_doc 00030 * @brief View for datasets implementation. 00031 * 00032 * The \ref gnn_dataset_view is a \ref gnn_dataset that provides a transparent 00033 * view for a slice of a ref gnn_dataset, acting as it where a normal dataset. 00034 * 00035 * A \ref gnn_subset can be built upon an arbitrary \ref gnn_dataset. It 00036 * provides a view of a slice of the underlying dataset. For example, 00037 * a subrange of another dataset can be described with a view, or two 00038 * views can be made which provide access to the even and odd patterns 00039 * of a dataset. 00040 * 00041 * A view consist of a <i>starting index</i> \f$I\f$, a <i>stride</i> \f$S\f$ 00042 * and a the <i>number of patterns</i> \f$P\f$. So, the <i>i</i>th-pattern 00043 * of the dataset view corresponds to the underlying dataset's pattern 00044 * with index 00045 * 00046 * \f[ i_{\textrm{real}} = I + S i_{\textrm{view}} \f] 00047 * 00048 * When building a \ref gnn_dataset_view upon a given \ref gnn_dataset, the 00049 * \ref gnn_dataset_view structure <b>won't own</b> the source-dataset. That 00050 * means that if you destroy the \ref gnn_dataset_view structure, the 00051 * underlying \ref gnn_dataset won't be destroyed. It should be destroyed 00052 * manually. 00053 */ 00054 00055 00056 00057 /******************************************/ 00058 /* Include Files */ 00059 /******************************************/ 00060 00061 #include "gnn_dataset_view.h" 00062 00063 00064 00065 /******************************************/ 00066 /* Static Declaration */ 00067 /******************************************/ 00068 00069 static int 00070 gnn_dataset_view_reset (gnn_dataset *set); 00071 00072 static int 00073 gnn_dataset_view_get (gnn_dataset *set, 00074 size_t k, 00075 gsl_vector **x, 00076 gsl_vector **t, 00077 double *p); 00078 00079 static void 00080 gnn_dataset_view_destroy (gnn_dataset *set); 00081 00082 00083 00084 /******************************************/ 00085 /* Static Implementation */ 00086 /******************************************/ 00087 00088 /** 00089 * @brief The "reset" function for a gnn_dataset_view. 00090 * @ingroup gnn_dataset_view_doc 00091 * 00092 * This function resets the \ref gnn_dataset_view. 00093 * 00094 * @param set A pointer to a \ref gnn_dataset_view. 00095 * @return 0 if succeeded. 00096 */ 00097 static int 00098 gnn_dataset_view_reset (gnn_dataset *set) 00099 { 00100 gnn_dataset_view *vset; 00101 00102 assert (set != NULL); 00103 00104 vset = (gnn_dataset_view *) set; 00105 00106 /* reset samplers */ 00107 gnn_dataset_reset (vset->data); 00108 00109 return 0; 00110 } 00111 00112 /** 00113 * @brief The "get" function for a gnn_dataset_view. 00114 * @ingroup gnn_dataset_view_doc 00115 * 00116 * This function is returns the k-th pattern in the dataset view. 00117 * 00118 * @param set A pointer to a \ref gnn_dataset_view. 00119 * @param k The index of the pattern to be retrieved. 00120 * @param x A reference for the input pattern. 00121 * @param t A reference for the output pattern. 00122 * @param p A pointer to a double for the pattern weight. 00123 * @return 0 if succeeded. 00124 */ 00125 static int 00126 gnn_dataset_view_get (gnn_dataset *set, 00127 size_t k, 00128 gsl_vector **x, 00129 gsl_vector **t, 00130 double *p) 00131 { 00132 size_t kp; 00133 gnn_dataset_view *vset; 00134 00135 assert (set != NULL); 00136 00137 /* get view as a dataset view */ 00138 vset = (gnn_dataset_view *) set; 00139 00140 /* compute new index */ 00141 kp = vset->start + vset->stride * k; 00142 00143 return gnn_dataset_get (vset->data, kp, x, t, p); 00144 } 00145 00146 /** 00147 * @brief Destroy function. 00148 * @ingroup gnn_dataset_view_doc 00149 * 00150 * This is the \ref gnn_dataset_view destroy function. 00151 * @param set A pointer to a \ref gnn_dataset_view dataset. 00152 */ 00153 static void 00154 gnn_dataset_view_destroy (gnn_dataset *set) 00155 { 00156 gnn_dataset_view *vset; 00157 00158 assert (set != NULL); 00159 00160 /* get view as a dataset view */ 00161 vset = (gnn_dataset_view *) set; 00162 00163 /* DO NOT DO ANYTHING :-) */ 00164 } 00165 00166 00167 00168 /******************************************/ 00169 /* Public Interface */ 00170 /******************************************/ 00171 00172 /** 00173 * @brief Builds a new dataset view. 00174 * @ingroup gnn_dataset_view_doc 00175 * 00176 * This functions creates a new view for a given dataset. 00177 * 00178 * @param data The \ref gnn_dataset which should serve as pattern source. 00179 * @param start The starting index. 00180 * @param stride The stride for each element. 00181 * @param size The amount of patterns in the new dataset. 00182 * @return Returns a pointer to a new \ref gnn_dataset_view. 00183 */ 00184 gnn_dataset * 00185 gnn_dataset_view_with_stride_new (gnn_dataset *data, 00186 size_t start, size_t stride, size_t size) 00187 { 00188 size_t n; 00189 size_t m; 00190 size_t P; 00191 int status; 00192 gnn_dataset *set; 00193 gnn_dataset_view *vset; 00194 00195 assert (data != NULL); 00196 assert (start >= 0); 00197 assert (stride > 0); 00198 assert (size > 0); 00199 00200 /* get sizes */ 00201 P = gnn_dataset_get_size (data); 00202 n = gnn_dataset_input_get_size (data); 00203 m = gnn_dataset_output_get_size (data); 00204 00205 /* check maximum index */ 00206 if (start + stride * (size - 1) >= P) 00207 { 00208 GSL_ERROR_VAL ("the gnn_dataset_view's size exceeds the source " 00209 "dataset's size", 00210 GSL_EINVAL, NULL); 00211 } 00212 00213 /* alloc memory for dataset */ 00214 vset = (gnn_dataset_view *) malloc (sizeof (gnn_dataset_view)); 00215 if (vset == NULL) 00216 { 00217 GSL_ERROR_VAL ("couldn't allocate memory for " 00218 "gnn_dataset_view structure", 00219 GSL_ENOMEM, NULL); 00220 } 00221 00222 /* get memory dataset view */ 00223 set = (gnn_dataset *) vset; 00224 00225 /* initialize */ 00226 status = gnn_dataset_init (set, 00227 size, 00228 n, 00229 m, 00230 gnn_dataset_view_reset, 00231 gnn_dataset_view_get, 00232 gnn_dataset_view_destroy); 00233 if (status) 00234 { 00235 GSL_ERROR_VAL ("could not initialize gnn_dataset_view", 00236 GSL_EINVAL, NULL); 00237 } 00238 00239 /* set fields */ 00240 vset->data = data; 00241 vset->start = start; 00242 vset->stride = stride; 00243 vset->size = size; 00244 00245 return set; 00246 } 00247 00248 /** 00249 * @brief Builds a new dataset view. 00250 * @ingroup gnn_dataset_view_doc 00251 * 00252 * This functions creates a new view for a given dataset. The stride is 00253 * assumed to be 1. 00254 * 00255 * @param data The \ref gnn_dataset which should serve as pattern source. 00256 * @param start The starting index. 00257 * @param size The amount of patterns in the new dataset. 00258 * @return Returns a pointer to a new \ref gnn_dataset_view. 00259 */ 00260 gnn_dataset * 00261 gnn_dataset_view_new (gnn_dataset *data, size_t start, size_t size) 00262 { 00263 return gnn_dataset_view_with_stride_new (data, start, 1, size); 00264 } 00265 00266 /** 00267 * @brief Returns a pointer to the internal gnn_dataset. 00268 * @ingroup gnn_dataset_view_doc 00269 * 00270 * This function returns a pointer to the internal \ref gnn_dataset structure, 00271 * which is used as source. 00272 * 00273 * @param set A pointer to a \ref gnn_dataset_view dataset. 00274 * @return Returns a pointer to the internal dataset. 00275 */ 00276 gnn_dataset * 00277 gnn_dataset_view_get_dataset (gnn_dataset *set) 00278 { 00279 gnn_dataset_view *vset; 00280 00281 assert (set != NULL); 00282 00283 vset = (gnn_dataset_view *) set; 00284 return vset->data; 00285 } 00286 00287
1.2.18