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 "SelectColumnsVMatrix.h"
00043
00044
namespace PLearn {
00045
using namespace std;
00046
00049
PLEARN_IMPLEMENT_OBJECT(SelectColumnsVMatrix,
00050
"Selects variables from a source matrix according to given vector of indices.",
00051
"Alternatively, the variables can be given by their names."
00052 );
00053
00055
00057 SelectColumnsVMatrix::SelectColumnsVMatrix()
00058 {}
00059
00060 SelectColumnsVMatrix::SelectColumnsVMatrix(
VMat the_source,
TVec<string> the_fields)
00061 : fields(the_fields)
00062 {
00063 source = the_source;
00064
build_();
00065 }
00066
00067 SelectColumnsVMatrix::SelectColumnsVMatrix(
VMat the_source,
TVec<int> the_indices)
00068 : indices(the_indices)
00069 {
00070 source = the_source;
00071
build_();
00072 }
00073
00074 SelectColumnsVMatrix::SelectColumnsVMatrix(
VMat the_source,
Vec the_indices)
00075 {
00076 source = the_source;
00077
indices.
resize(the_indices.
length());
00078
00079
indices << the_indices;
00080
build_();
00081 }
00082
00083 real SelectColumnsVMatrix::get(
int i,
int j)
const
00084
{
return source->get(i,
indices[j]); }
00085
00086 void SelectColumnsVMatrix::getSubRow(
int i,
int j,
Vec v)
const
00087
{
00088
for(
int jj=0; jj<v.
length(); jj++)
00089 v[jj] = source->get(i,
indices[j+jj]);
00090 }
00091
00092 void SelectColumnsVMatrix::declareOptions(
OptionList &ol)
00093 {
00094
declareOption(ol,
"fields", &SelectColumnsVMatrix::fields, OptionBase::buildoption,
00095
"The names of the fields to extract (will override 'indices' if provided).");
00096
00097
declareOption(ol,
"indices", &SelectColumnsVMatrix::indices, OptionBase::buildoption,
00098
"The array of column indices to extract.");
00099
00100 inherited::declareOptions(ol);
00101 }
00102
00103 void SelectColumnsVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00104 {
00105 inherited::makeDeepCopyFromShallowCopy(copies);
00106
deepCopyField(
indices, copies);
00107 }
00108
00110
00112 void SelectColumnsVMatrix::build()
00113 {
00114 inherited::build();
00115
build_();
00116 }
00117
00119
00121 void SelectColumnsVMatrix::build_()
00122 {
00123
if (source) {
00124
if (
fields.
isNotEmpty()) {
00125
00126
indices.
clear();
00127
for (
int i = 0; i <
fields.
length(); i++) {
00128
string the_field =
fields[i];
00129
indices.
append(source->
getFieldIndex(the_field));
00130 }
00131 }
00132 width_ =
indices.
length();
00133 length_ = source->
length();
00134
00135 fieldinfos.
resize(
width());
00136
if (source->getFieldInfos().size() > 0)
00137
for (
int i=0; i<
width(); ++i)
00138 fieldinfos[i] = source->getFieldInfos()[
indices[i]];
00139 }
00140 }
00141
00143
00145 const map<string,real>& SelectColumnsVMatrix::getStringToRealMapping(
int col)
const {
00146
return source->getStringToRealMapping(
indices[col]);
00147 }
00148
00150
00152 real SelectColumnsVMatrix::getStringVal(
int col,
const string & str)
const {
00153
return source->getStringVal(
indices[col], str);
00154 }
00155
00156
00158
00160 const map<real,string>& SelectColumnsVMatrix::getRealToStringMapping(
int col)
const {
00161
return source->getRealToStringMapping(
indices[col]);
00162 }
00163
00165
00167 string SelectColumnsVMatrix::getValString(
int col,
real val)
const {
00168
return source->getValString(
indices[col],
val);
00169 }
00170
00171 }