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::MovingAverageVMatrix Class Reference

#include <MovingAverageVMatrix.h>

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

List of all members.

Public Member Functions

 MovingAverageVMatrix ()
virtual void getNewRow (int i, Vec &v) const
 This is the only method requiring implementation.
virtual void build ()
 Simply calls inherited::build() then build_().
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual MovingAverageVMatrixdeepCopy (CopiesMap &copies) const

Static Public Member Functions

static string _classname_ ()
 Declares name and deepCopy methods.
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

TVec< string > columns_to_average
TVec< intwindow_sizes
bool centered_windows

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Protected Attributes

Mat sums
Mat ma
TMat< intnnonmissing
Vec row
Vec previous_sourcerow
TVec< intcolumns
int max_window_size

Private Types

typedef SourceVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 52 of file MovingAverageVMatrix.h.


Member Typedef Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 54 of file MovingAverageVMatrix.h.


Constructor & Destructor Documentation

PLearn::MovingAverageVMatrix::MovingAverageVMatrix ( )

Definition at line 51 of file MovingAverageVMatrix.cc.

    :inherited(), centered_windows(true)
    /* ### Initialise all fields to their default value */
{
    // ...

    // ### You may or may not want to call build_() to finish building the object
    // build_();
}

Member Function Documentation

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

Declares name and deepCopy methods.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 204 of file MovingAverageVMatrix.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 151 of file MovingAverageVMatrix.cc.

References PLearn::TMat< T >::clear(), columns, columns_to_average, PLearn::TMat< T >::fill(), PLearn::VMatrix::hasFieldInfos(), j, PLearn::VMat::length(), PLearn::TVec< T >::length(), PLearn::VMatrix::length_, ma, max_window_size, MISSING_VALUE, nnonmissing, PLERROR, previous_sourcerow, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), row, PLearn::VMatrix::setFieldInfos(), PLearn::TVec< T >::size(), PLearn::SourceVMatrix::source, PLearn::SourceVMatrix::sourcerow, PLearn::TVec< T >::subVec(), sums, PLearn::tostring(), PLearn::VMatrix::updateMtime(), w, PLearn::VMat::width(), PLearn::VMatrix::width_, and window_sizes.

Referenced by build().

{
    int nc=columns_to_average.length();
    if (source)
    {
        row.resize(source->width()+nc);
        sourcerow = row.subVec(0,source->width());
        previous_sourcerow.resize(source->width());
        columns.resize(nc);
        max_window_size=0;
        if (window_sizes.length()!=nc)
            PLERROR("MovingAverageVMatrix: the window_sizes option should have the same length as the columns_to_average option (got %d and %d)",
                    window_sizes.length(),nc);
        for (int j=0;j<nc;j++)
        {
            if ((columns[j] = source->fieldIndex(columns_to_average[j])) == -1)
                PLERROR("MovingAverageVMatrix: provided field name %s not found in source VMatrix",columns_to_average[j].c_str());
            if (window_sizes[j]>max_window_size)
                max_window_size=window_sizes[j];
        }

        updateMtime(source);

        // copy length and width from source if not set
        if(length_<0)
            length_ = source->length();
        if(width_<0)
            width_ = source->width() + nc;

        sums.resize(length_,nc);
        sums.fill(MISSING_VALUE);
        nnonmissing.resize(length_,nc);
        nnonmissing.clear();
        ma.resize(length_,nc);
        ma.fill(MISSING_VALUE);

        // copy fieldnames from source if not set and they look good
        if(!hasFieldInfos() && source->hasFieldInfos() )
        {
            Array<VMField>& sinfo = source->getFieldInfos();
            int w=sinfo.size();
            sinfo.resize(w+nc);
            for (int j=0;j<nc;j++)
            {
                sinfo[w+j]=sinfo[columns[j]];
                sinfo[w+j].name = "ma"+tostring(window_sizes[j])+"-"+sinfo[w+j].name;
            }
            setFieldInfos(sinfo);
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Declares this class' options.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 130 of file MovingAverageVMatrix.cc.

References PLearn::OptionBase::buildoption, centered_windows, columns_to_average, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), and window_sizes.

{
    // ### 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. Another possible flag to be combined with
    // ### is OptionBase::nosave

    declareOption(ol, "columns_to_average", &MovingAverageVMatrix::columns_to_average, OptionBase::buildoption,
                  "Names of the columns to average.");

    declareOption(ol, "window_sizes", &MovingAverageVMatrix::window_sizes, OptionBase::buildoption,
                  "Sizes (in number of rows) of the moving average windows for each column to average.");

    declareOption(ol, "centered_windows", &MovingAverageVMatrix::centered_windows, OptionBase::buildoption,
                  "Wether or not to center the window around the current example or to average only over previous examples");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 113 of file MovingAverageVMatrix.h.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

void PLearn::MovingAverageVMatrix::getNewRow ( int  i,
Vec v 
) const [virtual]

This is the only method requiring implementation.

Definition at line 67 of file MovingAverageVMatrix.cc.

References centered_windows, columns, columns_to_average, i, PLearn::is_missing(), j, PLearn::TVec< T >::length(), PLearn::VMatrix::length(), ma, max_window_size, PLearn::min(), MISSING_VALUE, n, nnonmissing, previous_sourcerow, row, PLearn::SourceVMatrix::source, PLearn::SourceVMatrix::sourcerow, sums, and window_sizes.

{
    source->getRow(i,sourcerow);
    int max_target = centered_windows?min(i+max_window_size/2,length()-1):i;
    if (is_missing(sums(max_target,0)))
    {
        int k=max_target-1;
        while (k>=0 && is_missing(sums(k,0))) k--;
        if (k<0)
        {
            k=0;
            source->getRow(k,previous_sourcerow);
            for (int j=0;j<columns.length();j++)
            {
                real new_value = previous_sourcerow[columns[j]];
                if (is_missing(new_value))
                {
                    sums(k,j) = 0;
                    ma(k,j) = MISSING_VALUE;
                }
                else
                {
                    sums(k,j) = new_value;
                    nnonmissing(k,j) = 1;
                    ma(k,j) = new_value;
                }
            }
        }
        for (;k<max_target;k++)
        {
            source->getRow(k+1,previous_sourcerow);
            for (int j=0;j<columns.length();j++)
            {
                real new_value = previous_sourcerow[columns[j]];
                if (!is_missing(new_value))
                {
                    sums(k+1,j) = sums(k,j) + new_value;
                    nnonmissing(k+1,j) = nnonmissing(k,j) + 1;
                }
                else sums(k+1,j) = sums(k,j);
                int delta = k+1-window_sizes[j];
                int n_at_window_start = (delta>=0)?nnonmissing(delta,j):0;
                int n = nnonmissing(k+1,j) - n_at_window_start;
                if (n>0)
                {
                    if (delta>=0)
                        ma(k+1,j) = (sums(k+1,j) - sums(delta,j))/n;
                    else
                        ma(k+1,j) = sums(k+1,j)/n;
                }
                else
                    ma(k+1,j) = MISSING_VALUE;
            }
        }
    }
    for (int j=0;j<columns_to_average.length();j++)
    {
        int target = centered_windows?min(i+window_sizes[j]/2,length()-1):i;
        row[sourcerow.length()+j]=ma(target,j);
    }
    v << row;
}

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 65 of file MovingAverageVMatrix.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 210 of file MovingAverageVMatrix.cc.

References columns, columns_to_average, PLearn::deepCopyField(), PLearn::SourceVMatrix::makeDeepCopyFromShallowCopy(), sums, and window_sizes.

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 113 of file MovingAverageVMatrix.h.

Definition at line 78 of file MovingAverageVMatrix.h.

Referenced by declareOptions(), and getNewRow().

Definition at line 66 of file MovingAverageVMatrix.h.

Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().

Definition at line 62 of file MovingAverageVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 67 of file MovingAverageVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 63 of file MovingAverageVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 65 of file MovingAverageVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 64 of file MovingAverageVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 61 of file MovingAverageVMatrix.h.

Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().


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