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
00044
#include "LocalNeighborsDifferencesVMatrix.h"
00045
#include "VMat_maths.h"
00046
00047
namespace PLearn {
00048
using namespace std;
00049
00050
00051 LocalNeighborsDifferencesVMatrix::LocalNeighborsDifferencesVMatrix()
00052 :
inherited(), n_neighbors(-1), concat_neighbors(false), append_indexes(false)
00053
00054 {
00055 }
00056
00057
PLEARN_IMPLEMENT_OBJECT(
LocalNeighborsDifferencesVMatrix,
00058
"Computes the difference between each input row and its nearest neighbors.",
00059
"For each row x of the source VMatrix, the resulting row will be the\n"
00060
"concatenation of n_neighbors vectors, each of which is the difference\n"
00061
"between one of the nearest neighbors of x in the source and x itself.\n"
00062 );
00063
00064 void LocalNeighborsDifferencesVMatrix::getNewRow(
int i,
const Vec& v)
const
00065
{
00066
if (width_<0)
00067
PLERROR(
"LocalNeighborsDifferencesVMatrix::getNewRow called but build was not done yet");
00068
if (
concat_neighbors)
00069 {
00070
00071
00072
neighbor_row.
resize(source->
width());
00073
ith_row.
resize(source->
width());
00074
diff_k = v;
00075
00076 source->getRow(i,
ith_row);
00077 source->getRow(
neighbors(i,
n_neighbors-1),
neighbor_row);
00078
00079
00080
substract(
neighbor_row,
ith_row,
diff_k);
00081
00082
if(
append_indexes)
00083 {
00084 v[source->
width()] = i;
00085 v[source->
width()+1] =
neighbors(i,
n_neighbors-1);
00086 }
00087
00088 }
00089
else
00090 {
00091
00092
Mat differences = v.
toMat(
n_neighbors,source->
width());
00093
neighbor_row.
resize(source->
width());
00094
ith_row.
resize(source->
width());
00095 source->getRow(i,
ith_row);
00096
for (
int k=0;
k<
n_neighbors;
k++)
00097 {
00098
diff_k = differences(
k);
00099 source->getRow(
neighbors(i,
k),
neighbor_row);
00100
substract(
neighbor_row,
ith_row,
diff_k);
00101
00102
00103 }
00104
00105
if(
append_indexes)
00106 {
00107 v[n_neighbors*source->
width()] = i;
00108
for(
int k=0;
k<n_neighbors;
k++)
00109 v[n_neighbors*source->
width()+
k+1] =
neighbors(i,
k);
00110 }
00111 }
00112
00113 }
00114
00115 void LocalNeighborsDifferencesVMatrix::declareOptions(
OptionList& ol)
00116 {
00117
declareOption(ol,
"n_neighbors", &LocalNeighborsDifferencesVMatrix::n_neighbors, OptionBase::buildoption,
00118
"Number of nearest neighbors. Determines the width of this vmatrix, which\n"
00119
"is source->width() * n_neighbors.\n");
00120
declareOption(ol,
"concat_neighbors", &LocalNeighborsDifferencesVMatrix::concat_neighbors, OptionBase::buildoption,
00121
"If true, returns the concatenation of nearest neighbors(x) -x from 1 to n_neighbors , else, returns the n_neighbor nearest neighbor(x) - x");
00122
declareOption(ol,
"append_indexes", &LocalNeighborsDifferencesVMatrix::append_indexes, OptionBase::buildoption,
00123
"Indication that the indexes of the current data point and of the k nearest neighbors\n"
00124
"should be appended to the row of the VMatrix.\n");
00125
00126
00127 inherited::declareOptions(ol);
00128 }
00129
00130 void LocalNeighborsDifferencesVMatrix::build_()
00131 {
00132
00133
if (source && (
neighbors.
length()==0 || source->
length()!=length_ || source->
width()*
n_neighbors!=width_))
00134
00135 {
00136
if (
concat_neighbors)
00137 {
00138 width_ = source->
width();
00139
if(
append_indexes) width_ += 2;
00140 }
00141
else
00142 {
00143 width_ = source->
width()*
n_neighbors;
00144
if(
append_indexes) width_ +=
n_neighbors+1;
00145 }
00146
00147 length_ = source->
length();
00148
neighbors.
resize(source->
length(),
n_neighbors);
00149
a_row.
resize(source->
width());
00150
for (
int i=0;i<source->
length();i++)
00151 {
00152 source->getRow(i,
a_row);
00153
TVec<int> neighbors_of_i =
neighbors(i);
00154
computeNearestNeighbors(source,
a_row,neighbors_of_i,i);
00155 }
00156 }
00157 }
00158
00159
00160 void LocalNeighborsDifferencesVMatrix::build()
00161 {
00162 inherited::build();
00163
build_();
00164 }
00165
00166 void LocalNeighborsDifferencesVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00167 {
00168 inherited::makeDeepCopyFromShallowCopy(copies);
00169
deepCopyField(
neighbor_row, copies);
00170
deepCopyField(
ith_row, copies);
00171
deepCopyField(
a_row, copies);
00172
deepCopyField(
neighbors, copies);
00173 }
00174
00175 }
00176