PLearn 0.1
learner_utils.cc
Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 
00004 // learner_utils.cc
00005 //
00006 // Copyright (C) 2003  Pascal Vincent 
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: learner_utils.cc 3994 2005-08-25 13:35:03Z chapados $ 
00038  ******************************************************* */
00039 
00041 #include "learner_utils.h"
00042 #include <plearn/vmat/VMat_basic_stats.h>
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 
00048 Mat compute_learner_outputs(PP<PLearner> learner, VMat inputs)
00049 {
00050     Vec input(learner->inputsize());
00051     int l = inputs.length();
00052     Mat outputs(l,learner->outputsize());
00053     for(int i=0; i<l; i++)
00054     {
00055         inputs->getRow(i,input);
00056         Vec output = outputs(i);
00057         learner->computeOutput(input, output);
00058     }
00059     return outputs;
00060 }
00061 
00062 
00063 // Finds appropriate x0, y0, deltax, deltay from the dataset range,
00064 // extraspace of .10 means we'll look 10% beyond the data range on every side
00065 void determine_grid_for_dataset(VMat dataset, int nx, int ny, 
00066                                 real& x0, real& y0, real& deltax, real& deltay, 
00067                                 real extraspace)
00068 {
00069     Vec minv(2);
00070     Vec maxv(2);
00071     computeRange(dataset.subMatColumns(0,2), minv, maxv);
00072     real extrax = (maxv[0]-minv[0])*extraspace;
00073     x0 = minv[0]-extrax;
00074     deltax = (maxv[0]+extrax-x0)/nx;
00075     real extray = (maxv[1]-minv[1])*extraspace;
00076     y0 = minv[1]-extray;
00077     deltay = (maxv[1]+extray-y0)/ny;
00078 }
00079 
00080 double determine_density_integral_from_log_densities_on_grid(Vec log_densities, real deltax, real deltay)
00081 {
00082     double logsum = logadd(log_densities);
00083     double surfelem = deltax*deltay;
00084     double surfintegral = exp(logsum)*surfelem;
00085     return surfintegral;
00086 }
00087 
00088 Mat compute_learner_outputs_on_grid(PP<PLearner> learner, int nx, int ny, real x0, real y0, real deltax, real deltay)
00089 {
00090     if(learner->inputsize()!=2)
00091         PLERROR("In compute_learner_outputs_on_grid learner's input must be 2D");
00092 
00093     int noutputs = learner->outputsize();
00094     Vec input(2);
00095     Mat results(nx*ny, noutputs);
00096 
00097     ProgressBar pb("Computing " + tostring(nx) + " x " + tostring(ny) + " learner outputs",nx*ny);
00098 
00099     real x = x0;
00100     for(int i=0; i<nx; i++, x+=deltax)
00101     {
00102         real y = y0;
00103         for(int j=0; j<ny; j++, y+=deltay)
00104         {
00105             input[0] = x;
00106             input[1] = y;
00107             Vec output = results(i*nx+j);
00108             learner->computeOutput(input,output);
00109             // cerr << input << " --> " << output << endl;
00110             pb.update(i*nx+j);
00111         }
00112     }
00113 
00114     return results;
00115 }
00116 
00117 void DX_write_2D_data(ostream& out, const string& basename, Mat data)
00118 {
00119     int l = data.length();
00120     int nvals = data.width()-2;
00121 
00122     // write 2D positions
00123     out << "object \"" << basename << "_pos\" class array type float rank 1 shape 2 items " << l << " data follows \n";
00124     for(int i=0; i<l; i++)
00125         out << data(i,0) << " " << data(i,1) << "\n";
00126     out << "\n\n\n";
00127 
00128     // Write data, which is in a one-to-one correspondence with the positions
00129     if(nvals==1)  // scalar field
00130     {
00131         out << "object \"" << basename << "_value\" class array type float rank 0 items " << l << " data follows \n";
00132         for(int i=0; i<l; i++)
00133             out << data(i,2) << "\n";
00134         out << "attribute \"dep\" string \"positions\" \n\n\n";
00135     }
00136     else if(nvals>1) // vector field
00137     {
00138         out << "object \"" << basename << "_value\" class array type float rank 1 shape " << nvals << " items " << l << " data follows \n";
00139         for(int i=0; i<l; i++)
00140         {
00141             for(int j=0; j<nvals; j++)
00142                 out << data(i,2+j) << " ";
00143             out << "\n";
00144         }
00145         out << "attribute \"dep\" string \"positions\" \n\n\n";
00146     }
00147 
00148     // Finally field is created with two components: "positions" and "data"
00149     out << "object \"" << basename << "\" class field \n"
00150         << "component \"positions\" \"" << basename << "_pos\" \n";
00151     if(nvals>0)
00152         out << "component \"data\" \"" << basename << "_value\" \n";
00153 
00154     out << "\n\n";
00155 }
00156 
00157 
00158 void DX_write_2D_data_for_grid(ostream& out, const string& basename, 
00159                                int nx, int ny, real x0, real y0, real deltax, real deltay,
00160                                Mat data)
00161 {
00162     int l = data.length();
00163     int nvals = data.width();
00164 
00165     string posname = string("\"") + basename + "_gridpos\"";
00166     out << "object " << posname << " class gridpositions counts " << nx << " " << ny << "\n"
00167         << "origin  " << x0 << " " << y0 << "\n"
00168         << "delta   " << deltax << " 0 \n"
00169         << "delta    0 " << deltay << " \n\n\n";
00170 
00171     string conname = string("\"") + basename + "_gridcon\"";
00172     out << "object " << conname << " class gridconnections counts " << nx << " " << ny << "\n"
00173         //      << "attribute \"element type\" string \"cubes\" \n"
00174         << "attribute \"ref\" string \"positions\" \n\n\n";
00175 
00176     string dataname = string("\"") + basename + "_values\"";
00177     // Write data, which is in a one-to-one correspondence with the positions
00178     if(nvals==1)  // scalar field
00179     {
00180         out << "object " << dataname << " class array type float rank 0 items " << l << " data follows \n";
00181         for(int i=0; i<l; i++)
00182             out << data(i,0) << "\n";
00183         out << "attribute \"dep\" string \"positions\" \n\n\n";
00184     }
00185     else if(nvals>1) // vector field
00186     {
00187         out << "object " << dataname << " class array type float rank 1 shape " << nvals << " items " << l << " data follows \n";
00188         for(int i=0; i<l; i++)
00189         {
00190             for(int j=0; j<nvals; j++)
00191                 out << data(i,j) << " ";
00192             out << "\n";
00193         }
00194         out << "attribute \"dep\" string \"positions\" \n\n\n";
00195     }
00196 
00197     // Finally field is created with 3 components: "positions" "connections" and "data"
00198     out << "object \"" << basename << "\" class field \n"
00199         << "component \"positions\" " << posname << " \n"
00200         << "component \"connections\" " << conname << " \n"
00201         << "component \"data\" " << dataname << " \n\n\n";
00202 
00203     out << "\n\n";
00204 }
00205 
00206 
00207 void DX_save_2D_data(const string& filename, const string& basename, Mat data)
00208 {
00209     ofstream out(filename.c_str());
00210     if(!out)
00211         PLERROR("Could not open %s for writing",filename.c_str());
00212     DX_write_2D_data(out, basename, data);
00213 }
00214 
00215 void DX_save_2D_data_for_grid(const string& filename, const string& basename, 
00216                               int nx, int ny, real x0, real y0, real deltax, real deltay,
00217                               Mat data)
00218 {
00219     ofstream out(filename.c_str());
00220     if(!out)
00221         PLERROR("Could not open %s for writing",filename.c_str());
00222     DX_write_2D_data_for_grid(out, basename, nx, ny, x0, y0, deltax, deltay, data); 
00223 }
00224 
00225 
00226 } // end of namespace PLearn
00227 
00228 
00229 /*
00230   Local Variables:
00231   mode:c++
00232   c-basic-offset:4
00233   c-file-style:"stroustrup"
00234   c-file-offsets:((innamespace . 0)(inline-open . 0))
00235   indent-tabs-mode:nil
00236   fill-column:79
00237   End:
00238 */
00239 // 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