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
00041
00042
#include "VMat.h"
00043
#include <plearn/io/fileutils.h>
00044
00045
#include "MemoryVMatrix.h"
00046
#include "FileVMatrix.h"
00047
#include "SelectRowsVMatrix.h"
00048
#include "SelectColumnsVMatrix.h"
00049
#include "SelectRowsFileIndexVMatrix.h"
00050
00051
namespace PLearn {
00052
using namespace std;
00053
00056 VMat::VMat() {}
00057 VMat::VMat(
VMatrix* d):
PP<
VMatrix>(d) {}
00058 VMat::VMat(
const VMat& d) :
PP<
VMatrix>(d) {}
00059 VMat::VMat(
const Mat& datamat):
PP<
VMatrix>(new
MemoryVMatrix(datamat)) {}
00060 VMat::~VMat() {}
00061
00062 VMat VMat::subMatRows(
int i,
int l)
const
00063
{
00064
VMat res = ptr->
subMat(i,0,l,
width());
00065 res->defineSizes(ptr->
inputsize(), ptr->
targetsize(), ptr->
weightsize());
00066
return res;
00067 }
00068
00069 VMat VMat::rows(
TVec<int> rows_indices)
const
00070
{
return new SelectRowsVMatrix(*
this, rows_indices); }
00071
00072 VMat VMat::rows(
Vec rows_indices)
const
00073
{
return new SelectRowsVMatrix(*
this, rows_indices); }
00074
00075 VMat VMat::rows(
const string& indexfile)
const
00076
{
return new SelectRowsFileIndexVMatrix(*
this, indexfile); }
00077
00078 VMat VMat::columns(
TVec<int> columns_indices)
const
00079
{
return new SelectColumnsVMatrix(*
this, columns_indices); }
00080
00081 VMat VMat::columns(
Vec columns_indices)
const
00082
{
return new SelectColumnsVMatrix(*
this, columns_indices); }
00083
00085
00087 void VMat::precompute() {
00088
VMat backup = *
this;
00089 *
this =
new MemoryVMatrix(
Mat(*
this));
00090 (*this)->setFieldInfos( backup->getFieldInfos() );
00091
00092
00093
00094
00095 (*this)->copySizesFrom(backup);
00096
00097
00098
00099 }
00100
00101 void VMat::precompute(
const string& pmatfile,
bool use_existing_file)
00102 {
00103
VMat backup = *
this;
00104
Array<VMField> infos = (*this)->getFieldInfos();
00105
if(!use_existing_file || !
file_exists(pmatfile))
00106
save(pmatfile);
00107 *
this =
new FileVMatrix(pmatfile);
00108 (*this)->setFieldInfos( infos );
00109 (*this)->copySizesFrom(backup);
00110
00111 }
00112
00113
00114
template <>
00115 void deepCopyField(
VMat& field, CopiesMap& copies)
00116 {
00117
if (field)
00118 field = static_cast<VMatrix*>(field->deepCopy(copies));
00119 }
00120
00122
00124 VMat loadAsciiAsVMat(
const string& filename)
00125 {
00126
Mat m;
00127
TVec<string> fn;
00128
TVec< map<string,real> >
map_sr;
00129
loadAscii(filename, m, fn, &
map_sr);
00130
VMat vm =
new MemoryVMatrix(m);
00131 vm->setMtime(
mtime(filename));
00132
00133
for (
int i = 0; i <
map_sr.length(); i++) {
00134 vm->setStringMapping(i,
map_sr[i]);
00135 }
00136
for(
int i=0;i<fn.
size();i++)
00137 vm->declareField(i, fn[i]);
00138
return vm;
00139 }
00140
00141 }