PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::ReplicateSamplesVMatrix Class Reference

The first sentence should be a BRIEF DESCRIPTION of what the class does. More...

#include <ReplicateSamplesVMatrix.h>

Inheritance diagram for PLearn::ReplicateSamplesVMatrix:
Inheritance graph
[legend]
Collaboration diagram for PLearn::ReplicateSamplesVMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ReplicateSamplesVMatrix ()
 Default constructor.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ReplicateSamplesVMatrixdeepCopy (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_ ()
 SelectRowsVMatrix.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Public Attributes

bool operate_on_bags
int bag_index
int32_t seed
bool replicate_missing_targets
bool replicate_negative_targets

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares the class options.

Protected Attributes

PP< PRandomrandom_gen

Private Types

typedef SelectRowsVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

The first sentence should be a BRIEF DESCRIPTION of what the class does.

Place the rest of the class programmer documentation here. Doxygen supports Javadoc-style comments. See http://www.doxygen.org/manual.html

Todo:
Write class to-do's here if there are any.
Deprecated:
Write deprecated stuff here if there is any. Indicate what else should be used instead.

Definition at line 58 of file ReplicateSamplesVMatrix.h.


Member Typedef Documentation

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.h.


Constructor & Destructor Documentation

PLearn::ReplicateSamplesVMatrix::ReplicateSamplesVMatrix ( )

Default constructor.

Definition at line 65 of file ReplicateSamplesVMatrix.cc.

                                                :
    operate_on_bags(false),
    bag_index(-1),
    seed(1827),
    replicate_missing_targets(true),
    replicate_negative_targets(true),
    random_gen(new PRandom())
{}

Member Function Documentation

string PLearn::ReplicateSamplesVMatrix::_classname_ ( ) [static]

SelectRowsVMatrix.

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

OptionList & PLearn::ReplicateSamplesVMatrix::_getOptionList_ ( ) [static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

RemoteMethodMap & PLearn::ReplicateSamplesVMatrix::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

bool PLearn::ReplicateSamplesVMatrix::_isa_ ( const Object o) [static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

Object * PLearn::ReplicateSamplesVMatrix::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

StaticInitializer ReplicateSamplesVMatrix::_static_initializer_ & PLearn::ReplicateSamplesVMatrix::_static_initialize_ ( ) [static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

void PLearn::ReplicateSamplesVMatrix::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 119 of file ReplicateSamplesVMatrix.cc.

References PLearn::SelectRowsVMatrix::build(), and build_().

{
    // ### Nothing to add here, simply calls build_
    inherited::build();
    build_();
}

Here is the call graph for this function:

void PLearn::ReplicateSamplesVMatrix::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 129 of file ReplicateSamplesVMatrix.cc.

References PLearn::TVec< T >::append(), bag_index, PLearn::SelectRowsVMatrix::build(), c, PLearn::TVec< T >::copy(), PLearn::VMat::getExample(), i, PLearn::SelectRowsVMatrix::indices, PLearn::SelectRowsVMatrix::indices_vmat, PLearn::is_missing(), j, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), PLearn::VMat::length(), operate_on_bags, PLASSERT, PLCHECK_MSG, PLERROR, random_gen, replicate_missing_targets, replicate_negative_targets, PLearn::TVec< T >::resize(), seed, PLearn::SourceVMatrix::source, PLearn::SumOverBagsVariable::TARGET_COLUMN_FIRST, PLearn::VMatrix::targetsize(), PLearn::tostring(), and PLearn::VMatrix::updateMtime().

Referenced by build().

{
    if (!source)
        return;

    PLCHECK_MSG(operate_on_bags || source->targetsize() >= 1,
            "In ReplicateSamplesVMatrix::build_ - The source VMat must have a "
            "targetsize at least 1 when not operating on bags, but its "
            "targetsize is " + tostring(source->targetsize()));

    PLCHECK_MSG(!operate_on_bags || source->targetsize() >= 2,
            "In ReplicateSamplesVMatrix::build_ - The source VMat must have a "
            "targetsize at least 2 when operating on bags, but its targetsize "
            "is " + tostring(source->targetsize()));

    updateMtime(indices_vmat);
    updateMtime(source);

    PLASSERT(bag_index < 0 || bag_index < source->targetsize());

    // Build the vector of indices.
    indices.resize(0);
    Vec input, target;
    real weight;
    TVec< TVec<int>  > class_indices;  // Indices of samples in each class.
    TVec<int> nan_indices; // Indices of missing class
    TVec< TVec<int> > negativeclass_indices;
    map<int, int> bag_sizes; // Map a source index to the size of its bag.
    int bag_start_idx = -1;
    int bag_idx = bag_index >= 0 ? bag_index : source->targetsize() - 1;
    for (int i = 0; i < source->length(); i++) {
        source->getExample(i, input, target, weight);
        real c_real = target[0];
        int c = int(round(c_real));
        if (!is_missing(c_real)) {
            if (c >= class_indices.length()) {
                int n_to_add = c - class_indices.length() + 1;
                for (int j = 0; j < n_to_add; j++)
                    class_indices.append(TVec<int>());
            }
            else if ( -c >= negativeclass_indices.length() ) {
                int n_to_add = -c - negativeclass_indices.length() + 1;
                for (int j = 0; j < n_to_add; j++)
                    negativeclass_indices.append(TVec<int>());
            }
        }
        
        if (!operate_on_bags || int(round(target[bag_idx])) &
                                SumOverBagsVariable::TARGET_COLUMN_FIRST) {
            if( is_missing(c_real) )
                nan_indices.append(i);
            else if( c>= 0 )
                class_indices[c].append(i);
            else if( c< 0 )
                negativeclass_indices[-c].append(i);
            else
                PLERROR("In ReplicateSamplesVMatrix::build_ - Invalid class");
            indices.append(i);
            bag_sizes[i] = 0;
            bag_start_idx = i;
        }
        if (operate_on_bags)
            bag_sizes[bag_start_idx]++;
    }

    if( replicate_missing_targets && nan_indices.length() > 0  )
        class_indices.append( nan_indices );
    if( replicate_negative_targets && negativeclass_indices.length() > 0 )
        for(int c = 0; c < negativeclass_indices.length(); c ++ )
            if( negativeclass_indices[c].length() > 0 )
                class_indices.append( negativeclass_indices[c] );
    int max_n = -1;
    for (int c = 0; c < class_indices.length(); c++) {
        if (class_indices[c].length() > max_n)
            max_n = class_indices[c].length();
        if (class_indices[c].isEmpty())
            PLERROR("In ReplicateSamplesVMatrix::build_ - Cannot replicate "
                    "samples for class %d since there are zero samples from "
                    "this class", c);
    }
    for (int c = 0; c < class_indices.length(); c++) {
        int n_replicated = max_n - class_indices[c].length();
        for (int i = 0; i < n_replicated; i++) {
            indices.append(class_indices[c][i % class_indices[c].length()]);
        }
    }

    // Shuffle data.
    random_gen->manual_seed(seed);
    random_gen->shuffleElements(indices);

    if (operate_on_bags) {
        // We now need to convert the list of start indices to the list of all
        // indices within each bag.
        TVec<int> start_idx = indices.copy();
        indices.resize(0);
        for (int i = 0; i < start_idx.length(); i++) {
            int start_i = start_idx[i];
            for (int j = 0; j < bag_sizes[start_i]; j++)
                indices.append(start_i + j);
        }
    }
    
    // Re-build since indices have changed.
    inherited::build();
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::ReplicateSamplesVMatrix::classname ( ) const [virtual]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

void PLearn::ReplicateSamplesVMatrix::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 77 of file ReplicateSamplesVMatrix.cc.

References bag_index, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SelectRowsVMatrix::declareOptions(), operate_on_bags, replicate_missing_targets, replicate_negative_targets, and seed.

{
    // ### Declare all of this object's options here.
    // ### For the "flags" of each option, you should typically specify
    // ### one of OptionBase::buildoption, OptionBase::learntoption or
    // ### OptionBase::tuningoption. If you don't provide one of these three,
    // ### this option will be ignored when loading values from a script.
    // ### You can also combine flags, for example with OptionBase::nosave:
    // ### (OptionBase::buildoption | OptionBase::nosave)

    declareOption(ol, "operate_on_bags",
                  &ReplicateSamplesVMatrix::operate_on_bags,
                  OptionBase::buildoption,
        "If set to 1, then bags in the source VMat will be taken into\n"
        "account so as to preserve their integrity. The classes will also be\n"
        "reweighted so that they have the same number of bags.");

    declareOption(ol, "bag_index",
                  &ReplicateSamplesVMatrix::bag_index,
                  OptionBase::buildoption,
        "Index of the target corresponding to the bag information (useful\n"
        "only when operate_on_bags is True). -1 means the last element.");

    declareOption(ol, "seed", &ReplicateSamplesVMatrix::seed,
                  OptionBase::buildoption,
        "Seed for the random number generator (to shuffle data).");

    declareOption(ol, "replicate_missing_targets", &ReplicateSamplesVMatrix::replicate_missing_targets,
                  OptionBase::buildoption,
        "Indication that samples with missing (NaN) targets should be replicated.");

    declareOption(ol, "replicate_negative_targets", &ReplicateSamplesVMatrix::replicate_negative_targets,
                  OptionBase::buildoption,
        "Indication that samples with negative targets should be replicated.");

    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::ReplicateSamplesVMatrix::declaringFile ( ) [inline, static]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 83 of file ReplicateSamplesVMatrix.h.

:

ReplicateSamplesVMatrix * PLearn::ReplicateSamplesVMatrix::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

OptionList & PLearn::ReplicateSamplesVMatrix::getOptionList ( ) const [virtual]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

OptionMap & PLearn::ReplicateSamplesVMatrix::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

RemoteMethodMap & PLearn::ReplicateSamplesVMatrix::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 60 of file ReplicateSamplesVMatrix.cc.

void PLearn::ReplicateSamplesVMatrix::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 239 of file ReplicateSamplesVMatrix.cc.

References PLearn::deepCopyField(), PLearn::SelectRowsVMatrix::makeDeepCopyFromShallowCopy(), and random_gen.

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::SelectRowsVMatrix.

Definition at line 83 of file ReplicateSamplesVMatrix.h.

Definition at line 66 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 65 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 93 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and makeDeepCopyFromShallowCopy().

Definition at line 68 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 69 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 67 of file ReplicateSamplesVMatrix.h.

Referenced by build_(), and declareOptions().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines