LoTTIMap.C

00001 /**
00002    \file  Robots/LoBot/misc/LoTTIMap.C
00003    \brief This file defines the non-inline member functions of the
00004    lobot::TTIMap class.
00005 */
00006 
00007 // //////////////////////////////////////////////////////////////////// //
00008 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00009 // by the University of Southern California (USC) and the iLab at USC.  //
00010 // See http://iLab.usc.edu for information about this project.          //
00011 // //////////////////////////////////////////////////////////////////// //
00012 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00013 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00014 // in Visual Environments, and Applications'' by Christof Koch and      //
00015 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00016 // pending; application number 09/912,225 filed July 23, 2001; see      //
00017 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00018 // //////////////////////////////////////////////////////////////////// //
00019 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00022 // redistribute it and/or modify it under the terms of the GNU General  //
00023 // Public License as published by the Free Software Foundation; either  //
00024 // version 2 of the License, or (at your option) any later version.     //
00025 //                                                                      //
00026 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00027 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00028 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00029 // PURPOSE.  See the GNU General Public License for more details.       //
00030 //                                                                      //
00031 // You should have received a copy of the GNU General Public License    //
00032 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00033 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00034 // Boston, MA 02111-1307 USA.                                           //
00035 // //////////////////////////////////////////////////////////////////// //
00036 //
00037 // Primary maintainer for this file: mviswana usc edu
00038 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/baylog/LoTTIMap.C $
00039 // $Id: LoTTIMap.C 14088 2010-10-01 19:43:37Z mviswana $
00040 //
00041 
00042 //------------------------------ HEADERS --------------------------------
00043 
00044 // lobot headers
00045 #include "Robots/LoBot/baylog/LoTTIMap.H"
00046 #include "Robots/LoBot/config/LoConfigHelpers.H"
00047 #include "Robots/LoBot/util/LoStats.H"
00048 #include "Robots/LoBot/util/LoMath.H"
00049 
00050 // Standard C++ headers
00051 #include <iomanip>
00052 #include <sstream>
00053 #include <utility>
00054 
00055 //-------------------------- KNOB TWIDDLING -----------------------------
00056 
00057 namespace {
00058 
00059 // Retrieve settings from global section of config file
00060 template<typename T>
00061 inline T conf(const std::string& key, const T& default_value)
00062 {
00063    return lobot::global_conf<T>(key, default_value) ;
00064 }
00065 
00066 /// This inner class encapsulates various parameters that can be used to
00067 /// tweak different aspects of the Bayesian TTI prediction analysis.
00068 class TTIMapParams : public lobot::singleton<TTIMapParams> {
00069    /// The Bayesian time-to-impact state estimator is usually configured
00070    /// to predict the TTI within the range of zero to ten seconds in
00071    /// steps of a tenth of a second. However, if the Robolocust settings
00072    /// for the Bayesian TTI prediction experiments were different, we can
00073    /// specify the maximum value for the TTI predictions with this
00074    /// setting, which should be a floating point number whose units is
00075    /// seconds.
00076    ///
00077    /// When analyzing the log files for the Bayesian TTI prediction
00078    /// experiments, we will only consider those entries whose actual
00079    /// times-to-impact are less than the value of this setting because,
00080    /// eventually, the results files output by the lobay program will be
00081    /// used to produce plots showing the LGMD spike rate and TTI
00082    /// predictions versus the actual times-to-impact. Actual
00083    /// times-to-impact that are outside the range of the estimator's
00084    /// bounds will show up as being highly errorneous, which would be
00085    /// unfair. Therefore, we ignore such readings and concentrate only on
00086    /// those that are within the state estimation bounds.
00087    float m_max_tti ;
00088 
00089    /// Private constructor because this is a singleton.
00090    TTIMapParams() ;
00091 
00092    // Boilerplate code to make generic singleton design pattern work
00093    friend class lobot::singleton<TTIMapParams> ;
00094 
00095 public:
00096    /// Accessing the various parameters.
00097    //@{
00098    static float max_tti() {return instance().m_max_tti ;}
00099    //@}
00100 } ;
00101 
00102 // Parameters initialization
00103 TTIMapParams::TTIMapParams()
00104    : m_max_tti(lobot::clamp(conf("max_tti", 10.0f), 1.0f, 60.0f))
00105 {}
00106 
00107 // Shortcut
00108 typedef TTIMapParams Params ;
00109 
00110 } // end of local anonymous namespace encapsulating above helpers
00111 
00112 //----------------------------- NAMESPACE -------------------------------
00113 
00114 namespace lobot {
00115 
00116 //-------------------------- INITIALIZATION -----------------------------
00117 
00118 TTIMap::TTIMap(){}
00119 
00120 void TTIMap::add(float tti, float lgmd, float predicted, float confidence)
00121 {
00122    if (tti > Params::max_tti())
00123       return ;
00124 
00125    using std::right ; using std::setfill ; using std::setw ;
00126    using std::fixed ; using std::setprecision ;
00127 
00128    std::ostringstream str ;
00129    str << setfill('0') << setw(4) << right << fixed << setprecision(1)
00130        << tti ;
00131 
00132    std::string  key = str.str() ;
00133    Map::iterator it = m_map.find(key) ;
00134    if (it == m_map.end())
00135    {
00136       List L, P, C ;
00137       L.push_back(lgmd) ;
00138       P.push_back(predicted) ;
00139       C.push_back(confidence) ;
00140       m_map.insert(std::make_pair(key, make_triple(L, P, C))) ;
00141    }
00142    else
00143    {
00144       it->second.first.push_back(lgmd) ;
00145       it->second.second.push_back(predicted) ;
00146       it->second.third.push_back(confidence) ;
00147    }
00148 }
00149 
00150 //------------------------------ OUTPUT ---------------------------------
00151 
00152 static std::pair<float, float> stats(const std::vector<float>& v)
00153 {
00154    return mean_stdev<float>(v.begin(), v.end()) ;
00155 }
00156 
00157 TTIMap::dump::dump(std::ostream& s)
00158    : os(s)
00159 {}
00160 
00161 void TTIMap::dump::operator()(const TTIMap::MapEntry& E)const
00162 {
00163    using std::fixed ; using std::setprecision ;
00164    using std::right ; using std::setw ;
00165 
00166    float actual_tti = from_string<float>(E.first) ;
00167    std::pair<float, float> lgmd = stats(E.second.first) ;
00168    std::pair<float, float> pred = stats(E.second.second);
00169    std::pair<float, float> conf = stats(E.second.third) ;
00170    conf.first  /= 100 ;
00171    conf.second /= 100 ;
00172 
00173    os << setw(4) << right << fixed << setprecision(1) << actual_tti  << ' '
00174       << setw(3) << right << fixed << setprecision(0) << lgmd.first  << ' '
00175       << setw(3) << right << fixed << setprecision(0) << lgmd.second << ' '
00176       << setw(4) << right << fixed << setprecision(1) << pred.first  << ' '
00177       << setw(4) << right << fixed << setprecision(1) << pred.second << ' '
00178       << setw(6) << right << fixed << setprecision(4) << conf.first  << ' '
00179       << setw(6) << right << fixed << setprecision(4) << conf.second << '\n' ;
00180 }
00181 
00182 std::ostream& operator<<(std::ostream& os, const TTIMap& M)
00183 {
00184    std::for_each(M.m_map.begin(), M.m_map.end(), TTIMap::dump(os)) ;
00185    return os ;
00186 }
00187 
00188 //-----------------------------------------------------------------------
00189 
00190 } // end of namespace encapsulating this file's definitions
00191 
00192 /* So things look consistent in everyone's emacs... */
00193 /* Local Variables: */
00194 /* indent-tabs-mode: nil */
00195 /* End: */
Generated on Sun May 8 08:41:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3