File: ClassificationTable.cpp

package info (click to toggle)
praat 6.4.27%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 206,060 kB
  • sloc: cpp: 1,409,811; ansic: 286,305; makefile: 946; python: 340; sh: 35
file content (125 lines) | stat: -rw-r--r-- 4,802 bytes parent folder | download | duplicates (3)
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
/* ClassificationTable.cpp
 *
 * Copyright (C) 1993-2021 David Weenink
 *
 * This code 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 code 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 PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this work. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 djmw 1998
 djmw 20020315 GPL header
 djmw 20040422 Added ClassificationTable_to_Categories_maximumProbability
 djmw 20040623 Added ClassificationTable_to_Strings_maximumProbability
 djmw 20040824 Added Strings_extensions.h header
 djmw 20101122 ClassificationTable_to_Correlation_columns
 djmw 20110304 Thing_new
*/

#include "ClassificationTable.h"
#include "Distributions_and_Strings.h"
#include "Strings_extensions.h"
#include "NUM2.h"

Thing_implement (ClassificationTable, TableOfReal, 0);

autoClassificationTable ClassificationTable_create (integer numberOfRows, integer numberOfClasses) {
	try {
		autoClassificationTable me = Thing_new (ClassificationTable);
		TableOfReal_init (me.get(), numberOfRows, numberOfClasses);
		return me;
	} catch (MelderError) {
		Melder_throw (U"ClassificationTable not created.");
	}
}

autoConfusion ClassificationTable_to_Confusion (ClassificationTable me, bool onlyClassLabels) {
	try {
		autoStrings responses = TableOfReal_extractColumnLabelsAsStrings (me);
		autoStrings s2 = TableOfReal_extractRowLabelsAsStrings (me);
		autoDistributions d2 = Strings_to_Distributions (s2.get());
		autoStrings stimuli = TableOfReal_extractRowLabelsAsStrings (d2.get());
		autoConfusion thee = Confusion_createFromStringses (( onlyClassLabels ? responses.get() : stimuli.get() ), responses.get());
		Confusion_ClassificationTable_increase (thee.get(), me);
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": confusions cannot be calculated.");
	}
}

void Confusion_ClassificationTable_increase (Confusion me, ClassificationTable thee) {
	Melder_require (my numberOfColumns == thy numberOfColumns, 
		U"The number of columns should be equal.");
	for (integer irow = 1; irow <= thy numberOfRows; irow ++) {
		const integer index = TableOfReal_getColumnIndexAtMaximumInRow (thee, irow);
		Confusion_increase (me, thy rowLabels [irow].get(), my columnLabels [index].get());
	}
}

autoStrings ClassificationTable_to_Strings_maximumProbability (ClassificationTable me) {
	try {
		autoStrings thee = Strings_createFixedLength (my numberOfRows);
		Melder_assert (my numberOfColumns > 0);
		for (integer irow = 1; irow <= my numberOfRows; irow ++) {
			const integer col = NUMmaxPos (my data.row (irow));
			if (my columnLabels [col])
				Strings_replace (thee.get(), irow, my columnLabels [col].get());
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": strings cannot be created.");
	}
}

autoCategories ClassificationTable_to_Categories_maximumProbability (ClassificationTable me) {
	try {
		autoCategories thee = Categories_create ();
		Melder_assert (my numberOfColumns > 0);
		for (integer irow = 1; irow <= my numberOfRows; irow ++) {
			const integer col = NUMmaxPos (my data.row (irow));
			OrderedOfString_append (thee.get(), my columnLabels [col].get());
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": no Categories created.");
	}
}

autoCorrelation ClassificationTable_to_Correlation_columns (ClassificationTable me) {
	try {
		autoCorrelation thee = Correlation_create (my numberOfColumns);
		for (integer icol = 1; icol <= thy numberOfColumns; icol ++) {
			const conststring32 label = my columnLabels [icol].get();
			TableOfReal_setRowLabel (thee.get(), icol, label);
			TableOfReal_setColumnLabel (thee.get(), icol, label);
		}
		for (integer irow = 1; irow <= thy numberOfColumns; irow ++) {
			thy data [irow] [irow] = 1.0;
			for (integer icol = irow + 1; icol <= thy numberOfColumns; icol ++) {
				const double n12 = NUMinner (my data.column (irow), my data.column (icol));
				const double n11 = NUMinner (my data.column (irow), my data.column (irow));
				const double n22 = NUMinner (my data.column (icol), my data.column (icol));
				// probabilities might be very low!
				if (n12 > 0.0 && n22 > 0.0) {
					thy data [irow] [icol] = thy data [icol] [irow] = n12 / sqrt (n11 * n22);
				}
			}
		}
		thy numberOfObservations = my numberOfRows;
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": no correlation created.");
	}
}

/* End of file ClassificationTable.cpp */