SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
StreamingFileFromSparseFeatures.h
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2011 Shashwat Lal Das
00008  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
00009  */
00010 #ifndef __STREAMING_FILEFROMSPARSE_H__
00011 #define __STREAMING_FILEFROMSPARSE_H__
00012 
00013 #include <shogun/io/streaming/StreamingFileFromFeatures.h>
00014 #include <shogun/features/SparseFeatures.h>
00015 
00016 namespace shogun
00017 {
00022 template <class T> class CStreamingFileFromSparseFeatures: public CStreamingFileFromFeatures
00023 {
00024 public:
00028     CStreamingFileFromSparseFeatures();
00029 
00035     CStreamingFileFromSparseFeatures(CSparseFeatures<T>* feat);
00036 
00043     CStreamingFileFromSparseFeatures(CSparseFeatures<T>* feat, float64_t* lab);
00044 
00048     virtual ~CStreamingFileFromSparseFeatures();
00049 
00058     virtual void get_sparse_vector(SGSparseVectorEntry<T>* &vec, int32_t &len);
00059 
00069     virtual void get_sparse_vector_and_label(SGSparseVectorEntry<T>* &vec, int32_t &len, float64_t &label);
00070 
00076     void reset_stream()
00077     {
00078         vector_num = 0;
00079     }
00080 
00082     virtual const char* get_name() const
00083     {
00084         return "StreamingFileFromSparseFeatures";
00085 
00086     }
00087 
00088 private:
00092     void init(CSparseFeatures<T>* feat);
00093 
00094 protected:
00096     CSparseFeatures<T>* features;
00097 
00099     int32_t vector_num;
00100 
00101 };
00102 
00103 template <class T>
00104 CStreamingFileFromSparseFeatures<T>::CStreamingFileFromSparseFeatures()
00105     : CStreamingFileFromFeatures()
00106 {
00107     init(NULL);
00108 }
00109 
00110 template <class T>
00111 CStreamingFileFromSparseFeatures<T>::CStreamingFileFromSparseFeatures(CSparseFeatures<T>* feat)
00112     : CStreamingFileFromFeatures(feat)
00113 {
00114     init(feat);
00115 }
00116 
00117 template <class T>
00118 CStreamingFileFromSparseFeatures<T>::CStreamingFileFromSparseFeatures(CSparseFeatures<T>* feat, float64_t* lab)
00119     : CStreamingFileFromFeatures(feat,lab)
00120 {
00121     init(feat);
00122 }
00123 
00124 template <class T>
00125 CStreamingFileFromSparseFeatures<T>::~CStreamingFileFromSparseFeatures()
00126 {
00127     SG_UNREF(features);
00128 }
00129 
00130 template <class T>
00131 void CStreamingFileFromSparseFeatures<T>::init(CSparseFeatures<T>* feat)
00132 {
00133     features = feat;
00134     SG_REF(features);
00135     vector_num=0;
00136 
00137     set_generic<T>();
00138 }
00139 
00140 /* Functions to return the vector from the SparseFeatures object */
00141 template <class T>
00142 void CStreamingFileFromSparseFeatures<T>::get_sparse_vector
00143 (SGSparseVectorEntry<T>*& vector, int32_t& len)
00144 {
00145     if (vector_num >= features->get_num_vectors())
00146     {
00147         vector=NULL;
00148         len=-1;
00149         return;
00150     }
00151 
00152     SGSparseVector<T> vec=
00153             features->get_sparse_feature_vector(vector_num);
00154     vector=vec.features;
00155     len=vec.num_feat_entries;
00156 
00157     /* TODO. check if vector needs to be freed? */
00158 
00159     vector_num++;
00160 }
00161 
00162 /* Functions to return the vector from the SparseFeatures object */
00163 template <class T>
00164 void CStreamingFileFromSparseFeatures<T>::get_sparse_vector_and_label
00165 (SGSparseVectorEntry<T>*& vector, int32_t& len, float64_t& label)
00166 {
00167     get_sparse_vector(vector, len);
00168     label=labels[vector_num];
00169 }
00170 
00171 }
00172 #endif //__STREAMING_FILEFROMSPARSE_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation