PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // FractionSplitter.cc 00004 // 00005 // Copyright (C) 2003 Pascal Vincent 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: FractionSplitter.cc 8795 2008-04-11 13:41:50Z tihocan $ 00037 ******************************************************* */ 00038 00040 #include "FractionSplitter.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00046 // FractionSplitter // 00048 FractionSplitter::FractionSplitter(): 00049 one_is_absolute(false), 00050 round_to_closest(0) 00051 {} 00052 00053 PLEARN_IMPLEMENT_OBJECT(FractionSplitter, 00054 "A Splitter that can extract several subparts of a dataset in each split.", 00055 "Ranges of the dataset are specified explicitly as start:end positions,\n" 00056 "that can be absolute or relative to the number of samples in the training set."); 00057 00059 // declareOptions // 00061 void FractionSplitter::declareOptions(OptionList& ol) 00062 { 00063 00064 declareOption(ol, "round_to_closest", &FractionSplitter::round_to_closest, OptionBase::buildoption, 00065 "If set to 1, then the integer value found when using fractions will\n" 00066 "be the closest integer, instead of the integer part."); 00067 00068 declareOption(ol, "splits", &FractionSplitter::splits, OptionBase::buildoption, 00069 "A matrix of start:end pairs. Each row represents a split. \n" 00070 "Each start:end element represents a range of samples in the dataset to be splitted. \n" 00071 "start and end, which are positions in the datataset, can be specified as either \n" 00072 "a fraction of the dataset length (if <=1), or an absolute number of elements (if >1).\n" 00073 "The range includes all samples from start to end, but excluding the end sample \n" 00074 "(so that, for ex., the same value can be used as the start of the next range \n" 00075 "without having the two ranges ovelap). \n" 00076 "The value 1 is a bit special as it always means \"until last element inclusive\".\n" 00077 "Ex: 1 2 [ 0:0.80, 0.80:1 ] yields a single split with the first part being the first 80% \n" 00078 "of the data, and the second the next 20% \n"); 00079 00080 declareOption(ol, "one_is_absolute", 00081 &FractionSplitter::one_is_absolute, OptionBase::buildoption, 00082 "If true, then 1 is always considered as an absolute index, not as a\n" 00083 "fraction giving the end of the dataset. This can be useful if you\n" 00084 "actually want a split with a single element in it.", 00085 OptionBase::advanced_level); 00086 00087 // Now call the parent class' declareOptions 00088 inherited::declareOptions(ol); 00089 } 00090 00092 // build_ // 00094 void FractionSplitter::build_() 00095 { 00096 } 00097 00099 // build // 00101 void FractionSplitter::build() 00102 { 00103 inherited::build(); 00104 build_(); 00105 } 00106 00107 void FractionSplitter::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00108 { 00109 inherited::makeDeepCopyFromShallowCopy(copies); 00110 deepCopyField(splits, copies); 00111 } 00112 00113 int FractionSplitter::nsplits() const 00114 { 00115 return splits.length(); 00116 } 00117 00118 int FractionSplitter::nSetsPerSplit() const 00119 { 00120 return splits.width(); 00121 } 00122 00123 00124 TVec<VMat> FractionSplitter::getSplit(int k) 00125 { 00126 TVec< pair<real,real> > frac_k = splits(k); 00127 int n = frac_k.length(); 00128 TVec<VMat> vms(n); 00129 int l = dataset.length(); 00130 int start = 0; 00131 int end = 0; 00132 for(int i=0; i<n; i++) 00133 { 00134 real fstart = frac_k[i].first; 00135 real fend = frac_k[i].second; 00136 00137 if(fstart>1 || (one_is_absolute && is_equal(fstart, 1))) // absolute position 00138 start = int(round(fstart)); 00139 else {// relative position 00140 if (round_to_closest) { 00141 start = int(fstart*l + 0.5); 00142 } else { 00143 start = int(fstart*l); 00144 } 00145 } 00146 00147 if(fend>1 || (one_is_absolute && is_equal(fend, 1))) // absolute end position 00148 end = int(round(fend)); 00149 else if(is_equal(fend,1)) // until last element inclusive 00150 end = l; 00151 else {// relative end position 00152 if (round_to_closest) { 00153 end = int(fend*l + 0.5); 00154 } else { 00155 end = int(fend*l); 00156 } 00157 } 00158 00159 vms[i] = dataset.subMatRows(start, end-start); 00160 } 00161 return vms; 00162 } 00163 00164 } // end of namespace PLearn 00165 00166 00167 /* 00168 Local Variables: 00169 mode:c++ 00170 c-basic-offset:4 00171 c-file-style:"stroustrup" 00172 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00173 indent-tabs-mode:nil 00174 fill-column:79 00175 End: 00176 */ 00177 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :