select-feats.cc
Go to the documentation of this file.
1 // featbin/select-feats.cc
2 
3 // Copyright 2012 Korbinian Riedhammer
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include <sstream>
21 #include <algorithm>
22 #include <iterator>
23 #include <utility>
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "matrix/kaldi-matrix.h"
28 
29 
30 int main(int argc, char *argv[]) {
31  try {
32  using namespace kaldi;
33  using namespace std;
34 
35  const char *usage =
36  "Select certain dimensions of the feature file; think of it as the unix\n"
37  "command cut -f ...\n"
38  "Usage: select-feats <selection> <in-rspecifier> <out-wspecifier>\n"
39  " e.g. select-feats 0,24-22,3-12 scp:feats.scp ark,scp:feat-red.ark,feat-red.scp\n"
40  "See also copy-feats, extract-feature-segments, subset-feats, subsample-feats\n";
41 
42  ParseOptions po(usage);
43 
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  string sspecifier = po.GetArg(1);
52  string rspecifier = po.GetArg(2);
53  string wspecifier = po.GetArg(3);
54 
55  // set up input (we'll need that to validate the selected indices)
56  SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
57 
58  if (kaldi_reader.Done()) {
59  KALDI_WARN << "Empty archive provided.";
60  return 0;
61  }
62 
63  int32 dim_in = kaldi_reader.Value().NumCols();
64  int32 dim_out = 0;
65 
66  // figure out the selected dimensions
67  istringstream iss(sspecifier);
68  string token;
69  vector<pair<int32, int32> > ranges;
70  vector<int32> offsets;
71  while (getline(iss, token, ',')) {
72  size_t p = token.find('-');
73  if (p != string::npos) {
74  int s, e;
75  istringstream(token.substr(0, token.length() - p - 1)) >> s;
76  istringstream(token.substr(p+1)) >> e;
77 
78  if (s < 0 || s > (dim_in-1)) {
79  KALDI_ERR << "Invalid range start: " << s;
80  return 1;
81  } else if (e < 0 || e > (dim_in-1)) {
82  KALDI_ERR << "Invalid range end: " << e;
83  return 1;
84  }
85 
86  // reverse range? make individual selections
87  if (s > e) {
88  for (int32 i = s; i >= e; --i) {
89  ranges.push_back(pair<int32, int32>(i, i));
90  offsets.push_back(dim_out);
91  dim_out += 1;
92  }
93  } else {
94  ranges.push_back(pair<int32, int32>(s, e));
95  offsets.push_back(dim_out);
96  dim_out += (e - s + 1);
97  }
98  } else {
99  int i;
100  istringstream(token) >> i;
101 
102  if (i < 0 || i > (dim_in - 1)) {
103  KALDI_ERR << "Invalid selection index: " << i;
104  return 1;
105  }
106 
107  ranges.push_back(pair<int32, int32>(i, i));
108  offsets.push_back(dim_out);
109  dim_out += 1;
110  }
111  }
112 
113  if (ranges.size() < 1) {
114  KALDI_ERR << "No ranges or indices in selection string!";
115  return 1;
116  }
117 
118  // set up output
119  BaseFloatMatrixWriter kaldi_writer(wspecifier);
120 
121  // process all keys
122  for (; !kaldi_reader.Done(); kaldi_reader.Next()) {
123  Matrix<BaseFloat> feats(kaldi_reader.Value().NumRows(), dim_out);
124 
125  // extract the desired ranges
126  for (int32 i = 0; i < ranges.size(); ++i) {
127  int32 f = ranges[i].first;
128  int32 ncol = ranges[i].second - f + 1;
129 
130  feats.Range(0, feats.NumRows(), offsets[i], ncol)
131  .CopyFromMat(kaldi_reader.Value().Range(0, feats.NumRows(), f, ncol));
132  }
133 
134  kaldi_writer.Write(kaldi_reader.Key(), feats);
135  }
136 
137  return 0;
138  } catch(const std::exception &e) {
139  std::cerr << e.what();
140  return -1;
141  }
142 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
int main(int argc, char *argv[])
Definition: select-feats.cc:30
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202