src/IO/ClassMappings.cpp

00001 /*
00002 * This file is part of MultiBoost, a multi-class 
00003 * AdaBoost learner/classifier
00004 *
00005 * Copyright (C) 2005 Norman Casagrande
00006 *
00007 * This library is free software; you can redistribute it and/or
00008 * modify it under the terms of the GNU Lesser General Public
00009 * License as published by the Free Software Foundation; either
00010 * version 2.1 of the License, or (at your option) any later version.
00011 *
00012 * This library is distributed in the hope that it will be useful,
00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 * 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 this library; if not, write to the Free Software
00019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 *
00021 */
00022 
00023 #include "ClassMappings.h"
00024 
00025 #include "Defaults.h" // for MB_DEBUG
00026 #include <fstream> // for ifstream
00027 #include <iostream> // for cerr
00028 
00031 
00032 namespace MultiBoost {
00033 
00034 // static objects
00035 vector<string> ClassMappings::_mapIdxToClass; 
00036 map<string, int> ClassMappings::_mapClassToIdx;
00037 int ClassMappings::_numRegClasses = 0;
00038 
00039 // ------------------------------------------------------------------------
00040 
00041 int ClassMappings::addClassName(const string& className)
00042 {
00043    // if we haven't seen yet this class, add it to the mapping structures
00044    if ( _mapClassToIdx.find(className) == _mapClassToIdx.end() )
00045    {
00046       _mapClassToIdx[className] = _numRegClasses;
00047       _mapIdxToClass.push_back(className);
00048       ++_numRegClasses;
00049    }
00050    
00051    return _mapClassToIdx[className];
00052 }
00053 
00054 // ------------------------------------------------------------------------
00055 
00056 void ClassMappings::loadClassMapFile(const string& classMapFileName)
00057 {
00058    ifstream classMapFile(classMapFileName.c_str());
00059    if ( !classMapFile.is_open() )
00060    {
00061       cerr << "ERROR: Cannot find classmap file <" << classMapFileName << ">!" << endl;
00062       exit(1);
00063    }
00064 
00065    string className;
00066    do {
00067       classMapFile >> className;
00068       addClassName(className);
00069    } while ( !classMapFile.eof() && !className.empty() );
00070 
00071 }
00072 
00073 
00074 // ------------------------------------------------------------------------
00075 
00076 string ClassMappings::getClassNameFromIdx(const int idx)
00077 {
00078 #if MB_DEBUG
00079    if ( idx >= _mapIdxToClass.size() )
00080    {
00081       cerr << "ERROR: trying to map a class index that does not exists. The input file" << endl;
00082       cerr << "might not have all the classes used for training. Use -classmap to define" << endl;
00083       cerr << "the list of classes instead." << endl;
00084       exit(1);
00085    }
00086 #endif
00087    return _mapIdxToClass[idx]; 
00088 }
00089 
00090 // ------------------------------------------------------------------------
00091 
00092 int ClassMappings::getIdxFromClassName(const string& className)
00093 { 
00094 #if MB_DEBUG
00095    if ( _mapClassToIdx.find(className) == _mapClassToIdx.end() )
00096    {
00097       cerr << "ERROR: trying to map a class (" << className << ") that does not exists." << endl;
00098       cerr << "The input file might not have all the classes used for training." << endl;
00099       cerr << "Use -classmap to define the list of classes instead." << endl;
00100       exit(1);
00101    }
00102 #endif
00103    return _mapClassToIdx[className]; 
00104 }
00105 
00106 // ------------------------------------------------------------------------
00107 
00108 
00109 } // end of namespace MultiBoost

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5