|
PLearn 0.1
|
Sort the samples of a VMatrix according to one (or more) given columns. More...
#include <SortRowsVMatrix.h>


Public Member Functions | |
| SortRowsVMatrix () | |
| virtual string | classname () const |
| virtual OptionList & | getOptionList () const |
| virtual OptionMap & | getOptionMap () const |
| virtual RemoteMethodMap & | getRemoteMethodMap () const |
| virtual SortRowsVMatrix * | deepCopy (CopiesMap &copies) const |
| virtual void | build () |
| Simply calls inherited::build() then build_(). | |
| virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
| Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
| static string | _classname_ () |
| SortRowsVMatrix. | |
| static OptionList & | _getOptionList_ () |
| static RemoteMethodMap & | _getRemoteMethodMap_ () |
| static Object * | _new_instance_for_typemap_ () |
| static bool | _isa_ (const Object *o) |
| static void | _static_initialize_ () |
| static const PPath & | declaringFile () |
Public Attributes | |
| bool | ignore_missing_fields |
| Public build options. | |
| bool | increasing_order |
| TVec< int > | sort_columns |
| TVec< string > | sort_columns_by_name |
Static Public Attributes | |
| static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
| static void | declareOptions (OptionList &ol) |
| Declares this class' options. | |
Private Types | |
| typedef SelectRowsVMatrix | inherited |
Private Member Functions | |
| void | build_ () |
| This does the actual building. | |
Static Private Member Functions | |
| static void | sortRows (VMat &m, TVec< int > &indices, TVec< int > &sort_columns, int istart, int iend, int colstart, bool increasing_order) |
| This function returns in indices the sorting of the rows of a VMat m, according to the columns specified by sort_columns[colstart, ...], where m is viewed as ordered by the indices given at calling time. | |
Sort the samples of a VMatrix according to one (or more) given columns.
Definition at line 52 of file SortRowsVMatrix.h.
typedef SelectRowsVMatrix PLearn::SortRowsVMatrix::inherited [private] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 57 of file SortRowsVMatrix.h.
| PLearn::SortRowsVMatrix::SortRowsVMatrix | ( | ) |
Definition at line 60 of file SortRowsVMatrix.cc.
References PLearn::SelectRowsVMatrix::warn_if_all_rows_selected.
:
ignore_missing_fields(false),
increasing_order(true)
{
warn_if_all_rows_selected = false;
}
| string PLearn::SortRowsVMatrix::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| OptionList & PLearn::SortRowsVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| RemoteMethodMap & PLearn::SortRowsVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| Object * PLearn::SortRowsVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| StaticInitializer SortRowsVMatrix::_static_initializer_ & PLearn::SortRowsVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| void PLearn::SortRowsVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 116 of file SortRowsVMatrix.cc.
References PLearn::SelectRowsVMatrix::build(), and build_().
{
inherited::build();
build_();
}

| void PLearn::SortRowsVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 125 of file SortRowsVMatrix.cc.
References PLearn::TVec< T >::append(), PLearn::SelectRowsVMatrix::build(), PLearn::VMat::column(), PLearn::TMat< T >::column(), PLearn::VMat::getFieldIndex(), i, ignore_missing_fields, increasing_order, PLearn::SelectRowsVMatrix::indices, PLearn::SelectRowsVMatrix::indices_vmat, PLearn::TVec< T >::isNotEmpty(), j, PLearn::VMat::length(), PLearn::TVec< T >::length(), PLERROR, PLearn::TVec< T >::resize(), sort_columns, sort_columns_by_name, sortRows(), PLearn::SourceVMatrix::source, PLearn::VMat::subMatColumns(), PLearn::VMatrix::updateMtime(), and PLearn::VMat::width().
Referenced by build().
{
if (sort_columns_by_name.isNotEmpty() && source) {
// Convert column names into column indices.
sort_columns.resize(0);
for (int i = 0; i < sort_columns_by_name.length(); i++) {
int idx = source->getFieldIndex(sort_columns_by_name[i],
!ignore_missing_fields);
if (idx >= 0)
sort_columns.append(idx);
}
}
// Check we don't try to sort twice by the same column (this can be confusing).
// Also verify fields do exist, just in case.
if (sort_columns.isNotEmpty()) {
for (int i = 0; i < sort_columns.length(); i++) {
if (!ignore_missing_fields &&
(sort_columns[i] < 0 || sort_columns[i] >= source->width()))
PLERROR("In SortRowsVMatrix::build_ - Field with index %d "
"cannot exist in the source VMatrix, whose width is "
"%d", sort_columns[i], source->width());
for (int j = i + 1; j < sort_columns.length(); j++) {
if (sort_columns[j] == sort_columns[i]) {
PLERROR("In SortRowsVMatrix::build_ - You have a duplicated index in the 'sort_columns' vector");
}
}
}
}
// Construct the indices vector.
if (source) {
updateMtime(source);
indices = TVec<int>(0, source.length()-1, 1);
if (sort_columns.length() > 1) {
// We need to sort many columns: we use the unefficient method.
sortRows(source, indices, sort_columns, 0, source->length()-1, 0, increasing_order);
} else if (sort_columns.length() > 0) {
// Only sorting one column: we can do this more efficiently.
Mat to_sort(source->length(), 2);
// Fill first column with the column to sort.
to_sort.column(0) << source.subMatColumns(sort_columns[0], 1);
// Fill 2nd column with indices.
to_sort.column(1) << Vec(0, to_sort.length() - 1, 1);
if (to_sort.column(0).hasMissing()) {
// We have missing values, so we use the unefficient method
sortRows(source, indices, sort_columns, 0, source->length()-1,
0, increasing_order);
} else {
// Perform the sort.
PLearn::sortRows(to_sort, 0, increasing_order);
// Get the indices.
indices << to_sort.column(1);
}
}
inherited::build(); // Since we have just changed the indices.
}
updateMtime(indices_vmat);
}


| string PLearn::SortRowsVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| void PLearn::SortRowsVMatrix::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 70 of file SortRowsVMatrix.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SelectRowsVMatrix::declareOptions(), ignore_missing_fields, increasing_order, PLearn::SelectRowsVMatrix::indices, PLearn::SelectRowsVMatrix::indices_vmat, PLearn::OptionBase::nosave, PLearn::redeclareOption(), sort_columns, and sort_columns_by_name.
{
declareOption(ol, "sort_columns", &SortRowsVMatrix::sort_columns,
OptionBase::buildoption,
"Indices of the column(s) that must be sorted (the first one is the\n"
"first criterion).");
declareOption(ol, "sort_columns_by_name",
&SortRowsVMatrix::sort_columns_by_name,
OptionBase::buildoption,
"Names of the column(s) that must be sorted (the first one is the\n"
"first criterion). This option is optional and, if provided, the\n"
"'sort_columns' option will be ignored.");
declareOption(ol, "ignore_missing_fields",
&SortRowsVMatrix::ignore_missing_fields,
OptionBase::buildoption,
"If true, then no error will be thrown when a column given in\n"
"sort_columns or sort_columns_by_name is missing.");
declareOption(ol, "increasing_order", &SortRowsVMatrix::increasing_order, OptionBase::buildoption,
" if set to 1, the data will be sorted in increasing order");
inherited::declareOptions(ol);
// Hide unused options.
redeclareOption(ol, "indices", &SortRowsVMatrix::indices, OptionBase::nosave,
"The indices are computed at build time.");
redeclareOption(ol, "indices_vmat", &SortRowsVMatrix::indices_vmat, OptionBase::nosave,
"Unused.");
}

| static const PPath& PLearn::SortRowsVMatrix::declaringFile | ( | ) | [inline, static] |
| SortRowsVMatrix * PLearn::SortRowsVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| OptionList & PLearn::SortRowsVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| OptionMap & PLearn::SortRowsVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| RemoteMethodMap & PLearn::SortRowsVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 55 of file SortRowsVMatrix.cc.
| void PLearn::SortRowsVMatrix::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 106 of file SortRowsVMatrix.cc.
References PLearn::deepCopyField(), PLearn::SelectRowsVMatrix::makeDeepCopyFromShallowCopy(), sort_columns, and sort_columns_by_name.
{
deepCopyField(sort_columns, copies);
deepCopyField(sort_columns_by_name, copies);
inherited::makeDeepCopyFromShallowCopy(copies);
}

| void PLearn::SortRowsVMatrix::sortRows | ( | VMat & | m, |
| TVec< int > & | indices, | ||
| TVec< int > & | sort_columns, | ||
| int | istart, | ||
| int | iend, | ||
| int | colstart, | ||
| bool | increasing_order | ||
| ) | [static, private] |
This function returns in indices the sorting of the rows of a VMat m, according to the columns specified by sort_columns[colstart, ...], where m is viewed as ordered by the indices given at calling time.
Only the samples between istart and iend are considered.
Definition at line 187 of file SortRowsVMatrix.cc.
References i, PLearn::is_equal(), PLearn::is_missing(), j, PLearn::TVec< T >::length(), m, and PLearn::TVec< T >::size().
Referenced by build_().
{
real best = 0; // Initialization only to prevent compiler warning.
real jval;
int tmp;
bool better;
if (sort_columns.size() > colstart) {
int col = sort_columns[colstart]; // The current column used to perform sorting.
for (int i = istart; i <= iend-1; i++) {
// Let's look for the i-th element of our result.
int i_nan = i;
// Find first non-missing.
while (i_nan <= iend && is_missing(best = m(indices[i_nan],col))) i_nan++;
if (i_nan > iend)
// All nan !
break;
else if (i_nan > i) {
// There were some nans. We swap i_nan and i.
tmp = indices[i];
indices[i] = indices[i_nan];
indices[i_nan] = tmp;
}
for (int j = i+1; j <= iend; j++) {
better = false;
jval = m(indices[j],col);
if (increasing_order && jval < best)
better = true;
else if (!increasing_order && jval > best)
better = true;
if (better) {
// Swap i and j.
tmp = indices[j];
indices[j] = indices[i];
indices[i] = tmp;
best = jval;
}
}
}
// At this point, we have only sorted according to one column.
if (sort_columns.length() > colstart + 1) {
// There are other sorting criteria.
// Let's find where we need to apply them.
int i = istart;
real val;
while (i <= iend - 1) {
val = m(indices[i],col);
int j = i+1;
while ( j <= iend
&& ( is_equal(m(indices[j],col), val, 1.0)))
j++;
j--;
if (j > i) {
// There are consecutive elements with the same value for the sorting
// criterion, thus we must use the other criteria to sort them correctly.
sortRows(m, indices, sort_columns, i, j, colstart + 1, increasing_order);
}
i = j+1;
}
}
}
}


Reimplemented from PLearn::SelectRowsVMatrix.
Definition at line 71 of file SortRowsVMatrix.h.
Public build options.
Definition at line 62 of file SortRowsVMatrix.h.
Referenced by build_(), and declareOptions().
Definition at line 63 of file SortRowsVMatrix.h.
Referenced by build_(), and declareOptions().
Definition at line 64 of file SortRowsVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 65 of file SortRowsVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
1.7.4