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
00040
00043
#include <plearn/base/ProgressBar.h>
00044
#include "FilteredVMatrix.h"
00045
00046
namespace PLearn {
00047
using namespace std;
00048
00049
00050 FilteredVMatrix::FilteredVMatrix()
00051 :
inherited(),
00052 build_complete(false)
00053 {
00054 }
00055
00056 FilteredVMatrix::FilteredVMatrix(
VMat the_source,
const string& program_string,
const string& the_metadatadir)
00057 :
SourceVMatrix(the_source),
00058 prg(program_string)
00059 {
00060 metadatadir = the_metadatadir;
00061
build_();
00062 }
00063
00064
PLEARN_IMPLEMENT_OBJECT(
FilteredVMatrix,
"A filtered view of its source vmatrix",
00065
"The filter is an exression in VPL language.\n"
00066
"The filtered indexes are saved in the metadata directory, that NEEDS to\n"
00067
"be provided.\n" );
00068
00069
00070
00071 void FilteredVMatrix::openIndex()
00072 {
00073
string idxfname =
getMetaDataDir()+
"filtered.idx";
00074
00075
if(
file_exists(idxfname) &&
mtime(idxfname)>=
getMtime())
00076
indexes.
open(idxfname);
00077
else
00078 {
00079
rm(idxfname);
00080
int l = source.
length();
00081
Vec result(1);
00082
indexes.
open(idxfname,
true);
00083
ProgressBar pb(
"Filtering source vmat", l);
00084
for(
int i=0; i<l; i++)
00085 {
00086 pb.
update(i);
00087
program.
run(i,result);
00088
if(result[0]!=0)
00089
indexes.
append(i);
00090 }
00091
indexes.
close();
00092
indexes.
open(idxfname);
00093 }
00094
00095 length_ =
indexes.
length();
00096 }
00097
00098 void FilteredVMatrix::setMetaDataDir(
const string& the_metadatadir)
00099 {
00100 inherited::setMetaDataDir(the_metadatadir);
00101
if (
build_complete) {
00102
00103
00104
openIndex();
00105 }
00106 }
00107
00108 void FilteredVMatrix::getNewRow(
int i,
const Vec& v)
const
00109
{
00110
if (
indexes.
length() == -1)
00111
PLERROR(
"In FilteredVMatrix::getNewRow - The filtered indexes are not set, make sure you provided a metadatadir");
00112 source->getRow(
indexes[i],v);
00113 }
00114
00115 void FilteredVMatrix::declareOptions(
OptionList& ol)
00116 {
00117
declareOption(ol,
"prg", &FilteredVMatrix::prg, OptionBase::buildoption,
00118
"The VPL code that should produce a single scalar, indicating whether \n"
00119
"we should keep the line (if the produced scalar is non zero) or throw it away (if it's zero)");
00120
00121
00122 inherited::declareOptions(ol);
00123 }
00124
00125 void FilteredVMatrix::build_()
00126 {
00127
vector<string> fieldnames;
00128
program.
setSource(source);
00129
00130
program.
compileString(
prg,fieldnames);
00131
if(metadatadir !=
"") {
00132
openIndex();
00133 }
00134
setMetaInfoFromSource();
00135
build_complete =
true;
00136 }
00137
00138
00139 void FilteredVMatrix::build()
00140 {
00141
build_complete =
false;
00142 inherited::build();
00143
build_();
00144 }
00145
00146 void FilteredVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00147 {
00148 inherited::makeDeepCopyFromShallowCopy(copies);
00149 }
00150
00151 }
00152