Low-level functions for manipulating statistics and event-maps

See Decision tree internals and specifically Classes and functions involved in tree-building for context. More...

Collaboration diagram for Low-level functions for manipulating statistics and event-maps:

Functions

void DeleteBuildTreeStats (BuildTreeStatsType *stats)
 This frees the Clusterable* pointers in "stats", where non-NULL, and sets them to NULL. More...
 
void WriteBuildTreeStats (std::ostream &os, bool binary, const BuildTreeStatsType &stats)
 Writes BuildTreeStats object. This works even if pointers are NULL. More...
 
void ReadBuildTreeStats (std::istream &is, bool binary, const Clusterable &example, BuildTreeStatsType *stats)
 Reads BuildTreeStats object. More...
 
bool PossibleValues (EventKeyType key, const BuildTreeStatsType &stats, std::vector< EventValueType > *ans)
 Convenience function e.g. More...
 
void SplitStatsByMap (const BuildTreeStatsType &stats_in, const EventMap &e, std::vector< BuildTreeStatsType > *stats_out)
 Splits stats according to the EventMap, indexing them at output by the leaf type. More...
 
void SplitStatsByKey (const BuildTreeStatsType &stats_in, EventKeyType key, std::vector< BuildTreeStatsType > *stats_out)
 SplitStatsByKey splits stats up according to the value of a particular key, which must be always defined and nonnegative. More...
 
bool ConvertStats (int32 oldN, int32 oldP, int32 newN, int32 newP, BuildTreeStatsType *stats)
 Converts stats from a given context-window (N) and central-position (P) to a different N and P, by possibly reducing context. More...
 
void FilterStatsByKey (const BuildTreeStatsType &stats_in, EventKeyType key, std::vector< EventValueType > &values, bool include_if_present, BuildTreeStatsType *stats_out)
 FilterStatsByKey filters the stats according the value of a specified key. More...
 
ClusterableSumStats (const BuildTreeStatsType &stats_in)
 Sums stats, or returns NULL stats_in has no non-NULL stats. More...
 
BaseFloat SumNormalizer (const BuildTreeStatsType &stats_in)
 Sums the normalizer [typically, data-count] over the stats. More...
 
BaseFloat SumObjf (const BuildTreeStatsType &stats_in)
 Sums the objective function over the stats. More...
 
void SumStatsVec (const std::vector< BuildTreeStatsType > &stats_in, std::vector< Clusterable * > *stats_out)
 Sum a vector of stats. More...
 
BaseFloat ObjfGivenMap (const BuildTreeStatsType &stats_in, const EventMap &e)
 Cluster the stats given the event map return the total objf given those clusters. More...
 
void FindAllKeys (const BuildTreeStatsType &stats, AllKeysType keys_type, std::vector< EventKeyType > *keys)
 FindAllKeys puts in *keys the (sorted, unique) list of all key identities in the stats. More...
 

Detailed Description

See Decision tree internals and specifically Classes and functions involved in tree-building for context.

Function Documentation

◆ ConvertStats()

bool ConvertStats ( int32  oldN,
int32  oldP,
int32  newN,
int32  newP,
BuildTreeStatsType stats 
)

Converts stats from a given context-window (N) and central-position (P) to a different N and P, by possibly reducing context.

This function does a job that's quite specific to the "normal" stats format we use. See Phonetic context windows for background. This function may delete some keys and change others, depending on the N and P values. It expects that at input, all keys will either be -1 or lie between 0 and oldN-1. At output, keys will be either -1 or between 0 and newN-1. Returns false if we could not convert the stats (e.g. because newN is larger than oldN).

Definition at line 1077 of file build-tree-utils.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, and KALDI_WARN.

Referenced by kaldi::InitAmGmmFromOld(), and kaldi::TestConvertStats().

1078  {
1079  bool warned = false;
1080  KALDI_ASSERT(stats != NULL && oldN > 0 && newN > 0 && oldP >= 0
1081  && newP >= 0 && newP < newN && oldP < oldN);
1082  if (newN > oldN) { // can't add unseen context.
1083  KALDI_WARN << "Cannot convert stats to larger context: " << newN
1084  << " > " << oldN;
1085  return false;
1086  }
1087  if (newP > oldP) {
1088  KALDI_WARN << "Cannot convert stats to have more left-context: " << newP
1089  << " > " << oldP;
1090  }
1091  if (newN-newP-1 > oldN-oldP-1) {
1092  KALDI_WARN << "Cannot convert stats to have more right-context: " << (newN-newP-1)
1093  << " > " << (oldN-oldP-1);
1094  }
1095  // if shift < 0, this is by how much we "shift down" key
1096  // values.
1097  int32 shift = newP - oldP; // shift <= 0.
1098 
1099  for (size_t i = 0; i < stats->size(); i++) {
1100  EventType &evec = (*stats)[i].first;
1101  EventType evec_new;
1102  for (size_t j = 0; j < evec.size(); j++) {
1103  EventKeyType key = evec[j].first;
1104  if (key >= 0 && key < oldN) { //
1105  key += shift;
1106  if (key >= 0 && key < newN) // within new context window...
1107  evec_new.push_back(std::make_pair(key, evec[j].second));
1108  } else {
1109  if (key != -1) {
1110  // don't understand this key value but assume for now
1111  // it's something that doesn't interact with the context window.
1112  if (!warned) {
1113  KALDI_WARN << "Stats had keys defined that we cannot interpret";
1114  warned = true;
1115  }
1116  }
1117  evec_new.push_back(evec[j]);
1118  }
1119  }
1120  evec = evec_new; // Assign the modified EventVector with possibly
1121  // deleted keys.
1122  }
1123  return true;
1124 }
kaldi::int32 int32
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ DeleteBuildTreeStats()

void DeleteBuildTreeStats ( BuildTreeStatsType stats)

This frees the Clusterable* pointers in "stats", where non-NULL, and sets them to NULL.

Does not delete the pointer "stats" itself.

Definition at line 754 of file build-tree-utils.cc.

References KALDI_ASSERT.

Referenced by kaldi::GenRandContextDependency(), kaldi::GenRandContextDependencyLarge(), main(), kaldi::TestBuildTree(), kaldi::TestClusterEventMap(), kaldi::TestClusterEventMapGetMappingAndRenumberEventMap(), kaldi::TestClusterEventMapGetMappingAndRenumberEventMap2(), kaldi::TestClusterEventMapRestricted(), kaldi::TestGenRandStats(), kaldi::TestShareEventMapLeaves(), and kaldi::TestSplitDecisionTree().

754  {
755  KALDI_ASSERT(stats != NULL);
756  BuildTreeStatsType::iterator iter = stats->begin(), end = stats->end();
757  for (; iter!= end; ++iter) if (iter->second != NULL) { delete iter->second; iter->second = NULL; } // set to NULL for extra safety.
758 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FilterStatsByKey()

void FilterStatsByKey ( const BuildTreeStatsType stats_in,
EventKeyType  key,
std::vector< EventValueType > &  values,
bool  include_if_present,
BuildTreeStatsType stats_out 
)

FilterStatsByKey filters the stats according the value of a specified key.

If include_if_present == true, it only outputs the stats whose key is in "values"; otherwise it only outputs the stats whose key is not in "values". At input, "values" must be sorted and unique, and all stats in "stats_in" must have "key" defined. At output, pointers to Clusterable* in stats_out are not newly allocated– they are the same as the ones in stats_in.

Definition at line 222 of file build-tree-utils.cc.

References kaldi::EventTypeToString(), kaldi::IsSortedAndUniq(), KALDI_ASSERT, KALDI_ERR, and EventMap::Lookup().

Referenced by kaldi::AutomaticallyObtainQuestions(), kaldi::BuildTree(), kaldi::BuildTreeTwoLevel(), and kaldi::KMeansClusterPhones().

227  {
228  KALDI_ASSERT(IsSortedAndUniq(values));
229  BuildTreeStatsType::const_iterator iter, end = stats_in.end();
230  KALDI_ASSERT(stats_out != NULL);
231  stats_out->clear();
232 
233  for (iter = stats_in.begin(); iter != end; ++iter) {
234  const EventType &evec = iter->first;
235  EventValueType val;
236  if (! EventMap::Lookup(evec, key, &val)) // no such key. // HERE.
237  KALDI_ERR << "SplitStats: key "<< key << " is not present in event vector " << EventTypeToString(evec);
238  bool in_values = std::binary_search(values.begin(), values.end(), val);
239  if (in_values == include_if_present)
240  stats_out->push_back(*iter);
241  }
242 }
std::string EventTypeToString(const EventType &evec)
Definition: event-map.cc:253
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63

◆ FindAllKeys()

void FindAllKeys ( const BuildTreeStatsType stats,
AllKeysType  keys_type,
std::vector< EventKeyType > *  keys 
)

FindAllKeys puts in *keys the (sorted, unique) list of all key identities in the stats.

If type == kAllKeysInsistIdentical, it will insist that this set of keys is the same for all the stats (else exception is thrown). if type == kAllKeysIntersection, it will return the smallest common set of keys present in the set of stats if type== kAllKeysUnion (currently probably not so useful since maps will return "undefined" if key is not present), it will return the union of all the keys present in the stats.

Definition at line 92 of file build-tree-utils.cc.

References kaldi::GetEventKeys(), KALDI_ASSERT, KALDI_ERR, kaldi::kAllKeysInsistIdentical, kaldi::kAllKeysIntersection, and kaldi::kAllKeysUnion.

Referenced by Questions::InitRand(), and kaldi::TestFindAllKeys().

92  {
93  KALDI_ASSERT(keys_out != NULL);
94  BuildTreeStatsType::const_iterator iter = stats.begin(), end = stats.end();
95  if (iter == end) return; // empty set of keys.
96  std::vector<EventKeyType> keys;
97  GetEventKeys(iter->first, &keys);
98  ++iter;
99  for (; iter!= end; ++iter) {
100  std::vector<EventKeyType> keys2;
101  GetEventKeys(iter->first, &keys2);
102  if (keys_type == kAllKeysInsistIdentical) {
103  if (keys2 != keys)
104  KALDI_ERR << "AllKeys: keys in events are not all the same [called with kAllKeysInsistIdentical and all are not identical.";
105  } else if (keys_type == kAllKeysIntersection) {
106  std::vector<EventKeyType> new_keys(std::max(keys.size(), keys2.size()));
107  // following line relies on sorted order of event keys.
108  std::vector<EventKeyType>::iterator end_iter =
109  std::set_intersection(keys.begin(), keys.end(), keys2.begin(), keys2.end(), new_keys.begin());
110  new_keys.erase(end_iter, new_keys.end());
111  keys = new_keys;
112  } else { // union.
113  KALDI_ASSERT(keys_type == kAllKeysUnion);
114  std::vector<EventKeyType> new_keys(keys.size()+keys2.size());
115  // following line relies on sorted order of event keys.
116  std::vector<EventKeyType>::iterator end_iter =
117  std::set_union(keys.begin(), keys.end(), keys2.begin(), keys2.end(), new_keys.begin());
118  new_keys.erase(end_iter, new_keys.end());
119  keys = new_keys;
120  }
121  }
122  *keys_out = keys;
123 }
static void GetEventKeys(const EventType &vec, std::vector< EventKeyType > *keys)
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ObjfGivenMap()

BaseFloat ObjfGivenMap ( const BuildTreeStatsType stats_in,
const EventMap e 
)

Cluster the stats given the event map return the total objf given those clusters.

Definition at line 285 of file build-tree-utils.cc.

References kaldi::DeletePointers(), kaldi::SplitStatsByMap(), kaldi::SumClusterableObjf(), and kaldi::SumStatsVec().

Referenced by kaldi::BuildTree(), kaldi::BuildTreeTwoLevel(), and kaldi::TestSplitDecisionTree().

285  {
286  std::vector<BuildTreeStatsType> split_stats;
287  SplitStatsByMap(stats_in, e, &split_stats);
288  std::vector<Clusterable*> summed_stats;
289  SumStatsVec(split_stats, &summed_stats);
290  BaseFloat ans = SumClusterableObjf(summed_stats);
291  DeletePointers(&summed_stats);
292  return ans;
293 }
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
void SplitStatsByMap(const BuildTreeStatsType &stats, const EventMap &e, std::vector< BuildTreeStatsType > *stats_out)
Splits stats according to the EventMap, indexing them at output by the leaf type. ...
float BaseFloat
Definition: kaldi-types.h:29
void SumStatsVec(const std::vector< BuildTreeStatsType > &stats_in, std::vector< Clusterable *> *stats_out)
Sum a vector of stats.
BaseFloat SumClusterableObjf(const std::vector< Clusterable *> &vec)
Returns the total objective function after adding up all the statistics in the vector (pointers may b...

◆ PossibleValues()

bool PossibleValues ( EventKeyType  key,
const BuildTreeStatsType stats,
std::vector< EventValueType > *  ans 
)

Convenience function e.g.

to work out possible values of the phones from just the stats. Returns true if key was always defined inside the stats. May be used with and == NULL to find out of key was always defined.

Definition at line 63 of file build-tree-utils.cc.

References kaldi::CopySetToVector(), and EventMap::Lookup().

Referenced by kaldi::DoTableSplit(), kaldi::FindBestSplitForKey(), Questions::InitRand(), main(), kaldi::TestPossibleValues(), and kaldi::TestShareEventMapLeaves().

65  {
66  bool all_present = true;
67  std::set<EventValueType> values;
68  BuildTreeStatsType::const_iterator iter = stats.begin(), end = stats.end();
69  for (; iter != end; ++iter) {
70  EventValueType val;
71  if (EventMap::Lookup(iter->first, key, &val))
72  values.insert(val);
73  else
74  all_present = false;
75  }
76  if (ans)
77  CopySetToVector(values, ans);
78  return all_present;
79 }
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51

◆ ReadBuildTreeStats()

void ReadBuildTreeStats ( std::istream &  is,
bool  binary,
const Clusterable example,
BuildTreeStatsType stats 
)

Reads BuildTreeStats object.

The "example" argument must be of the same type as the stats on disk, and is needed for access to the correct "Read" function. It was organized this way for easier extensibility (so adding new Clusterable derived classes isn't painful)

Definition at line 46 of file build-tree-utils.cc.

References kaldi::ExpectToken(), rnnlm::i, KALDI_ASSERT, kaldi::ReadBasicType(), kaldi::ReadEventType(), and Clusterable::ReadNew().

Referenced by main(), and kaldi::TestBuildTreeStatsIo().

46  {
47  KALDI_ASSERT(stats != NULL);
48  KALDI_ASSERT(stats->empty());
49  ExpectToken(is, binary, "BTS");
50  uint32 size;
51  ReadBasicType(is, binary, &size);
52  stats->resize(size);
53  for (size_t i = 0; i < size; i++) {
54  ReadEventType(is, binary, &((*stats)[i].first));
55  bool nonNull;
56  ReadBasicType(is, binary, &nonNull);
57  if (nonNull) (*stats)[i].second = example.ReadNew(is, binary);
58  else (*stats)[i].second = NULL;
59  }
60 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void ReadEventType(std::istream &is, bool binary, EventType *evec)
Definition: event-map.cc:239
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SplitStatsByKey()

void SplitStatsByKey ( const BuildTreeStatsType stats_in,
EventKeyType  key,
std::vector< BuildTreeStatsType > *  stats_out 
)

SplitStatsByKey splits stats up according to the value of a particular key, which must be always defined and nonnegative.

Like MapStats. Pointers to Clusterable* in stats_out are not newly allocated– they are the same as the ones in stats_in. Generally they will still be owned at stats_in (user can decide where to allocate ownership).

Definition at line 198 of file build-tree-utils.cc.

References kaldi::EventTypeToString(), KALDI_ASSERT, KALDI_ERR, and EventMap::Lookup().

Referenced by kaldi::AutomaticallyObtainQuestions(), kaldi::ClusterEventMapRestrictedHelper(), kaldi::FindBestSplitForKey(), kaldi::GetToLengthMap(), kaldi::KMeansClusterPhones(), and kaldi::TestSplitStatsByKey().

198  {
199  BuildTreeStatsType::const_iterator iter, end = stats_in.end();
200  KALDI_ASSERT(stats_out != NULL);
201  stats_out->clear();
202  size_t size = 0;
203  // This loop works out size of output vector.
204  for (iter = stats_in.begin(); iter != end; ++iter) {
205  const EventType &evec = iter->first;
206  EventValueType val;
207  if (! EventMap::Lookup(evec, key, &val)) // no such key.
208  KALDI_ERR << "SplitStats: key "<< key << " is not present in event vector " << EventTypeToString(evec);
209  size = std::max(size, (size_t)(val+1));
210  }
211  stats_out->resize(size);
212  // This loop splits up the stats.
213  for (iter = stats_in.begin(); iter != end; ++iter) {
214  const EventType &evec = iter->first;
215  EventValueType val;
216  EventMap::Lookup(evec, key, &val); // will not fail.
217  (*stats_out)[val].push_back(*iter);
218  }
219 }
std::string EventTypeToString(const EventType &evec)
Definition: event-map.cc:253
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51

◆ SplitStatsByMap()

void SplitStatsByMap ( const BuildTreeStatsType stats_in,
const EventMap e,
std::vector< BuildTreeStatsType > *  stats_out 
)

Splits stats according to the EventMap, indexing them at output by the leaf type.

A utility function. NOTE– pointers in stats_out point to the same memory location as those in stats. No copying of Clusterable* objects happens. Will add to stats in stats_out if non-empty at input. This function may increase the size of vector stats_out as necessary to accommodate stats, but will never decrease the size.

Definition at line 172 of file build-tree-utils.cc.

References kaldi::EventTypeToString(), KALDI_ASSERT, KALDI_ERR, and EventMap::Map().

Referenced by kaldi::ClusterEventMapGetMapping(), kaldi::ClusterEventMapRestrictedByMap(), kaldi::ClusterEventMapToNClustersRestrictedByMap(), kaldi::ComputeTreeMapping(), kaldi::DoTableSplit(), kaldi::GetOccs(), kaldi::InitAmGmm(), kaldi::InitAmGmmFromOld(), main(), kaldi::ObjfGivenMap(), kaldi::SplitDecisionTree(), and kaldi::TestSplitDecisionTree().

172  {
173  BuildTreeStatsType::const_iterator iter, end = stats.end();
174  KALDI_ASSERT(stats_out != NULL);
175  stats_out->clear();
176  size_t size = 0;
177  for (iter = stats.begin(); iter != end; ++iter) {
178  const EventType &evec = iter->first;
179  EventAnswerType ans;
180  if (!e.Map(evec, &ans)) // this is an error--could not map it.
181  KALDI_ERR << "SplitStatsByMap: could not map event vector " << EventTypeToString(evec)
182  << "if error seen during tree-building, check that "
183  << "--context-width and --central-position match stats, "
184  << "and that phones that are context-independent (CI) during "
185  << "stats accumulation do not share roots with non-CI phones.";
186  size = std::max(size, (size_t)(ans+1));
187  }
188  stats_out->resize(size);
189  for (iter = stats.begin(); iter != end; ++iter) {
190  const EventType &evec = iter->first;
191  EventAnswerType ans;
192  bool b = e.Map(evec, &ans);
193  KALDI_ASSERT(b);
194  (*stats_out)[ans].push_back(*iter);
195  }
196 }
std::string EventTypeToString(const EventType &evec)
Definition: event-map.cc:253
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
Definition: event-map.h:56

◆ SumNormalizer()

BaseFloat SumNormalizer ( const BuildTreeStatsType stats_in)

Sums the normalizer [typically, data-count] over the stats.

Definition at line 258 of file build-tree-utils.cc.

References Clusterable::Normalizer().

Referenced by kaldi::BuildTree(), kaldi::BuildTreeTwoLevel(), kaldi::GetOccs(), and main().

258  {
259  BaseFloat ans = 0.0;
260  BuildTreeStatsType::const_iterator iter = stats_in.begin(), end = stats_in.end();
261  for (; iter != end; ++iter) {
262  Clusterable *cl = iter->second;
263  if (cl != NULL) ans += cl->Normalizer();
264  }
265  return ans;
266 }
float BaseFloat
Definition: kaldi-types.h:29

◆ SumObjf()

BaseFloat SumObjf ( const BuildTreeStatsType stats_in)

Sums the objective function over the stats.

Definition at line 268 of file build-tree-utils.cc.

References Clusterable::Objf().

268  {
269  BaseFloat ans = 0.0;
270  BuildTreeStatsType::const_iterator iter = stats_in.begin(), end = stats_in.end();
271  for (; iter != end; ++iter) {
272  Clusterable *cl = iter->second;
273  if (cl != NULL) ans += cl->Objf();
274  }
275  return ans;
276 }
float BaseFloat
Definition: kaldi-types.h:29

◆ SumStats()

Clusterable * SumStats ( const BuildTreeStatsType stats_in)

Sums stats, or returns NULL stats_in has no non-NULL stats.

Stats are newly allocated, owned by caller.

Definition at line 245 of file build-tree-utils.cc.

References Clusterable::Add(), and Clusterable::Copy().

Referenced by DecisionTreeSplitter::DoSplitInternal(), kaldi::InitAmGmmFromOld(), and kaldi::SumStatsVec().

245  {
246  Clusterable *ans = NULL;
247  BuildTreeStatsType::const_iterator iter = stats_in.begin(), end = stats_in.end();
248  for (; iter != end; ++iter) {
249  Clusterable *cl = iter->second;
250  if (cl != NULL) {
251  if (!ans) ans = cl->Copy();
252  else ans->Add(*cl);
253  }
254  }
255  return ans;
256 }

◆ SumStatsVec()

void SumStatsVec ( const std::vector< BuildTreeStatsType > &  stats_in,
std::vector< Clusterable * > *  stats_out 
)

Sum a vector of stats.

Leaves NULL as pointer if no stats available. The pointers in stats_out are owned by caller. At output, there may be NULLs in the vector stats_out.

Definition at line 279 of file build-tree-utils.cc.

References rnnlm::i, KALDI_ASSERT, and kaldi::SumStats().

Referenced by kaldi::AutomaticallyObtainQuestions(), kaldi::ClusterEventMapGetMapping(), kaldi::ClusterEventMapToNClustersRestrictedByMap(), kaldi::FindBestSplitForKey(), kaldi::InitAmGmm(), kaldi::KMeansClusterPhones(), and kaldi::ObjfGivenMap().

279  {
280  KALDI_ASSERT(stats_out != NULL && stats_out->empty());
281  stats_out->resize(stats_in.size(), NULL);
282  for (size_t i = 0;i < stats_in.size();i++) (*stats_out)[i] = SumStats(stats_in[i]);
283 }
Clusterable * SumStats(const BuildTreeStatsType &stats_in)
Sums stats, or returns NULL stats_in has no non-NULL stats.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ WriteBuildTreeStats()

void WriteBuildTreeStats ( std::ostream &  os,
bool  binary,
const BuildTreeStatsType stats 
)

Writes BuildTreeStats object. This works even if pointers are NULL.

Definition at line 29 of file build-tree-utils.cc.

References rnnlm::i, KALDI_ERR, kaldi::WriteBasicType(), kaldi::WriteEventType(), and kaldi::WriteToken().

Referenced by main(), kaldi::TestBuildTree(), kaldi::TestBuildTreeStatsIo(), and kaldi::TestGenRandStats().

29  {
30  WriteToken(os, binary, "BTS");
31  uint32 size = stats.size();
32  WriteBasicType(os, binary, size);
33  for (size_t i = 0; i < size; i++) {
34  WriteEventType(os, binary, stats[i].first);
35  bool nonNull = (stats[i].second != NULL);
36  WriteBasicType(os, binary, nonNull);
37  if (nonNull) stats[i].second->Write(os, binary);
38  }
39  if (os.fail()) {
40  KALDI_ERR << "WriteBuildTreeStats: write failed.";
41  }
42  if (!binary) os << '\n';
43 }
void WriteEventType(std::ostream &os, bool binary, const EventType &evec)
Definition: event-map.cc:228
#define KALDI_ERR
Definition: kaldi-error.h:147
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34