PLearn 0.1
SoftHistogramBinner.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // SoftHistogramBinner.cc
00004 //
00005 // Copyright (C) 2004 Olivier Delalleau 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: SoftHistogramBinner.cc 6861 2007-04-09 19:04:15Z saintmlx $ 
00037  ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "SoftHistogramBinner.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00050 // SoftHistogramBinner //
00052 SoftHistogramBinner::SoftHistogramBinner() 
00053 /* ### Initialize all fields to their default value */
00054 {
00055     // ...
00056     // ### You may or may not want to call build_() to finish building the object
00057     // build_();
00058 }
00059 
00060 PLEARN_IMPLEMENT_OBJECT(SoftHistogramBinner,
00061                         "Computes bins which may overlap, but contain the same number of elements.",
00062                         "This binner does not compute a mapping to each bin, because a sample may\n"
00063                         "appear in different bins. Instead, one should use it directly through the\n"
00064                         "getBins() method.\n"
00065                         "The number of bins is given through the 'n_bins' option. The bins split\n"
00066                         "uniformly the interval [min_val, max_val] defined by the data. Each bin\n"
00067                         "contains 'samples_per_bin' samples (if this value is >= 1), or this fraction\n"
00068                         "of the total number of samples (if it is < 1). The value '-1' can be used\n"
00069                         "to specify that each bin contains all samples (though this is probably\n"
00070                         "not very useful).\n"
00071     );
00072 
00074 // declareOptions //
00076 void SoftHistogramBinner::declareOptions(OptionList& ol)
00077 {
00078     declareOption(ol, "samples_per_bin", &SoftHistogramBinner::samples_per_bin, OptionBase::buildoption,
00079                   "The number of samples in each bin (if >= 1), or as a fraction (if < 1).");
00080 
00081     declareOption(ol, "n_bins", &SoftHistogramBinner::n_bins, OptionBase::buildoption,
00082                   "The number of bins computed.");
00083 
00084     // Now call the parent class' declareOptions
00085     inherited::declareOptions(ol);
00086 }
00087 
00089 // build //
00091 void SoftHistogramBinner::build()
00092 {
00093     inherited::build();
00094     build_();
00095 }
00096 
00098 // build_ //
00100 void SoftHistogramBinner::build_()
00101 {
00102     // ### This method should do the real building of the object,
00103     // ### according to set 'options', in *any* situation. 
00104     // ### Typical situations include:
00105     // ###  - Initial building of an object from a few user-specified options
00106     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00107     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00108     // ### You should assume that the parent class' build_() has already been called.
00109   
00110     // Check validity of the 'samples_per_bin' option.
00111     if (!is_equal(samples_per_bin, -1)                 &&
00112         !(samples_per_bin >= 0 && samples_per_bin < 1) &&
00113         !(samples_per_bin >= 1 && fabs(samples_per_bin - int(samples_per_bin)) < 1e-8))
00114         PLERROR("In SoftHistogramBinner::build_ - Invalid value for 'samples_per_bin'");
00115 }
00116 
00118 // getBins //
00120 TVec< TVec<int> > SoftHistogramBinner::getBins(const Vec& v) const {
00121     // Find out how many samples we have in each bin.
00122     int n;
00123     if (is_equal(samples_per_bin, -1))
00124         n = v.length();
00125     else if (samples_per_bin < 1)
00126         n = int(samples_per_bin * v.length());
00127     else
00128         n = int(round(samples_per_bin));
00129     if (n == 0)
00130         PLERROR("In SoftHistogramBinner::getBins - You can't ask for empty bins");
00131     // Sort the data to make things easier.
00132     Mat w(v.length(), 2);
00133     w.column(0) << v;
00134     w.column(1) << TVec<int>(0, w.length() - 1, 1);
00135     sortRows(w);
00136     // Construct bins.
00137     TVec< TVec<int> > bins(n_bins);
00138     real min_w = w(0,0);
00139     real max_w = w(w.length() - 1, 0);
00140     real bin_width = (max_w - min_w) / real(n_bins);
00141     for (int i = 0; i < n_bins; i++) {
00142         real bin_left = min_w + i * bin_width;
00143         real bin_right = bin_left + bin_width;
00144         real bin_mid = (bin_left + bin_right) / 2;  // Center of the bin.
00145         // Find data point closest to bin_mid.
00146         int k = 0;
00147         int min;
00148         while (bin_mid > w(k,0)) k++;
00149         if (w(k,0) - bin_mid > bin_mid - w(k-1,0))
00150             min = k -1;
00151         else
00152             min = k;
00153         bins[i].append(int(w(min, 1)));
00154         // Expand to get n samples in the bin.
00155         int count = 1;
00156         int right = min;
00157         int left = min;
00158         int to_add;
00159         while (count < n) {
00160             if (right + 1 < w.length()) {
00161                 // We can get more data on our right.
00162                 if (left - 1 >= 0) {
00163                     // We can get more data on our left.
00164                     if (w(right + 1,0) - bin_mid < bin_mid - w(left - 1,0)) {
00165                         // Next point to add is on our right.
00166                         right++;
00167                         to_add = right;
00168                     } else {
00169                         // Next point to add is on our left.
00170                         left--;
00171                         to_add = left;
00172                     }
00173                 } else {
00174                     // No more data on the left.
00175                     right++;
00176                     to_add = right;
00177                 }
00178             } else {
00179                 // No more data on the right.
00180                 left--;
00181                 to_add = left;
00182             }
00183             bins[i].append(int(w(to_add, 1)));
00184             count++;
00185         }
00186     }
00187     return bins;
00188 }
00189 
00191 // makeDeepCopyFromShallowCopy //
00193 void SoftHistogramBinner::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00194 {
00195     inherited::makeDeepCopyFromShallowCopy(copies);
00196 
00197     // ### Call deepCopyField on all "pointer-like" fields 
00198     // ### that you wish to be deepCopied rather than 
00199     // ### shallow-copied.
00200     // ### ex:
00201     // deepCopyField(trainvec, copies);
00202 
00203     // ### Remove this line when you have fully implemented this method.
00204     PLERROR("SoftHistogramBinner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00205 }
00206 
00208 // nBins //
00210 int SoftHistogramBinner::nBins() const {
00211     return n_bins;
00212 }
00213 
00214 } // end of namespace PLearn
00215 
00216 
00217 /*
00218   Local Variables:
00219   mode:c++
00220   c-basic-offset:4
00221   c-file-style:"stroustrup"
00222   c-file-offsets:((innamespace . 0)(inline-open . 0))
00223   indent-tabs-mode:nil
00224   fill-column:79
00225   End:
00226 */
00227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines