[go: up one dir, main page]

Menu

[r999]: / mia2 / mia / core / probmap.cc  Maximize  Restore  History

Download this file

218 lines (174 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* -*- mona-c++ -*-
*
* Copyright (c) Leipzig, Madrid 2004 - 2009
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PUcRPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <mia/core/probmap.hh>
NS_MIA_BEGIN
using namespace std;
static const string pipe_console("-");
static const string map_signature("#probability-map");
static const string label_map_signature("#label-map");
CProbabilityVector::CProbabilityVector()
{
}
CProbabilityVector::CProbabilityVector(size_t nClass, size_t nElm):
std::vector<CDoubleVector>(nClass, CDoubleVector(nElm))
{
}
CProbabilityVector::CProbabilityVector(const std::string& in_fname)
{
if (in_fname == pipe_console) {
do_load(cin);
}else {
ifstream instr(in_fname.c_str());
do_load(instr);
}
}
bool CProbabilityVector::save(const std::string& out_fname) const
{
if (out_fname == pipe_console) {
return do_save(cout);
}else {
ofstream outstr(out_fname.c_str());
return do_save(outstr);
}
}
bool CProbabilityVector::do_save(ostream& os) const
{
os << map_signature <<"\n";
os << size() << " " << (*this)[0].size() << '\n';
for (size_t i = 0; i < (*this)[0].size(); ++i) {
for (size_t k = 0; k < size(); ++k) {
os << (*this)[k][i] << " ";
}
os <<'\n';
}
return os.good();
}
void CProbabilityVector::do_load(istream& is)
{
string buf;
is >> buf;
size_t hsize, nclasses;
if (buf != map_signature) {
throw invalid_argument(string("input file is not a probability map: signature:'") + buf + string("'"));
}
is >> nclasses >> hsize;
if (!is.good())
throw runtime_error("error reading from input file");
resize(nclasses);
for (size_t k = 0; k < size(); ++k) {
(*this)[k] = CDoubleVector(hsize);
}
for (size_t i = 0; i < hsize; ++i) {
for (size_t k = 0; k < size(); ++k) {
is >> (*this)[k][i];
}
}
if (!is.good())
throw runtime_error("error reading from input file");
}
EXPORT_CORE bool operator == (const CProbabilityVector& a, const CProbabilityVector& b)
{
if ( a.size() != b.size() )
return false;
CProbabilityVector::const_iterator ai = a.begin();
CProbabilityVector::const_iterator ae = a.end();
CProbabilityVector::const_iterator bi = b.begin();
while (ai != ae) {
if (*ai++ != *bi++)
return false;
}
return true;
}
CLabelMap::CLabelMap()
{
}
CLabelMap::CLabelMap(const std::string& in_fname)
{
if (in_fname == pipe_console) {
do_load(cin);
}else {
ifstream instr(in_fname.c_str());
do_load(instr);
}
}
bool CLabelMap::save(const std::string& out_fname) const
{
if (out_fname == pipe_console) {
return do_save(cout);
}else {
ofstream outstr(out_fname.c_str());
return do_save(outstr);
}
}
bool CLabelMap::do_save(ostream& os) const
{
os << label_map_signature << "\n";
os << size()<<'\n';
for (CLabelMap::const_iterator im = begin(), em = end();
im != em; ++im) {
os << im->first << ' ' << im->second << '\n';
}
return os.good();
}
void CLabelMap::do_load(istream& is)
{
string buf;
is >> buf;
size_t hsize;
if (buf != label_map_signature) {
throw invalid_argument(string("input file is not a label map: signature:'") + buf + string("'"));
}
is >> hsize;
if (!is.good())
throw runtime_error("error reading from input file");
size_t src, trgt;
for (size_t i = 0; i < hsize; ++i) {
is >> src >> trgt;
(*this)[src] = trgt;
}
if (!is.good())
throw runtime_error("error reading from input file");
}
EXPORT_CORE bool operator == (const CLabelMap& a, const CLabelMap& b)
{
if (a.size() != b.size())
return false;
CLabelMap::const_iterator ai = a.begin();
CLabelMap::const_iterator bi = b.begin();
CLabelMap::const_iterator ae = a.end();
while (ai != ae) {
if (ai->first != bi->first ||
ai->second != bi->second)
return false;
++ai;
++bi;
}
return true;
}
NS_MIA_END