LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiLncFeatureFile.h

00001 /*
00002  * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiLncFeatureFile.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 19.04.99
00030  * revisions ..: $Id: ltiLncFeatureFile.h,v 1.10 2006/02/08 12:05:56 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_LNC_FEATURE_FILE_H_
00034 #define _LTI_LNC_FEATURE_FILE_H_
00035 
00036 #include <string>
00037 #include <fstream>
00038 #include <set>
00039 #include <map>
00040 #include "ltiVector.h"
00041 #include "ltiIOFunctor.h"
00042 
00043 namespace lti {
00044 
00045   /**
00046    * parent class for the functors which deal with the ASCII file format
00047    * to store feature vectors.  This is an abstract class an cannot be
00048    * instantiated.  @see loadLnc, saveLnc
00049    */
00050   class lncFeatureFile : public ioFunctor {
00051   public:
00052 
00053     /**
00054      * header of a lnc feature file
00055      *
00056      * The header of the feature file must begin with something like
00057      *
00058      * \code
00059      * SNNS pattern definition file V3.2
00060      * generated at Thu Apr  6 17:12:10 2000
00061      * \endcode
00062      *
00063      * The following lines will be ignore except for those which
00064      * follow following squeme:
00065      *
00066      * \code
00067      * [itemName]:[itemValue]
00068      * \endcode
00069      *
00070      * where <code>[itemName]</code> is a string with no colon (':') and value
00071      * is a valid integer number.
00072      *
00073      * The last line of the header MUST be
00074      * \code
00075      * # FeatureType : xyz
00076      * \endcode
00077      *
00078      * where xyz represents any string
00079      *
00080      * There are five item-pairs, which will be directly parsed and
00081      *  stored in * the correspondent header attributes:
00082      *   - Objects: the number of objects or classes in the file.
00083      *   - No. of patterns: the number of patterns (or feature vector in the
00084      *     file
00085      *   - No. of input units: the dimension of the feature vectors
00086      *   - No. of output units: the number of classes, to which the pattern
00087      *                          belongs.  Usually 1
00088      *   - FeatureType: the name of the feature stored in the file
00089      *
00090      * This five pair will NOT be included in the items map when
00091      * reading the file, and should NOT be present in the items map when
00092      * storing the file.
00093      *
00094      */
00095     class header : public object {
00096     public:
00097       /**
00098        * default constructor
00099        */
00100       header();
00101 
00102       /**
00103        * copy constructor
00104        */
00105       header(const header& other);
00106 
00107       /**
00108        * copy member
00109        */
00110       header& copy(const header& other);
00111 
00112       /**
00113        * total number of object/classes in the file
00114        */
00115       int numberOfObjects;
00116 
00117       /**
00118        * number of patters (or feature vectors) in this file
00119        */
00120       int numberOfPatterns;
00121 
00122       /**
00123        * dimension of the feature vector
00124        */
00125       int numberOfInputUnits;
00126 
00127       /**
00128        * number of classes, to which each pattern belongs (usually 1)
00129        */
00130       int numberOfOutputUnits;
00131 
00132       /**
00133        * other items-pairs will be stored here
00134        */
00135       std::map<std::string,int> items;
00136 
00137       /**
00138        * name of the feature type
00139        */
00140       std::string featureType;
00141 
00142       /**
00143        * save this header on the given stream
00144        */
00145       bool save(std::ostream& dest);
00146 
00147       /**
00148        * load this header from the given stream
00149        */
00150       bool load(std::istream& source);
00151 
00152     };
00153 
00154     /**
00155      * The ID of a class is determined with three attributes:
00156      *
00157      *  - a numerical id number
00158      *  - the name of the class, which may not contain spaces!
00159      *  - a set of attributes.  (here they will be stored in a string)
00160      */
00161     struct patternId {
00162     public:
00163       /**
00164        * the numerical ID of the object or class, to which the pattern belongs
00165        */
00166       int objectId;
00167 
00168       /**
00169        * name of the object or class
00170        */
00171       std::string name;
00172 
00173       /**
00174        * attributes
00175        */
00176       std::string attributes;
00177     };
00178 
00179 
00180     // ----------------------------------------------------
00181     // Parameters-class
00182     // ----------------------------------------------------
00183 
00184     /**
00185      * lncFeatureFile parameters class
00186      */
00187     class parameters : public ioFunctor::parameters {
00188     public:
00189       /**
00190        * default constructor
00191        */
00192       parameters();
00193 
00194       /**
00195        * copy constructor
00196        */
00197       parameters(const parameters& other) : ioFunctor::parameters() {
00198         copy(other);
00199       };
00200 
00201       /**
00202        * copy member
00203        */
00204       parameters& copy(const parameters& other);
00205 
00206       /**
00207        * returns a pointer to a clone of the parameters.
00208        */
00209       virtual functor::parameters* clone() const;
00210 
00211       /**
00212        * returns name of this type
00213        */
00214       virtual const char* getTypeName() const;
00215 
00216       /**
00217        * write the parameters in the given ioHandler
00218        * @param handler the ioHandler to be used
00219        * @param complete if true (the default) the enclosing begin/end will
00220        *        be also written, otherwise only the data block will be written.
00221        * @return true if write was successful
00222        */
00223       virtual bool write(ioHandler& handler,const bool complete=true) const;
00224 
00225       /**
00226        * write the parameters in the given ioHandler
00227        * @param handler the ioHandler to be used
00228        * @param complete if true (the default) the enclosing begin/end will
00229        *        be also written, otherwise only the data block will be written.
00230        * @return true if write was successful
00231        */
00232       virtual bool read(ioHandler& handler,const bool complete=true);
00233 
00234 #     ifdef _LTI_MSC_6
00235       /**
00236        * this function is required by MSVC only, as a workaround for a
00237        * very awful bug, which exists since MSVC V.4.0, and still by
00238        * V.6.0 with all bugfixes (so called "service packs") remains
00239        * there...  This method is also public due to another bug, so please
00240        * NEVER EVER call this method directly: use read() instead
00241        */
00242       bool readMS(ioHandler& handler,const bool complete=true);
00243 
00244       /**
00245        * this function is required by MSVC only, as a workaround for a
00246        * very awful bug, which exists since MSVC V.4.0, and still by
00247        * V.6.0 with all bugfixes (so called "service packs") remains
00248        * there...  This method is also public due to another bug, so please
00249        * NEVER EVER call this method directly: use write() instead
00250        */
00251       bool writeMS(ioHandler& handler,const bool complete=true) const;
00252 #     endif
00253 
00254       // ------------------------------------------------
00255       // the parameters
00256       // ------------------------------------------------
00257 
00258       /**
00259        * specify if a class-name-file (cnc) should be used
00260        */
00261       bool useCnc;
00262 
00263       /**
00264        * name of the class-names-file (cnc file).  This name MUST be
00265        * specified if "useCnc" is true!
00266        */
00267       std::string cncFilename;
00268 
00269       /**
00270        * cnc file type
00271        *
00272        * The cnc files contain one object per line.
00273        * This parameter specifies if the id is expected as first column
00274        * or in the second one.
00275        *
00276        * Default mode: true (i.e. id first column)
00277        */
00278       bool idFirst;
00279     };
00280 
00281     // ---------------------------------
00282   public:
00283 
00284     /**
00285      * destructor
00286      */
00287     virtual ~lncFeatureFile();
00288 
00289     /**
00290      * check the consistency of the file
00291      */
00292     bool checkConsistency();
00293 
00294     /**
00295      * copy member
00296      */
00297     lncFeatureFile& copy(const lncFeatureFile& other);
00298 
00299     /**
00300      * set the header information
00301      */
00302     void setHeader(const header& other) {
00303       theHeader = other;
00304     };
00305 
00306     /**
00307      * get the header information
00308      */
00309     header& getHeader() {
00310       return theHeader;
00311     };
00312 
00313     /**
00314      * get the header information
00315      */
00316     const header& getHeader() const {
00317       return theHeader;
00318     };
00319 
00320     /**
00321      * get the used parameters
00322      */
00323     const parameters& getParameters() const;
00324 
00325   protected:
00326     /**
00327      * default constructor
00328      */
00329     lncFeatureFile();
00330 
00331     /**
00332      * copy constructor
00333      */
00334     lncFeatureFile(const lncFeatureFile& other) 
00335       : ioFunctor() {
00336       copy(other);
00337     };
00338 
00339     /**
00340      * use the parameter values to start a reading/writing session
00341      */
00342     bool initialize(const bool deleteFile = false);
00343 
00344     /**
00345      * load next vector in the feature file
00346      * @return true if everything is ok, false if there is no more data
00347      *              available on the file.
00348      */
00349     bool loadNext(dvector& theVct,patternId& id);
00350 
00351     /**
00352      * save next vector at the end of the feature file
00353      * @return true if everything is ok, if not an exception will be
00354      *              thrown
00355      *
00356      */
00357     bool saveNext(const dvector& theVct, const patternId& id);
00358 
00359     /**
00360      * check if the input stream is ready to be readed
00361      */
00362     bool isReadyToRead() const {
00363       return (!input.eof() && input.good());
00364     };
00365 
00366     /**
00367      * shortcut to the the number of patterns of the file
00368      */
00369     int getNumberOfPatterns() const {return theHeader.numberOfPatterns;};
00370 
00371     /**
00372      * shortcut to the the dimension of the vectors on the file
00373      */
00374     int getNumberOfInputUnits() const {return theHeader.numberOfInputUnits;};
00375 
00376     /**
00377      * the header of the file actually being used
00378      */
00379     header theHeader;
00380 
00381     /**
00382      * read the cnc file
00383      */
00384     bool loadCncFile();
00385 
00386     /**
00387      * write the whole name-id map to the given cnc file
00388      * (the contents of the file will be destroyed if the file already
00389      *  exists)
00390      */
00391     bool writeCncFile(const std::string& filename);
00392 
00393     /**
00394      * append a class name to the cnc-file (class-name-file)
00395      */
00396     bool writeToCncFile(const std::string& name);
00397 
00398     /**
00399      * specify if the file already existed
00400      */
00401     bool mayCheckConsistence;
00402 
00403     /**
00404      * the first time the file is readed
00405      */
00406     bool initialized;
00407 
00408     /**
00409      * the input stream when reading a file
00410      */
00411     std::ifstream input;
00412 
00413     /**
00414      * the output stream when writing a file
00415      */
00416     std::ofstream output;
00417 
00418     /**
00419      * the table with the correspondence between class name and id
00420      */
00421     std::map<std::string,int> nameToIdMap;
00422 
00423     /**
00424      * a set with the ids actually being used
00425      */
00426     std::set<int> usedObjects;
00427 
00428     /**
00429      * read from the given stream a pattern (class name, data and class id)
00430      */
00431     bool loadPattern(std::istream& source,dvector& theVct,patternId& id);
00432 
00433     /**
00434      * write in the given stream a pattern (class name, data and class id)
00435      */
00436     bool savePattern(std::ostream& dest,
00437                      const dvector& theVct,
00438                      const patternId& id) const;
00439 
00440   };
00441 
00442   /**
00443    * class to load lnc-files (ascii format to store feature vectors)
00444    * @see saveLnc
00445    */
00446   class loadLnc : public lncFeatureFile {
00447   public:
00448 
00449     typedef lncFeatureFile::parameters parameters;
00450 
00451     /**
00452      * Default constructor
00453      */
00454     loadLnc();
00455 
00456     /**
00457      * Constructor with parameters
00458      */
00459     loadLnc(const parameters& par);
00460 
00461     /**
00462      * load the next vector in the file.
00463      * @param vct the vector object, where the readed data will be written
00464      * @param id the id of the class the vector belongs to.
00465      * @return true if everything is ok, false if there
00466      *         is no more data left on the file. If the file is
00467      *         corrupted, a lti::exception will be thrown.
00468      */
00469     bool apply(dvector& vct,patternId& id);
00470 
00471     /**
00472      * load all the vectors in the file and leave the results in the given
00473      * matrix.  The i-th row of the matrix will be the i-th vector on the file.
00474      * @param vcts the matrix where the vectors will be written
00475      * @param ids a vector the class ids for each vector in the matrix.  The
00476      *            i-th element of the vector contains the id of the i-th row
00477      *            of the matrix.
00478      * @return true if everything is ok or false if the file is corrupted or
00479      *              not found.
00480      */
00481     bool apply(dmatrix& vcts,ivector& ids);
00482 
00483     /**
00484      * load all the vectors in the file and leave the results in the given
00485      * matrix.  The i-th row of the matrix will be the i-th vector on the file.
00486      * @param vcts the matrix where the vectors will be written
00487      * @param ids a vector the class ids for each vector in the matrix.  The
00488      *            i-th element of the vector contains the id of the i-th row
00489      *            of the matrix.
00490      * @param names a table with the corresponding names for each object id
00491      * @return true if everything is ok or false if the file is corrupted or
00492      *              not found.
00493      */
00494     bool apply(dmatrix& vcts,ivector& ids,std::map<int,std::string>& names);
00495 
00496     /**
00497      * load all the vectors in the file and leave the results in the given
00498      * matrix.  The i-th row of the matrix will be the i-th vector on the file.
00499      * @param vcts the matrix where the vectors will be written
00500      * @param ids a vector the class ids for each vector in the matrix.  The
00501      *            i-th element of the vector contains the id of the i-th row
00502      *            of the matrix.
00503      * @param attribs a vector of std::string with the attributes for each
00504      *                pattern in the file
00505      * @param names a table with the corresponding names for each object id
00506      * @return true if everything is ok or false if the file is corrupted or
00507      *              not found.
00508      */
00509     bool apply(dmatrix& vcts,
00510                ivector& ids,
00511                std::vector<std::string>& attribs,
00512                std::map<int,std::string>& names);
00513 
00514     /**
00515      * load all the vectors in the file and leave the results in the given
00516      * matrix.  The i-th row of the matrix will be the i-th vector on the file.
00517      * @param vcts the matrix where the vectors will be written
00518      * @param ids a vector, the class ids for each vector in the matrix.  The
00519      *            i-th element of the vector contains the id of the i-th row
00520      *            of the matrix.
00521      * @param attribs a vector of std::string with the attributes for each
00522      *                pattern in the file
00523      * @return true if everything is ok or false if the file is corrupted or
00524      *              not found.
00525      */
00526     bool apply(dmatrix& vcts,
00527                ivector& ids,
00528                std::vector<std::string>& attribs);
00529 
00530     /**
00531      * shortcut to read the given lnc file, ignoring the filenames
00532      * @param filename name of the file to be loaded
00533      * @param vcts the matrix where the vectors will be written
00534      * @param ids a vector the class ids for each vector in the matrix.  The
00535      *            i-th element of the vector contains the id of the i-th row
00536      *            of the matrix.
00537      * @param useCnc use a class name file (extension cnc) to get the object
00538      *              names and corresponding ids.
00539      * @return true if everything is ok or false if the file is corrupted or
00540      *              not found.
00541      */
00542     bool load(const std::string& filename,
00543               dmatrix& vcts,
00544               ivector& ids,
00545               const bool useCnc=false);
00546 
00547     /**
00548      * shortcut to read the given lnc file, ignoring the filenames
00549      * @param filename name of the file to be loaded
00550      * @param vcts the matrix where the vectors will be written
00551      * @param ids a vector the class ids for each vector in the matrix.  The
00552      *            i-th element of the vector contains the id of the i-th row
00553      *            of the matrix.
00554      * @param attribs a vector of std::string with the attributes for each
00555      *                pattern in the file
00556      * @param useCnc use a class name file (extension cnc) to get the object
00557      *              names and corresponding ids.
00558      * @return true  if everything is ok or false if the file is corrupted or
00559      *              not found.
00560      */
00561     bool load(const std::string& filename,
00562               dmatrix& vcts,
00563               ivector& ids,
00564               std::vector<std::string>& attribs,
00565               const bool useCnc=false);
00566 
00567     /**
00568      * shortcut to read the given lnc file, ignoring the filenames
00569      * @param filename name of the file to be loaded
00570      * @param vcts the matrix where the vectors will be written
00571      * @param ids a vector the class ids for each vector in the matrix.  The
00572      *            i-th element of the vector contains the id of the i-th row
00573      *            of the matrix.
00574      * @param names a table with the corresponding names for each object id
00575      * @param useCnc use a class name file (extension cnc) to get the object
00576      *              names and corresponding ids.
00577      * @return true  if everything is ok or false if the file is corrupted or
00578      *              not found.
00579      */
00580     bool load(const std::string& filename,
00581               dmatrix& vcts,
00582               ivector& ids,
00583               std::map<int,std::string>& names,
00584               const bool useCnc=false);
00585 
00586     /**
00587      * shortcut to read the given lnc file, ignoring the filenames
00588      * @param filename name of the file to be loaded
00589      * @param vcts the matrix where the vectors will be written
00590      * @param ids a vector the class ids for each vector in the matrix.  The
00591      *            i-th element of the vector contains the id of the i-th row
00592      *            of the matrix.
00593      * @param attribs a vector of std::string with the attributes for each
00594      *                pattern in the file
00595      * @param names a table with the corresponding names for each object id
00596      * @param useCnc use a class name file (extension cnc) to get the object
00597      *              names and corresponding ids.
00598      * @return true  if everything is ok or false if the file is corrupted or
00599      *              not found.
00600      */
00601     bool load(const std::string& filename,
00602               dmatrix& vcts,
00603               ivector& ids,
00604               std::vector<std::string>& attribs,
00605               std::map<int,std::string>& names,
00606               const bool useCnc=false);
00607 
00608     /**
00609      * returns a map with the names of the classes for each object id.
00610      */
00611     void getClassNames(std::map<int,std::string>& names) const;
00612 
00613     /**
00614      * restart reading the file (use only when a file need to be readed again)
00615      */
00616     void reset();
00617 
00618     /**
00619      * clone member
00620      */
00621     virtual functor* clone() const {
00622       return new loadLnc(*this);
00623     }
00624   };
00625 
00626   /**
00627    * class to save lnc-files (ascii format to store feature vectors)
00628    * @see loadLnc
00629    */
00630   class saveLnc : public lncFeatureFile {
00631   public:
00632     /**
00633      * lncFeatureFile parameters class
00634      */
00635     class parameters : public lncFeatureFile::parameters {
00636     public:
00637       /**
00638        * default constructor
00639        */
00640       parameters();
00641 
00642       /**
00643        * copy constructor
00644        */
00645       parameters(const parameters& other) 
00646         : lncFeatureFile::parameters() {copy(other);};
00647 
00648       /**
00649        * copy member
00650        */
00651       parameters& copy(const parameters& other);
00652 
00653       /**
00654        * returns a pointer to a clone of the parameters.
00655        */
00656       virtual functor::parameters* clone() const;
00657 
00658       /**
00659        * returns name of this type
00660        */
00661       virtual const char* getTypeName() const;
00662 
00663       /**
00664        * write the parameters in the given ioHandler
00665        * @param handler the ioHandler to be used
00666        * @param complete if true (the default) the enclosing begin/end will
00667        *        be also written, otherwise only the data block will be written.
00668        * @return true if write was successful
00669        */
00670       virtual bool write(ioHandler& handler,const bool complete=true) const;
00671 
00672       /**
00673        * write the parameters in the given ioHandler
00674        * @param handler the ioHandler to be used
00675        * @param complete if true (the default) the enclosing begin/end will
00676        *        be also written, otherwise only the data block will be written.
00677        * @return true if write was successful
00678        */
00679       virtual bool read(ioHandler& handler,const bool complete=true);
00680 
00681 #     ifdef _LTI_MSC_6
00682       /**
00683        * this function is required by MSVC only, as a workaround for a
00684        * very awful bug, which exists since MSVC V.4.0, and still by
00685        * V.6.0 with all bugfixes (so called "service packs") remains
00686        * there...  This method is also public due to another bug, so please
00687        * NEVER EVER call this method directly: use read() instead
00688        */
00689       bool readMS(ioHandler& handler,const bool complete=true);
00690 
00691       /**
00692        * this function is required by MSVC only, as a workaround for a
00693        * very awful bug, which exists since MSVC V.4.0, and still by
00694        * V.6.0 with all bugfixes (so called "service packs") remains
00695        * there...  This method is also public due to another bug, so please
00696        * NEVER EVER call this method directly: use write() instead
00697        */
00698       bool writeMS(ioHandler& handler,const bool complete=true) const;
00699 #     endif
00700 
00701       // ------------------------------------------------
00702       // the parameters
00703       // ------------------------------------------------
00704 
00705       /**
00706        * if true, then the file deleted first, if it already exists,
00707        * otherwise, the new patterns will be appended at the end of the file.
00708        *
00709        * Default value: true
00710        */
00711       bool deleteIfExists;
00712     };
00713 
00714     /**
00715      * Default constructor
00716      */
00717     saveLnc();
00718 
00719     /**
00720      * Constructor with parameters
00721      */
00722     saveLnc(const parameters& par);
00723 
00724     /**
00725      * get the used parameters
00726      */
00727     const parameters& getParameters() const;
00728 
00729     /**
00730      * clone member
00731      */
00732     virtual functor* clone() const {
00733       return new saveLnc(*this);
00734     }
00735 
00736     /**
00737      * save the vector at the end of the file.
00738      * @param vct the vector object to be saved
00739      * @param id the id of the class the vector belongs to.
00740      * @return true if everything is ok, otherwise  a lti::exception will
00741      *              be thrown.
00742      */
00743     bool apply(const dvector& vct,const patternId& id);
00744 
00745     /**
00746      * restart writing the file
00747      */
00748     void reset();
00749 
00750     /**
00751      * write the whole name-id map to the given cnc file
00752      * (the contents of the file will be destroyed if the file already
00753      *  exists)
00754      */
00755     bool writeCncFile(const std::string& filename);
00756 
00757     /**
00758      * set the current name to object id table
00759      */
00760     void setNameToIdMap(const std::map<std::string,int>& newMap);
00761 
00762   };
00763 
00764 }
00765 
00766 #endif

Generated on Sat Apr 10 15:25:46 2010 for LTI-Lib by Doxygen 1.6.1