00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00041
#include "learner_utils.h"
00042
#include <plearn/vmat/VMat_maths.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
00064
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
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
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
00129
if(nvals==1)
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)
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
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
00174 <<
"attribute \"ref\" string \"positions\" \n\n\n";
00175
00176 string dataname = string(
"\"") + basename +
"_values\"";
00177
00178
if(nvals==1)
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)
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
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 }