| 1 |
/* Confusion.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2020 David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
djmw 20010628 |
| 21 |
djmw 20020813 GPL header |
| 22 |
djmw 20061214 Changed info to Melder_writeLine<x> format. |
| 23 |
djmw 20070620 Latest modification. |
| 24 |
djmw 20080521 +Confusion_drawAsNumbers |
| 25 |
djmw 20110304 Thing_new |
| 26 |
djmw 20111110 Use autostringvector |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "Confusion.h" |
| 30 |
#include "Polygon_extensions.h" |
| 31 |
#include "Matrix_extensions.h" |
| 32 |
#include "TableOfReal_extensions.h" |
| 33 |
#include "Collection_extensions.h" |
| 34 |
#include "Distributions_and_Strings.h" |
| 35 |
#include "NUM2.h" |
| 36 |
|
| 37 |
Thing_implement (Confusion, TableOfReal, 0); |
| 38 |
|
| 39 |
void structConfusion :: v1_info () { |
| 40 |
// BUG: skipping parent classes |
| 41 |
double h, hx, hy, hygx, hxgy, uygx, uxgy, uxy, frac; |
| 42 |
integer nCorrect; |
| 43 |
|
| 44 |
Confusion_getEntropies (this, & h, & hx, & hy, & hygx, & hxgy, & uygx, & uxgy, & uxy); |
| 45 |
Confusion_getFractionCorrect (this, & frac, & nCorrect); |
| 46 |
MelderInfo_writeLine (U"Number of rows: ", numberOfRows); |
| 47 |
MelderInfo_writeLine (U"Number of colums: ", numberOfColumns); |
| 48 |
MelderInfo_writeLine (U"Entropies (y is row variable):"); |
| 49 |
MelderInfo_writeLine (U" Total: ", h); |
| 50 |
MelderInfo_writeLine (U" Y: ", hy); |
| 51 |
MelderInfo_writeLine (U" X: ", hx); |
| 52 |
MelderInfo_writeLine (U" Y given x: ", hygx); |
| 53 |
MelderInfo_writeLine (U" X given y: ", hxgy); |
| 54 |
MelderInfo_writeLine (U" Dependency of y on x; ", uygx); |
| 55 |
MelderInfo_writeLine (U" Dependency of x on y: ", uxgy); |
| 56 |
MelderInfo_writeLine (U" Symmetrical dependency: ", uxy); |
| 57 |
MelderInfo_writeLine (U" Total number of entries: ", Confusion_getNumberOfEntries (this)); |
| 58 |
MelderInfo_writeLine (U" Fraction correct: ", frac); |
| 59 |
} |
| 60 |
|
| 61 |
autoConfusion Confusion_createFromStringses (Strings me, Strings thee) { |
| 62 |
try { |
| 63 |
Melder_require (my numberOfStrings > 0 && thy numberOfStrings > 0, U"Both Strings should not be empty."); |
| 64 |
|
| 65 |
autoConfusion him = Confusion_create (my numberOfStrings, thy numberOfStrings); |
| 66 |
for (integer irow = 1; irow <= my numberOfStrings; irow ++) { |
| 67 |
conststring32 label = my strings [irow].get(); |
| 68 |
TableOfReal_setRowLabel (him.get(), irow, label); |
| 69 |
} |
| 70 |
for (integer icol = 1; icol <= thy numberOfStrings; icol ++) { |
| 71 |
conststring32 label = thy strings [icol].get(); |
| 72 |
TableOfReal_setColumnLabel (him.get(), icol, label); |
| 73 |
} |
| 74 |
return him; |
| 75 |
} catch (MelderError) { |
| 76 |
Melder_throw (me, U": could not create Confusion with ", thee); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
autoConfusion Confusion_create (integer numberOfStimuli, integer numberOfResponses) { |
| 81 |
try { |
| 82 |
autoConfusion me = Thing_new (Confusion); |
| 83 |
TableOfReal_init (me.get(), numberOfStimuli, numberOfResponses); |
| 84 |
return me; |
| 85 |
} catch (MelderError) { |
| 86 |
Melder_throw (U"Confusion not created."); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
autoConfusion Confusion_createSimple (conststring32 labels_string) { |
| 91 |
try { |
| 92 |
autoSTRVEC labels = splitByWhitespace_STRVEC (labels_string); |
| 93 |
Melder_require (labels.size > 0, U"There should be at least one label."); |
| 94 |
|
| 95 |
autoConfusion me = Confusion_create (labels.size, labels.size); |
| 96 |
integer ilabel = 1; |
| 97 |
for (integer itoken = 1; itoken <= labels.size; itoken ++) { |
| 98 |
const conststring32 token = labels [itoken].get(); |
| 99 |
for (integer i = 1; i <= ilabel - 1; i ++) { |
| 100 |
if (Melder_equ (token, my rowLabels [i].get())) { |
| 101 |
Melder_throw (U"Label ", i, U" and ", ilabel, U" should not be equal."); |
| 102 |
} |
| 103 |
} |
| 104 |
TableOfReal_setRowLabel (me.get(), ilabel, token); |
| 105 |
TableOfReal_setColumnLabel (me.get(), ilabel, token); |
| 106 |
ilabel ++; |
| 107 |
} |
| 108 |
return me; |
| 109 |
} catch (MelderError) { |
| 110 |
Melder_throw (U"Simple Confusion not created."); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
autoConfusion Categories_to_Confusion (Categories me, Categories thee) { |
| 115 |
try { |
| 116 |
Melder_require (my size == thy size, |
| 117 |
U"Both Categories should have the same number of items."); |
| 118 |
|
| 119 |
autoCategories ul1 = Categories_selectUniqueItems (me); |
| 120 |
autoCategories ul2 = Categories_selectUniqueItems (thee); |
| 121 |
autoConfusion him = Confusion_create (ul1->size, ul2->size); |
| 122 |
|
| 123 |
for (integer i = 1; i <= ul1->size; i ++) { |
| 124 |
const SimpleString s = ul1->at [i]; |
| 125 |
TableOfReal_setRowLabel (him.get(), i, s -> string.get()); |
| 126 |
} |
| 127 |
for (integer i = 1; i <= ul2->size; i ++) { |
| 128 |
const SimpleString s = ul2->at [i]; |
| 129 |
TableOfReal_setColumnLabel (him.get(), i, s -> string.get()); |
| 130 |
} |
| 131 |
for (integer i = 1; i <= my size; i ++) { |
| 132 |
const SimpleString myi = my at [i], thyi = thy at [i]; |
| 133 |
Confusion_increase (him.get(), myi -> string.get(), thyi -> string.get()); |
| 134 |
} |
| 135 |
return him; |
| 136 |
} catch (MelderError) { |
| 137 |
Melder_throw (me, U": no Confusion created."); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void Confusion_getEntropies (Confusion me, double *out_h, double *out_hx, double *out_hy, |
| 142 |
double *out_hygx, double *out_hxgy, double *out_uygx, double *out_uxgy, double *out_uxy) |
| 143 |
{ |
| 144 |
NUMgetEntropies (my data.get(), out_h, out_hx, |
| 145 |
out_hy, out_hygx, out_hxgy, out_uygx, out_uxgy, out_uxy); |
| 146 |
} |
| 147 |
|
| 148 |
void Confusion_increase (Confusion me, conststring32 stimulus, conststring32 response) { |
| 149 |
try { |
| 150 |
const integer stimulusIndex = TableOfReal_rowLabelToIndex (me, stimulus); |
| 151 |
Melder_require (stimulusIndex > 0, |
| 152 |
U"The stimulus name should be valid."); |
| 153 |
|
| 154 |
const integer responseIndex = TableOfReal_columnLabelToIndex (me, response); |
| 155 |
Melder_require (responseIndex > 0, |
| 156 |
U"The response name should be valid."); |
| 157 |
|
| 158 |
my data [stimulusIndex] [responseIndex] += 1.0; |
| 159 |
} catch (MelderError) { |
| 160 |
Melder_throw (me, U": not increased."); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
double Confusion_getValue (Confusion me, conststring32 stimulus, conststring32 response) { |
| 165 |
const integer stimulusIndex = TableOfReal_rowLabelToIndex (me, stimulus); |
| 166 |
Melder_require (stimulusIndex > 0, |
| 167 |
U"The stimulus name should be valid."); |
| 168 |
|
| 169 |
const integer responseIndex = TableOfReal_columnLabelToIndex (me, response); |
| 170 |
Melder_require (responseIndex > 0, |
| 171 |
U"The response name should be valid."); |
| 172 |
|
| 173 |
return my data [stimulusIndex] [responseIndex]; |
| 174 |
} |
| 175 |
|
| 176 |
void Confusion_getFractionCorrect (Confusion me, double *out_fraction, integer *out_numberOfCorrect) { |
| 177 |
double fraction = undefined; |
| 178 |
integer numberOfCorrect = -1; |
| 179 |
|
| 180 |
double c = 0.0, ct = 0.0; |
| 181 |
for (integer i = 1; i <= my numberOfRows; i ++) { |
| 182 |
for (integer j = 1; j <= my numberOfColumns; j ++) { |
| 183 |
if (! my rowLabels [i] || ! my columnLabels [j]) |
| 184 |
return; |
| 185 |
ct += my data [i] [j]; |
| 186 |
if (str32equ (my rowLabels [i].get(), my columnLabels [j].get())) |
| 187 |
c += my data [i] [j]; |
| 188 |
} |
| 189 |
} |
| 190 |
if (ct != 0.0) |
| 191 |
fraction = c / ct; |
| 192 |
if (out_fraction) |
| 193 |
*out_fraction = fraction; |
| 194 |
numberOfCorrect = Melder_ifloor (c); |
| 195 |
if (out_numberOfCorrect) |
| 196 |
*out_numberOfCorrect = numberOfCorrect; |
| 197 |
} |
| 198 |
|
| 199 |
/*************** Confusion_Matrix_draw ****************************************/ |
| 200 |
|
| 201 |
static autoPolygon Polygon_createPointer () { |
| 202 |
try { |
| 203 |
constexpr integer numberOfPoints = 6; |
| 204 |
double x [numberOfPoints + 1] = { 0.0, 0.0, 0.9, 1.0, 0.9, 0.0, 0.0 }; |
| 205 |
double y [numberOfPoints + 1] = { 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0 }; |
| 206 |
autoPolygon me = Polygon_create (numberOfPoints); |
| 207 |
for (integer i = 1; i <= numberOfPoints; i ++) { |
| 208 |
my x [i] = x [i]; |
| 209 |
my y [i] = y [i]; |
| 210 |
} |
| 211 |
return me; |
| 212 |
} catch (MelderError) { |
| 213 |
Melder_throw (U"Polygon not created."); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static void Polygon_drawInside (Polygon me, Graphics g) { |
| 218 |
Graphics_polyline (g, my numberOfPoints, & my x [1], & my y [1]); |
| 219 |
} |
| 220 |
|
| 221 |
void Confusion_Matrix_draw (Confusion me, Matrix thee, Graphics g, integer index, double lowerPercentage, double xmin, double xmax, double ymin, double ymax, bool garnish) { |
| 222 |
integer ib = 1, ie = my numberOfRows; |
| 223 |
if (index > 0 && index <= my numberOfColumns) { |
| 224 |
ib = ie = index; |
| 225 |
} |
| 226 |
Melder_require (thy ny == my numberOfRows, |
| 227 |
U"The number of stimuli should equal the number of rows in the matrix."); |
| 228 |
|
| 229 |
if (xmax <= xmin) |
| 230 |
(void) Matrix_getWindowExtrema (thee, 1, 1, 1, thy ny, & xmin, & xmax); |
| 231 |
if (xmax <= xmin) |
| 232 |
return; |
| 233 |
if (ymax <= ymin) |
| 234 |
(void) Matrix_getWindowExtrema (thee, 2, 2, 1, thy ny, & ymin, & ymax); |
| 235 |
if (ymax <= ymin) |
| 236 |
return; |
| 237 |
|
| 238 |
const double rmax = fabs (xmax - xmin) / 10.0; |
| 239 |
const double rmin = rmax / 10; |
| 240 |
|
| 241 |
Graphics_setInner (g); |
| 242 |
Graphics_setWindow (g, xmin - rmax, xmax + rmax, ymin - rmax, ymax + rmax); |
| 243 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 244 |
for (integer i = 1; i <= my numberOfRows; i ++) { |
| 245 |
Graphics_text (g, thy z [i] [1], thy z [i] [2], my rowLabels [i].get()); |
| 246 |
} |
| 247 |
for (integer i = ib; i <= ie; i ++) { |
| 248 |
const double xSum = NUMsum (my data.row (i)); |
| 249 |
if (xSum <= 0.0) |
| 250 |
continue; /* no confusions */ |
| 251 |
const double x1 = thy z [i] [1]; |
| 252 |
const double y1 = thy z [i] [2]; |
| 253 |
const double r = rmax * my data [i] [i] / xSum; |
| 254 |
|
| 255 |
Graphics_circle (g, x1, y1, ( r > rmin ? r : rmin )); |
| 256 |
|
| 257 |
for (integer j = 1; j <= my numberOfColumns; j ++) { |
| 258 |
const double x2 = thy z [j] [1], y2 = thy z [j] [2]; |
| 259 |
const double perc = 100.0 * my data [i] [j] / xSum; |
| 260 |
const double dx = x2 - x1, dy = y2 - y1; |
| 261 |
const double alpha = atan2 (dy, dx); |
| 262 |
|
| 263 |
if (perc == 0.0 || perc < lowerPercentage || j == i) |
| 264 |
continue; |
| 265 |
xmin = x1; |
| 266 |
xmax = x2; |
| 267 |
if (x2 < x1) { |
| 268 |
xmin = x2; |
| 269 |
xmax = x1; |
| 270 |
} |
| 271 |
ymin = y1; |
| 272 |
xmax = y2; |
| 273 |
if (y2 < y1) { |
| 274 |
ymin = y2; |
| 275 |
ymax = y1; |
| 276 |
} |
| 277 |
autoPolygon p = Polygon_createPointer(); |
| 278 |
double xs = hypot (dx, dy) - 2.2 * r; |
| 279 |
if (xs < 0.0) |
| 280 |
xs = 0.0; |
| 281 |
const double ys = perc * rmax / 100.0; |
| 282 |
Polygon_scale (p.get(), xs, ys); |
| 283 |
Polygon_translate (p.get(), x1, y1 - ys / 2); |
| 284 |
Polygon_rotate (p.get(), alpha, x1, y1); |
| 285 |
Polygon_translate (p.get(), 1.1 * r * cos (alpha), 1.1 * r * sin (alpha)); |
| 286 |
Polygon_drawInside (p.get(), g); |
| 287 |
} |
| 288 |
} |
| 289 |
Graphics_unsetInner (g); |
| 290 |
if (garnish) { |
| 291 |
Graphics_drawInnerBox (g); |
| 292 |
Graphics_marksBottom (g, 2, true, true, false); |
| 293 |
if (ymin * ymax < 0.0) { |
| 294 |
Graphics_markLeft (g, 0.0, true, true, true, nullptr); |
| 295 |
} |
| 296 |
Graphics_marksLeft (g, 2, true, true, false); |
| 297 |
if (xmin * xmax < 0.0) { |
| 298 |
Graphics_markBottom (g, 0.0, true, true, true, nullptr); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
autoMatrix Confusion_difference (Confusion me, Confusion thee) { |
| 304 |
try { |
| 305 |
/* categories must be the same too*/ |
| 306 |
Melder_require (my numberOfColumns == thy numberOfColumns && my numberOfRows == thy numberOfRows, |
| 307 |
U"The dimensions should be equal."); |
| 308 |
|
| 309 |
autoMatrix him = Matrix_create (0.5, my numberOfColumns + 0.5, my numberOfColumns, 1.0, 1.0, 0.5, my numberOfRows + 0.5, my numberOfRows, 1.0, 1.0); |
| 310 |
|
| 311 |
his z.all() <<= my data.all() - thy data.all(); |
| 312 |
|
| 313 |
return him; |
| 314 |
} catch (MelderError) { |
| 315 |
Melder_throw (U"Matrix not created from two Confusions."); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
integer Confusion_getNumberOfEntries (Confusion me) { |
| 320 |
const double total = NUMsum (my data.get()); |
| 321 |
return Melder_ifloor (total); |
| 322 |
} |
| 323 |
|
| 324 |
static autoINTVEC create_index (constSTRVEC const& s, constSTRVEC const& ref) { |
| 325 |
autoINTVEC index = raw_INTVEC (s.size); |
| 326 |
for (integer i = 1; i <= s.size; i ++) { |
| 327 |
integer indxj = 0; |
| 328 |
for (integer j = 1; j <= ref.size; j ++) { |
| 329 |
if (str32equ (s [i], ref [j])) { |
| 330 |
indxj = j; |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
index [i] = indxj; |
| 335 |
} |
| 336 |
return index; |
| 337 |
} |
| 338 |
|
| 339 |
autoConfusion Confusion_condense (Confusion me, conststring32 search, conststring32 replace, |
| 340 |
integer maximumNumberOfReplaces, bool use_regexp) { |
| 341 |
try { |
| 342 |
integer nmatches, nstringmatches; |
| 343 |
Melder_require (my rowLabels && my columnLabels, |
| 344 |
U"Both row and column labels should be present."); |
| 345 |
|
| 346 |
autoSTRVEC rowLabels = string32vector_searchAndReplace (my rowLabels.get(), |
| 347 |
search, replace, maximumNumberOfReplaces, & nmatches, & nstringmatches, use_regexp); |
| 348 |
|
| 349 |
autoSTRVEC columnLabels = string32vector_searchAndReplace (my columnLabels.get(), |
| 350 |
search, replace, maximumNumberOfReplaces, & nmatches, & nstringmatches, use_regexp); |
| 351 |
|
| 352 |
autoStrings srow = Thing_new (Strings); |
| 353 |
srow -> numberOfStrings = my numberOfRows; |
| 354 |
srow -> strings = std::move (rowLabels); |
| 355 |
|
| 356 |
autoStrings scol = Thing_new (Strings); |
| 357 |
scol -> numberOfStrings = my numberOfColumns; |
| 358 |
scol -> strings = std::move (columnLabels); |
| 359 |
|
| 360 |
/* Find dimension of new Confusion */ |
| 361 |
autoDistributions dcol = Strings_to_Distributions (scol.get()); |
| 362 |
const integer nresp = dcol -> numberOfRows; |
| 363 |
|
| 364 |
autoDistributions drow = Strings_to_Distributions (srow.get()); |
| 365 |
const integer nstim = drow -> numberOfRows; |
| 366 |
|
| 367 |
autoConfusion thee = Confusion_create (nstim, nresp); |
| 368 |
|
| 369 |
thy rowLabels.all() <<= drow -> rowLabels.all(); |
| 370 |
thy columnLabels.all() <<= dcol -> rowLabels.all(); |
| 371 |
|
| 372 |
autoINTVEC rowIndex = create_index (srow -> strings.get(), drow -> rowLabels.get()); |
| 373 |
autoINTVEC columnIndex = create_index (scol -> strings.get(), dcol -> rowLabels.get()); |
| 374 |
|
| 375 |
for (integer i = 1; i <= my numberOfRows; i ++) |
| 376 |
for (integer j = 1; j <= my numberOfColumns; j ++) |
| 377 |
thy data [rowIndex [i]] [columnIndex [j]] += my data [i] [j]; |
| 378 |
return thee; |
| 379 |
} catch (MelderError) { |
| 380 |
Melder_throw (me, U": not condensed."); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
autoConfusion TableOfReal_to_Confusion (TableOfReal me) { |
| 385 |
try { |
| 386 |
Melder_require (TableOfReal_isNonNegative (me), |
| 387 |
U"No cell in the table should be negative."); |
| 388 |
autoConfusion thee = Thing_new (Confusion); |
| 389 |
my structTableOfReal :: v1_copy (thee.get()); |
| 390 |
return thee; |
| 391 |
} catch (MelderError) { |
| 392 |
Melder_throw (me, U": not converted to Confusion."); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
autoConfusion Confusion_group (Confusion me, conststring32 labels, conststring32 newLabel, integer newpos) { |
| 397 |
try { |
| 398 |
autoConfusion stim = Confusion_groupStimuli (me, labels, newLabel, newpos); |
| 399 |
autoConfusion thee = Confusion_groupResponses (stim.get(), labels, newLabel, newpos); |
| 400 |
return thee; |
| 401 |
} catch (MelderError) { |
| 402 |
Melder_throw (me, U": not grouped."); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
autoConfusion Confusion_groupStimuli (Confusion me, conststring32 labels_string, conststring32 newLabel, integer newpos) { |
| 407 |
try { |
| 408 |
autoSTRVEC labels = splitByWhitespace_STRVEC (labels_string); |
| 409 |
const integer ncondense = labels.size; |
| 410 |
autoINTVEC irow = to_INTVEC (my numberOfRows); |
| 411 |
|
| 412 |
for (integer itoken = 1; itoken <= labels.size; itoken ++) { |
| 413 |
const conststring32 token = labels [itoken].get(); |
| 414 |
for (integer i = 1; i <= my numberOfRows; i ++) { |
| 415 |
if (Melder_equ (token, my rowLabels [i].get())) { |
| 416 |
irow [i] = 0; |
| 417 |
break; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
integer nfound = 0; |
| 422 |
for (integer i = 1; i <= my numberOfRows; i ++) { |
| 423 |
if (irow [i] == 0) |
| 424 |
nfound ++; |
| 425 |
} |
| 426 |
Melder_require (nfound > 0, |
| 427 |
U"The stimulus labels are invalid."); |
| 428 |
|
| 429 |
if (nfound != ncondense) |
| 430 |
Melder_warning (U"One or more of the given stimulus labels are suspect."); |
| 431 |
const integer newnstim = my numberOfRows - nfound + 1; |
| 432 |
Melder_clip (1_integer, & newpos, newnstim); |
| 433 |
autoConfusion thee = Confusion_create (newnstim, my numberOfColumns); |
| 434 |
thy columnLabels.all() <<= my columnLabels.all(); |
| 435 |
TableOfReal_setRowLabel (thee.get(), newpos, newLabel); |
| 436 |
integer inewrow = 1; |
| 437 |
for (integer i = 1; i <= my numberOfRows; i ++) { |
| 438 |
integer rowpos = newpos; |
| 439 |
if (irow [i] > 0) { |
| 440 |
if (inewrow == newpos) |
| 441 |
inewrow ++; |
| 442 |
rowpos = inewrow; |
| 443 |
inewrow ++; |
| 444 |
TableOfReal_setRowLabel (thee.get(), rowpos, my rowLabels [i].get()); |
| 445 |
} |
| 446 |
thy data.row (rowpos) += my data.row (i); |
| 447 |
} |
| 448 |
return thee; |
| 449 |
} catch (MelderError) { |
| 450 |
Melder_throw (me, U": stimuli not grouped."); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
autoConfusion Confusion_groupResponses (Confusion me, conststring32 labels_string, conststring32 newLabel, integer newpos) { |
| 455 |
try { |
| 456 |
autoSTRVEC labels = splitByWhitespace_STRVEC (labels_string); |
| 457 |
const integer ncondense = labels.size; |
| 458 |
autoINTVEC icol = to_INTVEC (my numberOfColumns); |
| 459 |
|
| 460 |
for (integer itoken = 1; itoken <= labels.size; itoken ++) { |
| 461 |
const conststring32 token = labels [itoken].get(); |
| 462 |
for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 463 |
if (Melder_equ (token, my columnLabels [i].get())) { |
| 464 |
icol [i] = 0; |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
integer nfound = 0; |
| 470 |
for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 471 |
if (icol [i] == 0) |
| 472 |
nfound ++; |
| 473 |
} |
| 474 |
Melder_require (nfound > 0, |
| 475 |
U"The response labels are invalid."); |
| 476 |
|
| 477 |
if (nfound != ncondense) |
| 478 |
Melder_warning (U"One or more of the given response labels are suspect."); |
| 479 |
const integer newnresp = my numberOfColumns - nfound + 1; |
| 480 |
Melder_clip (1_integer, & newpos, newnresp); |
| 481 |
autoConfusion thee = Confusion_create (my numberOfRows, newnresp); |
| 482 |
thy rowLabels.all() <<= my rowLabels.all(); |
| 483 |
TableOfReal_setColumnLabel (thee.get(), newpos, newLabel); |
| 484 |
integer inewcol = 1; |
| 485 |
for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 486 |
integer colpos = newpos; |
| 487 |
if (icol [i] > 0) { |
| 488 |
if (inewcol == newpos) |
| 489 |
inewcol ++; |
| 490 |
colpos = inewcol; |
| 491 |
inewcol ++; |
| 492 |
TableOfReal_setColumnLabel (thee.get(), colpos, my columnLabels [i].get()); |
| 493 |
} |
| 494 |
thy data.column (colpos) += my data.column (i); |
| 495 |
} |
| 496 |
return thee; |
| 497 |
} catch (MelderError) { |
| 498 |
Melder_throw (me, U": responses not grouped."); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
autoTableOfReal Confusion_to_TableOfReal_marginals (Confusion me) { |
| 503 |
try { |
| 504 |
autoTableOfReal thee = TableOfReal_create (my numberOfRows + 1, my numberOfColumns + 1); |
| 505 |
|
| 506 |
thy data.part (1, my numberOfRows, 1, my numberOfColumns) <<= my data.get(); |
| 507 |
autoVEC columnSums = columnSums_VEC (my data.get()); |
| 508 |
thy data.row (my numberOfRows + 1).part (1, my numberOfColumns) <<= columnSums.get(); |
| 509 |
autoVEC rowSums = rowSums_VEC (my data.get()); |
| 510 |
thy data.column (my numberOfColumns + 1).part (1, my numberOfRows) <<= rowSums.get(); |
| 511 |
|
| 512 |
thy data [my numberOfRows + 1] [my numberOfColumns + 1] = NUMsum (rowSums.get()); |
| 513 |
|
| 514 |
thy rowLabels.part (1, my numberOfRows) <<= my rowLabels.all(); |
| 515 |
thy columnLabels.part (1, my numberOfColumns) <<= my columnLabels.all(); |
| 516 |
return thee; |
| 517 |
} catch (MelderError) { |
| 518 |
Melder_throw (me, U": table with marginals not created."); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
void Confusion_drawAsNumbers (Confusion me, Graphics g, bool marginals, int iformat, int precision) { |
| 523 |
TableOfReal thee = me; |
| 524 |
autoTableOfReal athee; |
| 525 |
if (marginals) { |
| 526 |
athee = Confusion_to_TableOfReal_marginals (me); |
| 527 |
thee = athee.get(); |
| 528 |
} |
| 529 |
TableOfReal_drawAsNumbers (thee, g, 1, thy numberOfRows, iformat, precision); |
| 530 |
} |
| 531 |
|
| 532 |
/* End of file Confusion.cpp */ |