Skip to content

Commit

Permalink
src/util: Enabled reading partial matrices for TableReader (initial w…
Browse files Browse the repository at this point in the history
…ork by xzhang, rebased by dan)
  • Loading branch information
xiaohui-zhang authored and danpovey committed Apr 3, 2016
1 parent adb67c4 commit 0a60eaf
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 73 deletions.
11 changes: 11 additions & 0 deletions src/feat/wave-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class WaveData {
data_.Swap(&(other->data_));
std::swap(samp_freq_, other->samp_freq_);
}

private:
static const uint32 kBlockSize = 1024 * 1024; // Use 1M bytes.
Matrix<BaseFloat> data_;
Expand Down Expand Up @@ -171,6 +172,11 @@ class WaveHolder {
t_.Swap(&(other->t_));
}

bool ExtractRange(WaveHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

private:
T t_;
};
Expand Down Expand Up @@ -216,6 +222,11 @@ class WaveInfoHolder {
void Swap(WaveInfoHolder *other) {
t_.Swap(&(other->t_));
}

bool ExtractRange(WaveInfoHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}
private:
T t_;
};
Expand Down
5 changes: 5 additions & 0 deletions src/fstext/kaldi-fst-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class VectorFstTplHolder {
std::swap(t_, other->t_);
}

bool ExtractRange(VectorFstTplHolder<Arc> *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~VectorFstTplHolder() { Clear(); }
// No destructor. Assignment and
// copy constructor take their default implementations.
Expand Down
10 changes: 10 additions & 0 deletions src/hmm/posterior.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class PosteriorHolder {
void Swap(PosteriorHolder *other) {
t_.swap(other->t_);
}

bool ExtractRange(PosteriorHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(PosteriorHolder);
T t_;
Expand Down Expand Up @@ -116,6 +121,11 @@ class GaussPostHolder {
void Swap(GaussPostHolder *other) {
t_.swap(other->t_);
}

bool ExtractRange(GaussPostHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(GaussPostHolder);
T t_;
Expand Down
10 changes: 10 additions & 0 deletions src/lat/kaldi-lattice.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class CompactLatticeHolder {
std::swap(t_, other->t_);
}

bool ExtractRange(CompactLatticeHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~CompactLatticeHolder() { Clear(); }
private:
T *t_;
Expand Down Expand Up @@ -127,6 +132,11 @@ class LatticeHolder {
std::swap(t_, other->t_);
}

bool ExtractRange(LatticeHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~LatticeHolder() { Clear(); }
private:
T *t_;
Expand Down
2 changes: 1 addition & 1 deletion src/util/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TESTFILES = const-integer-set-test stl-utils-test text-utils-test \
kaldi-table-test simple-options-test

OBJFILES = text-utils.o kaldi-io.o \
kaldi-table.o parse-options.o simple-options.o simple-io-funcs.o
kaldi-holder.o kaldi-table.o parse-options.o simple-options.o simple-io-funcs.o

LIBNAME = kaldi-util

Expand Down
50 changes: 50 additions & 0 deletions src/util/kaldi-holder-inl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// util/kaldi-holder-inl.h

// Copyright 2009-2011 Microsoft Corporation
// 2016 Xiaohui Zhang

// See ../../COPYING for clarification regarding multiple authors
//
Expand Down Expand Up @@ -101,6 +102,12 @@ template<class KaldiType> class KaldiObjectHolder {
std::swap(t_, other->t_);
}

bool ExtractRange(KaldiObjectHolder<T> *other, const std::string &range) {
delete other->t_;
other->t_ = new T;
return ExtractObjectRange(*t_, range, other->t_);
}

~KaldiObjectHolder() { delete t_; }
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(KaldiObjectHolder);
Expand Down Expand Up @@ -194,6 +201,11 @@ template<class BasicType> class BasicHolder {
std::swap(t_, other->t_);
}

bool ExtractRange(BasicHolder<T> *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~BasicHolder() { }
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(BasicHolder);
Expand Down Expand Up @@ -309,6 +321,12 @@ template<class BasicType> class BasicVectorHolder {
t_.swap(other->t_);
}

bool ExtractRange(BasicVectorHolder<BasicType> *other,
const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~BasicVectorHolder() { }
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(BasicVectorHolder);
Expand Down Expand Up @@ -456,6 +474,12 @@ template<class BasicType> class BasicVectorVectorHolder {
t_.swap(other->t_);
}

bool ExtractRange(BasicVectorVectorHolder<BasicType> *other,
const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~BasicVectorVectorHolder() { }
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(BasicVectorVectorHolder);
Expand Down Expand Up @@ -597,6 +621,12 @@ template<class BasicType> class BasicPairVectorHolder {
t_.swap(other->t_);
}

bool ExtractRange(BasicPairVectorHolder<BasicType> *other,
const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

~BasicPairVectorHolder() { }
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(BasicPairVectorHolder);
Expand Down Expand Up @@ -652,6 +682,11 @@ class TokenHolder {
t_.swap(other->t_);
}

bool ExtractRange(TokenHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

private:
KALDI_DISALLOW_COPY_AND_ASSIGN(TokenHolder);
T t_;
Expand Down Expand Up @@ -709,6 +744,11 @@ class TokenVectorHolder {
t_.swap(other->t_);
}

bool ExtractRange(TokenVectorHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

private:
KALDI_DISALLOW_COPY_AND_ASSIGN(TokenVectorHolder);
T t_;
Expand Down Expand Up @@ -752,6 +792,11 @@ class HtkMatrixHolder {
std::swap(t_.second, other->t_.second);
}

bool ExtractRange(HtkMatrixHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

// No destructor.
private:
KALDI_DISALLOW_COPY_AND_ASSIGN(HtkMatrixHolder);
Expand Down Expand Up @@ -857,6 +902,11 @@ template<int kFeatDim> class SphinxMatrixHolder {
feats_.Swap(&(other->feats_));
}

bool ExtractRange(SphinxMatrixHolder *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

private:
KALDI_DISALLOW_COPY_AND_ASSIGN(SphinxMatrixHolder);
T feats_;
Expand Down
77 changes: 77 additions & 0 deletions src/util/kaldi-holder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// util/kaldi-holder.cc

// Copyright 2009-2011 Microsoft Corporation
// 2016 Xiaohui Zhang

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.

#include "util/kaldi-holder.h"
#include "matrix/kaldi-matrix.h"

namespace kaldi {

template<class Real>
bool ExtractObjectRange(const Matrix<Real> &input, const std::string &range,
Matrix<Real> *output) {
if (range.empty()) {
KALDI_ERR << "Empty range specifier.";
return false;
}
std::vector<std::string> splits;
SplitStringToVector(range, ",", false, &splits);
if (!((splits.size() == 1 && !splits[0].empty()) ||
(splits.size() == 2 && !splits[0].empty() && !splits[1].empty()))) {
KALDI_ERR << "Invalid range specifier: " << range;
return false;
}
std::vector<int32> row_range, col_range;
bool status = true;
if (splits[0] != ":")
status = SplitStringToIntegers(splits[0], ":", false, &row_range);
if (splits.size() == 2 && splits[1] != ":") {
status = status && SplitStringToIntegers(splits[1], ":", false, &col_range);
}
if (row_range.size() == 0) {
row_range.push_back(0);
row_range.push_back(input.NumRows() - 1);
}
if (col_range.size() == 0) {
col_range.push_back(0);
col_range.push_back(input.NumCols() - 1);
}
if (!(status && row_range.size() == 2 && col_range.size() == 2 &&
row_range[0] >= 0 && row_range[0] < row_range[1] &&
row_range[1] < input.NumRows() && col_range[0] >=0 &&
col_range[0] < col_range[1] && col_range[1] < input.NumCols())) {
KALDI_ERR << "Invalid range specifier: " << range;
return false;
}
int32 row_size = row_range[1] - row_range[0] + 1,
col_size = col_range[1] - col_range[0] + 1;
output->Resize(row_size, col_size, kUndefined);
output->CopyFromMat(input.Range(row_range[0], row_size,
col_range[0], col_size));
return true;
}

// template instantiation
template bool ExtractObjectRange(const Matrix<double> &, const std::string &,
Matrix<double> *);
template bool ExtractObjectRange(const Matrix<BaseFloat> &, const std::string &,
Matrix<BaseFloat> *);


} // end namespace kaldi
29 changes: 29 additions & 0 deletions src/util/kaldi-holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Copyright 2009-2011 Microsoft Corporation
// 2016 Johns Hopkins University (author: Daniel Povey)
// 2016 Xiaohui Zhang

// See ../../COPYING for clarification regarding multiple authors
//
Expand Down Expand Up @@ -134,6 +135,15 @@ template<class SomeType> class GenericHolder {
/// of holder, not with some nonexistent base-class.
void Swap(GenericHolder<T> *other) { std::swap(t_, other->t_); }

/// This is only defined for KaldiObjectHolder holding matrix objects,
/// in order to extract a holder holding a sub-matrix specified by 'range',
/// e.g. [1:2,2:10]. It returns true with successful extraction.
/// For other types of holder it just throws an error.
bool ExtractRange(GenericHolder<T> *other, const std::string &range) {
KALDI_ERR << "ExtractRange is not defined for this type of holder.";
return false;
}

/// If the object held pointers, the destructor would free them.
~GenericHolder() { }

Expand Down Expand Up @@ -205,6 +215,25 @@ class HtkMatrixHolder;
/// A class for reading/writing Sphinx format matrices.
template<int kFeatDim = 13> class SphinxMatrixHolder;

/// This templated function exists so that we can write .scp files with
/// 'object ranges' specified: the canonical example is a [first:last] range
/// of rows of a matrix, or [first-row:last-row,first-column,last-column]
/// of a matrix. We can also support [begin-time:end-time] of a wave
/// file. The string 'range' is whatever is in the square brackets; it is
/// parsed inside this function.
/// This function returns true if the partial object was successfully extracted,
/// and false if there was an error such as an invalid range.
/// The generic version of this function just fails; we overload the template
/// whenever we need it for a specific class.
template <class T>
bool ExtractObjectRange(const T &input, const std::string &range, T *output) {
KALDI_ERR << "Ranges not supported for objects of this type.";
return false;
}

template <class Real>
bool ExtractObjectRange(const Matrix<Real> &input, const std::string &range,
Matrix<Real> *output);

/// @} end "addtogroup holders"

Expand Down
Loading

0 comments on commit 0a60eaf

Please sign in to comment.